KickJava   Java API By Example, From Geeks To Geeks.

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


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.LifecycleListener;
24 import org.apache.catalina.Valve;
25 import org.apache.catalina.cluster.CatalinaCluster;
26 import org.apache.catalina.cluster.deploy.FarmWarDeployer;
27 import org.apache.catalina.cluster.mcast.McastService;
28 import org.apache.catalina.cluster.tcp.ReplicationListener;
29 import org.apache.catalina.cluster.tcp.ReplicationTransmitter;
30 import org.apache.catalina.cluster.tcp.ReplicationValve;
31 import org.apache.catalina.cluster.tcp.SimpleTcpCluster;
32 import org.apache.catalina.core.StandardContext;
33 import org.apache.catalina.core.StandardHost;
34 import org.apache.catalina.realm.JAASRealm;
35 import org.apache.catalina.storeconfig.StandardHostSF;
36 import org.apache.catalina.storeconfig.StoreDescription;
37 import org.apache.catalina.storeconfig.StoreRegistry;
38
39 /**
40  * @author Peter Rossbach
41  *
42  */

43 public class StandardHostSFTest extends TestCase {
44     StoreRegistry registry;
45
46     StringWriter JavaDoc writer = new StringWriter JavaDoc();
47
48     PrintWriter JavaDoc pWriter = new PrintWriter JavaDoc(writer);
49
50     StandardHost standardHost;
51
52     StandardHostSF factory;
53
54     StoreDescription desc;
55
56     /*
57      * create registry and register Host and all subelement descriptors
58      *
59      * @see junit.framework.TestCase#setUp()
60      */

61     protected void setUp() throws Exception JavaDoc {
62
63         super.setUp();
64         registry = new StoreRegistry();
65         desc = new StoreDescription();
66         desc.setTag("Host");
67         desc.setTagClass("org.apache.catalina.core.StandardHost");
68         desc.setStandard(true);
69         desc.setStoreFactoryClass("org.apache.catalina.core.StandardHostSF");
70         desc.addTransientAttribute("domain");
71         registry.registerDescription(desc);
72         factory = new StandardHostSF();
73         desc.setStoreFactory(factory);
74         factory.setRegistry(registry);
75         StoreDescription listenerdesc = registerDescriptor("Listener",
76                 LifecycleListener.class);
77
78         String JavaDoc listenerskippables[] = {
79                 "org.apache.catalina.core.NamingContextListener",
80                 "org.apache.catalina.startup.HostConfig", };
81         for (int i = 0; i < listenerskippables.length; i++)
82             listenerdesc.addTransientChild(listenerskippables[i]);
83
84         StoreDescription realmdesc = registerDescriptor("Realm",
85                 JAASRealm.class,
86                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
87                 false);
88         StoreDescription contextdesc = registerDescriptor("Context",
89                 StandardContext.class,
90                 "org.apache.catalina.storeconfig.StoreFactoryBase", true, false);
91         String JavaDoc exceptions[] = { "available", "configFile", "configured",
92                 "distributable", "domain", "engineName", "name", "publicId",
93                 "sessionTimeout", "startupTime", "tldScanTime" };
94         for (int i = 0; i < exceptions.length; i++)
95             contextdesc.addTransientAttribute(exceptions[i]);
96
97         StoreDescription valvedesc = registerDescriptor("Valve", Valve.class,
98                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
99                 false);
100
101         String JavaDoc skippables[] = { "org.apache.catalina.core.StandardHostValve",
102                 "org.apache.catalina.valves.CertificatesValve",
103                 "org.apache.catalina.valves.ErrorReportValve",
104                 "org.apache.catalina.cluster.tcp.ReplicationValve",
105                 "org.apache.catalina.valves.RequestListenerValve", };
106         for (int i = 0; i < skippables.length; i++)
107             valvedesc.addTransientChild(skippables[i]);
108
109         DescriptorHelper.registerClusterDescriptor(desc, registry);
110         standardHost = new StandardHost();
111         standardHost.setName("localhost");
112
113     }
114
115     private StoreDescription registerDescriptor(String JavaDoc tag, Class JavaDoc aClass) {
116         return registerDescriptor(tag, aClass,
117                 "org.apache.catalina.storeconfig.StoreFactoryBase", false,
118                 false);
119     }
120
121     private StoreDescription registerDescriptor(String JavaDoc tag, Class JavaDoc aClass,
122             String JavaDoc factoryClass, boolean fstandard, boolean fdefault) {
123         return DescriptorHelper.registerDescriptor(desc, registry, aClass
124                 .getName(), tag, aClass.getName(), factoryClass, fstandard,
125                 fdefault);
126     }
127
128     public void testStore() throws Exception JavaDoc {
129         standardHost
130                 .addLifecycleListener(new org.apache.catalina.storeconfig.InfoLifecycleListener());
131         String JavaDoc aspectedResult = "<Host"
132                 + LF.LINE_SEPARATOR
133                 + " name=\"localhost\">"
134                 + LF.LINE_SEPARATOR
135                 + " <Listener className=\"org.apache.catalina.storeconfig.InfoLifecycleListener\"/>"
136                 + LF.LINE_SEPARATOR + "</Host>" + LF.LINE_SEPARATOR;
137
138         check(aspectedResult);
139     }
140
141     public void testElements() throws Exception JavaDoc {
142         standardHost
143                 .addLifecycleListener(new org.apache.catalina.storeconfig.InfoLifecycleListener());
144         standardHost
145                 .addLifecycleListener(new org.apache.catalina.startup.HostConfig());
146         standardHost.setRealm(new JAASRealm());
147         StandardContext context = new StandardContext();
148         context.setDocBase("myapps");
149         context.setPath("/myapps");
150         standardHost.addChild(context);
151         standardHost.addAlias("jovi");
152         String JavaDoc aspectedResult = "<Host"
153                 + LF.LINE_SEPARATOR
154                 + " name=\"localhost\">"
155                 + LF.LINE_SEPARATOR
156                 + " <Listener className=\"org.apache.catalina.storeconfig.InfoLifecycleListener\"/>"
157                 + LF.LINE_SEPARATOR + " <Alias>jovi</Alias>"
158                 + LF.LINE_SEPARATOR
159                 + " <Realm className=\"org.apache.catalina.realm.JAASRealm\""
160                 + LF.LINE_SEPARATOR + " appName=\"localhost\"/>"
161                 + LF.LINE_SEPARATOR + " <Context" + LF.LINE_SEPARATOR
162                 + " docBase=\"myapps\"" + LF.LINE_SEPARATOR
163                 + " path=\"/myapps\"/>" + LF.LINE_SEPARATOR + "</Host>"
164                 + LF.LINE_SEPARATOR;
165         check(aspectedResult);
166     }
167
168     public void testValve() throws Exception JavaDoc {
169         standardHost
170                 .addLifecycleListener(new org.apache.catalina.storeconfig.InfoLifecycleListener());
171         standardHost
172                 .addValve(new org.apache.catalina.valves.ErrorReportValve());
173         standardHost
174                 .addValve(new org.apache.catalina.valves.RequestDumperValve());
175         standardHost.addValve(new ReplicationValve());
176         String JavaDoc aspectedResult = "<Host"
177                 + LF.LINE_SEPARATOR
178                 + " name=\"localhost\">"
179                 + LF.LINE_SEPARATOR
180                 + " <Listener className=\"org.apache.catalina.storeconfig.InfoLifecycleListener\"/>"
181                 + LF.LINE_SEPARATOR
182                 + " <Valve className=\"org.apache.catalina.valves.RequestDumperValve\"/>"
183                 + LF.LINE_SEPARATOR + "</Host>" + LF.LINE_SEPARATOR;
184         check(aspectedResult);
185     }
186
187     public void testClusterEmpty() throws Exception JavaDoc {
188         CatalinaCluster cluster = new SimpleTcpCluster();
189         standardHost.setCluster(cluster);
190         String JavaDoc aspectedResult = "<Host"
191                 + LF.LINE_SEPARATOR
192                 + " name=\"localhost\">"
193                 + LF.LINE_SEPARATOR
194                 + " <Cluster className=\"org.apache.catalina.cluster.tcp.SimpleTcpCluster\">"
195                 + LF.LINE_SEPARATOR + " </Cluster>" + LF.LINE_SEPARATOR
196                 + "</Host>" + LF.LINE_SEPARATOR;
197         check(aspectedResult);
198     }
199
200     public void testCluster() throws Exception JavaDoc {
201         SimpleTcpCluster cluster = new SimpleTcpCluster();
202         cluster.setClusterName("cluster");
203         cluster.setExpireSessionsOnShutdown(false);
204         cluster.setPrintToScreen(false);
205         cluster
206                 .setManagerClassName("org.apache.catalina.cluster.session.DeltaManager");
207         McastService service = new McastService();
208         service.setMcastAddr("228.0.0.4");
209         service.setMcastPort(45564);
210         service.setMcastFrequency(500l);
211         service.setMcastDropTime(3000l);
212         cluster.setMembershipService(service);
213         ReplicationListener receiver = new ReplicationListener();
214         receiver.setTcpListenAddress("auto");
215         receiver.setTcpListenPort(4001);
216         receiver.setTcpSelectorTimeout(100l);
217         receiver.setTcpThreadCount(6);
218         cluster.setClusterReceiver(receiver);
219         ReplicationTransmitter sender = new ReplicationTransmitter();
220         sender.setReplicationMode("pooled");
221         cluster.setClusterSender(sender);
222         ReplicationValve valve = new ReplicationValve();
223         valve
224                 .setFilter(".*\\.gif;.*\\.js;.*\\.jpg;.*\\.jpeg;.*\\.htm;.*\\.html;.*\\.txt;");
225         cluster.addValve(valve);
226         FarmWarDeployer deployer = new FarmWarDeployer();
227         deployer.setTempDir("/tmp/war-temp/");
228         deployer.setDeployDir("/tmp/war-deploy/");
229         deployer.setWatchDir("/tmp/war-listen/");
230         deployer.setWatchEnabled(false);
231         cluster.setClusterDeployer(deployer);
232         standardHost.setCluster(cluster);
233         // DeltaManager is default!
234
String JavaDoc aspectedResult = "<Host"
235                 + LF.LINE_SEPARATOR
236                 + " name=\"localhost\">"
237                 + LF.LINE_SEPARATOR
238                 + " <Cluster className=\"org.apache.catalina.cluster.tcp.SimpleTcpCluster\""
239                 + LF.LINE_SEPARATOR
240                 + " clusterName=\"cluster\">"
241                 + LF.LINE_SEPARATOR
242                 + " <Membership className=\"org.apache.catalina.cluster.mcast.McastService\""
243                 + LF.LINE_SEPARATOR
244                 + " mcastAddr=\"228.0.0.4\""
245                 + LF.LINE_SEPARATOR
246                 + " mcastDropTime=\"3000\""
247                 + LF.LINE_SEPARATOR
248                 + " mcastFrequency=\"500\""
249                 + LF.LINE_SEPARATOR
250                 + " mcastPort=\"45564\"/>"
251                 + LF.LINE_SEPARATOR
252                 + " <Sender className=\"org.apache.catalina.cluster.tcp.ReplicationTransmitter\""
253                 + LF.LINE_SEPARATOR
254                 + " replicationMode=\"pooled\"/>"
255                 + LF.LINE_SEPARATOR
256                 + " <Receiver className=\"org.apache.catalina.cluster.tcp.ReplicationListener\""
257                 + LF.LINE_SEPARATOR
258                 + " tcpListenAddress=\"auto\""
259                 + LF.LINE_SEPARATOR
260                 + " tcpListenPort=\"4001\""
261                 + LF.LINE_SEPARATOR
262                 + " tcpSelectorTimeout=\"100\""
263                 + LF.LINE_SEPARATOR
264                 + " tcpThreadCount=\"6\"/>"
265                 + LF.LINE_SEPARATOR
266                 + " <Deployer className=\"org.apache.catalina.cluster.deploy.FarmWarDeployer\""
267                 + LF.LINE_SEPARATOR
268                 + " deployDir=\"/tmp/war-deploy/\""
269                 + LF.LINE_SEPARATOR
270                 + " tempDir=\"/tmp/war-temp/\""
271                 + LF.LINE_SEPARATOR
272                 + " watchDir=\"/tmp/war-listen/\"/>"
273                 + LF.LINE_SEPARATOR
274                 + " <Valve className=\"org.apache.catalina.cluster.tcp.ReplicationValve\""
275                 + LF.LINE_SEPARATOR
276                 + " filter=\".*\\.gif;.*\\.js;.*\\.jpg;.*\\.jpeg;.*\\.htm;.*\\.html;.*\\.txt;\"/>"
277                 + LF.LINE_SEPARATOR + " </Cluster>" + LF.LINE_SEPARATOR
278                 + "</Host>" + LF.LINE_SEPARATOR;
279         check(aspectedResult);
280     }
281
282     public void testStoreEmpty() throws Exception JavaDoc {
283         String JavaDoc aspectedResult = "<Host" + LF.LINE_SEPARATOR
284                 + " name=\"localhost\">" + LF.LINE_SEPARATOR + "</Host>"
285                 + LF.LINE_SEPARATOR;
286         check(aspectedResult);
287     }
288
289     protected void check(String JavaDoc aspectedResult) throws Exception JavaDoc {
290         factory.store(pWriter, -2, standardHost);
291         assertEquals(aspectedResult, writer.toString());
292     }
293
294 }
Popular Tags