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