d33b65d79af26f3fe00b22d8de3f92d99c5af25e
[utils] / security / src / test / java / org / wamblee / security / authorization / DefaultOperationRegistryTest.java
1 /*
2  * Copyright 2005 the original author or authors.
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 package org.wamblee.security.authorization;
17
18 import junit.framework.TestCase;
19
20
21 /**
22  * Test of the operation registry.
23  *
24  * @author Erik Brakkee
25  */
26 public class DefaultOperationRegistryTest extends TestCase {
27     /**
28      * DOCUMENT ME!
29      */
30     private OperationRegistry registry;
31
32     /* (non-Javadoc)
33      * @see junit.framework.TestCase#setUp()
34      */
35     /**
36      * DOCUMENT ME!
37      *
38      * @throws Exception DOCUMENT ME!
39      */
40     @Override
41     protected void setUp() throws Exception {
42         registry = new DefaultOperationRegistry(new Operation[] {
43                     new AllOperation(), new ReadOperation(),
44                     new WriteOperation(), new DeleteOperation(),
45                     new CreateOperation()
46                 });
47     }
48
49     /**
50      * Tests encoding and decoding of no operations.
51      */
52     public void testEncodeDecodeNooperations() {
53         assertEquals("", registry.encode(new Operation[0]));
54         assertEquals(0, registry.decode(Object.class, "").length);
55     }
56
57     /**
58      * Verifies that encoding of operations into a string works.
59      */
60     public void testEncode() {
61         assertEquals("read,write",
62             registry.encode(
63                 new Operation[] { new ReadOperation(), new WriteOperation() }));
64     }
65
66     /**
67      * Verifies that decoding of operation from a string works.
68      */
69     public void testDecode() {
70         Operation[] operations = registry.decode(Object.class, "read,write");
71         assertTrue(operations[0] instanceof ReadOperation);
72         assertTrue(operations[1] instanceof WriteOperation);
73     }
74
75     /**
76      * Verifies that an IllegalArgumentException occurs when attempting
77      * to decode an operation that is not known.
78      */
79     public void testDecodeUnknownOperation() {
80         try {
81             registry.decode(Object.class, "bla");
82             fail();
83         } catch (IllegalArgumentException e) {
84             // ok
85         }
86     }
87 }