KickJava   Java API By Example, From Geeks To Geeks.

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


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.deploy.ContextEjb;
24 import org.apache.catalina.deploy.ContextEnvironment;
25 import org.apache.catalina.deploy.ContextLocalEjb;
26 import org.apache.catalina.deploy.ContextResource;
27 import org.apache.catalina.deploy.ContextResourceEnvRef;
28 import org.apache.catalina.deploy.ContextResourceLink;
29 import org.apache.catalina.deploy.NamingResources;
30 import org.apache.catalina.storeconfig.GlobalNamingResourcesSF;
31 import org.apache.catalina.storeconfig.StoreDescription;
32 import org.apache.catalina.storeconfig.StoreRegistry;
33
34 /**
35  * @author Peter Rossbach
36  *
37  */

38 public class GlobalNamingResourcesSFTest extends TestCase {
39     StoreRegistry registry;
40
41     StringWriter JavaDoc writer = new StringWriter JavaDoc();
42
43     PrintWriter JavaDoc pWriter = new PrintWriter JavaDoc(writer);
44
45     NamingResources reource = new NamingResources();
46
47     GlobalNamingResourcesSF factory;
48
49     StoreDescription desc;
50
51     StoreDescription nameingDesc;
52
53     /*
54      * create registery and configure naming decriptors
55      *
56      * @see junit.framework.TestCase#setUp()
57      */

58     protected void setUp() throws Exception JavaDoc {
59         registry = new StoreRegistry();
60         desc = DescriptorHelper.registerDescriptor(null, registry,
61                 NamingResources.class.getName() + ".[GlobalNamingResources]",
62                 "GlobalNamingResources", NamingResources.class.getName(),
63                 "org.apache.catalina.storeconfig.GlobalNamingResourcesSF",
64                 true, false);
65         factory = (GlobalNamingResourcesSF) desc.getStoreFactory();
66         nameingDesc = DescriptorHelper.registerNamingDescriptor(desc, registry);
67         super.setUp();
68     }
69
70     protected void registerDescriptor(String JavaDoc tag, Class JavaDoc aClass,
71             String JavaDoc factoryClass, boolean fstandard, boolean fdefault) {
72         DescriptorHelper.registerDescriptor(nameingDesc, registry, aClass
73                 .getName(), tag, aClass.getName(), factoryClass, fstandard,
74                 fdefault);
75     }
76
77     public void testStore() throws Exception JavaDoc {
78         assertNotNull(registry.findDescription(NamingResources.class));
79         assertNotNull(registry.findDescription(ContextResourceEnvRef.class
80                 .getName()));
81         assertEquals("ResourceEnvRef", registry.findDescription(
82                 ContextResourceEnvRef.class.getName()).getTag());
83
84         NamingResources resources = new NamingResources();
85         ContextResourceEnvRef ref = new ContextResourceEnvRef();
86         ref.setName("peter");
87         ref.setType("type");
88         resources.addResourceEnvRef(ref);
89         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
90                 + " <ResourceEnvRef" + LF.LINE_SEPARATOR
91                 + " name=\"peter\"" + LF.LINE_SEPARATOR
92                 + " type=\"type\"/>" + LF.LINE_SEPARATOR
93                 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR;
94         check(resources, aspectedResult);
95
96     }
97
98     public void testEJBStore() throws Exception JavaDoc {
99
100         NamingResources resources = new NamingResources();
101         ContextEjb ejb = new ContextEjb();
102         ejb.setName("ejb/Service");
103         ejb.setType("org.super.Bean");
104         ejb.setHome("org.super.BeanHome");
105         resources.addEjb(ejb);
106         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
107                 + " <EJB" + LF.LINE_SEPARATOR
108                 + " home=\"org.super.BeanHome\"" + LF.LINE_SEPARATOR
109                 + " name=\"ejb/Service\"" + LF.LINE_SEPARATOR
110                 + " type=\"org.super.Bean\"/>" + LF.LINE_SEPARATOR
111                 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR;
112         check(resources, aspectedResult);
113     }
114
115     public void testLocalEjbStore() throws Exception JavaDoc {
116
117         NamingResources resources = new NamingResources();
118         ContextLocalEjb ejb = new ContextLocalEjb();
119         ejb.setName("ejb/Service");
120         ejb.setType("org.super.Bean");
121         ejb.setHome("org.super.BeanHome");
122         resources.addLocalEjb(ejb);
123         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
124                 + " <LocalEjb" + LF.LINE_SEPARATOR
125                 + " home=\"org.super.BeanHome\"" + LF.LINE_SEPARATOR
126                 + " name=\"ejb/Service\"" + LF.LINE_SEPARATOR
127                 + " type=\"org.super.Bean\"/>" + LF.LINE_SEPARATOR
128                 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR;
129         check(resources, aspectedResult);
130     }
131
132     public void testEnvironmentStore() throws Exception JavaDoc {
133
134         NamingResources resources = new NamingResources();
135         ContextEnvironment env = new ContextEnvironment();
136         env.setName("env/SelectEmp");
137         env.setType("java.lang.String");
138         env.setValue("select * from emp");
139         resources.addEnvironment(env);
140         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
141                 + " <Environment" + LF.LINE_SEPARATOR
142                 + " name=\"env/SelectEmp\"" + LF.LINE_SEPARATOR
143                 + " type=\"java.lang.String\"" + LF.LINE_SEPARATOR
144                 + " value=\"select * from emp\"/>" + LF.LINE_SEPARATOR
145                 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR;
146         check(resources, aspectedResult);
147     }
148
149     public void testResourceStore() throws Exception JavaDoc {
150
151         NamingResources resources = new NamingResources();
152         ContextResource res = new ContextResource();
153         res.setName("jdbc/Emp");
154         res.setType("javax.sql.DataSource");
155         res.setAuth("Container");
156         resources.addResource(res);
157         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
158                 + " <Resource" + LF.LINE_SEPARATOR + " auth=\"Container\""
159                 + LF.LINE_SEPARATOR + " name=\"jdbc/Emp\""
160                 + LF.LINE_SEPARATOR + " type=\"javax.sql.DataSource\"/>"
161                 + LF.LINE_SEPARATOR + "</GlobalNamingResources>"
162                 + LF.LINE_SEPARATOR;
163         check(resources, aspectedResult);
164     }
165
166     public void testResourceStoreProperty() throws Exception JavaDoc {
167
168         NamingResources resources = new NamingResources();
169         ContextResource res = new ContextResource();
170         res.setName("mail/MailSession");
171         res.setType("javax.mail.Session");
172         res.setAuth("Container");
173         res.setProperty("mail.host", "localhost");
174         resources.addResource(res);
175         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
176                 + " <Resource" + LF.LINE_SEPARATOR + " auth=\"Container\""
177                 + LF.LINE_SEPARATOR + " name=\"mail/MailSession\""
178                 + LF.LINE_SEPARATOR + " type=\"javax.mail.Session\""
179                 + LF.LINE_SEPARATOR + " mail.host=\"localhost\"/>"
180                 + LF.LINE_SEPARATOR + "</GlobalNamingResources>"
181                 + LF.LINE_SEPARATOR;
182         check(resources, aspectedResult);
183     }
184
185     // @TODO ResourceLink can only be exists at Context Tag
186
public void testResourceLinkStore() throws Exception JavaDoc {
187
188         NamingResources resources = new NamingResources();
189         ContextResourceLink res = new ContextResourceLink();
190         res.setName("jdbc/Emp1");
191         res.setType("javax.sql.DataSource");
192         res.setGlobal("jdbc/Emp");
193         resources.addResourceLink(res);
194         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
195                 + " <ResourceLink" + LF.LINE_SEPARATOR
196                 + " global=\"jdbc/Emp\"" + LF.LINE_SEPARATOR
197                 + " name=\"jdbc/Emp1\"" + LF.LINE_SEPARATOR
198                 + " type=\"javax.sql.DataSource\"/>" + LF.LINE_SEPARATOR
199                 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR;
200         check(resources, aspectedResult);
201     }
202
203     public void testStoreEmpty() throws Exception JavaDoc {
204         NamingResources resources = new NamingResources();
205         String JavaDoc aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR
206                 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR;
207         check(resources, aspectedResult);
208     }
209
210     protected void check(NamingResources resources, String JavaDoc aspectedResult)
211             throws Exception JavaDoc {
212         factory.store(pWriter, -2, resources);
213         assertEquals(aspectedResult, writer.toString());
214     }
215
216 }
Popular Tags