KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestWebAppDelegator


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 /*
21  * TestWebAppDelegator - test the basic features.
22  *
23  * The following test assumes that we know the content of the
24  * graph as we get elements, add and change them. Therefore, the TestWebAppDelegator.xml
25  * file and this java test should be kept in sync.
26  *
27  * Test the following:
28  *
29  * single String: get/set/remove/set/get
30  * boolean (from true): get/set true/get/set false/get/set true/get
31  * boolean (from false): get/set false/get/set true/get/set false/get
32  * String[]: get/set (null & !null)/add/remove
33  * Bean: remove/set(null)/create bean and graph of beans/set/add
34  *
35  */

36
37 import java.io.*;
38 import java.util.*;
39 import org.w3c.dom.*;
40
41 import webapp.*;
42
43
44 public class TestWebAppDelegator extends BaseTest {
45     public static void main(String JavaDoc[] argv) {
46         TestWebAppDelegator o = new TestWebAppDelegator();
47         if (argv.length > 0)
48             o.setDocumentDir(argv[0]);
49         try {
50             o.run();
51         } catch (Exception JavaDoc e) {
52             e.printStackTrace();
53             System.exit(1);
54         }
55         System.exit(0);
56     }
57     
58     public void run() throws Exception JavaDoc {
59         WebAppDelegator webApp;
60
61         this.readDocument();
62
63         out("creating the bean graph");
64         webApp = WebAppDelegator.read(doc);
65
66         // Check that we can read the graph an it is complete
67
out("bean graph created");
68         webApp.write(out);
69
70         out("making some minor changes");
71         webApp.setDescription(0, "Changed the description");
72         webApp.getFilter(0).setFilterClass("foo");
73         webApp.write(out);
74
75         out("set some goodnesses");
76         SecurityConstraintType sc = new SecurityConstraintType();
77         webApp.addSecurityConstraint(sc);
78         sc.setAuthConstraint(new AuthConstraintType());
79         sc.getAuthConstraint().addGoodPresidentCandidate(true);
80         sc.getAuthConstraint().addGoodPresidentCandidate(false);
81         boolean firstCandiateGoodness = sc.getAuthConstraint().isGoodPresidentCandidate(0);
82         check(firstCandiateGoodness == true, "firstCandiateGoodness is good");
83         boolean[] allCandidateGoodnesses = sc.getAuthConstraint().getGoodPresidentCandidate();
84         check(allCandidateGoodnesses.length == 2, "2 candidates goodness");
85         check(allCandidateGoodnesses[0] == true, "candidate 0 is good");
86         check(allCandidateGoodnesses[1] == false, "candidate 1 is not good");
87         webApp.write(out);
88
89         try {
90             webApp.validate();
91             check(false, "Failed to get validate exception");
92         } catch (webapp.WebApp.ValidateException e) {
93             check(true, "Got good validate exception: "+e.getMessage());
94         }
95         webApp.setVersion("2.4");
96         try {
97             webApp.validate();
98             check(false, "Failed to get validate exception");
99         } catch (webapp.WebApp.ValidateException e) {
100             check(true, "Got good validate exception: "+e.getMessage());
101         }
102         sc.addWebResourceCollection(new WebResourceCollectionType());
103         try {
104             webApp.validate();
105             check(false, "Failed to get validate exception");
106         } catch (webapp.WebApp.ValidateException e) {
107             check(true, "Got good validate exception: "+e.getMessage());
108         }
109         sc.getWebResourceCollection(0).setWebResourceName("blue");
110         try {
111             webApp.validate();
112             check(false, "Failed to get validate exception");
113         } catch (webapp.WebApp.ValidateException e) {
114             check(true, "Got good validate exception: "+e.getMessage());
115         }
116         sc.getWebResourceCollection(0).addUrlPattern("*.html");
117         ErrorPageType errorPage = new ErrorPageType("over there");
118         webApp.addErrorPage(errorPage);
119         try {
120             webApp.validate();
121             check(false, "Failed to get validate exception");
122         } catch (webapp.WebApp.ValidateException e) {
123             check(true, "Got good validate exception: "+e.getMessage());
124         }
125         errorPage.setErrorCode(new Long JavaDoc(3l));
126         webApp.write(out);
127         webApp.validate();
128
129         FilterMappingType fm = new FilterMappingType();
130         webApp.addFilterMapping(fm);
131         fm.setServletName("Bob");
132         fm.addDispatcher("I am a dispatcher.");
133         try {
134             webApp.validate();
135             check(false, "Failed to get validate exception");
136         } catch (webapp.WebApp.ValidateException e) {
137             check(true, "Got good validate exception: "+e.getMessage());
138         }
139         fm.setDispatcher(0, "FORWARD");
140         webApp.write(out);
141         webApp.validate();
142
143         check(webApp._hasChanged(), "Some changes were made correctly.");
144         webApp._setChanged(false);
145         check(!webApp._hasChanged(), "Was able to reset the dirty flag.");
146         fm.setServletName("Robert");
147         check(webApp._hasChanged(), "Some changes were made correctly.");
148     }
149 }
150
Popular Tags