KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > StandardContextSFTest


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

16 package org.apache.catalina.storeconfig;
17
18 import java.io.PrintWriter JavaDoc;
19 import java.io.StringWriter JavaDoc;
20
21 import javax.naming.directory.DirContext JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.catalina.LifecycleListener;
26 import org.apache.catalina.Valve;
27 import org.apache.catalina.core.StandardContext;
28 import org.apache.catalina.core.StandardHost;
29 import org.apache.catalina.deploy.ContextResourceEnvRef;
30 import org.apache.catalina.deploy.ContextResourceLink;
31 import org.apache.catalina.deploy.NamingResources;
32 import org.apache.catalina.realm.JAASRealm;
33 import org.apache.catalina.session.FileStore;
34 import org.apache.catalina.session.JDBCStore;
35 import org.apache.catalina.session.PersistentManager;
36 import org.apache.catalina.session.StandardManager;
37 import org.apache.catalina.storeconfig.StandardContextSF;
38 import org.apache.catalina.storeconfig.StoreDescription;
39 import org.apache.catalina.storeconfig.StoreRegistry;
40 import org.apache.naming.resources.FileDirContext;
41 import org.apache.naming.resources.ProxyDirContext;
42 import org.apache.naming.resources.WARDirContext;
43
44 /**
45  * @author Peter Rossbach
46  *
47  */

