001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: PolicyTest.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.ac.impl;
022:
023: import org.apache.lenya.ac.AccessControlException;
024: import org.apache.lenya.ac.Credential;
025: import org.apache.lenya.ac.InheritingPolicyManager;
026: import org.apache.lenya.ac.Policy;
027: import org.apache.lenya.ac.Role;
028: import org.apache.lenya.cms.publication.Publication;
029: import org.apache.lenya.cms.publication.PublicationException;
030:
031: /**
032: * Policy Test
033: *
034: */
035: public class PolicyTest extends AbstractAccessControlTest {
036: protected static final String URL = "/test/authoring/index.html";
037:
038: protected static final String SAVE_URL = "/test/authoring/tutorial.html";
039:
040: /**
041: * A test.
042: *
043: * @throws AccessControlException when something went wrong.
044: * @throws PublicationException
045: */
046: public void testLoadPolicy() throws AccessControlException,
047: PublicationException {
048: Publication pub = getPublication("test");
049: String url = "/" + pub.getId() + URL;
050: Policy policy = getPolicy(url);
051:
052: Role[] allRoles = getAccreditableManager().getRoleManager()
053: .getRoles();
054:
055: getLogger().info("Roles: ");
056: for (int i = 0; i < allRoles.length; i++) {
057: int result = policy.check(getIdentity(), allRoles[i]);
058: if (result == Policy.RESULT_GRANTED) {
059: getLogger().info(allRoles[i].getId() + ": granted");
060: } else {
061: getLogger().info(allRoles[i].getId() + ": denied");
062: }
063: }
064: }
065:
066: /**
067: * Returns the policy for a URL.
068: *
069: * @param url The URL.
070: * @return The policy.
071: * @throws AccessControlException when something went wrong.
072: */
073: protected Policy getPolicy(String url)
074: throws AccessControlException {
075: Policy policy = getPolicyManager().getPolicy(
076: getAccessController().getAccreditableManager(), url);
077:
078: return policy;
079: }
080:
081: /**
082: * A test.
083: *
084: * @throws AccessControlException when something went wrong.
085: */
086: public void testSavePolicy() throws AccessControlException {
087: InheritingPolicyManager policyManager = (InheritingPolicyManager) getPolicyManager();
088: DefaultPolicy subtreePolicy = (DefaultPolicy) policyManager
089: .buildSubtreePolicy(getAccessController()
090: .getAccreditableManager(), URL);
091: DefaultPolicy newPolicy = new DefaultPolicy();
092:
093: Credential[] credentials = subtreePolicy.getCredentials();
094:
095: for (int i = 0; i < credentials.length; i++) {
096: Role role = credentials[i].getRole();
097: CredentialImpl credential = new CredentialImpl(
098: credentials[i].getAccreditable(), role);
099: credential.setMethod(credentials[i].getMethod());
100: newPolicy.addCredential(credential);
101: }
102:
103: assertEquals(subtreePolicy.getCredentials().length, newPolicy
104: .getCredentials().length);
105:
106: policyManager.saveSubtreePolicy(SAVE_URL, newPolicy);
107:
108: newPolicy = (DefaultPolicy) policyManager.buildSubtreePolicy(
109: getAccessController().getAccreditableManager(),
110: SAVE_URL);
111: assertEquals(subtreePolicy.getCredentials().length, newPolicy
112: .getCredentials().length);
113:
114: Credential[] newCredentials = newPolicy.getCredentials();
115:
116: for (int i = 0; i < credentials.length; i++) {
117: Role role = credentials[i].getRole();
118: CredentialImpl credential = new CredentialImpl(
119: credentials[i].getAccreditable(), role);
120: credential.setMethod(credential.getMethod());
121: Credential newCredential = null;
122:
123: for (int k = 0; k < newCredentials.length; k++) {
124: if (newCredentials[k].getAccreditable().equals(
125: credential.getAccreditable())) {
126: newCredential = newCredentials[k];
127: }
128: }
129:
130: getLogger().info(
131: "Accreditable: [" + credential.getAccreditable()
132: + "]");
133: assertNotNull(newCredential);
134:
135: Role oldRole = credential.getRole();
136: Role newRole = newCredential.getRole();
137: assertEquals(oldRole, newRole);
138:
139: /*
140: * for (int j = 0; j < roles.length; j++) { assertEquals(roles[j],
141: * newRoles[j]); getLogger().info(" Role: [" + roles[j] + "]"); }
142: */
143: }
144: }
145: }
|