KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
22
23 import org.apache.catalina.Lifecycle;
24 import org.apache.catalina.LifecycleListener;
25 import org.apache.catalina.core.StandardServer;
26 import org.apache.catalina.core.StandardService;
27 import org.apache.catalina.deploy.NamingResources;
28 import org.apache.catalina.storeconfig.StandardServerSF;
29 import org.apache.catalina.storeconfig.StoreDescription;
30 import org.apache.catalina.storeconfig.StoreRegistry;
31
32 /**
33  * @author Peter Rossbach
34  *
35  */

36 public class ServerChildsTest extends TestCase {
37
38     StoreRegistry registry;
39
40     StringWriter JavaDoc writer = new StringWriter JavaDoc();
41
42     PrintWriter JavaDoc pWriter = new PrintWriter JavaDoc(writer);
43
44     StandardServer standardServer = new StandardServer();
45
46     StandardServerSF factory;
47
48     StoreDescription desc;
49
50     /*
51      * create registery and register Server and direct subelement descriptors
52      *
53      * @see junit.framework.TestCase#setUp()
54      */

55     protected void setUp() throws Exception JavaDoc {
56
57         super.setUp();
58         registry = new StoreRegistry();
59         desc = new StoreDescription();
60         desc.setTag("Server");
61         desc.setTagClass("org.apache.catalina.core.StandardServer");
62         desc.setStandard(true);
63         desc
64                 .setStoreFactoryClass("org.apache.catalina.storeconfig.StandardServerSF");
65         registry.registerDescription(desc);
66         factory = new StandardServerSF();
67         desc.setStoreFactory(factory);
68         factory.setRegistry(registry);
69         StoreDescription listdesc = registerDescriptor("Listener",
70                 LifecycleListener.class);
71         listdesc
72                 .addTransientChild("org.apache.catalina.core.NamingContextListener");
73         listdesc
74                 .addTransientChild("org.apache.catalina.mbeans.ServerLifecycleListener");
75         standardServer
76                 .addLifecycleListener(new org.apache.catalina.mbeans.ServerLifecycleListener());
77         // add GlobalNamingResource
78
DescriptorHelper.registerDescriptor(desc, registry,
79                 NamingResources.class.getName() + ".[GlobalNamingResources]",
80                 "GlobalNamingResources", NamingResources.class.getName(),
81                 "org.apache.catalina.storeconfig.GlobalNamingResourcesSF",
82                 true, false);
83         DescriptorHelper.registerNamingDescriptor(desc, registry);
84         registerDescriptor("Service", StandardService.class,
85                 "org.apache.catalina.storeconfig.StandardServiceSF", true,
86                 false);
87         DescriptorHelper.registerDescriptor(desc, registry,
88                 StandardServer.class.getName() + ".[ServerLifecycleListener]",
89                 "ServerLifecycleListener", StandardServer.class.getName(),
90                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
91                 false);
92         standardServer.addService(new StandardService());
93
94     }
95
96     private StoreDescription registerDescriptor(String JavaDoc tag, Class JavaDoc aClass) {
97         return registerDescriptor(tag, aClass,
98                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
99                 false);
100     }
101
102     private StoreDescription registerDescriptor(String JavaDoc tag, Class JavaDoc aClass,
103             String JavaDoc factoryClass, boolean fstandard, boolean fdefault) {
104         return DescriptorHelper.registerDescriptor(desc, registry, aClass
105                 .getName(), tag, aClass.getName(), factoryClass, fstandard,
106                 fdefault);
107     }
108
109     public void testSaveListenerAddServer() throws Exception JavaDoc {
110         assertTrue(standardServer instanceof Lifecycle);
111         assertNotNull(
112                 "No Listener Descriptor",
113                 registry
114                         .findDescription("org.apache.catalina.mbeans.ServerLifecycleListener"));
115         assertNotNull(
116                 "No Listener StoreFactory",
117                 registry
118                         .findStoreFactory("org.apache.catalina.mbeans.ServerLifecycleListener"));
119         factory.store(pWriter, -2, standardServer);
120  
121         String JavaDoc aspectedResult = "<?xml version=\"1.0\" encoding=\""
122                 + registry.getEncoding()
123                 + "\"?>"
124                 + LF.LINE_SEPARATOR
125                 + "<Server>"
126                 + LF.LINE_SEPARATOR
127                 + " <Listener className=\"org.apache.catalina.mbeans.ServerLifecycleListener\"/>"
128                 + LF.LINE_SEPARATOR + " <GlobalNamingResources>"
129                 + LF.LINE_SEPARATOR + " </GlobalNamingResources>"
130                 + LF.LINE_SEPARATOR + " <Service/>" + LF.LINE_SEPARATOR
131                 + "</Server>" + LF.LINE_SEPARATOR;
132         assertEquals(aspectedResult, writer.toString());
133     }
134 }
Popular Tags