48 public class StandardContextSFTest extends TestCase {
49     StoreRegistry registry;
50
51     StringWriter JavaDoc writer = new StringWriter JavaDoc();
52
53     PrintWriter JavaDoc pWriter = new PrintWriter JavaDoc(writer);
54
55     StandardContext standardContext;
56
57     StandardContextSF factory;
58
59     StoreDescription desc;
60
61     /*
62      * create registery and register Context and all subelements
63      *
64      * @see junit.framework.TestCase#setUp()
65      */

66     protected void setUp() throws Exception JavaDoc {
67
68         super.setUp();
69         registry = new StoreRegistry();
70         desc = new StoreDescription();
71         desc.setTag("Context");
72         desc.setTagClass("org.apache.catalina.core.StandardContext");
73         desc.setStandard(true);
74         desc
75                 .setStoreFactoryClass("org.apache.catalina.storeconfig.StandardContextSF");
76         String JavaDoc exceptions[] = { "available", "configFile", "configured",
77                 "distributable", "domain", "engineName", "name", "override",
78                 "publicId", "replaceWelcomeFiles", "sessionTimeout",
79                 "startupTime", "tldScanTime" };
80         for (int i = 0; i < exceptions.length; i++)
81             desc.addTransientAttribute(exceptions[i]);
82
83         registry.registerDescription(desc);
84         factory = new StandardContextSF();
85         desc.setStoreFactory(factory);
86         factory.setRegistry(registry);
87
88         StoreDescription listenerdesc = registerDescriptor("Listener",
89                 LifecycleListener.class);
90
91         String JavaDoc listenerskippables[] = {
92                 "org.apache.catalina.core.NamingContextListener",
93                 "org.apache.catalina.startup.ContextConfig", };
94         for (int i = 0; i < listenerskippables.length; i++)
95             listenerdesc.addTransientChild(listenerskippables[i]);
96
97         StoreDescription realmdesc = registerDescriptor("Realm",
98                 JAASRealm.class,
99                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
100                 false);
101         StoreDescription managerdesc = registerDescriptor("Manager",
102                 StandardManager.class,
103                 "org.apache.catalina.storeconfig.ManagerSF", false, false);
104         managerdesc.addTransientAttribute("entropy");
105         managerdesc.addTransientAttribute("distributable");
106         StoreDescription pmanagerdesc = registerDescriptor("Manager",
107                 PersistentManager.class,
108                 "org.apache.catalina.storeconfig.PersistentManagerSF", false,
109                 false);
110         pmanagerdesc.addTransientAttribute("entropy");
111         pmanagerdesc.addTransientAttribute("distributable");
112         DescriptorHelper.registerDescriptor(pmanagerdesc, registry,
113                 FileStore.class.getName(), "Store", FileStore.class.getName(),
114                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
115                 false);
116         DescriptorHelper.registerDescriptor(pmanagerdesc, registry,
117                 JDBCStore.class.getName(), "Store", JDBCStore.class.getName(),
118                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
119                 false);
120         DescriptorHelper.registerNamingDescriptor(desc, registry);
121         StoreDescription valvedesc = registerDescriptor("Valve", Valve.class,
122                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
123                 false);
124         String JavaDoc skippables[] = {
125                 "org.apache.catalina.authenticator.BasicAuthenticator",
126                 "org.apache.catalina.authenticator.DigestAuthenticator",
127                 "org.apache.catalina.authenticator.FormAuthenticator",
128                 "org.apache.catalina.authenticator.NonLoginAuthenticator",
129                 "org.apache.catalina.authenticator.SSLAuthenticator",
130                 "org.apache.catalina.core.StandardContextValve",
131                 "org.apache.catalina.valves.CertificatesValve" };
132         for (int i = 0; i < skippables.length; i++)
133             valvedesc.addTransientChild(skippables[i]);
134
135         StoreDescription resdesc = registerDescriptor("Resources",
136                 DirContext JavaDoc.class,
137                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
138                 false);
139         resdesc.addTransientAttribute("docBase");
140         resdesc.addTransientAttribute("allowLinking");
141         resdesc.addTransientAttribute("cacheMaxSize");
142         resdesc.addTransientAttribute("cacheTTL");
143         resdesc.addTransientAttribute("caseSensitive");
144         resdesc.addTransientChild(FileDirContext.class.getName());
145         resdesc.addTransientChild(ProxyDirContext.class.getName());
146         resdesc.addTransientChild(WARDirContext.class.getName());
147         standardContext = new StandardContext();
148         standardContext.setPath("/myapps");
149         standardContext.setDocBase("myapps");
150
151     }
152
153     private StoreDescription registerDescriptor(String JavaDoc tag, Class JavaDoc aClass) {
154         return registerDescriptor(tag, aClass,
155                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
156                 false);
157     }
158
159     private StoreDescription registerDescriptor(String JavaDoc tag, Class JavaDoc aClass,
160             String JavaDoc factoryClass, boolean fstandard, boolean fdefault) {
161         return DescriptorHelper.registerDescriptor(desc, registry, aClass
162                 .getName(), tag, aClass.getName(), factoryClass, fstandard,
163                 fdefault);
164     }
165
166     /**
167      * @TODO Listener only saved at real context. Wrong when next Context
168      * deployed! see Changes.txt
169      * @throws Exception
170      */

171     public void testListenerStore() throws Exception JavaDoc {
172         standardContext
173                 .addLifecycleListener(new org.apache.catalina.storeconfig.InfoLifecycleListener());
174         standardContext
175                 .addInstanceListener("org.apache.catalina.ContainerListener");
176         standardContext
177                 .addWrapperListener("org.apache.catalina.ContainerListener");
178         standardContext
179                 .addWrapperLifecycle("org.apache.catalina.ContainerListener");
180         standardContext.addWatchedResource("/tmp/reloaded");
181         String JavaDoc aspectedResult = "<Context"
182                 + LF.LINE_SEPARATOR
183                 + " docBase=\"myapps\""
184                 + LF.LINE_SEPARATOR
185                 + " path=\"/myapps\">"
186                 + LF.LINE_SEPARATOR
187                 + " <Listener className=\"org.apache.catalina.storeconfig.InfoLifecycleListener\"/>"
188                 + LF.LINE_SEPARATOR
189                 + " <InstanceListener>org.apache.catalina.ContainerListener</InstanceListener>"
190                 + LF.LINE_SEPARATOR
191                 + " <WrapperListener>org.apache.catalina.ContainerListener</WrapperListener>"
192                 + LF.LINE_SEPARATOR
193                 + " <WrapperLifecycle>org.apache.catalina.ContainerListener</WrapperLifecycle>"
194                 + LF.LINE_SEPARATOR
195                 + " <WatchedResource>/tmp/reloaded</WatchedResource>"
196                 + LF.LINE_SEPARATOR + "</Context>" + LF.LINE_SEPARATOR;
197         check(aspectedResult);
198     }
199
200     public void testNamingStore() throws Exception JavaDoc {
201         standardContext
202                 .addLifecycleListener(new org.apache.catalina.storeconfig.InfoLifecycleListener());
203         NamingResources resources = standardContext.getNamingResources();
204         ContextResourceEnvRef ref = new ContextResourceEnvRef();
205         ref.setName("foo");
206         ref.setType("type");
207         resources.addResourceEnvRef(ref);
208         ContextResourceLink res = new ContextResourceLink();
209         res.setName("jdbc/Barlocal");
210         res.setType("javax.sql.DataSource");
211         res.setGlobal("jdbc/Bar");
212         resources.addResourceLink(res);
213         String JavaDoc aspectedResult = "<Context"
214                 + LF.LINE_SEPARATOR
215                 + " docBase=\"myapps\""
216                 + LF.LINE_SEPARATOR
217                 + " path=\"/myapps\">"
218                 + LF.LINE_SEPARATOR
219                 + " <Listener className=\"org.apache.catalina.storeconfig.InfoLifecycleListener\"/>"
220                 + LF.LINE_SEPARATOR + " <ResourceEnvRef" + LF.LINE_SEPARATOR
221                 + " name=\"foo\"" + LF.LINE_SEPARATOR
222                 + " type=\"type\"/>" + LF.LINE_SEPARATOR + " <ResourceLink"
223                 + LF.LINE_SEPARATOR + " global=\"jdbc/Bar\""
224                 + LF.LINE_SEPARATOR + " name=\"jdbc/Barlocal\""
225                 + LF.LINE_SEPARATOR + " type=\"javax.sql.DataSource\"/>"
226                 + LF.LINE_SEPARATOR + "</Context>" + LF.LINE_SEPARATOR;
227         check(aspectedResult);
228     }
229
230     public void testManagerStore() throws Exception JavaDoc {
231         standardContext.setManager(new StandardManager());
232         String JavaDoc aspectedResult = "<Context" + LF.LINE_SEPARATOR
233                 + " docBase=\"myapps\"" + LF.LINE_SEPARATOR
234                 + " path=\"/myapps\">" + LF.LINE_SEPARATOR
235                 + "</Context>\r\n";
236         check(aspectedResult);
237     }
238
239     public void testRealmStore() throws Exception JavaDoc {
240         standardContext.setManager(new StandardManager());
241         JAASRealm realm = new JAASRealm();
242         standardContext.setRealm(realm);
243         String JavaDoc aspectedResult = "<Context" + LF.LINE_SEPARATOR
244                 + " docBase=\"myapps\"" + LF.LINE_SEPARATOR
245                 + " path=\"/myapps\">" + LF.LINE_SEPARATOR
246                 + " <Realm className=\"org.apache.catalina.realm.JAASRealm\""
247                 + LF.LINE_SEPARATOR + " appName=\"myapps\"/>"
248                 + LF.LINE_SEPARATOR + "</Context>" + LF.LINE_SEPARATOR;
249         check(aspectedResult);
250     }
251
252     // @TODO Why the MaxInactiveInterval is after setManager set to 1800 sec?
253
public void testManagerNonStandardStore() throws Exception JavaDoc {
254         StandardManager manager = new StandardManager();
255         manager.setMaxActiveSessions(100);
256         assertEquals(60, manager.getMaxInactiveInterval());
257         standardContext.setManager(manager);
258         assertEquals(1800, manager.getMaxInactiveInterval());
259         String JavaDoc aspectedResult = "<Context"
260                 + LF.LINE_SEPARATOR
261                 + " docBase=\"myapps\""
262                 + LF.LINE_SEPARATOR
263                 + " path=\"/myapps\">"
264                 + LF.LINE_SEPARATOR
265                 + " <Manager className=\"org.apache.catalina.session.StandardManager\""
266                 + LF.LINE_SEPARATOR + " maxActiveSessions=\"100\""
267                 + LF.LINE_SEPARATOR + " maxInactiveInterval=\"1800\"/>"
268                 + LF.LINE_SEPARATOR + "</Context>\r\n";
269         check(aspectedResult);
270     }
271
272     public void testPersistentManagerStore() throws Exception JavaDoc {
273         PersistentManager manager = new PersistentManager();
274         manager.setSaveOnRestart(false);
275         standardContext.setManager(manager);
276         String JavaDoc aspectedResult = "<Context"
277                 + LF.LINE_SEPARATOR
278                 + " docBase=\"myapps\""
279                 + LF.LINE_SEPARATOR
280                 + " path=\"/myapps\">"
281                 + LF.LINE_SEPARATOR
282                 + " <Manager className=\"org.apache.catalina.session.PersistentManager\""
283                 + LF.LINE_SEPARATOR + " maxInactiveInterval=\"1800\""
284                 + LF.LINE_SEPARATOR + " saveOnRestart=\"false\">"
285                 + LF.LINE_SEPARATOR + " </Manager>" + LF.LINE_SEPARATOR
286                 + "</Context>" + LF.LINE_SEPARATOR;
287         check(aspectedResult);
288     }
289
290     public void testPersistentManagerFileStore() throws Exception JavaDoc {
291         PersistentManager manager = new PersistentManager();
292         manager.setSaveOnRestart(false);
293         FileStore store = new FileStore();
294         manager.setStore(store);
295         standardContext.setManager(manager);
296         String JavaDoc aspectedResult = "<Context"
297                 + LF.LINE_SEPARATOR
298                 + " docBase=\"myapps\""
299                 + LF.LINE_SEPARATOR
300                 + " path=\"/myapps\">"
301                 + LF.LINE_SEPARATOR
302                 + " <Manager className=\"org.apache.catalina.session.PersistentManager\""
303                 + LF.LINE_SEPARATOR
304                 + " maxInactiveInterval=\"1800\""
305                 + LF.LINE_SEPARATOR
306                 + " saveOnRestart=\"false\">"
307                 + LF.LINE_SEPARATOR
308                 + " <Store className=\"org.apache.catalina.session.FileStore\"/>"
309                 + LF.LINE_SEPARATOR + " </Manager>" + LF.LINE_SEPARATOR
310                 + "</Context>" + LF.LINE_SEPARATOR;
311         check(aspectedResult);
312
313     }
314
315     public void testDefaultResources() throws Exception JavaDoc {
316         FileDirContext dirC = new FileDirContext();
317         standardContext.setAllowLinking(true);
318         standardContext.setResources(dirC);
319         StandardHost host = new StandardHost();
320         host.addChild(standardContext);
321         host.setName("localhost");
322         host.setAppBase("webapps");
323         standardContext.resourcesStart();
324         assertNotNull(standardContext.getResources());
325         String JavaDoc aspectedResult = "<Context" + LF.LINE_SEPARATOR
326                 + " docBase=\"myapps\"" + LF.LINE_SEPARATOR
327                 + " path=\"/myapps\">" + LF.LINE_SEPARATOR + "</Context>"
328                 + LF.LINE_SEPARATOR;
329         check(aspectedResult);
330     }
331
332     /*
333      * + " <Resources className=\"org.apache.naming.resources.FileDirContext\"" +
334      * Constants.LINE_SEPARATOR + " allowLinking=\"true\"/>" +
335      * Constants.LINE_SEPARATOR
336      */

337     public void testStoreEmpty() throws Exception JavaDoc {
338         String JavaDoc aspectedResult = "<Context" + LF.LINE_SEPARATOR
339                 + " docBase=\"myapps\"" + LF.LINE_SEPARATOR
340                 + " path=\"/myapps\">" + LF.LINE_SEPARATOR + "</Context>"
341                 + LF.LINE_SEPARATOR;
342         check(aspectedResult);
343     }
344
345     protected void check(String JavaDoc aspectedResult) throws Exception JavaDoc {
346         factory.store(pWriter, -2, standardContext);
347         assertEquals(aspectedResult, writer.toString());
348     }
349
350 }
Popular Tags