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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
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:
28 * http://www.collectionspace.org
29 * http://wiki.collectionspace.org
31 * Copyright 2009 University of California at Berkeley
33 * Licensed under the Educational Community License (ECL), Version 2.0.
34 * You may not use this file except in compliance with this License.
36 * You may obtain a copy of the ECL 2.0 License at
38 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
47 * To change this template, choose Tools | Templates
48 * and open the template in the editor.
50 package org.collectionspace.services.authorization.test;
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;
88 public abstract class AbstractAuthorizationTestImpl {
90 final Logger logger = LoggerFactory.getLogger(AbstractAuthorizationTestImpl.class);
91 private org.springframework.jdbc.datasource.DataSourceTransactionManager txManager;
94 * Returns the name of the currently running test.
96 * Note: although the return type is listed as Object[][],
97 * this method instead returns a String.
99 * @param m The currently running test method.
101 * @return The name of the currently running test method.
103 @DataProvider(name = "testName")
104 protected static Object[][] testName(Method m) {
105 return new Object[][]{
106 new Object[]{m.getName()}
111 protected void setup() {
112 ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
113 new String[]{"applicationContext-authorization-test.xml"});
115 AuthZ authZ = AuthZ.get();
116 txManager = (org.springframework.jdbc.datasource.DataSourceTransactionManager) appContext.getBean("transactionManager");
119 protected void login() {
120 GrantedAuthority gauth = new GrantedAuthorityImpl("ROLE_ADMINISTRATOR");
121 HashSet<GrantedAuthority> gauths = new HashSet<GrantedAuthority>();
123 Authentication authRequest = new UsernamePasswordAuthenticationToken("test", "test", gauths);
124 SecurityContextHolder.getContext().setAuthentication(authRequest);
127 protected void logout() {
128 SecurityContextHolder.getContext().setAuthentication(null);
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
135 def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
136 return txManager.getTransaction(def);
139 protected void rollbackTransaction(TransactionStatus status) {
140 txManager.rollback(status);
143 protected void commitTransaction(TransactionStatus status) {
144 txManager.commit(status);
148 protected void toFile(Object o, Class jaxbClass, String fileName) {
149 File f = new File(fileName);
151 JAXBContext jc = JAXBContext.newInstance(jaxbClass);
152 Marshaller m = jc.createMarshaller();
153 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
156 } catch (Exception e) {
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));
171 @Test(dataProvider = "testName", dataProviderClass = AbstractAuthorizationTestImpl.class)
172 public void test(String testName) {
173 if (logger.isDebugEnabled()) {
174 logger.debug(testName);