KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > config > test > VergeBaseConfigTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.config.test;
8
9
10 import javax.servlet.ServletException JavaDoc;
11
12 import org.jdom.Document;
13 import org.jdom.Element;
14
15 import com.inversoft.junit.WebTestCase;
16 import com.inversoft.verge.config.Constants;
17 import com.inversoft.verge.config.VergeBaseConfigBuilder;
18 import com.inversoft.verge.config.VergeBaseConfigFactory;
19 import com.inversoft.verge.config.VergeConfigConstants;
20 import com.inversoft.verge.config.servlet.ConfigServlet;
21 import com.inversoft.verge.mvc.MVCRegistry;
22
23
24 /**
25  * <p>
26  * This class has the tests for the base verge config.
27  * </p>
28  *
29  * @author Brian Pontarelli
30  * @since 2.0
31  * @version 2.0
32  */

33 public class VergeBaseConfigTest extends WebTestCase {
34
35     /**
36      * Constructor for VergeBaseConfigTest.
37      *
38      * @param name
39      */

40     public VergeBaseConfigTest(String JavaDoc name) {
41         super(name);
42         setLocal(true);
43     }
44
45
46     public void testFactory() {
47         VergeBaseConfigFactory factory = new VergeBaseConfigFactory();
48         assertNull("Should be null registry", factory.createRegistry());
49         assertNotNull("Should not be null builder", factory.createBuilder());
50         assertEquals("Should be same builder", factory.createBuilder(),
51             factory.createBuilder());
52     }
53
54     public void testMVC() {
55         Element root = new Element("verge");
56         Document doc = new Document(root);
57
58         Element mvc = new Element(Constants.MVC_ELEMENT);
59         root.addContent(mvc);
60
61         Element controllerParser = new Element(Constants.CONTROLLER_PARSER_ELEMENT);
62         controllerParser.setAttribute(Constants.NAME_ATTRIBUTE, "controllerParserFoo");
63         controllerParser.setText("com.inversoft.verge.mvc.test.TestControllerParser");
64         mvc.addContent(controllerParser);
65
66         Element modelParser = new Element(Constants.MODEL_PARSER_ELEMENT);
67         modelParser.setAttribute(Constants.NAME_ATTRIBUTE, "modelParserFoo");
68         modelParser.setText("com.inversoft.verge.mvc.test.TestModelParser");
69         mvc.addContent(modelParser);
70
71         Element validatorParser = new Element(Constants.VALIDATOR_PARSER_ELEMENT);
72         validatorParser.setAttribute(Constants.NAME_ATTRIBUTE, "validatorParserFoo");
73         validatorParser.setText("com.inversoft.verge.mvc.test.TestValidatorParser");
74         mvc.addContent(validatorParser);
75
76         Element validatorHandler = new Element(Constants.VALIDATOR_HANDLER_ELEMENT);
77         validatorHandler.setAttribute(Constants.NAME_ATTRIBUTE, "validatorHandlerFoo");
78         validatorHandler.setText("com.inversoft.verge.mvc.test.TestValidatorHandler");
79         mvc.addContent(validatorHandler);
80
81         Element controllerHandler = new Element(Constants.CONTROLLER_HANDLER_ELEMENT);
82         controllerHandler.setAttribute(Constants.NAME_ATTRIBUTE, "controllerHandlerFoo");
83         controllerHandler.setText("com.inversoft.verge.mvc.test.TestControllerHandler");
84         mvc.addContent(controllerHandler);
85
86         Element modelResolver = new Element(Constants.MODEL_RESOLVER_ELEMENT);
87         modelResolver.setAttribute(Constants.NAME_ATTRIBUTE, "modelResolverFoo");
88         modelResolver.setText("com.inversoft.verge.mvc.test.TestModelResolver");
89         mvc.addContent(modelResolver);
90
91         Element modelHandler = new Element(Constants.MODEL_HANDLER_ELEMENT);
92         modelHandler.setAttribute(Constants.NAME_ATTRIBUTE, "modelHandlerFoo");
93         modelHandler.setText("com.inversoft.verge.mvc.test.TestModelHandler");
94         mvc.addContent(modelHandler);
95
96         VergeBaseConfigBuilder builder = new VergeBaseConfigBuilder();
97         builder.build(doc, null);
98
99         assertNotNull(MVCRegistry.lookupControllerParser("controllerParserFoo"));
100         assertNotNull(MVCRegistry.lookupModelParser("modelParserFoo"));
101         assertNotNull(MVCRegistry.lookupValidatorParser("validatorParserFoo"));
102         assertNotNull(MVCRegistry.lookupControllerHandler("controllerHandlerFoo"));
103         assertNotNull(MVCRegistry.lookupModelHandler("modelHandlerFoo"));
104         assertNotNull(MVCRegistry.lookupValidatorHandler("validatorHandlerFoo"));
105         assertNotNull(MVCRegistry.lookupModelResolver("modelResolverFoo"));
106     }
107
108     /**
109      * Test the loading of the configuration via the the config servlet
110      */

111     public void testConfigServlet() {
112         getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
113             "src/com/inversoft/verge/config/test/verge-test.xml");
114         try {
115             ConfigServlet servlet = new ConfigServlet();
116             servlet.init(createConfig("ConfigServlet"));
117         } catch (ServletException JavaDoc se) {
118             fail(se.toString());
119         }
120
121         assertNotNull(MVCRegistry.lookupControllerParser("cp"));
122         assertNotNull(MVCRegistry.lookupModelParser("mp"));
123         assertNotNull(MVCRegistry.lookupValidatorParser("vp"));
124         assertNotNull(MVCRegistry.lookupControllerHandler("ch"));
125         assertNotNull(MVCRegistry.lookupModelHandler("mh"));
126         assertNotNull(MVCRegistry.lookupValidatorHandler("vh"));
127         assertNotNull(MVCRegistry.lookupModelResolver("mr"));
128     }
129
130     /**
131      * Tests bad classes
132      */

133     public void testBadClass() {
134         Element root = new Element("verge");
135         Document doc = new Document(root);
136
137         Element mvc = new Element(Constants.MVC_ELEMENT);
138         root.addContent(mvc);
139
140         Element controllerParser = new Element(Constants.CONTROLLER_PARSER_ELEMENT);
141         controllerParser.setAttribute(Constants.NAME_ATTRIBUTE, "testControllerParser");
142         controllerParser.setText("com.inversoft.verge.mvc.test.TestControllerHandler");
143         mvc.addContent(controllerParser);
144
145         VergeBaseConfigBuilder builder = new VergeBaseConfigBuilder();
146         builder.build(doc, null);
147
148         assertNull(MVCRegistry.lookupControllerParser("testControllerParser"));
149     }
150
151     /**
152      * Tests bad wrong type
153      */

154     public void testWrongType() {
155         Element root = new Element("verge");
156         Document doc = new Document(root);
157
158         Element mvc = new Element(Constants.MVC_ELEMENT);
159         root.addContent(mvc);
160
161         Element controllerParser = new Element(Constants.CONTROLLER_PARSER_ELEMENT);
162         controllerParser.setAttribute(Constants.NAME_ATTRIBUTE, "testControllerParser2");
163         controllerParser.setText("com.inversoft.verge.config.test.BadClass");
164         mvc.addContent(controllerParser);
165
166         VergeBaseConfigBuilder builder = new VergeBaseConfigBuilder();
167         builder.build(doc, null);
168
169         assertNull(MVCRegistry.lookupControllerParser("testControllerParser2"));
170     }
171 }
Popular Tags