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