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