KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > tests > DBSecurityMatrixTests


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

64
65 package com.jcorporate.expresso.services.controller.tests;
66
67 import com.jcorporate.expresso.core.controller.Block;
68 import com.jcorporate.expresso.core.controller.ControllerElement;
69 import com.jcorporate.expresso.core.controller.ControllerResponse;
70 import com.jcorporate.expresso.core.controller.Input;
71 import com.jcorporate.expresso.core.controller.Output;
72 import com.jcorporate.expresso.core.controller.Transition;
73 import com.jcorporate.expresso.services.test.ControllerTestCase;
74 import com.jcorporate.expresso.services.test.ControllerTestSuite;
75 import junit.framework.TestSuite;
76 import org.apache.cactus.WebRequest;
77
78 import java.util.Enumeration JavaDoc;
79
80
81 /**
82  * Unit tests some of the states for the DBSecurityMatrix Controller.
83  *
84  * @author Michael Rimov
85  * @version $Revision: 1.2 $ $Date: 2004/11/17 20:48:23 $
86  * @todo Implement 'update state' unit tests
87  */

88 public class DBSecurityMatrixTests
89         extends ControllerTestCase {
90     /**
91      * Instantiates this controller. All that is needed is call super() with
92      * the testName passed to it as well as the name of the class we're testing.
93      *
94      * @param testName the name of the test
95      * @throws Exception upon error
96      */

97     public DBSecurityMatrixTests(String JavaDoc testName)
98             throws Exception JavaDoc {
99         super(testName,
100                 com.jcorporate.expresso.services.controller.DBSecurityMatrix.class);
101     }
102
103     /**
104      * Convenience method so we can test just this class.
105      *
106      * @param args the Command line arguments
107      * @throws Exception upon error
108      */

109     public static void main(String JavaDoc[] args)
110             throws Exception JavaDoc {
111         junit.textui.TestRunner.run(suite());
112     }
113
114     /**
115      * Determines what tests are run in the test suite. We also tell ControllerTestSuite
116      * that we need ExpressoSchema to exist for our tests. We also tell it that
117      * we aren't modifying the underlying database for our own tests so the schema
118      * doesn't necessarily have to be deleted when we're done with it.
119      *
120      * @return Instantiated TestSuite
121      * @throws Exception upon error
122      */

123     public static TestSuite suite()
124             throws Exception JavaDoc {
125         ControllerTestSuite cts = new ControllerTestSuite();
126         cts.addReadOnlySchemaDependency(com.jcorporate.expresso.core.ExpressoSchema.class);
127         cts.addTestSuite(DBSecurityMatrixTests.class);
128
129         return cts;
130     }
131
132     /**
133      * The next three functions are for testing the prompt state.
134      * beginPromptState is executed on the client side. It sets up the appropriate
135      * parameters including using what Controller state we're testing. As well
136      * as setting up cookies for proper security credentials
137      *
138      * @param theRequest a "hyper" ServletTestRequest that allows us to set URLS,
139      * set cookies, set URL parameters, etc before the request is sent to the
140      * servlet container.
141      * @throws Exception if something went wrong with the Crypto stuff
142      */

143     public void beginPromptState(WebRequest theRequest)
144             throws Exception JavaDoc {
145         super.logIn(theRequest);
146         super.setupParameters("prompt", theRequest);
147     }
148
149     /**
150      * Executed on the server side. All it has to do is call the super's
151      * controllerProcess() to the Controller's State properly executed.
152      *
153      * @throws Exception upon error
154      */

155     public void testPromptState()
156             throws Exception JavaDoc {
157         ControllerResponse response = super.controllerProcess();
158         assertTrue("Got a null response", response != null);
159         assertTrue("Incorrect Number Of Inputs",
160                 response.getInputs().size() == 2);
161         assertTrue("Incorrect Number Of Transitions",
162                 response.getTransitions().size() == 1);
163         assertTrue("GroupNames are not multivalued",
164                 response.getInput("GroupName").getValidValues().size() > 0);
165         assertTrue("SchemaClass is not multivalued",
166                 response.getInput("SchemaClass").getValidValues().size() > 0);
167     }
168
169     /**
170      * The next three classes are for testing getting the dbobjmatrix state for
171      * the dbobject security matrix.
172      * beginSecurityMatrix() is executed on the client side. It sets up the appropriate
173      * parameters including using what Controller state we're testing. As well
174      * as setting up cookies for proper security credentials
175      *
176      * @param theRequest the Cactus WebRequest object
177      * @throws Exception if something went wrong with the Crypto stuff
178      */

179     public void beginSecurityMatrix(WebRequest theRequest)
180             throws Exception JavaDoc {
181         super.logIn(theRequest);
182         super.setupParameters("dbobjmatrix", theRequest);
183         theRequest.addParameter("SchemaClass",
184                 "com.jcorporate.expresso.core.ExpressoSchema");
185         theRequest.addParameter("GroupName", "Admin");
186     }
187
188     /**
189      * Executed on the server side. All it has to do is call the super's
190      * controllerProcess() to the Controller's State properly executed.
191      *
192      * @throws Exception upon error
193      */

194     public void testSecurityMatrix()
195             throws Exception JavaDoc {
196         ControllerResponse theResponse = super.controllerProcess();
197         assertTrue("Got a null response", theResponse != null);
198         assertTrue("Returned one security matrix block",
199                 theResponse.getBlocks().size() == 1);
200
201         Block b = (Block) theResponse.getBlocks().elementAt(0);
202         Block matrix = (Block) b.getNested().elementAt(0);
203         assertTrue("Batch of security matrix blocks",
204                 matrix.getBlocks().size() > 4);
205
206         //Check each oneRow nested block and make sure that it's properly formed.
207
for (Enumeration JavaDoc eRow = matrix.getNested().elements();
208              eRow.hasMoreElements();) {
209             ControllerElement ce = (ControllerElement) eRow.nextElement();
210
211             if (ce instanceof Block) {
212                 Block oneRow = (Block) ce;
213
214                 if (ce.getName().equals("oneRow")) {
215                     int outputCount = 0;
216                     int inputCount = 0;
217                     assertTrue("Five Elements for Security Matrix Row",
218                             oneRow.getNested().size() == 5 //BUG BUG: Why is there an empty null element at the end
219
);
220
221                     for (Enumeration JavaDoc e = oneRow.getNested().elements();
222                          e.hasMoreElements();) {
223                         ControllerElement oneElement = (ControllerElement) e.nextElement();
224
225                         if (oneElement != null) {
226                             if (oneElement instanceof Output) {
227                                 assertTrue("Output Label is non-empty string",
228                                         ((Output) oneElement).getContent().length() > 0);
229                                 outputCount++;
230                             } else if (oneElement instanceof Input) {
231                                 assertTrue("Input is a check box",
232                                         oneElement.getAttribute("checkbox") != null);
233                                 inputCount++;
234                             } else if (oneElement instanceof Transition) {
235                                 fail("Did not expect a tranition within the security matrix fields");
236                             } else {
237                                 fail("Got Controller Element of: " +
238                                         oneElement.getClass().getName() +
239                                         " not expected element.");
240                             }
241                         }
242                     }
243
244                     assertTrue("Proper number of outputs for row",
245                             outputCount == 1);
246                     assertTrue("Proper number of inputs for a row",
247                             inputCount == 4);
248                 } else if (ce.getName().equals("TransitionRow")) {
249                     super.getLog().debug("Got Here");
250                 }
251             } else {
252                 fail("Expected Matrix to have only blocks nested");
253             }
254         }
255     }
256 }
Popular Tags