KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > security > jacc > WebResourcePermissionTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 //
19
// This source code implements specifications defined by the Java
20
// Community Process. In order to remain compliant with the specification
21
// DO NOT add / change / or delete method signatures!
22
//
23

24 package javax.security.jacc;
25
26 import java.security.Permission;
27 import java.io.ByteArrayOutputStream;
28 import java.io.ObjectOutputStream;
29 import java.io.ByteArrayInputStream;
30 import java.io.ObjectInputStream;
31
32 import junit.framework.TestCase;
33
34 /**
35  *
36  * @version $Rev: 159082 $ $Date: 2005-03-25 16:57:52 -0800 (Fri, 25 Mar 2005) $
37  */

38 public class WebResourcePermissionTest extends TestCase {
39
40     public void testSerialization() throws Exception {
41         WebResourcePermission permission = new WebResourcePermission("/bar/*:/bar/stool", "GET,POST");
42         ByteArrayOutputStream baos = new ByteArrayOutputStream();
43         ObjectOutputStream oos = new ObjectOutputStream(baos);
44         oos.writeObject(permission);
45         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
46         ObjectInputStream ois = new ObjectInputStream(bais);
47         Object o = ois.readObject();
48         assertEquals(permission, o);
49     }
50
51     /*
52      * Testing WebResourcePermission(java.lang.String, java.lang.String)
53      */

54     public void testConstructorStringString() {
55
56         WebResourcePermission permission = new WebResourcePermission("/foo", "GET,POST");
57
58         assertTrue(permission.equals(permission));
59         assertEquals(permission.getName(), "/foo");
60         assertEquals(permission.getActions(), "GET,POST");
61         
62         permission = new WebResourcePermission("/foo", "GET,POST,POST,GET");
63         assertEquals(permission.getActions(), "GET,POST");
64         
65         permission = new WebResourcePermission("/", "GET,POST");
66         permission = new WebResourcePermission("/:/foo", "GET,POST");
67         permission = new WebResourcePermission("/:*.asp", "GET,POST");
68         permission = new WebResourcePermission("/:/foo:*.asp", "GET,POST");
69         permission = new WebResourcePermission("/bar/*", "GET,POST");
70         permission = new WebResourcePermission("", "GET,POST");
71         permission = new WebResourcePermission("/*", "GET,POST");
72         permission = new WebResourcePermission("/*:/bar/stool", "GET,POST");
73         permission = new WebResourcePermission("/bar/*:/bar/stool", "GET,POST");
74
75         // bad HTTP method
76
try {
77             permission = new WebResourcePermission("/foo", "GET,POST,BAR");
78             fail("Bad HTTP method");
79         } catch(IllegalArgumentException iae) {
80         }
81
82         // bad HTTP method for a WebResourcePermission
83
try {
84             permission = new WebResourcePermission("/foo", "GET,POST:INTEGRAL");
85         } catch(IllegalArgumentException iae) {
86         }
87
88         // null URLPatternSpec for a WebResourcePermission
89
try {
90             permission = new WebResourcePermission(null, "GET,POST");
91             fail("null URLPatternSpec for a WebResourcePermission");
92         } catch(IllegalArgumentException iae) {
93         }
94
95         // missing qualifiers
96
try {
97             permission = new WebResourcePermission("/foo:", "GET,POST");
98             fail("/foo:");
99         } catch(IllegalArgumentException iae) {
100         }
101
102         // qualifer provided when first pattern isn't path-prefix
103
try {
104             permission = new WebResourcePermission("/foo:/foo/bar", "GET,POST");
105             fail("/foo:/foo/bar");
106         } catch(IllegalArgumentException iae) {
107         }
108
109         try {
110             permission = new WebResourcePermission("/foo/*:*.asp", "GET,POST");
111             fail("/foo/*:*.asp");
112         } catch(IllegalArgumentException iae) {
113         }
114
115         try {
116             permission = new WebResourcePermission("/foo:/", "GET,POST");
117             fail("/foo:/");
118         } catch(IllegalArgumentException iae) {
119         }
120
121         try {
122             permission = new WebResourcePermission("/bar/*:/cat/stool/*", "GET,POST");
123             fail("/bar/*:/cat/stool/*");
124         } catch(IllegalArgumentException iae) {
125         }
126
127         try {
128             permission = new WebResourcePermission("/bar/*:/*", "GET,POST");
129             fail("/bar/*:/");
130         } catch(IllegalArgumentException iae) {
131         }
132
133         try {
134             permission = new WebResourcePermission("/bar/stool/*:/bar", "GET,POST");
135             fail("/bar/stool/*:/bar");
136         } catch(IllegalArgumentException iae) {
137         }
138         
139     }
140
141     public void testImpliesStringString() {
142
143         // The argument is an instanceof WebResourcePermission
144
Permission pA = new WebResourcePermission("/foo", "");
145         Permission pB = new WebUserDataPermission("/foo", "");
146         
147         assertFalse(pA.implies(pB));
148         assertFalse(pB.implies(pA));
149     
150         pA = new WebResourcePermission("/foo", "");
151         pB = new WebResourcePermission("/foo", "GET,POST");
152         
153         assertTrue(pA.implies(pB));
154         assertFalse(pB.implies(pA));
155         
156         pA = new WebResourcePermission("/foo/*:/foo/bar", "");
157         pB = new WebResourcePermission("/foo/bar", "");
158         
159         assertFalse(pA.implies(pB));
160         assertFalse(pB.implies(pA));
161
162         pA = new WebResourcePermission("/foo/bar/*:/foo/bar/cat/dog", "");
163         pB = new WebResourcePermission("/foo/bar/*:/foo/bar/cat/*", "");
164
165         assertTrue(pA.implies(pB));
166         assertFalse(pB.implies(pA));
167
168         pA = new WebResourcePermission("/:/a.jsp:/b.jsp:/c.jsp", "GET,POST,PUT,DELETE,HEAD,OPTIONS,TRACE");
169         pB = new WebResourcePermission("/:/a.jsp:/c.jsp:/b.jsp", (String) null);
170
171         assertTrue(pA.implies(pB));
172         assertTrue(pB.implies(pA));
173     }
174
175     /*
176      * Testing WebResourcePermission(String, String[])
177      */

178     public void testConstructorStringStringArray() {
179     }
180     
181     public void testImpliesStringStringArray() {
182     }
183
184     /*
185      * Testing WebResourcePermission(HttpServletRequest)
186      */

187     public void testConstructorHttpServletRequest() {
188     }
189     
190     public void testImpliesHttpServletRequest() {
191     }
192     
193     public static void main(String[] args) {
194         WebResourcePermissionTest test = new WebResourcePermissionTest();
195         test.testConstructorStringString();
196         test.testImpliesStringString();
197         test.testConstructorStringStringArray();
198         test.testImpliesStringStringArray();
199         test.testConstructorHttpServletRequest();
200         test.testImpliesHttpServletRequest();
201     }
202 }
203
204
Popular Tags