001: package com.ecyrd.jspwiki.auth.permissions;
002:
003: import java.security.Permission;
004: import java.util.Enumeration;
005:
006: import junit.framework.TestCase;
007:
008: /**
009: * @author Andrew R. Jaquith
010: */
011: public class AllPermissionCollectionTest extends TestCase {
012:
013: AllPermissionCollection c_all;
014:
015: /**
016: * @see junit.framework.TestCase#setUp()
017: */
018: protected void setUp() throws Exception {
019: super .setUp();
020: c_all = new AllPermissionCollection();
021: }
022:
023: public void testAddAllPermission() {
024: AllPermission all1 = new AllPermission("*");
025: AllPermission all2 = new AllPermission("JSPWiki");
026: AllPermission all3 = new AllPermission("myWiki");
027: AllPermission all4 = new AllPermission("*");
028:
029: c_all.add(all1);
030: assertEquals(1, count(c_all));
031:
032: c_all.add(all2);
033: assertEquals(2, count(c_all));
034:
035: c_all.add(all3);
036: assertEquals(3, count(c_all));
037:
038: // The last one is a duplicate and shouldn't be counted...
039: c_all.add(all4);
040: assertEquals(3, count(c_all));
041: }
042:
043: public void testAddPagePermission() {
044: PagePermission p1 = new PagePermission("JSPWiki:Main", "edit");
045: PagePermission p2 = new PagePermission("JSPWiki:GroupAdmin",
046: "edit");
047: PagePermission p3 = new PagePermission("JSPWiki:Foobar",
048: "delete");
049: PagePermission p4 = new PagePermission("JSPWiki:Main", "edit");
050:
051: c_all.add(p1);
052: assertEquals(1, count(c_all));
053:
054: c_all.add(p2);
055: assertEquals(2, count(c_all));
056:
057: c_all.add(p3);
058: assertEquals(3, count(c_all));
059:
060: // The last one is a duplicate and shouldn't be counted...
061: c_all.add(p4);
062: assertEquals(3, count(c_all));
063: }
064:
065: public void testAddWikiPermission() {
066: WikiPermission p1 = new WikiPermission("JSPWiki", "login");
067: WikiPermission p2 = new WikiPermission("JSPWiki",
068: "createGroups");
069: WikiPermission p3 = new WikiPermission("JSPWiki",
070: "editPreferences");
071: WikiPermission p4 = new WikiPermission("JSPWiki", "login");
072:
073: c_all.add(p1);
074: assertEquals(1, count(c_all));
075:
076: c_all.add(p2);
077: assertEquals(2, count(c_all));
078:
079: c_all.add(p3);
080: assertEquals(3, count(c_all));
081:
082: // The last one is a duplicate and shouldn't be counted...
083: c_all.add(p4);
084: assertEquals(3, count(c_all));
085: }
086:
087: public void testReadOnly() {
088: AllPermission all1 = new AllPermission("*");
089: AllPermission all2 = new AllPermission("JSPWiki");
090:
091: assertFalse(c_all.isReadOnly());
092: c_all.add(all1);
093: assertFalse(c_all.isReadOnly());
094:
095: // Mark as read-only; isReadOnly should return "true", as we'll get an
096: // error
097: c_all.setReadOnly();
098: assertTrue(c_all.isReadOnly());
099: boolean exception = false;
100: try {
101: c_all.add(all2);
102: } catch (SecurityException e) {
103: exception = true;
104: }
105:
106: if (!exception) {
107: // We should never get here
108: assertTrue(false);
109: }
110: }
111:
112: public void testImpliesAllPermission() {
113: AllPermission all1 = new AllPermission("JSPWiki");
114: AllPermission all2 = new AllPermission("myWiki");
115: AllPermission all3 = new AllPermission("big*");
116: AllPermission all4 = new AllPermission("*");
117:
118: c_all.add(all1);
119: assertTrue(c_all.implies(new AllPermission("JSPWiki")));
120: assertFalse(c_all.implies(new AllPermission("myWiki")));
121:
122: c_all.add(all2);
123: assertTrue(c_all.implies(new AllPermission("JSPWiki")));
124: assertTrue(c_all.implies(new AllPermission("myWiki")));
125: assertFalse(c_all.implies(new AllPermission("bigTimeWiki")));
126:
127: c_all.add(all3);
128: assertTrue(c_all.implies(new AllPermission("JSPWiki")));
129: assertTrue(c_all.implies(new AllPermission("myWiki")));
130: assertTrue(c_all.implies(new AllPermission("bigTimeWiki")));
131: assertTrue(c_all.implies(new AllPermission("bigBigBigWiki")));
132: assertFalse(c_all.implies(new AllPermission("bittyWiki")));
133:
134: c_all.add(all4);
135: assertTrue(c_all.implies(new AllPermission("JSPWiki")));
136: assertTrue(c_all.implies(new AllPermission("myWiki")));
137: assertTrue(c_all.implies(new AllPermission("bigTimeWiki")));
138: assertTrue(c_all.implies(new AllPermission("bigBigBigWiki")));
139: assertTrue(c_all.implies(new AllPermission("bittyWiki")));
140: }
141:
142: public void testImpliesPagePermission() {
143: AllPermission all1 = new AllPermission("JSPWiki");
144: AllPermission all2 = new AllPermission("*");
145:
146: c_all.add(all1);
147: assertTrue(c_all.implies(new PagePermission("JSPWiki:Main",
148: "edit")));
149: assertTrue(c_all.implies(new PagePermission(
150: "JSPWiki:GroupAdmin", "edit")));
151: assertTrue(c_all.implies(new PagePermission("JSPWiki:Foobar",
152: "delete")));
153: assertFalse(c_all.implies(new PagePermission("myWiki:Foobar",
154: "delete")));
155: assertFalse(c_all.implies(new PagePermission("bigTimeWiki:*",
156: "view")));
157:
158: c_all.add(all2);
159: assertTrue(c_all.implies(new PagePermission("JSPWiki:Main",
160: "edit")));
161: assertTrue(c_all.implies(new PagePermission(
162: "JSPWiki:GroupAdmin", "edit")));
163: assertTrue(c_all.implies(new PagePermission("JSPWiki:Foobar",
164: "delete")));
165: assertTrue(c_all.implies(new PagePermission("myWiki:Foobar",
166: "delete")));
167: assertTrue(c_all.implies(new PagePermission("bigTimeWiki:*",
168: "view")));
169: }
170:
171: public void testImpliesWikiPermission() {
172: AllPermission all1 = new AllPermission("JSPWiki");
173: AllPermission all2 = new AllPermission("*");
174:
175: c_all.add(all1);
176: assertTrue(c_all
177: .implies(new WikiPermission("JSPWiki", "login")));
178: assertTrue(c_all.implies(new WikiPermission("JSPWiki",
179: "createGroups")));
180: assertTrue(c_all.implies(new WikiPermission("JSPWiki",
181: "editPreferences")));
182: assertFalse(c_all.implies(new WikiPermission("myWiki",
183: "editPreferences")));
184: assertFalse(c_all.implies(new WikiPermission("bigTimeWiki",
185: "login")));
186:
187: c_all.add(all2);
188: assertTrue(c_all
189: .implies(new WikiPermission("JSPWiki", "login")));
190: assertTrue(c_all.implies(new WikiPermission("JSPWiki",
191: "createGroups")));
192: assertTrue(c_all.implies(new WikiPermission("JSPWiki",
193: "editPreferences")));
194: assertTrue(c_all.implies(new WikiPermission("myWiki",
195: "editPreferences")));
196: assertTrue(c_all.implies(new WikiPermission("bigTimeWiki",
197: "login")));
198: }
199:
200: public void testImpliesMixedPermissions() {
201: Permission p1 = new AllPermission("JSPWiki");
202: Permission p2 = new WikiPermission("myWiki", "editPreferences");
203: Permission p3 = new PagePermission("bigTimeWiki:FooBar",
204: "modify");
205: Permission p4 = new AllPermission("*");
206:
207: c_all.add(p1);
208: assertTrue(c_all
209: .implies(new WikiPermission("JSPWiki", "login")));
210: assertTrue(c_all.implies(new PagePermission("JSPWiki:FooBar",
211: "edit")));
212: assertFalse(c_all.implies(new WikiPermission("myWiki",
213: "editPreferences")));
214: assertFalse(c_all
215: .implies(new WikiPermission("myWiki", "login")));
216: assertFalse(c_all.implies(new WikiPermission("bigTimeWiki",
217: "login")));
218: assertFalse(c_all.implies(new PagePermission(
219: "bigTimeWiki:FooBar", "edit")));
220: assertFalse(c_all.implies(new PagePermission(
221: "bigTimeWiki:FooBar", "delete")));
222: assertFalse(c_all.implies(new PagePermission("bigTimeWiki:Bar",
223: "delete")));
224:
225: c_all.add(p2);
226: assertTrue(c_all
227: .implies(new WikiPermission("JSPWiki", "login")));
228: assertTrue(c_all.implies(new PagePermission("JSPWiki:FooBar",
229: "edit")));
230: assertTrue(c_all.implies(new WikiPermission("myWiki",
231: "editPreferences")));
232: assertFalse(c_all
233: .implies(new WikiPermission("myWiki", "login")));
234: assertFalse(c_all.implies(new WikiPermission("bigTimeWiki",
235: "login")));
236: assertFalse(c_all.implies(new PagePermission(
237: "bigTimeWiki:FooBar", "edit")));
238: assertFalse(c_all.implies(new PagePermission(
239: "bigTimeWiki:FooBar", "delete")));
240: assertFalse(c_all.implies(new PagePermission("bigTimeWiki:Bar",
241: "delete")));
242:
243: c_all.add(p3);
244: assertTrue(c_all
245: .implies(new WikiPermission("JSPWiki", "login")));
246: assertTrue(c_all.implies(new PagePermission("JSPWiki:FooBar",
247: "edit")));
248: assertTrue(c_all.implies(new WikiPermission("myWiki",
249: "editPreferences")));
250: assertFalse(c_all
251: .implies(new WikiPermission("myWiki", "login")));
252: assertFalse(c_all.implies(new WikiPermission("bigTimeWiki",
253: "login")));
254: assertTrue(c_all.implies(new PagePermission(
255: "bigTimeWiki:FooBar", "edit")));
256: assertFalse(c_all.implies(new PagePermission(
257: "bigTimeWiki:FooBar", "delete")));
258: assertFalse(c_all.implies(new PagePermission("bigTimeWiki:Bar",
259: "delete")));
260:
261: c_all.add(p4);
262: assertTrue(c_all
263: .implies(new WikiPermission("JSPWiki", "login")));
264: assertTrue(c_all.implies(new PagePermission("JSPWiki:FooBar",
265: "edit")));
266: assertTrue(c_all.implies(new WikiPermission("myWiki",
267: "editPreferences")));
268: assertTrue(c_all.implies(new WikiPermission("myWiki", "login")));
269: assertTrue(c_all.implies(new WikiPermission("bigTimeWiki",
270: "login")));
271: assertTrue(c_all.implies(new PagePermission(
272: "bigTimeWiki:FooBar", "edit")));
273: assertTrue(c_all.implies(new PagePermission(
274: "bigTimeWiki:FooBar", "delete")));
275: assertTrue(c_all.implies(new PagePermission("bigTimeWiki:Bar",
276: "delete")));
277: }
278:
279: private int count(AllPermissionCollection collection) {
280: int i = 0;
281: Enumeration perms = collection.elements();
282: while (perms.hasMoreElements()) {
283: perms.nextElement();
284: i++;
285: }
286: return i;
287: }
288:
289: }
|