KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > api > web > DDApiTest


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.j2ee.dd.api.web;
21
22 import org.netbeans.modules.j2ee.dd.api.web.*;
23 import org.netbeans.modules.j2ee.dd.api.common.InitParam;
24 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
25 import org.netbeans.modules.j2ee.dd.api.common.NameAlreadyUsedException;
26 import java.io.*;
27 import junit.framework.*;
28 import org.netbeans.junit.*;
29 import org.openide.filesystems.*;
30 import java.util.*;
31
32
33 public class DDApiTest extends NbTestCase {
34     private static final String JavaDoc VERSION="2.4";
35     private static final int TIMEOUT=30;
36     private static final int WF_NUMBER=3;
37     private static final String JavaDoc SERVLET_NAME = "FordServlet";
38     private static final String JavaDoc SERVLET_CLASS = "org.package.mypackage.CarServlet";
39     private static final String JavaDoc SERVLET_NAME1 = "VolvoServlet";
40     private static final String JavaDoc URL_PATTERN = "/ford";
41     private static final String JavaDoc URL_PATTERN1 = "/volvo";
42     private static final java.math.BigInteger JavaDoc LOAD_ON_STARTUP = java.math.BigInteger.valueOf(10);
43     private static final java.math.BigInteger JavaDoc LOAD_ON_STARTUP1 = java.math.BigInteger.valueOf(25);
44     private static final String JavaDoc PARAM1 = "car";
45     private static final String JavaDoc VALUE1 = "Ford";
46     private static final String JavaDoc VALUE11 = "Volvo";
47     private static final String JavaDoc PARAM2 = "color";
48     private static final String JavaDoc VALUE2 = "red";
49     private static final String JavaDoc PARAM3 = "type";
50     private static final String JavaDoc VALUE3 = "Puma";
51     private static final String JavaDoc DESCRIPTION = "the color of the car";
52     private static final String JavaDoc DESCRIPTION_EN = "the colour of the car";
53     private static final String JavaDoc DESCRIPTION_DE = "die automobile farbe";
54     private static final String JavaDoc DESCRIPTION_CZ = "barva automobilu";
55     private static final String JavaDoc DESCRIPTION_SK = "farba automobilu";
56     private static final String JavaDoc URL_PATTERN_JSP = "*.jsp";
57     private static final String JavaDoc PRELUDE = "/jsp/prelude.jsp";
58     private static final String JavaDoc CODA = "/jsp/coda.jsp";
59     private static final String JavaDoc LARGE_ICON = "/img/icon32x32.gif";
60     private static final String JavaDoc SMALL_ICON = "/img/icon16x16.gif";
61     
62     private WebApp webApp;
63     /*
64     static {
65         FileObject workDir = FileUtil.toFileObject(getWorkDir());
66         foOut = workDir.createData("web.xml");
67     }
68      */

69     public DDApiTest(java.lang.String JavaDoc testName) {
70         super(testName);
71     }
72     
73     public static void main(java.lang.String JavaDoc[] args) {
74         junit.textui.TestRunner.run(suite());
75     }
76     
77     public static Test suite() {
78         TestSuite suite = new NbTestSuite(DDApiTest.class);
79         
80         return suite;
81     }
82     
83     /** Test of greeting method, of class HelloWorld. */
84     public void test_InitDataChecking () {
85         System.out.println("Init Data Checking");
86         String JavaDoc version = webApp.getVersion();
87         assertEquals("Incorrect servlet spec.version :",VERSION,version);
88         assertEquals("Incorrect number of servlets :",0,webApp.sizeServlet());
89         assertEquals("Incorrect Session Timeout :",TIMEOUT,webApp.getSingleSessionConfig().getSessionTimeout().intValue());
90         assertEquals("Incorrect number of welcome files : ",WF_NUMBER,webApp.getSingleWelcomeFileList().sizeWelcomeFile());
91     }
92     
93     public void test_Servlet() {
94         System.out.println("Testing servlet, servlet-mapping");
95         try {
96             Servlet servlet = (Servlet) webApp.createBean("Servlet");
97             servlet.setServletName(SERVLET_NAME);
98             servlet.setServletClass(SERVLET_CLASS);
99             servlet.setLoadOnStartup(LOAD_ON_STARTUP);
100             webApp.addServlet(servlet);
101             ServletMapping mapping = (ServletMapping) webApp.createBean("ServletMapping");
102             mapping.setServletName(SERVLET_NAME);
103             mapping.setUrlPattern(URL_PATTERN);
104             webApp.addServletMapping(mapping);
105             webApp.write(fo);
106         } catch (ClassNotFoundException JavaDoc ex) {
107             throw new AssertionFailedErrorException("createBean() method failed",ex);
108         } catch (java.io.IOException JavaDoc ex) {
109             throw new AssertionFailedErrorException("write method failed",ex);
110         }
111         assertEquals("Incorrect number of servlets :",1,webApp.sizeServlet());
112         Servlet s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME);
113         assertTrue("Servlet "+SERVLET_NAME+" not found", null != s);
114         assertEquals("Wrong Servlet Name :",SERVLET_NAME,s.getServletName());
115         assertEquals("Wrong Servlet Class :",SERVLET_CLASS,s.getServletClass());
116         assertEquals("Wrong load-on-startup :",LOAD_ON_STARTUP,s.getLoadOnStartup());
117         
118     }
119     
120     public void test_InitParams() {
121         System.out.println("Testing init-params, context-params");
122         Servlet s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME);
123         assertTrue("Servlet "+SERVLET_NAME+" not found", null != s);
124         try {
125             InitParam param = (InitParam) s.createBean("InitParam");
126             param.setParamName(PARAM1);
127             param.setParamValue(VALUE1);
128             s.addInitParam(param);
129             InitParam clonnedParam1 = (InitParam)param.clone();
130             param = (InitParam) s.createBean("InitParam");
131             param.setParamName(PARAM2);
132             param.setParamValue(VALUE2);
133             s.addInitParam(param);
134             InitParam clonnedParam2 = (InitParam)param.clone();
135             webApp.setContextParam(new InitParam[]{clonnedParam1, clonnedParam2});
136             webApp.write(fo);
137         } catch (ClassNotFoundException JavaDoc ex) {
138             throw new AssertionFailedErrorException("createBean() method failed",ex);
139         } catch (java.io.IOException JavaDoc ex) {
140             throw new AssertionFailedErrorException("write method failed",ex);
141         }
142         s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME);
143         assertTrue("Servlet "+SERVLET_NAME+" not found", null != s);
144         assertEquals("Incorrect number of context-params :",2,webApp.sizeContextParam());
145         assertEquals("Incorrect number of init-params in servlet:",2,s.sizeInitParam());
146         // context-param test
147
InitParam[] params = webApp.getContextParam();
148         assertEquals("Incorrect context-param name :",PARAM1,params[0].getParamName());
149         assertEquals("Incorrect context-param name :",PARAM2,params[1].getParamName());
150         assertEquals("Incorrect context-param value :",VALUE1,params[0].getParamValue());
151         assertEquals("Incorrect context-param value :",VALUE2,params[1].getParamValue());
152         // init-param test
153
assertEquals("Incorrect servlet's init-param name :",PARAM1,s.getInitParam(0).getParamName());
154         assertEquals("Incorrect servlet's init-param name :",PARAM2,s.getInitParam(1).getParamName());
155         assertEquals("Incorrect servlet's init-param value :",VALUE1,s.getInitParam(0).getParamValue());
156         assertEquals("Incorrect servlet's init-param value :",VALUE2,s.getInitParam(1).getParamValue());
157         // init-param/context-param, searching
158
InitParam p = (InitParam)s.findBeanByName("InitParam","ParamName",PARAM2);
159         assertTrue("InitParam "+PARAM2+" not found", null != p);
160         p = (InitParam)webApp.findBeanByName("InitParam","ParamName",PARAM1);
161         assertTrue("Context Param "+PARAM1+" not found", null != p);
162     }
163     
164     public void test_VersionNotSupportedException() {
165         System.out.println("Testing VersionNotSupportedException for Taglibs in Servlet2.4");
166         try {
167             Taglib[] taglibs = webApp.getTaglib();
168             throw new AssertionFailedError("method getTaglib() shouldn't be supported in version:"+VERSION);
169         } catch (VersionNotSupportedException ex) {
170             System.out.println("Expected exception : "+ex);
171         }
172         try {
173             Taglib taglib = (Taglib) webApp.createBean("Taglib");
174             taglib.setTaglibLocation("xxx");
175             taglib.setTaglibUri("xxx");
176             webApp.addTaglib(taglib);
177             throw new AssertionFailedError("method setTaglib (int i, Taglib taglib) shouldn't be supported in version:"+VERSION);
178         } catch (ClassNotFoundException JavaDoc ex) {
179             throw new AssertionFailedErrorException("createBean() method failed",ex);
180         } catch (VersionNotSupportedException ex) {
181             System.out.println("Expected exception : "+ex);
182         }
183     }
184     
185     public void test_Description() {
186         System.out.println("Testing description, description for locales");
187         Servlet s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME);
188         assertTrue("Servlet "+SERVLET_NAME+" not found", null != s);
189         InitParam p = (InitParam)s.findBeanByName("InitParam","ParamName",PARAM2);
190         assertTrue("InitParam "+PARAM2+" not found", null != p);
191         p.setDescription(DESCRIPTION);
192         try {
193             p.setDescription("en",DESCRIPTION_EN);
194             p.setDescription("de",DESCRIPTION);
195             p.setDescription("cz",DESCRIPTION_CZ);
196             p.setDescription("sk",DESCRIPTION_SK);
197             p.setDescription("de",DESCRIPTION_DE); // correction
198
} catch (VersionNotSupportedException ex) {
199             throw new AssertionFailedErrorException("setDescription() method failed",ex);
200         }
201         java.util.Map JavaDoc map = p.getAllDescriptions();
202         assertEquals("Incorrect size of description :",5,map.size());
203         assertEquals("Incorrect default description :",DESCRIPTION,map.get(null));
204         assertEquals("Incorrect english description :",DESCRIPTION_EN,map.get("en"));
205         assertEquals("Incorrect german description :",DESCRIPTION_DE,map.get("de"));
206         assertEquals("Incorrect czech description :",DESCRIPTION_CZ,map.get("cz"));
207         assertEquals("Incorrect slovak description :",DESCRIPTION_SK,map.get("sk"));
208         try {
209             p.removeDescriptionForLocale("de");
210         } catch (VersionNotSupportedException ex) {
211             throw new AssertionFailedErrorException("removeDescription() method failed",ex);
212         }
213         assertEquals("Incorrect size of description :",4,p.getAllDescriptions().size());
214         assertEquals("Incorrect default description :",DESCRIPTION,p.getDefaultDescription());
215         try {
216             assertEquals("Incorrect default description :",DESCRIPTION,p.getDescription(null));
217             assertEquals("Incorrect english description :",DESCRIPTION_EN,p.getDescription("en"));
218             assertEquals("German description was removed :",null,p.getDescription("de"));
219             assertEquals("Incorrect czech description :",DESCRIPTION_CZ,p.getDescription("cz"));
220             assertEquals("Incorrect slovak description :",DESCRIPTION_SK,p.getDescription("sk"));
221         } catch (VersionNotSupportedException ex) {
222             throw new AssertionFailedErrorException("getDescription(String locale) method failed",ex);
223         }
224         try {
225             webApp.write(fo);
226         } catch (java.io.IOException JavaDoc ex) {
227             throw new AssertionFailedErrorException("write method failed",ex);
228         }
229     }
230     
231     public void test_addBean() {
232         System.out.println("Testing addBean method");
233         try {
234             InitParam context = (InitParam)webApp.addBean("InitParam",null,null, null);
235             context.setParamName(PARAM3);
236             context.setParamValue(VALUE3);
237             JspConfig jspConfig = (JspConfig)webApp.addBean("JspConfig", null, null, null);
238             jspConfig.addBean("JspPropertyGroup",new String JavaDoc[]{"UrlPattern","IncludePrelude","IncludeCoda"},
239                     new String JavaDoc[]{URL_PATTERN_JSP,PRELUDE,CODA},null);
240             webApp.addBean("Icon",new String JavaDoc[]{"LargeIcon","SmallIcon"},new String JavaDoc[]{LARGE_ICON,SMALL_ICON},null);
241         } catch (Exception JavaDoc ex){
242             throw new AssertionFailedErrorException("addBean() method failed for ContextParam,JspConfig or Icon",ex);
243         }
244         // addinng new Servlet
245
try {
246             Servlet servlet = (Servlet)webApp.addBean("Servlet", new String JavaDoc[]{"ServletName","ServletClass","LoadOnStartup"},
247                     new Object JavaDoc[]{SERVLET_NAME1,SERVLET_CLASS,LOAD_ON_STARTUP1}, "ServletName");
248             servlet.addBean("InitParam", new String JavaDoc[]{"ParamName","ParamValue"}, new String JavaDoc[]{PARAM1,VALUE11},null);
249             webApp.addBean("ServletMapping", new String JavaDoc[]{"ServletName","UrlPattern"},new String JavaDoc[]{SERVLET_NAME1,URL_PATTERN1},"UrlPattern");
250         } catch (Exception JavaDoc ex){
251             new AssertionFailedErrorException("addBean() method failed for Servlet",ex);
252         }
253         // attempt to add servlet with the same name
254
try {
255             Servlet servlet = (Servlet)webApp.addBean("Servlet", new String JavaDoc[]{"ServletName","ServletClass"},
256                     new Object JavaDoc[]{SERVLET_NAME1,SERVLET_CLASS}, "ServletName");
257             throw new AssertionFailedError("Servlet shouldn't have been added because of the same name");
258         } catch (NameAlreadyUsedException ex){
259             System.out.println("Expected exception : "+ex);
260         } catch (ClassNotFoundException JavaDoc ex) {
261             new AssertionFailedErrorException("addBean() method failed for Servlet",ex);
262         }
263         try {
264             webApp.write(fo);
265         } catch (java.io.IOException JavaDoc ex) {
266             throw new AssertionFailedErrorException("write method failed",ex);
267         }
268     }
269     
270     public void test_Result() {
271         System.out.println("Comparing result with golden file");
272         
273         String JavaDoc testDataDirS = System.getProperty("test.data.dir");
274         java.io.File JavaDoc pass = new File(getDataDir(),"/web.pass");
275         File test = FileUtil.toFile(fo);
276         try {
277             BufferedReader reader1 = new BufferedReader(new FileReader(test));
278             BufferedReader reader2 = new BufferedReader(new FileReader(pass));
279             String JavaDoc line1=null;
280             Set set1 = new HashSet();
281             Set set2 = new HashSet();
282             while((line1=reader1.readLine())!=null) {
283                 line1 = line1.trim();
284                 String JavaDoc line2 = reader2.readLine();
285                 if (line2==null) {
286                     assertFile("Result different than golden file", pass, test, test.getParentFile());
287                 }
288                 line2=line2.trim();
289                 // description order can be changed so it must be compared differently
290
if (line1.startsWith("<description")) {
291                     set1.add(line1);
292                     set2.add(line2);
293                 } else if (!line1.equals(line2)) {
294                     assertFile("Result different than golden file", pass, test, test.getParentFile());
295                 }
296             }
297             reader1.close();reader2.close();
298             if (!set1.equals(set2)) {
299                 assertFile("Problem with descriotion elements", pass, test, test.getParentFile());
300             }
301         } catch (IOException ex) {
302             throw new AssertionFailedErrorException("Comparing to golden file failed",ex);
303         }
304     }
305     
306     private static DDProvider ddProvider;
307     private static FileObject fo;
308     private static boolean initialized;
309     
310     protected void setUp() throws Exception JavaDoc {
311         super.setUp();
312         System.out.println("setUp() .......................");
313         
314         if (ddProvider==null) ddProvider = DDProvider.getDefault();
315         assertTrue("DDProvider object not found",null != ddProvider);
316         
317         FileObject dataFolder = FileUtil.toFileObject(getDataDir());
318         
319         if (!initialized){
320             FileObject old = dataFolder.getFileObject("web", "xml");
321             if (old != null){
322                 old.delete();
323             }
324             initialized = true;
325         }
326         
327         if (fo==null) {
328             fo = FileUtil.copyFile(dataFolder.getFileObject("web_org","xml"), dataFolder, "web");
329         }
330         
331         
332         assertTrue("FileObject web.xml not found",null != fo);
333         
334         try {
335             webApp = ddProvider.getDDRoot(fo);
336         } catch (Exception JavaDoc ex) {
337             ex.printStackTrace();
338         }
339         assertTrue("WebApp object not found", null != webApp);
340         
341     }
342 }
343
Popular Tags