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 org.apache.tools.ant.BuildFileTest;
021:
022: import java.io.InputStream;
023: import java.io.IOException;
024: import java.net.URL;
025:
026: public class MakeUrlTest extends BuildFileTest {
027:
028: public MakeUrlTest(String s) {
029: super (s);
030: }
031:
032: public void setUp() {
033: configureProject("src/etc/testcases/taskdefs/makeurl.xml");
034: }
035:
036: public void testEmpty() {
037: expectBuildExceptionContaining("testEmpty", "missing property",
038: "property");
039: }
040:
041: public void testNoProperty() {
042: expectBuildExceptionContaining("testNoProperty",
043: "missing property", "property");
044: }
045:
046: public void testNoFile() {
047: expectBuildExceptionContaining("testNoFile", "missing file",
048: "file");
049: }
050:
051: public void testValidation() {
052: expectBuildExceptionContaining("testValidation",
053: MakeUrl.ERROR_MISSING_FILE, "file");
054: }
055:
056: public void testWorks() {
057: executeTarget("testWorks");
058: assertPropertyContains("testWorks", "file:");
059: assertPropertyContains("testWorks", "/foo");
060: }
061:
062: public void testIllegalChars() {
063: executeTarget("testIllegalChars");
064: assertPropertyContains("testIllegalChars", "file:");
065: assertPropertyContains("testIllegalChars", "fo%20o%25");
066: }
067:
068: /**
069: * test that we can round trip by opening a url that exists
070: *
071: * @throws IOException
072: */
073: public void testRoundTrip() throws IOException {
074: executeTarget("testRoundTrip");
075: assertPropertyContains("testRoundTrip", "file:");
076: String property = getProperty("testRoundTrip");
077: URL url = new URL(property);
078: InputStream instream = url.openStream();
079: instream.close();
080: }
081:
082: public void testIllegalCombinations() {
083: executeTarget("testIllegalCombinations");
084: assertPropertyContains("testIllegalCombinations", "/foo");
085: assertPropertyContains("testIllegalCombinations", ".xml");
086: }
087:
088: public void testFileset() {
089: executeTarget("testFileset");
090: assertPropertyContains("testFileset", ".xml ");
091: String result = getProperty("testFileset");
092: assertPropertyEndsWith("testFileset", ".xml");
093: }
094:
095: public void testFilesetSeparator() {
096: executeTarget("testFilesetSeparator");
097: assertPropertyContains("testFilesetSeparator", ".xml\",\"");
098: assertPropertyEndsWith("testFilesetSeparator", ".xml");
099: }
100:
101: public void testPath() {
102: executeTarget("testPath");
103: assertPropertyContains("testPath", "makeurl.xml");
104: }
105:
106: /**
107: * assert that a property ends with
108: *
109: * @param property
110: * @param ending
111: */
112: private void assertPropertyEndsWith(String property, String ending) {
113: String result = getProperty(property);
114: String substring = result.substring(result.length()
115: - ending.length());
116: assertEquals(ending, substring);
117: }
118:
119: /**
120: * assert that a property contains a string
121: *
122: * @param property name of property to look for
123: * @param contains what to search for in the string
124: */
125: protected void assertPropertyContains(String property,
126: String contains) {
127: String result = getProperty(property);
128:
129: assertTrue("expected " + contains + " in " + result,
130: result != null && result.indexOf(contains) >= 0);
131: }
132:
133: /**
134: * get a property from the project
135: *
136: * @param property
137: * @return
138: */
139: protected String getProperty(String property) {
140: return project.getProperty(property);
141: }
142: }
|