]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0a3d7965b79e13e47f3cedb7a34b0116c53f6000
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  *//**
24  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46 /*
47  * To change this template, choose Tools | Templates
48  * and open the template in the editor.
49  */
50 package org.collectionspace.services.authorization.importer;
51
52 import java.io.File;
53 import java.io.FileInputStream;
54 import java.io.InputStream;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import java.lang.reflect.Method;
58 import java.util.HashSet;
59 import javax.xml.bind.JAXBContext;
60 import javax.xml.bind.Marshaller;
61 import javax.xml.bind.Unmarshaller;
62 import org.collectionspace.services.authorization.AuthZ;
63 import org.collectionspace.services.authorization.PermissionsList;
64 import org.collectionspace.services.authorization.PermissionsRolesList;
65 import org.springframework.context.support.ClassPathXmlApplicationContext;
66 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
67 import org.springframework.security.core.Authentication;
68 import org.springframework.security.core.GrantedAuthority;
69 import org.springframework.security.core.authority.GrantedAuthorityImpl;
70 import org.springframework.security.core.context.SecurityContextHolder;
71 import org.springframework.transaction.TransactionDefinition;
72 import org.springframework.transaction.TransactionStatus;
73 import org.springframework.transaction.support.DefaultTransactionDefinition;
74 import org.testng.annotations.DataProvider;
75 import org.testng.annotations.Test;
76
77 /**
78  *
79  * @author 
80  */
81 public abstract class AbstractAuthorizationTestImpl {
82
83     static protected final String MAVEN_BASEDIR_PROPERTY = "maven.basedir";
84     final Logger logger = LoggerFactory.getLogger(AbstractAuthorizationTestImpl.class);
85     private org.springframework.jdbc.datasource.DataSourceTransactionManager txManager;
86     final static String importDataDir = "src/main/resources/import-data/";
87     static String baseDir;
88
89     static {
90         baseDir = System.getProperty(AbstractAuthorizationTestImpl.MAVEN_BASEDIR_PROPERTY);
91         if (baseDir == null || baseDir.isEmpty()) {
92             baseDir = System.getProperty("user.dir");
93         }
94         baseDir = baseDir + System.getProperty("file.separator");
95     }
96
97     /**
98      * Returns the name of the currently running test.
99      *
100      * Note: although the return type is listed as Object[][],
101      * this method instead returns a String.
102      *
103      * @param   m  The currently running test method.
104      *
105      * @return  The name of the currently running test method.
106      */
107     @DataProvider(name = "testName")
108     protected static Object[][] testName(Method m) {
109         return new Object[][]{
110                     new Object[]{m.getName()}
111                 };
112     }
113
114     protected void setup() {
115         ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
116                 new String[]{"applicationContext-authorization-test.xml"});
117         login();
118         AuthZ authZ = AuthZ.get();
119         txManager = (org.springframework.jdbc.datasource.DataSourceTransactionManager) appContext.getBean("transactionManager");
120     }
121
122     protected void login() {
123         GrantedAuthority gauth = new GrantedAuthorityImpl("ROLE_ADMINISTRATOR");
124         HashSet<GrantedAuthority> gauths = new HashSet<GrantedAuthority>();
125         gauths.add(gauth);
126         Authentication authRequest = new UsernamePasswordAuthenticationToken("test", "test", gauths);
127         SecurityContextHolder.getContext().setAuthentication(authRequest);
128     }
129
130     protected void logout() {
131         SecurityContextHolder.getContext().setAuthentication(null);
132     }
133
134     protected TransactionStatus beginTransaction(String name) {
135         DefaultTransactionDefinition def = new DefaultTransactionDefinition();
136         // explicitly setting the transaction name is something that can only be done programmatically
137         def.setName(name);
138         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
139         return txManager.getTransaction(def);
140     }
141
142     protected void rollbackTransaction(TransactionStatus status) {
143         txManager.rollback(status);
144     }
145
146     protected void commitTransaction(TransactionStatus status) {
147         txManager.commit(status);
148     }
149
150     static void toFile(Object o, Class jaxbClass, String fileName) {
151         File f = new File(fileName);
152         try {
153             JAXBContext jc = JAXBContext.newInstance(jaxbClass);
154             Marshaller m = jc.createMarshaller();
155             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
156                     Boolean.TRUE);
157             m.marshal(o, f);
158         } catch (Exception e) {
159             e.printStackTrace();
160         }
161     }
162
163     static Object fromFile(Class jaxbClass, String fileName) throws Exception {
164         InputStream is = new FileInputStream(fileName);
165         try {
166             JAXBContext context = JAXBContext.newInstance(jaxbClass);
167             Unmarshaller unmarshaller = context.createUnmarshaller();
168             //note: setting schema to null will turn validator off
169             unmarshaller.setSchema(null);
170             return jaxbClass.cast(unmarshaller.unmarshal(is));
171         } finally {
172             if (is != null) {
173                 try {
174                     is.close();
175                 } catch (Exception e) {
176                 }
177             }
178         }
179     }
180
181
182     public void writePermissions(PermissionsList pcList, String fileName) {
183         AbstractAuthorizationTestImpl.toFile(pcList, PermissionsList.class,
184                 AbstractAuthorizationTestImpl.importDataDir + fileName);
185         logger.info("generated permissions to "
186                 + AbstractAuthorizationTestImpl.importDataDir + fileName);
187     }
188
189     
190     public void writePermissionRoles(PermissionsRolesList psrsl, String fileName) {
191         AbstractAuthorizationTestImpl.toFile(psrsl, PermissionsRolesList.class,
192                 AbstractAuthorizationTestImpl.importDataDir + fileName);
193         logger.info("generated permissions-roles to "
194                 + AbstractAuthorizationTestImpl.importDataDir + fileName);
195     }
196
197     @Test(dataProvider = "testName", dataProviderClass = AbstractAuthorizationTestImpl.class)
198     public void test(String testName) {
199         if (logger.isDebugEnabled()) {
200             logger.debug(testName);
201         }
202     }
203 }