KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > PolicyTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: PolicyTest.java 42625 2004-03-04 15:45:03Z egli $ */
19
20 package org.apache.lenya.ac.impl;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
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 /**
34  * To change the template for this generated type comment go to
35  * Window>Preferences>Java>Code Generation>Code and Comments
36  */

37 public class PolicyTest extends AccessControlTest {
38     /**
39      * Executes this test.
40      * @param test The test.
41      */

42     public PolicyTest(String JavaDoc test) {
43         super(test);
44     }
45
46     /**
47      * The main method.
48      * @param args The command-line arguments.
49      */

50     public static void main(String JavaDoc[] args) {
51         PublicationHelper.extractPublicationArguments(args);
52         TestRunner.run(PolicyTest.class);
53     }
54
55     protected static final String JavaDoc URL = "/test/authoring/index.html";
56     protected static final String JavaDoc SAVE_URL = "/test/authoring/tutorial.html";
57
58     /**
59      * A test.
60      * @throws AccessControlException when something went wrong.
61      */

62     public void testLoadPolicy() throws AccessControlException {
63         String JavaDoc 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     /**
76      * Returns the policy for a URL.
77      * @param url The URL.
78      * @return The policy.
79      * @throws AccessControlException when something went wrong.
80      */

81     protected Policy getPolicy(String JavaDoc url) throws AccessControlException {
82         Policy policy =
83             getPolicyManager().getPolicy(getAccessController().getAccreditableManager(), url);
84
85         return policy;
86     }
87
88     /**
89      * A test.
90      * @throws AccessControlException when something went wrong.
91      */

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 JavaDoc oldRoles = new HashSet JavaDoc(Arrays.asList(credential.getRoles()));
137             Set JavaDoc newRoles = new HashSet JavaDoc(Arrays.asList(newCredential.getRoles()));
138             assertEquals(oldRoles, newRoles);
139
140             /*
141             for (int j = 0; j < roles.length; j++) {
142                 assertEquals(roles[j], newRoles[j]);
143                 System.out.println(" Role: [" + roles[j] + "]");
144             }
145             */

146         }
147     }
148 }
149
Popular Tags