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;
019:
020: import java.io.FileReader;
021: import java.io.IOException;
022: import java.io.Reader;
023:
024: import org.apache.tools.ant.BuildFileTest;
025: import org.apache.tools.ant.taskdefs.optional.XsltTest;
026: import org.apache.tools.ant.util.FileUtils;
027:
028: /**
029: * TestCases for {@link XSLTProcess} task.
030: * XXX merge with {@link XsltTest}?
031: * @version 2003-08-05
032: */
033: public class StyleTest extends BuildFileTest {
034:
035: private static final FileUtils FILE_UTILS = FileUtils
036: .getFileUtils();
037:
038: public StyleTest(String s) {
039: super (s);
040: }
041:
042: protected void setUp() throws Exception {
043: configureProject("src/etc/testcases/taskdefs/style/build.xml");
044: //executeTarget("setup");
045: //commented out for performance while target is empty
046: }
047:
048: protected void tearDown() throws Exception {
049: executeTarget("teardown");
050: }
051:
052: public void testStyleIsSet() throws Exception {
053: expectSpecificBuildException("testStyleIsSet",
054: "no stylesheet specified", "specify the "
055: + "stylesheet either as a filename in style "
056: + "attribute or as a nested resource");
057: }
058:
059: public void testTransferParameterSet() throws Exception {
060: expectFileContains("testTransferParameterSet", // target
061: "out/out.xml", // file
062: "set='myvalue'"); // exptected string
063: }
064:
065: public void testTransferParameterEmpty() throws Exception {
066: expectFileContains("testTransferParameterEmpty", "out/out.xml",
067: "empty=''");
068: }
069:
070: public void testTransferParameterUnset() throws Exception {
071: expectFileContains("testTransferParameterUnset", "out/out.xml",
072: "undefined='${value}'");
073: }
074:
075: public void testTransferParameterUnsetWithIf() throws Exception {
076: expectFileContains("testTransferParameterUnsetWithIf",
077: "out/out.xml", "undefined='undefined default value'");
078: }
079:
080: public void testNewerStylesheet() throws Exception {
081: expectFileContains("testNewerStylesheet", "out/out.xml",
082: "new-value");
083: }
084:
085: public void testDefaultMapper() throws Exception {
086: testDefaultMapper("testDefaultMapper");
087: }
088:
089: public void testExplicitFileset() throws Exception {
090: testDefaultMapper("testExplicitFileset");
091: }
092:
093: public void testDefaultMapper(String target) throws Exception {
094: assertTrue(!(FileUtils.getFileUtils().resolveFile(getProject()
095: .getBaseDir(), "out/data.html")).exists());
096: expectFileContains(target, "out/data.html", "set='myvalue'");
097: }
098:
099: public void testCustomMapper() throws Exception {
100: assertTrue(!FILE_UTILS.resolveFile(getProject().getBaseDir(),
101: "out/out.xml").exists());
102: expectFileContains("testCustomMapper", "out/out.xml",
103: "set='myvalue'");
104: }
105:
106: public void testTypedMapper() throws Exception {
107: assertTrue(!FILE_UTILS.resolveFile(getProject().getBaseDir(),
108: "out/out.xml").exists());
109: expectFileContains("testTypedMapper", "out/out.xml",
110: "set='myvalue'");
111: }
112:
113: public void testDirectoryHierarchyWithDirMatching()
114: throws Exception {
115: executeTarget("testDirectoryHierarchyWithDirMatching");
116: assertTrue(FILE_UTILS.resolveFile(getProject().getBaseDir(),
117: "out/dest/level1/data.html").exists());
118: }
119:
120: public void testDirsWithSpaces() throws Exception {
121: executeTarget("testDirsWithSpaces");
122: assertTrue(FILE_UTILS.resolveFile(getProject().getBaseDir(),
123: "out/d est/data.html").exists());
124: }
125:
126: public void testWithStyleAttrAndResource() throws Exception {
127: expectSpecificBuildException("testWithStyleAttrAndResource",
128: "Must throws a BuildException", "specify the "
129: + "stylesheet either as a filename in style "
130: + "attribute or as a nested resource but not "
131: + "as both");
132: }
133:
134: public void testWithFileResource() throws Exception {
135: expectFileContains("testWithFileResource", "out/out.xml",
136: "set='value'");
137: }
138:
139: public void testWithUrlResource() throws Exception {
140: expectFileContains("testWithUrlResource", "out/out.xml",
141: "set='value'");
142: }
143:
144: public void testFilenameAsParam() throws Exception {
145: executeTarget("testFilenameAsParam");
146: assertFileContains("out/out/one.txt", "filename='one.xml'");
147: assertFileContains("out/out/two.txt", "filename='two.xml'");
148: assertFileContains("out/out/three.txt", "filename='three.xml'");
149: assertFileContains("out/out/dir/four.txt",
150: "filename='four.xml'");
151: assertFileContains("out/out/dir/four.txt",
152: "filedir ='-not-set-'");
153: }
154:
155: public void testFilenameAsParamNoSetting() throws Exception {
156: executeTarget("testFilenameAsParamNoSetting");
157: assertFileContains("out/out/one.txt", "filename='-not-set-'");
158: assertFileContains("out/out/two.txt", "filename='-not-set-'");
159: assertFileContains("out/out/three.txt", "filename='-not-set-'");
160: assertFileContains("out/out/dir/four.txt",
161: "filename='-not-set-'");
162: }
163:
164: public void testFilenameAndFiledirAsParam() throws Exception {
165: executeTarget("testFilenameAndFiledirAsParam");
166: assertFileContains("out/out/one.txt", "filename='one.xml'");
167: assertFileContains("out/out/one.txt", "filedir ='.'");
168: assertFileContains("out/out/dir/four.txt",
169: "filename='four.xml'");
170: assertFileContains("out/out/dir/four.txt", "filedir ='dir'");
171: }
172:
173: // ************* copied from ConcatTest *************
174:
175: // ------------------------------------------------------
176: // Helper methods - should be in BuildFileTest
177: // -----------------------------------------------------
178:
179: private String getFileString(String filename) throws IOException {
180: Reader r = null;
181: try {
182: r = new FileReader(getProject().resolveFile(filename));
183: return FileUtils.readFully(r);
184: } finally {
185: FileUtils.close(r);
186: }
187: }
188:
189: private void expectFileContains(String target, String filename,
190: String contains) throws IOException {
191: executeTarget(target);
192: assertFileContains(filename, contains);
193: }
194:
195: private void assertFileContains(String filename, String contains)
196: throws IOException {
197: String content = getFileString(filename);
198: assertTrue("expecting file " + filename + " to contain "
199: + contains + " but got " + content, content
200: .indexOf(contains) > -1);
201: }
202:
203: }
|