1 17 18 19 20 package org.apache.lenya.ac.impl; 21 22 import java.util.Arrays ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import junit.textui.TestRunner; 27 28 import org.apache.lenya.ac.AccessControlException; 29 import org.apache.lenya.ac.Policy; 30 import org.apache.lenya.ac.Role; 31 import org.apache.lenya.cms.PublicationHelper; 32 33 37 public class PolicyTest extends AccessControlTest { 38 42 public PolicyTest(String test) { 43 super(test); 44 } 45 46 50 public static void main(String [] args) { 51 PublicationHelper.extractPublicationArguments(args); 52 TestRunner.run(PolicyTest.class); 53 } 54 55 protected static final String URL = "/test/authoring/index.html"; 56 protected static final String SAVE_URL = "/test/authoring/tutorial.html"; 57 58 62 public void testLoadPolicy() throws AccessControlException { 63 String url = "/" + PublicationHelper.getPublication().getId() + URL; 64 Policy policy = getPolicy(url); 65 Role[] roles = policy.getRoles(getIdentity()); 66 System.out.print("Roles: "); 67 68 for (int i = 0; i < roles.length; i++) { 69 System.out.print(roles[i]); 70 } 71 72 System.out.println(); 73 } 74 75 81 protected Policy getPolicy(String url) throws AccessControlException { 82 Policy policy = 83 getPolicyManager().getPolicy(getAccessController().getAccreditableManager(), url); 84 85 return policy; 86 } 87 88 92 public void testSavePolicy() throws AccessControlException { 93 DefaultPolicy urlPolicy = 94 getPolicyManager().buildURLPolicy(getAccessController().getAccreditableManager(), URL); 95 DefaultPolicy newPolicy = new DefaultPolicy(); 96 97 Credential[] credentials = urlPolicy.getCredentials(); 98 99 for (int i = 0; i < credentials.length; i++) { 100 Credential credential = new Credential(credentials[i].getAccreditable()); 101 Role[] roles = credentials[i].getRoles(); 102 103 for (int j = 0; j < roles.length; j++) { 104 credential.addRole(roles[j]); 105 } 106 107 newPolicy.addCredential(credential); 108 } 109 110 assertEquals(urlPolicy.getCredentials().length, newPolicy.getCredentials().length); 111 112 getPolicyManager().saveURLPolicy(SAVE_URL, newPolicy); 113 114 newPolicy = 115 getPolicyManager().buildURLPolicy( 116 getAccessController().getAccreditableManager(), 117 SAVE_URL); 118 assertEquals(urlPolicy.getCredentials().length, newPolicy.getCredentials().length); 119 120 Credential[] newCredentials = newPolicy.getCredentials(); 121 122 for (int i = 0; i < credentials.length; i++) { 123 Credential credential = new Credential(credentials[i].getAccreditable()); 124 125 Credential newCredential = null; 126 127 for (int k = 0; k < newCredentials.length; k++) { 128 if (newCredentials[k].getAccreditable().equals(credential.getAccreditable())) { 129 newCredential = newCredentials[k]; 130 } 131 } 132 133 System.out.println("Accreditable: [" + credential.getAccreditable() + "]"); 134 assertNotNull(newCredential); 135 136 Set oldRoles = new HashSet (Arrays.asList(credential.getRoles())); 137 Set newRoles = new HashSet (Arrays.asList(newCredential.getRoles())); 138 assertEquals(oldRoles, newRoles); 139 140 146 } 147 } 148 } 149 | Popular Tags |