001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional;
019:
020: import java.io.File;
021:
022: import org.apache.tools.ant.BuildFileTest;
023: import org.apache.tools.ant.taskdefs.optional.jsp.Jasper41Mangler;
024: import org.apache.tools.ant.taskdefs.optional.jsp.JspMangler;
025: import org.apache.tools.ant.taskdefs.optional.jsp.JspNameMangler;
026: import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapter;
027: import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapterFactory;
028:
029: /**
030: * Tests the Jspc task.
031: *
032: * @created 07 March 2002
033: * @since Ant 1.5
034: */
035: public class JspcTest extends BuildFileTest {
036: /**
037: * Description of the Field
038: */
039: private File baseDir;
040: /**
041: * Description of the Field
042: */
043: private File outDir;
044:
045: /**
046: * Description of the Field
047: */
048: private final static String TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/";
049:
050: /**
051: * Constructor for the JspcTest object
052: *
053: * @param name Description of Parameter
054: */
055: public JspcTest(String name) {
056: super (name);
057: }
058:
059: /**
060: * The JUnit setup method
061: */
062: public void setUp() {
063: configureProject(TASKDEFS_DIR + "jspc.xml");
064: baseDir = new File(System.getProperty("root"), TASKDEFS_DIR);
065: outDir = new File(baseDir, "jsp/java");
066: }
067:
068: /**
069: * The teardown method for JUnit
070: */
071: public void tearDown() {
072: executeTarget("cleanup");
073: }
074:
075: /**
076: * A unit test for JUnit
077: */
078: public void testSimple() throws Exception {
079: executeJspCompile("testSimple", "simple_jsp.java");
080: }
081:
082: /**
083: * A unit test for JUnit
084: */
085: public void testUriroot() throws Exception {
086: executeJspCompile("testUriroot", "uriroot_jsp.java");
087: }
088:
089: /**
090: * A unit test for JUnit
091: */
092: public void testXml() throws Exception {
093: executeJspCompile("testXml", "xml_jsp.java");
094: }
095:
096: /**
097: * try a keyword in a file
098: */
099: public void testKeyword() throws Exception {
100: executeJspCompile("testKeyword", "default_jsp.java");
101: }
102:
103: /**
104: * what happens to 1nvalid-classname
105: */
106: public void testInvalidClassname() throws Exception {
107: executeJspCompile("testInvalidClassname",
108: "_1nvalid_0002dclassname_jsp.java");
109: }
110:
111: /**
112: * A unit test for JUnit
113: */
114: public void testNoTld() throws Exception {
115: // expectBuildExceptionContaining("testNoTld",
116: // "Jasper found an error in a file",
117: // "Java returned: 9");
118: expectBuildExceptionContaining("testNoTld", "not found",
119: "Java returned: 9");
120: }
121:
122: /**
123: * A unit test for JUnit
124: */
125: public void testNotAJspFile() throws Exception {
126: executeTarget("testNotAJspFile");
127: }
128:
129: /**
130: * webapp test is currently broken, because it picks up
131: * on the missing_tld file, and bails.
132: */
133: /*
134: public void testWebapp() throws Exception {
135: executeTarget("testWebapp");
136: }
137: */
138: /**
139: * run a target then verify the named file gets created
140: *
141: * @param target Description of Parameter
142: * @param javafile Description of Parameter
143: * @exception Exception trouble
144: */
145: protected void executeJspCompile(String target, String javafile)
146: throws Exception {
147: executeTarget(target);
148: assertJavaFileCreated(javafile);
149: }
150:
151: /**
152: * verify that a named file was created
153: *
154: * @param filename Description of Parameter
155: * @exception Exception trouble
156: */
157: protected void assertJavaFileCreated(String filename)
158: throws Exception {
159: File file = getOutputFile(filename);
160: assertTrue("file " + filename + " not found", file.exists());
161: assertTrue("file " + filename + " is empty", file.length() > 0);
162: }
163:
164: /**
165: * Gets the OutputFile attribute of the JspcTest object
166: *
167: * @param subpath Description of Parameter
168: * @return The OutputFile value
169: */
170: protected File getOutputFile(String subpath) {
171: return new File(outDir, subpath);
172: }
173:
174: /**
175: * verify that we select the appropriate mangler
176: */
177: public void testJasperNameManglerSelection() {
178: JspCompilerAdapter adapter = JspCompilerAdapterFactory
179: .getCompiler("jasper", null, null);
180: JspMangler mangler = adapter.createMangler();
181: assertTrue(mangler instanceof JspNameMangler);
182: adapter = JspCompilerAdapterFactory.getCompiler("jasper41",
183: null, null);
184: mangler = adapter.createMangler();
185: assertTrue(mangler instanceof Jasper41Mangler);
186: }
187:
188: public void testJasper41() {
189: JspMangler mangler = new Jasper41Mangler();
190: //java keywords are not special
191: assertMapped(mangler, "for.jsp", "for_jsp");
192: //underscores go in front of invalid start chars
193: assertMapped(mangler, "0.jsp", "_0_jsp");
194: //underscores at the front get an underscore too
195: assertMapped(mangler, "_.jsp", "___jsp");
196: //non java char at start => underscore then the the _hex value
197: assertMapped(mangler, "-.jsp", "__0002d_jsp");
198: //and paths are stripped
199: char s = File.separatorChar;
200: assertMapped(mangler, "" + s + s + "somewhere" + s + "file" + s
201: + "index.jsp", "index_jsp");
202: }
203:
204: /**
205: * assert our mapping rules
206: * @param mangler
207: * @param filename
208: * @param classname
209: */
210: protected void assertMapped(JspMangler mangler, String filename,
211: String classname) {
212: String mappedname = mangler
213: .mapJspToJavaName(new File(filename));
214: assertTrue(filename + " should have mapped to " + classname
215: + " but instead mapped to " + mappedname, classname
216: .equals(mappedname));
217: }
218:
219: }
|