]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f8364c9fc88d0b27517fda769af6de7d894a9427
[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.test;
51
52 import java.io.File;
53 import java.io.InputStream;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import java.lang.reflect.Method;
57 import java.util.ArrayList;
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.ActionType;
63 import org.collectionspace.services.authorization.AuthZ;
64 import org.collectionspace.services.authorization.Permission;
65 import org.collectionspace.services.authorization.EffectType;
66 import org.collectionspace.services.authorization.PermissionAction;
67 import org.collectionspace.services.authorization.PermissionsList;
68 import org.collectionspace.services.authorization.PermissionRole;
69 import org.collectionspace.services.authorization.PermissionsList;
70 import org.collectionspace.services.authorization.PermissionsRolesList;
71 import org.springframework.context.support.ClassPathXmlApplicationContext;
72 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
73 import org.springframework.security.core.Authentication;
74 import org.springframework.security.core.GrantedAuthority;
75 import org.springframework.security.core.authority.GrantedAuthorityImpl;
76 import org.springframework.security.core.context.SecurityContextHolder;
77 import org.springframework.transaction.TransactionDefinition;
78 import org.springframework.transaction.TransactionStatus;
79 import org.springframework.transaction.support.DefaultTransactionDefinition;
80 import org.testng.annotations.BeforeClass;
81 import org.testng.annotations.DataProvider;
82 import org.testng.annotations.Test;
83
84 /**
85  *
86  * @author 
87  */
88 public abstract class AbstractAuthorizationTestImpl {
89
90     final Logger logger = LoggerFactory.getLogger(AbstractAuthorizationTestImpl.class);
91     private org.springframework.jdbc.datasource.DataSourceTransactionManager txManager;
92
93     /**
94      * Returns the name of the currently running test.
95      *
96      * Note: although the return type is listed as Object[][],
97      * this method instead returns a String.
98      *
99      * @param   m  The currently running test method.
100      *
101      * @return  The name of the currently running test method.
102      */
103     @DataProvider(name = "testName")
104     protected static Object[][] testName(Method m) {
105         return new Object[][]{
106                     new Object[]{m.getName()}
107                 };
108     }
109
110
111     protected void setup() {
112         ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
113                 new String[]{"applicationContext-authorization-test.xml"});
114         login();
115         AuthZ authZ = AuthZ.get();
116         txManager = (org.springframework.jdbc.datasource.DataSourceTransactionManager) appContext.getBean("transactionManager");
117     }
118
119     protected void login() {
120         GrantedAuthority gauth = new GrantedAuthorityImpl("ROLE_ADMINISTRATOR");
121         HashSet<GrantedAuthority> gauths = new HashSet<GrantedAuthority>();
122         gauths.add(gauth);
123         Authentication authRequest = new UsernamePasswordAuthenticationToken("test", "test", gauths);
124         SecurityContextHolder.getContext().setAuthentication(authRequest);
125     }
126
127     protected void logout() {
128         SecurityContextHolder.getContext().setAuthentication(null);
129     }
130
131     protected TransactionStatus beginTransaction(String name) {
132         DefaultTransactionDefinition def = new DefaultTransactionDefinition();
133         // explicitly setting the transaction name is something that can only be done programmatically
134         def.setName(name);
135         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
136         return txManager.getTransaction(def);
137     }
138
139     protected void rollbackTransaction(TransactionStatus status) {
140         txManager.rollback(status);
141     }
142
143     protected void commitTransaction(TransactionStatus status) {
144         txManager.commit(status);
145     }
146
147
148     protected void toFile(Object o, Class jaxbClass, String fileName) {
149         File f = new File(fileName);
150         try {
151             JAXBContext jc = JAXBContext.newInstance(jaxbClass);
152             Marshaller m = jc.createMarshaller();
153             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
154                     Boolean.TRUE);
155             m.marshal(o, f);
156         } catch (Exception e) {
157             e.printStackTrace();
158         }
159     }
160
161     protected Object fromFile(Class jaxbClass, String fileName) throws Exception {
162         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
163         InputStream is = tccl.getResourceAsStream(fileName);
164         JAXBContext context = JAXBContext.newInstance(jaxbClass);
165         Unmarshaller unmarshaller = context.createUnmarshaller();
166         //note: setting schema to null will turn validator off
167         unmarshaller.setSchema(null);
168         return jaxbClass.cast(unmarshaller.unmarshal(is));
169     }
170
171     @Test(dataProvider = "testName", dataProviderClass = AbstractAuthorizationTestImpl.class)
172     public void test(String testName) {
173         if (logger.isDebugEnabled()) {
174             logger.debug(testName);
175         }
176     }
177 }