KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > JspcTest


1 /*
2  * Copyright 2002-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  */

17 package org.apache.tools.ant.taskdefs.optional;
18
19 import java.io.*;
20 import java.util.Properties JavaDoc;
21
22 import org.apache.tools.ant.BuildFileTest;
23 import org.apache.tools.ant.taskdefs.optional.jsp.JspMangler;
24 import org.apache.tools.ant.taskdefs.optional.jsp.Jasper41Mangler;
25 import org.apache.tools.ant.taskdefs.optional.jsp.JspC;
26 import org.apache.tools.ant.taskdefs.optional.jsp.JspNameMangler;
27 import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapterFactory;
28 import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapter;
29
30 /**
31  * Tests the Jspc task.
32  *
33  * @created 07 March 2002
34  * @since Ant 1.5
35  */

36 public class JspcTest extends BuildFileTest {
37     /**
38      * Description of the Field
39      */

40     private File baseDir;
41     /**
42      * Description of the Field
43      */

44     private File outDir;
45
46     /**
47      * Description of the Field
48      */

49     private final static String JavaDoc TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/";
50
51
52     /**
53      * Constructor for the JspcTest object
54      *
55      * @param name Description of Parameter
56      */

57     public JspcTest(String JavaDoc name) {
58         super(name);
59     }
60
61
62     /**
63      * The JUnit setup method
64      */

65     public void setUp() {
66         configureProject(TASKDEFS_DIR + "jspc.xml");
67         baseDir = new File(TASKDEFS_DIR);
68         outDir = new File(baseDir, "jsp/java");
69     }
70
71
72     /**
73      * The teardown method for JUnit
74      */

75     public void tearDown() {
76         executeTarget("cleanup");
77     }
78
79
80     /**
81      * A unit test for JUnit
82      */

83     public void testSimple() throws Exception JavaDoc {
84         executeJspCompile("testSimple", "simple_jsp.java");
85     }
86
87
88     /**
89      * A unit test for JUnit
90      */

91     public void testUriroot() throws Exception JavaDoc {
92         executeJspCompile("testUriroot", "uriroot_jsp.java");
93     }
94
95
96     /**
97      * A unit test for JUnit
98      */

99     public void testXml() throws Exception JavaDoc {
100         executeJspCompile("testXml", "xml_jsp.java");
101     }
102
103
104     /**
105      * try a keyword in a file
106      */

107     public void testKeyword() throws Exception JavaDoc {
108         executeJspCompile("testKeyword", "default_jsp.java");
109     }
110
111
112     /**
113      * what happens to 1nvalid-classname
114      */

115     public void testInvalidClassname() throws Exception JavaDoc {
116         executeJspCompile("testInvalidClassname",
117                 "_1nvalid_0002dclassname_jsp.java");
118     }
119
120
121     /**
122      * A unit test for JUnit
123      */

124     public void testNoTld() throws Exception JavaDoc {
125 // expectBuildExceptionContaining("testNoTld",
126
// "Jasper found an error in a file",
127
// "Java returned: 9");
128
expectBuildExceptionContaining("testNoTld",
129                  "not found",
130                  "Java returned: 9");
131     }
132
133
134     /**
135      * A unit test for JUnit
136      */

137     public void testNotAJspFile() throws Exception JavaDoc {
138         executeTarget("testNotAJspFile");
139     }
140
141     /**
142      * webapp test is currently broken, because it picks up
143      * on the missing_tld file, and bails.
144      */

145 /*
146     public void testWebapp() throws Exception {
147         executeTarget("testWebapp");
148     }
149 */

150     /**
151      * run a target then verify the named file gets created
152      *
153      * @param target Description of Parameter
154      * @param javafile Description of Parameter
155      * @exception Exception trouble
156      */

157     protected void executeJspCompile(String JavaDoc target, String JavaDoc javafile)
158         throws Exception JavaDoc {
159         executeTarget(target);
160         assertJavaFileCreated(javafile);
161     }
162
163
164     /**
165      * verify that a named file was created
166      *
167      * @param filename Description of Parameter
168      * @exception Exception trouble
169      */

170     protected void assertJavaFileCreated(String JavaDoc filename)
171         throws Exception JavaDoc {
172         File file = getOutputFile(filename);
173         assertTrue("file " + filename + " not found", file.exists());
174         assertTrue("file " + filename + " is empty", file.length() > 0);
175     }
176
177     /**
178      * Gets the OutputFile attribute of the JspcTest object
179      *
180      * @param subpath Description of Parameter
181      * @return The OutputFile value
182      */

183     protected File getOutputFile(String JavaDoc subpath) {
184         return new File(outDir, subpath);
185     }
186
187     /**
188      * verify that we select the appropriate mangler
189      */

190     public void testJasperNameManglerSelection() {
191         JspCompilerAdapter adapter=
192                 JspCompilerAdapterFactory.getCompiler("jasper", null,null);
193         JspMangler mangler=adapter.createMangler();
194         assertTrue(mangler instanceof JspNameMangler);
195         adapter= JspCompilerAdapterFactory.getCompiler("jasper41", null, null);
196         mangler = adapter.createMangler();
197         assertTrue(mangler instanceof Jasper41Mangler);
198     }
199
200     public void testJasper41() {
201         JspMangler mangler = new Jasper41Mangler();
202         //java keywords are not special
203
assertMapped(mangler, "for.jsp", "for_jsp");
204         //underscores go in front of invalid start chars
205
assertMapped(mangler, "0.jsp", "_0_jsp");
206         //underscores at the front get an underscore too
207
assertMapped(mangler, "_.jsp", "___jsp");
208         //non java char at start => underscore then the the _hex value
209
assertMapped(mangler, "-.jsp", "__0002d_jsp");
210         //and paths are stripped
211
char s = File.separatorChar;
212         assertMapped(mangler, "" + s + s + "somewhere" + s + "file" + s + "index.jsp", "index_jsp");
213     }
214
215     /**
216      * assert our mapping rules
217      * @param mangler
218      * @param filename
219      * @param classname
220      */

221     protected void assertMapped(JspMangler mangler, String JavaDoc filename, String JavaDoc classname) {
222         String JavaDoc mappedname = mangler.mapJspToJavaName(new File(filename));
223         assertTrue(filename+" should have mapped to "+classname
224                     +" but instead mapped to "+mappedname,
225                     classname.equals(mappedname));
226     }
227
228
229 }
230
231
Popular Tags