KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > registry > TestMutableRequestRegistry


1 /* The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
2 *
3 * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. The end-user documentation included with the redistribution,
18 * if any, must include the following acknowledgment:
19 * "This product includes software developed by Jcorporate Ltd.
20 * (http://www.jcorporate.com/)."
21 * Alternately, this acknowledgment may appear in the software itself,
22 * if and wherever such third-party acknowledgments normally appear.
23 *
24 * 4. "Jcorporate" and product names such as "Expresso" must
25 * not be used to endorse or promote products derived from this
26 * software without prior written permission. For written permission,
27 * please contact info@jcorporate.com.
28 *
29 * 5. Products derived from this software may not be called "Expresso",
30 * or other Jcorporate product names; nor may "Expresso" or other
31 * Jcorporate product names appear in their name, without prior
32 * written permission of Jcorporate Ltd.
33 *
34 * 6. No product derived from this software may compete in the same
35 * market space, i.e. framework, without prior written permission
36 * of Jcorporate Ltd. For written permission, please contact
37 * partners@jcorporate.com.
38 *
39 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
45 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This software consists of voluntary contributions made by many
54 * individuals on behalf of the Jcorporate Ltd. Contributions back
55 * to the project(s) are encouraged when you make modifications.
56 * Please send them to support@jcorporate.com. For more information
57 * on Jcorporate Ltd. and its products, please see
58 * <http://www.jcorporate.com/>.
59 *
60 * Portions of this software are based upon other open source
61 * products and are subject to their respective licenses.
62  */

63
64 package com.jcorporate.expresso.core.registry;
65
66 import com.jcorporate.expresso.core.security.SuperUser;
67 import com.jcorporate.expresso.core.security.User;
68 import com.jcorporate.expresso.services.test.TestSystemInitializer;
69 import junit.framework.TestCase;
70
71 /**
72  * @author Michael Rimov
73  * @version 1.0
74  */

75 public class TestMutableRequestRegistry extends TestCase {
76     private MutableRequestRegistry mutableRequestRegistry = null;
77
78     protected void setUp() throws Exception JavaDoc {
79         super.setUp();
80         User admin = User.getUserFromId(User.getAdminId(TestSystemInitializer.getTestContext()),
81                 TestSystemInitializer.getTestContext());
82
83
84         mutableRequestRegistry = new MutableRequestRegistry(TestSystemInitializer
85                 .getTestContext(), admin);
86     }
87
88     protected void tearDown() throws Exception JavaDoc {
89         mutableRequestRegistry.releaseSettings();
90         mutableRequestRegistry = null;
91         super.tearDown();
92         //Reset to administrator. Cleanup so that the following
93
//threads can continue on nicely.
94
new MutableRequestRegistry(TestSystemInitializer
95                 .getTestContext(),
96                 SuperUser.SUPER_USER);
97     }
98
99     public void testReleaseSettings() {
100         mutableRequestRegistry.releaseSettings();
101         assertEquals(null, RequestRegistry.getDataContext());
102         assertEquals(null, RequestRegistry.getDataContext());
103     }
104
105     public void testSetDataContext() {
106         assertEquals(TestSystemInitializer.getTestContext(),
107                 RequestRegistry.getDataContext());
108
109         mutableRequestRegistry.setDataContext("abcdefg");
110         assertEquals("abcdefg",
111                 RequestRegistry.getDataContext());
112     }
113
114     public void testSetUser() throws Exception JavaDoc {
115         User u = new User();
116         u.setDataContext(TestSystemInitializer.getTestContext());
117         u.setLoginName(User.UNKNOWN_USER);
118         if (!u.find()) {
119             throw new IllegalStateException JavaDoc("Couldn't find Unknown user");
120         }
121
122         mutableRequestRegistry.setUser(u);
123
124         assertEquals(u.getLoginName(),
125                 RequestRegistry.getUser().getLoginName());
126     }
127
128     public void testMutableRequestRegistry() {
129         mutableRequestRegistry = new MutableRequestRegistry();
130         assertEquals(null, RequestRegistry.getDataContext());
131         assertEquals(null, RequestRegistry.getUser());
132     }
133
134     public void testMutableRequestRegistry1() throws Exception JavaDoc {
135         User u = new User();
136         u.setDataContext(TestSystemInitializer.getTestContext());
137         u.setLoginName(User.UNKNOWN_USER);
138         if (!u.find()) {
139             throw new IllegalStateException JavaDoc("Couldn't find Unknown user");
140         }
141         String JavaDoc dataContext = "testing!Testing!";
142         mutableRequestRegistry = new MutableRequestRegistry(dataContext, u);
143         assertEquals(dataContext, RequestRegistry.getDataContext());
144         assertEquals(u.getLoginName(), RequestRegistry.getUser().getLoginName());
145     }
146
147     public void testGetDataContext() {
148         assertEquals(TestSystemInitializer.getTestContext(),
149                 RequestRegistry.getDataContext());
150     }
151
152     public void testGetUser() throws Exception JavaDoc {
153         assertEquals(User.getAdminId(TestSystemInitializer.getTestContext()),
154                 RequestRegistry.getUser().getUid());
155     }
156
157     public void testSuperUser() throws Exception JavaDoc {
158         User u = new User();
159         u.setDataContext(TestSystemInitializer.getTestContext());
160         u.setLoginName(User.UNKNOWN_USER);
161         if (!u.find()) {
162             throw new IllegalStateException JavaDoc("Couldn't find Unknown user");
163         }
164         RequestRegistry.superUser(u);
165         assertEquals(User.UNKNOWN_USER, RequestRegistry.getUser().getLoginName());
166     }
167
168
169     public void testRevertUser() throws Exception JavaDoc {
170         User u = new User();
171         u.setDataContext(TestSystemInitializer.getTestContext());
172         u.setLoginName(User.UNKNOWN_USER);
173         if (!u.find()) {
174             throw new IllegalStateException JavaDoc("Couldn't find Unknown user");
175         }
176         RequestRegistry.superUser(u);
177         User newUser = RequestRegistry.revertUser();
178         assertEquals(newUser.getLoginName(), RequestRegistry.getUser().getLoginName());
179         assertEquals(User.getAdminId(TestSystemInitializer.getTestContext()),
180                 RequestRegistry.getUser().getUid());
181     }
182
183     public void testRevertUserShouldThrowExceptionForUnmatchedPushPops() throws Exception JavaDoc {
184         try {
185             RequestRegistry.revertUser();
186             fail("RequestRegistry.revertUser should have thrown " +
187                     "ArrayIndexOutOfBoundsException");
188
189         } catch (java.lang.ArrayIndexOutOfBoundsException JavaDoc ex) {
190             //A-ok
191
}
192
193         User u = new User();
194         u.setDataContext(TestSystemInitializer.getTestContext());
195         u.setLoginName(User.UNKNOWN_USER);
196         if (!u.find()) {
197             throw new IllegalStateException JavaDoc("Couldn't find Unknown user");
198         }
199         RequestRegistry.superUser(u);
200         User newUser = RequestRegistry.revertUser();
201
202         try {
203             RequestRegistry.revertUser();
204             fail("RequestRegistry.revertUser should have thrown " +
205                     "ArrayIndexOutOfBoundsException");
206
207         } catch (java.lang.ArrayIndexOutOfBoundsException JavaDoc ex) {
208             //A-ok
209
}
210     }
211
212
213 }
214
Popular Tags