Add testcase for <replaceregexp>, this task now works with ORO, Jakarta Regexp and JDK 1.4 if you use \1 to refer to parens in the regular expression. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269822 13f79535-47bb-0310-9956-ffa450edef68master
@@ -4,6 +4,12 @@ Changes from Ant 1.4.1 to current CVS version | |||||
Changes that could break older environments: | Changes that could break older environments: | ||||
-------------------------------------------- | -------------------------------------------- | ||||
* the RegexpMatcher interface has been extended to support case | |||||
insensitive matches and other options - custom implementations of | |||||
this interface won't work any longer. We recommend to use the new | |||||
Regexp interface that also supports substitution instead of the | |||||
RegexpMatcher interface in the future. | |||||
Fixed bugs: | Fixed bugs: | ||||
----------- | ----------- | ||||
@@ -36,6 +36,7 @@ | |||||
<property name="ant.package" value="org/apache/tools/ant"/> | <property name="ant.package" value="org/apache/tools/ant"/> | ||||
<property name="optional.package" value="${ant.package}/taskdefs/optional"/> | <property name="optional.package" value="${ant.package}/taskdefs/optional"/> | ||||
<property name="regexp.package" value="${ant.package}/util/regexp"/> | |||||
<property name="manifest" value="src/etc/manifest"/> | <property name="manifest" value="src/etc/manifest"/> | ||||
@@ -218,11 +219,11 @@ | |||||
deprecation="${deprecation}" | deprecation="${deprecation}" | ||||
optimize="${optimize}" > | optimize="${optimize}" > | ||||
<classpath refid="classpath" /> | <classpath refid="classpath" /> | ||||
<exclude name="${ant.package}/util/regexp/JakartaRegexp*.java" | |||||
<exclude name="${regexp.package}/JakartaRegexp*.java" | |||||
unless="jakarta.regexp.present" /> | unless="jakarta.regexp.present" /> | ||||
<exclude name="${ant.package}/util/regexp/JakartaOro*.java" | |||||
<exclude name="${regexp.package}/JakartaOro*.java" | |||||
unless="jakarta.oro.present" /> | unless="jakarta.oro.present" /> | ||||
<exclude name="${ant.package}/util/regexp/Jdk14Regexp*.java" | |||||
<exclude name="${regexp.package}/Jdk14Regexp*.java" | |||||
unless="jdk1.4+" /> | unless="jdk1.4+" /> | ||||
<exclude name="${ant.package}/AntSecurityManager.java" | <exclude name="${ant.package}/AntSecurityManager.java" | ||||
unless="jdk1.2+" /> | unless="jdk1.2+" /> | ||||
@@ -650,11 +651,11 @@ | |||||
<exclude name="org/apache/tools/ant/taskdefs/optional/ANTLRTest.java" | <exclude name="org/apache/tools/ant/taskdefs/optional/ANTLRTest.java" | ||||
unless="antlr.present" /> | unless="antlr.present" /> | ||||
<exclude name="org/apache/tools/ant/util/regexp/JakartaRegexp*Test.java" | |||||
<exclude name="${regexp.package}/JakartaRegexp*Test.java" | |||||
unless="jakarta.regexp.present" /> | unless="jakarta.regexp.present" /> | ||||
<exclude name="org/apache/tools/ant/util/regexp/JakartaOro*Test.java" | |||||
<exclude name="${regexp.package}/JakartaOro*Test.java" | |||||
unless="jakarta.oro.present" /> | unless="jakarta.oro.present" /> | ||||
<exclude name="org/apache/tools/ant/util/regexp/Jdk14Regexp*Test.java" | |||||
<exclude name="${regexp.package}/Jdk14Regexp*Test.java" | |||||
unless="jdk1.4+" /> | unless="jdk1.4+" /> | ||||
<exclude name="org/apache/tools/ant/taskdefs/optional/sitraka/*.java" | <exclude name="org/apache/tools/ant/taskdefs/optional/sitraka/*.java" | ||||
unless="jakarta.oro.present" /> | unless="jakarta.oro.present" /> | ||||
@@ -705,7 +706,8 @@ | |||||
<include name="**/*Test*" /> | <include name="**/*Test*" /> | ||||
<!-- abstract class, not a testcase --> | <!-- abstract class, not a testcase --> | ||||
<exclude name="org/apache/tools/ant/taskdefs/TaskdefsTest.java" /> | <exclude name="org/apache/tools/ant/taskdefs/TaskdefsTest.java" /> | ||||
<exclude name="org/apache/tools/ant/util/regexp/RegexpMatcherTest.java" /> | |||||
<exclude name="${regexp.package}/RegexpMatcherTest.java" /> | |||||
<exclude name="${regexp.package}/RegexpTest.java" /> | |||||
<!-- helper classes, not testcases --> | <!-- helper classes, not testcases --> | ||||
<exclude name="org/apache/tools/ant/taskdefs/TaskdefTest*Task.java" /> | <exclude name="org/apache/tools/ant/taskdefs/TaskdefTest*Task.java" /> | ||||
@@ -720,14 +722,20 @@ | |||||
<!-- only run these tests if their required libraries are installed --> | <!-- only run these tests if their required libraries are installed --> | ||||
<exclude name="org/apache/tools/ant/taskdefs/optional/ANTLRTest.java" | <exclude name="org/apache/tools/ant/taskdefs/optional/ANTLRTest.java" | ||||
unless="antlr.present" /> | unless="antlr.present" /> | ||||
<exclude name="org/apache/tools/ant/util/regexp/JakartaRegexp*Test.java" | |||||
<exclude name="${regexp.package}/JakartaRegexp*Test.java" | |||||
unless="jakarta.regexp.present" /> | unless="jakarta.regexp.present" /> | ||||
<exclude name="org/apache/tools/ant/util/regexp/JakartaOro*Test.java" | |||||
<exclude name="${regexp.package}/JakartaOro*Test.java" | |||||
unless="jakarta.oro.present" /> | unless="jakarta.oro.present" /> | ||||
<exclude name="org/apache/tools/ant/util/regexp/Jdk14Regexp*Test.java" | |||||
<exclude name="${regexp.package}/Jdk14Regexp*Test.java" | |||||
unless="jdk1.4+" /> | unless="jdk1.4+" /> | ||||
<exclude name="${optional.package}/ide/VAJExportTest.java" unless="vaj.present" /> | <exclude name="${optional.package}/ide/VAJExportTest.java" unless="vaj.present" /> | ||||
<exclude name="${optional.package}/sitraka/*.java" unless="jakarta.oro.present" /> | <exclude name="${optional.package}/sitraka/*.java" unless="jakarta.oro.present" /> | ||||
<exclude name="${optional.package}/ReplaceRegExpTest.java" | |||||
unless="some.regexp.support" /> | |||||
<!-- defer for now --> | |||||
<exclude name="${regexp.package}/MatcherWrappedAsRegexpTest.java" | |||||
unless="invalid-some.regexp.support" /> | |||||
<!-- run when you have the environment setup to support them --> | <!-- run when you have the environment setup to support them --> | ||||
<exclude name="org/apache/tools/ant/taskdefs/optional/net/FtpTest.java" /> | <exclude name="org/apache/tools/ant/taskdefs/optional/net/FtpTest.java" /> | ||||
@@ -1,10 +1,14 @@ | |||||
<project name="test" default="def" basedir="."> | <project name="test" default="def" basedir="."> | ||||
<target name="def"> | |||||
<fail>This build file should only be run from within the testcase</fail> | |||||
</target> | |||||
<target name="setup"> | <target name="setup"> | ||||
<copy file="replaceregexp.properties" tofile="test.properties" /> | <copy file="replaceregexp.properties" tofile="test.properties" /> | ||||
</target> | </target> | ||||
<target name="def" depends="setup"> | |||||
<target name="testReplace" depends="setup"> | |||||
<replaceregexp file="test.properties" byline="true"> | <replaceregexp file="test.properties" byline="true"> | ||||
<regularexpression pattern="Old(.*)=(.*)" /> | <regularexpression pattern="Old(.*)=(.*)" /> | ||||
<substitution expression="NewProp=\1\2" /> | <substitution expression="NewProp=\1\2" /> | ||||
@@ -75,15 +75,38 @@ public class JakartaOroRegexp extends JakartaOroMatcher implements Regexp | |||||
public String substitute(String input, String argument, int options) | public String substitute(String input, String argument, int options) | ||||
throws BuildException | throws BuildException | ||||
{ | { | ||||
// Determine replacement Type | |||||
int sOptions = getSubsOptions(options); | |||||
// translate \1 to $1 so that the Perl5Substitution will work | |||||
StringBuffer subst = new StringBuffer(); | |||||
for (int i=0; i<argument.length(); i++) { | |||||
char c = argument.charAt(i); | |||||
if (c == '\\') { | |||||
if (++i < argument.length()) { | |||||
c = argument.charAt(i); | |||||
int value = Character.digit(c, 10); | |||||
if (value > -1) { | |||||
subst.append("$").append(value); | |||||
} else { | |||||
subst.append(c); | |||||
} | |||||
} else { | |||||
// XXX - should throw an exception instead? | |||||
subst.append('\\'); | |||||
} | |||||
} else { | |||||
subst.append(c); | |||||
} | |||||
} | |||||
// Do the substitution | // Do the substitution | ||||
Substitution s = new Perl5Substitution(argument, sOptions); | |||||
Substitution s = | |||||
new Perl5Substitution(subst.toString(), | |||||
Perl5Substitution.INTERPOLATE_ALL); | |||||
return Util.substitute(matcher, | return Util.substitute(matcher, | ||||
getCompiledPattern(options), | getCompiledPattern(options), | ||||
s, | s, | ||||
input); | |||||
input, | |||||
getSubsOptions(options)); | |||||
} | } | ||||
protected int getSubsOptions(int options) | protected int getSubsOptions(int options) | ||||
@@ -81,8 +81,33 @@ public class JakartaRegexpRegexp extends JakartaRegexpMatcher implements Regexp | |||||
public String substitute(String input, String argument, int options) | public String substitute(String input, String argument, int options) | ||||
throws BuildException | throws BuildException | ||||
{ | { | ||||
int sOptions = getSubsOptions(options); | |||||
Vector v = getGroups(input, options); | |||||
// replace \1 with the corresponding group | |||||
StringBuffer result = new StringBuffer(); | |||||
for (int i=0; i<argument.length(); i++) { | |||||
char c = argument.charAt(i); | |||||
if (c == '\\') { | |||||
if (++i < argument.length()) { | |||||
c = argument.charAt(i); | |||||
int value = Character.digit(c, 10); | |||||
if (value > -1) { | |||||
result.append((String) v.elementAt(value)); | |||||
} else { | |||||
result.append(c); | |||||
} | |||||
} else { | |||||
// XXX - should throw an exception instead? | |||||
result.append('\\'); | |||||
} | |||||
} else { | |||||
result.append(c); | |||||
} | |||||
} | |||||
argument = result.toString(); | |||||
RE reg = getCompiledPattern(options); | RE reg = getCompiledPattern(options); | ||||
int sOptions = getSubsOptions(options); | |||||
return reg.subst(input, argument, sOptions); | return reg.subst(input, argument, sOptions); | ||||
} | } | ||||
} | } |
@@ -83,6 +83,29 @@ public class Jdk14RegexpRegexp extends Jdk14RegexpMatcher implements Regexp | |||||
public String substitute(String input, String argument, int options) | public String substitute(String input, String argument, int options) | ||||
throws BuildException | throws BuildException | ||||
{ | { | ||||
// translate \1 to $(1) so that the Matcher will work | |||||
StringBuffer subst = new StringBuffer(); | |||||
for (int i=0; i<argument.length(); i++) { | |||||
char c = argument.charAt(i); | |||||
if (c == '\\') { | |||||
if (++i < argument.length()) { | |||||
c = argument.charAt(i); | |||||
int value = Character.digit(c, 10); | |||||
if (value > -1) { | |||||
subst.append("$(").append(value).append(")"); | |||||
} else { | |||||
subst.append(c); | |||||
} | |||||
} else { | |||||
// XXX - should throw an exception instead? | |||||
subst.append('\\'); | |||||
} | |||||
} else { | |||||
subst.append(c); | |||||
} | |||||
} | |||||
argument = subst.toString(); | |||||
int sOptions = getSubsOptions(options); | int sOptions = getSubsOptions(options); | ||||
Pattern p = getCompiledPattern(options); | Pattern p = getCompiledPattern(options); | ||||
StringBuffer sb = new StringBuffer(); | StringBuffer sb = new StringBuffer(); | ||||
@@ -95,10 +118,12 @@ public class Jdk14RegexpRegexp extends Jdk14RegexpMatcher implements Regexp | |||||
else | else | ||||
{ | { | ||||
boolean res = m.find(); | boolean res = m.find(); | ||||
if (res) | |||||
if (res) { | |||||
m.appendReplacement(sb, argument); | m.appendReplacement(sb, argument); | ||||
else | |||||
m.appendTail(sb); | |||||
} else { | |||||
sb.append(input); | sb.append(input); | ||||
} | |||||
} | } | ||||
return sb.toString(); | return sb.toString(); | ||||
@@ -0,0 +1,160 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.util.regexp; | |||||
import org.apache.tools.ant.BuildException; | |||||
import java.util.Vector; | |||||
/** | |||||
* Helper class that adapts a RegexpMatcher to a Regexp. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
* @version $Revision$ | |||||
*/ | |||||
public class MatcherWrappedAsRegexp implements Regexp { | |||||
private RegexpMatcher matcher; | |||||
public MatcherWrappedAsRegexp(RegexpMatcher matcher) { | |||||
this.matcher = matcher; | |||||
} | |||||
/** | |||||
* Set the regexp pattern from the String description. | |||||
*/ | |||||
public void setPattern(String pattern) throws BuildException { | |||||
matcher.setPattern(pattern); | |||||
} | |||||
/** | |||||
* Get a String representation of the regexp pattern | |||||
*/ | |||||
public String getPattern() throws BuildException { | |||||
return matcher.getPattern(); | |||||
} | |||||
/** | |||||
* Does the given argument match the pattern? | |||||
*/ | |||||
public boolean matches(String argument) throws BuildException { | |||||
return matcher.matches(argument); | |||||
} | |||||
/** | |||||
* Returns a Vector of matched groups found in the argument. | |||||
* | |||||
* <p>Group 0 will be the full match, the rest are the | |||||
* parenthesized subexpressions</p>. | |||||
*/ | |||||
public Vector getGroups(String argument) throws BuildException { | |||||
return matcher.getGroups(argument); | |||||
} | |||||
/** | |||||
* Does this regular expression match the input, given | |||||
* certain options | |||||
* @param input The string to check for a match | |||||
* @param options The list of options for the match. See the | |||||
* MATCH_ constants above. | |||||
*/ | |||||
public boolean matches(String input, int options) throws BuildException { | |||||
return matcher.matches(input, options); | |||||
} | |||||
/** | |||||
* Get the match groups from this regular expression. The return | |||||
* type of the elements is always String. | |||||
* @param input The string to check for a match | |||||
* @param options The list of options for the match. See the | |||||
* MATCH_ constants above. | |||||
*/ | |||||
public Vector getGroups(String input, int options) throws BuildException { | |||||
return matcher.getGroups(input, options); | |||||
} | |||||
/** | |||||
* Perform a substitution on the regular expression. | |||||
* @param input The string to substitute on | |||||
* @param argument The string which defines the substitution | |||||
* @param options The list of options for the match and replace. See the | |||||
* MATCH_ and REPLACE_ constants above. | |||||
* REPLACE_ constants will be ignored. | |||||
*/ | |||||
public String substitute(String input, String argument, int options) | |||||
throws BuildException { | |||||
Vector v = matcher.getGroups(input, options); | |||||
StringBuffer result = new StringBuffer(); | |||||
char[] to = argument.toCharArray(); | |||||
for (int i=0; i<to.length; i++) { | |||||
if (to[i] == '\\') { | |||||
if (++i < to.length) { | |||||
int value = Character.digit(to[i], 10); | |||||
if (value > -1) { | |||||
result.append((String) v.elementAt(value)); | |||||
} else { | |||||
result.append(to[i]); | |||||
} | |||||
} else { | |||||
// XXX - should throw an exception instead? | |||||
result.append('\\'); | |||||
} | |||||
} else { | |||||
result.append(to[i]); | |||||
} | |||||
} | |||||
return result.toString(); | |||||
} | |||||
} |
@@ -63,6 +63,17 @@ import org.apache.tools.ant.BuildException; | |||||
*/ | */ | ||||
public interface Regexp extends RegexpMatcher | public interface Regexp extends RegexpMatcher | ||||
{ | { | ||||
/** | |||||
* Replace only the first occurance of the regular expression | |||||
*/ | |||||
int REPLACE_FIRST = 0x00000001; | |||||
/** | |||||
* Replace all occurances of the regular expression | |||||
*/ | |||||
int REPLACE_ALL = 0x00000010; | |||||
/** | /** | ||||
* Perform a substitution on the regular expression. | * Perform a substitution on the regular expression. | ||||
* @param input The string to substitute on | * @param input The string to substitute on | ||||
@@ -90,24 +90,33 @@ public class RegexpFactory extends RegexpMatcherFactory | |||||
} | } | ||||
if (systemDefault != null) { | if (systemDefault != null) { | ||||
return (Regexp)createInstance(systemDefault); | |||||
return createRegexpInstance(systemDefault); | |||||
// XXX should we silently catch possible exceptions and try to | // XXX should we silently catch possible exceptions and try to | ||||
// load a different implementation? | // load a different implementation? | ||||
} | } | ||||
try { | try { | ||||
return (Regexp)createInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp"); | |||||
return createRegexpInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp"); | |||||
} catch (BuildException be) {} | } catch (BuildException be) {} | ||||
try { | try { | ||||
return (Regexp)createInstance("org.apache.tools.ant.util.regexp.JakartaOroRegexp"); | |||||
return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaOroRegexp"); | |||||
} catch (BuildException be) {} | } catch (BuildException be) {} | ||||
try { | try { | ||||
return (Regexp)createInstance("org.apache.tools.ant.util.regexp.JakartaRegexpRegexp"); | |||||
return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaRegexpRegexp"); | |||||
} catch (BuildException be) {} | } catch (BuildException be) {} | ||||
throw new BuildException("No supported regular expression matcher found"); | throw new BuildException("No supported regular expression matcher found"); | ||||
} | } | ||||
protected Regexp createRegexpInstance(String classname) { | |||||
RegexpMatcher m = createInstance(classname); | |||||
if (m instanceof Regexp) { | |||||
return (Regexp) m; | |||||
} else { | |||||
return new MatcherWrappedAsRegexp(m); | |||||
} | |||||
} | |||||
} | } |
@@ -65,16 +65,6 @@ import java.util.Vector; | |||||
*/ | */ | ||||
public interface RegexpMatcher { | public interface RegexpMatcher { | ||||
/*** | |||||
* Replace only the first occurance of the regular expression | |||||
*/ | |||||
int REPLACE_FIRST = 0x00000001; | |||||
/*** | |||||
* Replace all occurances of the regular expression | |||||
*/ | |||||
int REPLACE_ALL = 0x00000010; | |||||
/*** | /*** | ||||
* Default Mask (case insensitive, neither multiline nor | * Default Mask (case insensitive, neither multiline nor | ||||
* singleline specified). | * singleline specified). | ||||
@@ -0,0 +1,114 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.optional; | |||||
import org.apache.tools.ant.taskdefs.TaskdefsTest; | |||||
import java.util.Properties; | |||||
import java.io.FileInputStream; | |||||
import java.io.IOException; | |||||
/** | |||||
* JUnit Testcase for the optional replaceregexp task. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
* @version $Revision$ | |||||
*/ | |||||
public class ReplaceRegExpTest extends TaskdefsTest { | |||||
public ReplaceRegExpTest(String name) { | |||||
super(name); | |||||
} | |||||
public void setUp() { | |||||
configureProject("src/etc/testcases/taskdefs/optional/replaceregexp.xml"); | |||||
} | |||||
public void tearDown() { | |||||
executeTarget("cleanup"); | |||||
} | |||||
public void testReplace() throws IOException { | |||||
Properties original = new Properties(); | |||||
FileInputStream propsFile = null; | |||||
try { | |||||
propsFile = new FileInputStream("src/etc/testcases/taskdefs/optional/replaceregexp.properties"); | |||||
original.load(propsFile); | |||||
} finally { | |||||
if (propsFile != null) { | |||||
propsFile.close(); | |||||
propsFile = null; | |||||
} | |||||
} | |||||
assertEquals("Def", original.get("OldAbc")); | |||||
executeTarget("testReplace"); | |||||
Properties after = new Properties(); | |||||
try { | |||||
propsFile = new FileInputStream("src/etc/testcases/taskdefs/optional/test.properties"); | |||||
after.load(propsFile); | |||||
} finally { | |||||
if (propsFile != null) { | |||||
propsFile.close(); | |||||
propsFile = null; | |||||
} | |||||
} | |||||
assertNull(after.get("OldAbc")); | |||||
assertEquals("AbcDef", after.get("NewProp")); | |||||
} | |||||
}// ReplaceRegExpTest |
@@ -0,0 +1,72 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2000 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.util.regexp; | |||||
/** | |||||
* Tests for the jakarta-oro implementation of the Regexp interface. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class JakartaOroRegexpTest extends RegexpTest { | |||||
public Regexp getRegexpImplementation() { | |||||
return new JakartaOroRegexp(); | |||||
} | |||||
public JakartaOroRegexpTest(String name) { | |||||
super(name); | |||||
} | |||||
} |
@@ -0,0 +1,76 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2000 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.util.regexp; | |||||
/** | |||||
* Tests for the jakarta-regexp implementation of the Regexp interface. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class JakartaRegexpRegexpTest extends RegexpTest { | |||||
public Regexp getRegexpImplementation() { | |||||
return new JakartaRegexpRegexp(); | |||||
} | |||||
public JakartaRegexpRegexpTest(String name) { | |||||
super(name); | |||||
} | |||||
/** | |||||
* Fails for "default" mode. | |||||
*/ | |||||
protected void doEndTest2(String text) {} | |||||
} |
@@ -0,0 +1,72 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.util.regexp; | |||||
/** | |||||
* Tests for the JDK 1.4 implementation of the Regexp interface. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class Jdk14RegexpRegexpTest extends RegexpTest { | |||||
public Regexp getRegexpImplementation() { | |||||
return new Jdk14RegexpRegexp(); | |||||
} | |||||
public Jdk14RegexpRegexpTest(String name) { | |||||
super(name); | |||||
} | |||||
} |
@@ -0,0 +1,85 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.util.regexp; | |||||
/** | |||||
* Test for the wrapped implementation of the Regexp Interface. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class MatcherWrappedAsRegexpTest extends RegexpTest { | |||||
public MatcherWrappedAsRegexpTest(String name) { | |||||
super(name); | |||||
} | |||||
public Regexp getRegexpImplementation() { | |||||
RegexpMatcherFactory fac = new RegexpMatcherFactory(); | |||||
return new MatcherWrappedAsRegexp(fac.newRegexpMatcher()); | |||||
} | |||||
/** | |||||
* Jakarta Regexp fails for "default" mode. | |||||
*/ | |||||
protected void doEndTest2(String text) { | |||||
RegexpMatcherFactory fac = new RegexpMatcherFactory(); | |||||
RegexpMatcher m = fac.newRegexpMatcher(); | |||||
if (m.getClass().getName().equals("org.apache.tools.ant.util.regexp.JakartaRegexpMatcher")) { | |||||
// ignore | |||||
} else { | |||||
super.doEndTest2(text); | |||||
} | |||||
} | |||||
} |
@@ -72,6 +72,8 @@ public abstract class RegexpMatcherTest extends TestCase { | |||||
public abstract RegexpMatcher getImplementation(); | public abstract RegexpMatcher getImplementation(); | ||||
protected final RegexpMatcher getReg() {return reg;} | |||||
public RegexpMatcherTest(String name) { | public RegexpMatcherTest(String name) { | ||||
super(name); | super(name); | ||||
} | } | ||||
@@ -0,0 +1,100 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.util.regexp; | |||||
/** | |||||
* Tests for all implementations of the Regexp interface. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public abstract class RegexpTest extends RegexpMatcherTest { | |||||
private static final String test = "abcdefg-abcdefg"; | |||||
private static final String pattern = "ab([^d]*)d([^f]*)f"; | |||||
public RegexpTest(String name) { | |||||
super(name); | |||||
} | |||||
public final RegexpMatcher getImplementation() { | |||||
return getRegexpImplementation(); | |||||
} | |||||
public abstract Regexp getRegexpImplementation(); | |||||
public void testSubstitution() { | |||||
Regexp reg = (Regexp) getReg(); | |||||
reg.setPattern(pattern); | |||||
assertTrue(reg.matches(test)); | |||||
assertEquals("abedcfg-abcdefg", reg.substitute(test, "ab\\2d\\1f", | |||||
Regexp.MATCH_DEFAULT)); | |||||
} | |||||
public void testReplaceFirstSubstitution() { | |||||
Regexp reg = (Regexp) getReg(); | |||||
reg.setPattern(pattern); | |||||
assertTrue(reg.matches(test)); | |||||
assertEquals("abedcfg-abcdefg", reg.substitute(test, "ab\\2d\\1f", | |||||
Regexp.REPLACE_FIRST)); | |||||
} | |||||
public void testReplaceAllSubstitution() { | |||||
Regexp reg = (Regexp) getReg(); | |||||
reg.setPattern(pattern); | |||||
assertTrue(reg.matches(test)); | |||||
assertEquals("abedcfg-abedcfg", reg.substitute(test, "ab\\2d\\1f", | |||||
Regexp.REPLACE_ALL)); | |||||
} | |||||
} |