001: /* The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
002: *
003: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution,
018: * if any, must include the following acknowledgment:
019: * "This product includes software developed by Jcorporate Ltd.
020: * (http://www.jcorporate.com/)."
021: * Alternately, this acknowledgment may appear in the software itself,
022: * if and wherever such third-party acknowledgments normally appear.
023: *
024: * 4. "Jcorporate" and product names such as "Expresso" must
025: * not be used to endorse or promote products derived from this
026: * software without prior written permission. For written permission,
027: * please contact info@jcorporate.com.
028: *
029: * 5. Products derived from this software may not be called "Expresso",
030: * or other Jcorporate product names; nor may "Expresso" or other
031: * Jcorporate product names appear in their name, without prior
032: * written permission of Jcorporate Ltd.
033: *
034: * 6. No product derived from this software may compete in the same
035: * market space, i.e. framework, without prior written permission
036: * of Jcorporate Ltd. For written permission, please contact
037: * partners@jcorporate.com.
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
043: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
044: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
045: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE.
051: * ====================================================================
052: *
053: * This software consists of voluntary contributions made by many
054: * individuals on behalf of the Jcorporate Ltd. Contributions back
055: * to the project(s) are encouraged when you make modifications.
056: * Please send them to support@jcorporate.com. For more information
057: * on Jcorporate Ltd. and its products, please see
058: * <http://www.jcorporate.com/>.
059: *
060: * Portions of this software are based upon other open source
061: * products and are subject to their respective licenses.
062: */
063:
064: package com.jcorporate.expresso.core.registry;
065:
066: import com.jcorporate.expresso.core.security.SuperUser;
067: import com.jcorporate.expresso.core.security.User;
068: import com.jcorporate.expresso.services.test.TestSystemInitializer;
069: import junit.framework.TestCase;
070:
071: /**
072: * @author Michael Rimov
073: * @version 1.0
074: */
075: public class TestMutableRequestRegistry extends TestCase {
076: private MutableRequestRegistry mutableRequestRegistry = null;
077:
078: protected void setUp() throws Exception {
079: super .setUp();
080: User admin = User.getUserFromId(User
081: .getAdminId(TestSystemInitializer.getTestContext()),
082: TestSystemInitializer.getTestContext());
083:
084: mutableRequestRegistry = new MutableRequestRegistry(
085: TestSystemInitializer.getTestContext(), admin);
086: }
087:
088: protected void tearDown() throws Exception {
089: mutableRequestRegistry.releaseSettings();
090: mutableRequestRegistry = null;
091: super .tearDown();
092: //Reset to administrator. Cleanup so that the following
093: //threads can continue on nicely.
094: new MutableRequestRegistry(TestSystemInitializer
095: .getTestContext(), SuperUser.SUPER_USER);
096: }
097:
098: public void testReleaseSettings() {
099: mutableRequestRegistry.releaseSettings();
100: assertEquals(null, RequestRegistry.getDataContext());
101: assertEquals(null, RequestRegistry.getDataContext());
102: }
103:
104: public void testSetDataContext() {
105: assertEquals(TestSystemInitializer.getTestContext(),
106: RequestRegistry.getDataContext());
107:
108: mutableRequestRegistry.setDataContext("abcdefg");
109: assertEquals("abcdefg", RequestRegistry.getDataContext());
110: }
111:
112: public void testSetUser() throws Exception {
113: User u = new User();
114: u.setDataContext(TestSystemInitializer.getTestContext());
115: u.setLoginName(User.UNKNOWN_USER);
116: if (!u.find()) {
117: throw new IllegalStateException(
118: "Couldn't find Unknown user");
119: }
120:
121: mutableRequestRegistry.setUser(u);
122:
123: assertEquals(u.getLoginName(), RequestRegistry.getUser()
124: .getLoginName());
125: }
126:
127: public void testMutableRequestRegistry() {
128: mutableRequestRegistry = new MutableRequestRegistry();
129: assertEquals(null, RequestRegistry.getDataContext());
130: assertEquals(null, RequestRegistry.getUser());
131: }
132:
133: public void testMutableRequestRegistry1() throws Exception {
134: User u = new User();
135: u.setDataContext(TestSystemInitializer.getTestContext());
136: u.setLoginName(User.UNKNOWN_USER);
137: if (!u.find()) {
138: throw new IllegalStateException(
139: "Couldn't find Unknown user");
140: }
141: String dataContext = "testing!Testing!";
142: mutableRequestRegistry = new MutableRequestRegistry(
143: dataContext, u);
144: assertEquals(dataContext, RequestRegistry.getDataContext());
145: assertEquals(u.getLoginName(), RequestRegistry.getUser()
146: .getLoginName());
147: }
148:
149: public void testGetDataContext() {
150: assertEquals(TestSystemInitializer.getTestContext(),
151: RequestRegistry.getDataContext());
152: }
153:
154: public void testGetUser() throws Exception {
155: assertEquals(User.getAdminId(TestSystemInitializer
156: .getTestContext()), RequestRegistry.getUser().getUid());
157: }
158:
159: public void testSuperUser() throws Exception {
160: User u = new User();
161: u.setDataContext(TestSystemInitializer.getTestContext());
162: u.setLoginName(User.UNKNOWN_USER);
163: if (!u.find()) {
164: throw new IllegalStateException(
165: "Couldn't find Unknown user");
166: }
167: RequestRegistry.super User(u);
168: assertEquals(User.UNKNOWN_USER, RequestRegistry.getUser()
169: .getLoginName());
170: }
171:
172: public void testRevertUser() throws Exception {
173: User u = new User();
174: u.setDataContext(TestSystemInitializer.getTestContext());
175: u.setLoginName(User.UNKNOWN_USER);
176: if (!u.find()) {
177: throw new IllegalStateException(
178: "Couldn't find Unknown user");
179: }
180: RequestRegistry.super User(u);
181: User newUser = RequestRegistry.revertUser();
182: assertEquals(newUser.getLoginName(), RequestRegistry.getUser()
183: .getLoginName());
184: assertEquals(User.getAdminId(TestSystemInitializer
185: .getTestContext()), RequestRegistry.getUser().getUid());
186: }
187:
188: public void testRevertUserShouldThrowExceptionForUnmatchedPushPops()
189: throws Exception {
190: try {
191: RequestRegistry.revertUser();
192: fail("RequestRegistry.revertUser should have thrown "
193: + "ArrayIndexOutOfBoundsException");
194:
195: } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
196: //A-ok
197: }
198:
199: User u = new User();
200: u.setDataContext(TestSystemInitializer.getTestContext());
201: u.setLoginName(User.UNKNOWN_USER);
202: if (!u.find()) {
203: throw new IllegalStateException(
204: "Couldn't find Unknown user");
205: }
206: RequestRegistry.super User(u);
207: User newUser = RequestRegistry.revertUser();
208:
209: try {
210: RequestRegistry.revertUser();
211: fail("RequestRegistry.revertUser should have thrown "
212: + "ArrayIndexOutOfBoundsException");
213:
214: } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
215: //A-ok
216: }
217: }
218:
219: }
|