KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > WebappConfigurationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tomcat5;
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.enterprise.deploy.model.DDBean JavaDoc;
31 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
32 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
33 import javax.enterprise.deploy.model.XpathListener JavaDoc;
34 import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
35 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
36 import org.netbeans.api.db.explorer.ConnectionManager;
37 import org.netbeans.api.db.explorer.DatabaseConnection;
38 import org.netbeans.api.db.explorer.JDBCDriver;
39 import org.netbeans.junit.NbTestCase;
40 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException;
41 import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion;
42 import org.netbeans.modules.tomcat5.config.WebappConfiguration;
43 import org.netbeans.modules.tomcat5.config.gen.Context;
44 import org.netbeans.modules.tomcat5.config.gen.Parameter;
45 import org.netbeans.modules.tomcat5.config.gen.ResourceParams;
46 import org.netbeans.modules.tomcat5.util.TestBase;
47
48 /**
49  *
50  * @author Radim Kubacki
51  */

52 public class WebappConfigurationTest extends TestBase {
53     
54     final String JavaDoc DRIVER_NAME = "driverName";
55     final String JavaDoc DRIVER_DISPLAY_NAME = "driverDisplayName";
56     final String JavaDoc DRIVER_CLASS = "driverClass";
57     final String JavaDoc USER = "user";
58     final String JavaDoc PASSWORD = "password";
59     final String JavaDoc DB_URL = "dburl";
60     final String JavaDoc SCHEMA = "schema";
61     
62     public WebappConfigurationTest (java.lang.String JavaDoc testName) {
63         super (testName);
64     }
65     
66     protected void setUp() throws Exception JavaDoc {
67         super.setUp();
68         registerConnection();
69     }
70     
71     private void registerConnection() throws Exception JavaDoc {
72         JDBCDriver driver = JDBCDriver.create(DRIVER_NAME, DRIVER_DISPLAY_NAME, DRIVER_CLASS, new URL JavaDoc[]{});
73         DatabaseConnection dbCon = DatabaseConnection.create(driver, DB_URL, USER, SCHEMA, PASSWORD, true);
74         ConnectionManager.getDefault().addConnection(dbCon);
75     }
76     
77     // work-around private access
78
private void createJDBCReference(WebappConfiguration conf, String JavaDoc name, String JavaDoc url,
79             String JavaDoc username, String JavaDoc password, String JavaDoc driverClassName) throws Exception JavaDoc {
80         Method JavaDoc addResReference = WebappConfiguration.class.getDeclaredMethod(
81             "createJDBCReference",
82             new Class JavaDoc [] {
83                 String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class
84             }
85         );
86         addResReference.setAccessible(true);
87         addResReference.invoke(conf, new Object JavaDoc[]{name, url, username, password, driverClassName});
88     }
89     
90     public void testAddResReference50() throws Exception JavaDoc {
91         
92         final String JavaDoc[][] res = {
93             {"jdbc/myDatabase", "jdbc:derby://localhost:1527/sample", "app", "app", "org.apache.derby.jdbc.ClientDriver"},
94             {"jdbc/myDatabase1", "jdbc:derby://localhost:1527/sample1", "app1", "app1", "org.apache.derby.jdbc.ClientDriver"},
95         };
96             
97         DeployableObject JavaDoc depObj = new DeployableObjectImpl();
98         WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_50);
99         clearWorkDir();
100         
101         conf.init(new File JavaDoc(getWorkDir(), "context.xml"));
102         
103         String JavaDoc dbName = ConnectionManager.getDefault().getConnections()[0].getName();
104         for (int i = 0; i < res.length; i++) {
105             createJDBCReference(conf, res[i][0], res[i][1], res[i][2], res[i][3], res[i][4]);
106         }
107         
108         Context ctx = conf.getContext();
109         boolean[] resource = ctx.getResource();
110         for (int i = 0; i < res.length; i++) {
111             assertEquals("javax.sql.DataSource", ctx.getResourceType(i));
112             assertEquals("Container", ctx.getResourceAuth(i));
113             assertEquals(res[i][0], ctx.getResourceName(i));
114             
115             ResourceParams[] resourceParams = ctx.getResourceParams();
116             assertEquals(res[i][0], resourceParams[i].getName());
117             Map JavaDoc<String JavaDoc,String JavaDoc> paramMap = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
118             for (Parameter param : resourceParams[i].getParameter()) {
119                 paramMap.put(param.getName(), param.getValue());
120             }
121             assertEquals(res[i][1], paramMap.get("url"));
122             assertEquals(res[i][2], paramMap.get("username"));
123             assertEquals(res[i][3], paramMap.get("password"));
124             assertEquals(res[i][4], paramMap.get("driverClassName"));
125         }
126         
127         // lets try to add an already defined resource now
128
try {
129             createJDBCReference(conf, res[0][0], res[0][1], res[0][2], res[0][3], res[0][4]);
130             fail();
131         } catch (InvocationTargetException JavaDoc e) {
132             assertTrue(e.getCause() instanceof DatasourceAlreadyExistsException);
133         }
134     }
135     
136     public void testAddResReference55() throws Exception JavaDoc {
137         
138         final String JavaDoc[][] res = {
139             {"jdbc/myDatabase", "jdbc:derby://localhost:1527/sample", "app", "app", "org.apache.derby.jdbc.ClientDriver"},
140             {"jdbc/myDatabase1", "jdbc:derby://localhost:1527/sample1", "app1", "app1", "org.apache.derby.jdbc.ClientDriver"},
141         };
142             
143         DeployableObject JavaDoc depObj = new DeployableObjectImpl();
144         WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_55);
145         clearWorkDir();
146         conf.init(new File JavaDoc(getWorkDir(), "context.xml"));
147         
148         for (int i = 0; i < res.length; i++) {
149             createJDBCReference(conf, res[i][0], res[i][1], res[i][2], res[i][3], res[i][4]);
150         }
151         
152         Context ctx = conf.getContext();
153         boolean[] resource = ctx.getResource();
154         for (int i = 0; i < res.length; i++) {
155             assertEquals("javax.sql.DataSource", ctx.getResourceType(i));
156             assertEquals(res[i][0], ctx.getResourceName(i));
157             assertEquals(res[i][1], ctx.getResourceUrl(i));
158             assertEquals(res[i][2], ctx.getResourceUsername(i));
159             assertEquals(res[i][3], ctx.getResourcePassword(i));
160             assertEquals(res[i][4], ctx.getResourceDriverClassName(i));
161         }
162         
163         // lets try to add an already defined resource now
164
try {
165             createJDBCReference(conf, res[0][0], res[0][1], res[0][2], res[0][3], res[0][4]);
166             fail();
167         } catch (InvocationTargetException JavaDoc e) {
168             assertTrue(e.getCause() instanceof DatasourceAlreadyExistsException);
169         }
170     }
171     
172     
173     public void testSetContextPath50() throws Exception JavaDoc {
174         DeployableObject JavaDoc depObj = new DeployableObjectImpl();
175         WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_50);
176         clearWorkDir();
177         conf.init(new File JavaDoc(getWorkDir(), "context.xml"));
178         String JavaDoc[][] res = {
179             {"/test", "test."},
180             {"/test/test", "test_test."},
181             {"", "ROOT."},
182         };
183         for (int i = 0; i < res.length; i++) {
184             conf.setContextPath(res[i][0]);
185             assertEquals(res[i][0], conf.getContextPath());
186             assertEquals(res[i][1], conf.getContext().getLoggerPrefix());
187         }
188     }
189     
190     public void testSetContextPath55() throws Exception JavaDoc {
191         DeployableObject JavaDoc depObj = new DeployableObjectImpl();
192         WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_55);
193         clearWorkDir();
194         conf.init(new File JavaDoc(getWorkDir(), "context.xml"));
195         String JavaDoc[] res = {
196             "/test",
197             "/test/test",
198             "",
199         };
200         for (int i = 0; i < res.length; i++) {
201             conf.setContextPath(res[i]);
202             assertEquals(res[i], conf.getContextPath());
203         }
204     }
205     
206     private static class DeployableObjectImpl implements DeployableObject JavaDoc {
207         public ModuleType JavaDoc getType() {
208             return null;
209         }
210
211         public DDBeanRoot JavaDoc getDDBeanRoot() {
212             return new DDBeanRootImpl();
213         }
214
215         public DDBean JavaDoc[] getChildBean(String JavaDoc string) {
216             return null;
217         }
218
219         public String JavaDoc[] getText(String JavaDoc string) {
220             return null;
221         }
222
223         public Class JavaDoc getClassFromScope(String JavaDoc string) {
224             return null;
225         }
226
227         public String JavaDoc getModuleDTDVersion() {
228             return null;
229         }
230
231         public DDBeanRoot JavaDoc getDDBeanRoot(String JavaDoc string) throws FileNotFoundException JavaDoc, DDBeanCreateException {
232             return null;
233         }
234
235         public Enumeration JavaDoc entries() {
236             return null;
237         }
238
239         public InputStream JavaDoc getEntry(String JavaDoc string) {
240             return null;
241         }
242
243     };
244     
245     private static class DDBeanRootImpl implements DDBeanRoot JavaDoc {
246         public ModuleType JavaDoc getType() {
247             return null;
248         }
249
250         public DeployableObject JavaDoc getDeployableObject() {
251             return null;
252         }
253
254         public String JavaDoc getModuleDTDVersion() {
255             return null;
256         }
257
258         public String JavaDoc getDDBeanRootVersion() {
259             return null;
260         }
261
262         public String JavaDoc getXpath() {
263             return null;
264         }
265
266         public String JavaDoc getFilename() {
267             return null;
268         }
269
270         public String JavaDoc getText() {
271             return null;
272         }
273
274         public String JavaDoc getId() {
275             return null;
276         }
277
278         public DDBeanRoot JavaDoc getRoot() {
279             return null;
280         }
281
282         public DDBean JavaDoc[] getChildBean(String JavaDoc string) {
283             return null;
284         }
285
286         public String JavaDoc[] getText(String JavaDoc string) {
287             return null;
288         }
289
290         public void addXpathListener(String JavaDoc string, XpathListener JavaDoc xpathListener) {
291         }
292
293         public void removeXpathListener(String JavaDoc string, XpathListener JavaDoc xpathListener) {
294         }
295
296         public String JavaDoc[] getAttributeNames() {
297             return null;
298         }
299
300         public String JavaDoc getAttributeValue(String JavaDoc string) {
301             return null;
302         }
303     };
304 }
305
Popular Tags