You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2004/06/17 20:53:42 UTC
cvs commit: jakarta-tapestry/framework/src/org/apache/tapestry/util RegexpMatcher.java
hlship 2004/06/17 11:53:42
Modified: framework/src/org/apache/tapestry/test/assertions
AssertOutput.java
framework/src/org/apache/tapestry/test
ScriptParser.properties ScriptParser.java
ScriptedTestSession.java
framework/src/org/apache/tapestry/parse
BeanSetPropertySetter.java SpecificationParser.java
DescriptionSetter.java
junit/src/org/apache/tapestry/test TestScriptParser.java
TestAssertOutput.java
junit/src/org/apache/tapestry/junit TapestryTestCase.java
eclipse Tapestry-Junit.launch
. .project .classpath
framework/src/org/apache/tapestry TapestryStrings.properties
junit build.xml
junit/src/org/apache/tapestry/junit/utils
TestRegexpMatcher.java
framework/src/org/apache/tapestry/util RegexpMatcher.java
Added: framework/src/org/apache/tapestry/test/assertions
RegexpMatch.java AssertRegexp.java
framework/src/org/apache/tapestry/test ScriptMessages.java
ScriptStrings.properties
junit/src/org/apache/tapestry/test AssertRegexp.sdl
AssertRegexpMatch.sdl TestAssertRegexp.java
Removed: junit/src/org/apache/tapestry/junit/valid ValidSuite.java
junit/src/org/apache/tapestry/junit BasicTestSuite.java
TapestrySuite.java
Log:
Update HiveMind libs to latest 1.0-beta-1-snapshot.
Revision Changes Path
1.2 +8 -1 jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/AssertOutput.java
Index: AssertOutput.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/AssertOutput.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AssertOutput.java 5 May 2004 02:41:20 -0000 1.1
+++ AssertOutput.java 17 Jun 2004 18:53:42 -0000 1.2
@@ -17,9 +17,16 @@
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.impl.BaseLocatable;
import org.apache.tapestry.test.ResponseAssertion;
+import org.apache.tapestry.test.ScriptMessages;
import org.apache.tapestry.test.ScriptedTestSession;
import org.apache.tapestry.test.mock.MockResponse;
+/**
+ * Assertion that checks for the presence of a particular
+ * substring within the response text.
+ *
+ * @author Howard Lewis Ship
+ */
public class AssertOutput extends BaseLocatable implements ResponseAssertion
{
private String _expectedSubstring;
@@ -39,7 +46,7 @@
return;
throw new ApplicationRuntimeException(
- "Expected output '" + _expectedSubstring + "' not found in response.",
+ ScriptMessages.expectedSubstringMissing(_expectedSubstring, getLocation()),
getLocation(),
null);
}
1.1 jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/RegexpMatch.java
Index: RegexpMatch.java
===================================================================
//Copyright 2004 The Apache Software Foundation
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package org.apache.tapestry.test.assertions;
import org.apache.hivemind.impl.BaseLocatable;
/**
* Used within a {@link org.apache.tapestry.test.assertions.AssertRegexp}
* to identify a single substring matched against the response. Basically,
* a wrapper around a string so that the location of the string can be tracked
* for error reporting purposes.
*
* @author Howard Lewis Ship
* @version $Id: RegexpMatch.java,v 1.1 2004/06/17 18:53:42 hlship Exp $
*/
public class RegexpMatch extends BaseLocatable
{
private String _expectedString;
public String getExpectedString()
{
return _expectedString;
}
public void setExpectedString(String string)
{
_expectedString = string;
}
}
1.1 jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/AssertRegexp.java
Index: AssertRegexp.java
===================================================================
//Copyright 2004 The Apache Software Foundation
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package org.apache.tapestry.test.assertions;
import java.util.ArrayList;
import java.util.List;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.impl.BaseLocatable;
import org.apache.tapestry.test.ResponseAssertion;
import org.apache.tapestry.test.ScriptMessages;
import org.apache.tapestry.test.ScriptedTestSession;
/**
* Assertion used to check for the presence of a regular expression
* within the response output. Alternately, it can check for a specific
* list of matches of a regular expression within the response.
*
* @author Howard Lewis Ship
* @version $Id: AssertRegexp.java,v 1.1 2004/06/17 18:53:42 hlship Exp $
*/
public class AssertRegexp extends BaseLocatable implements ResponseAssertion
{
private List _matches;
private String _regexp;
private int _subgroup;
public void addMatch(RegexpMatch m)
{
if (_matches == null)
_matches = new ArrayList();
_matches.add(m);
}
public void execute(ScriptedTestSession session)
{
if (_matches != null)
{
executeMatches(session);
return;
}
String responseContent = session.getResponse().getOutputString();
if (session.getMatcher().contains(_regexp, responseContent))
return;
throw new ApplicationRuntimeException(
ScriptMessages.expectedRegexpMissing(_regexp, getLocation()),
getLocation(),
null);
}
private void executeMatches(ScriptedTestSession session)
{
String responseContent = session.getResponse().getOutputString();
String[] matches = session.getMatcher().getMatches(_regexp, responseContent, _subgroup);
int expectedCount = _matches.size();
int count = Math.min(expectedCount, matches.length);
for (int i = 0; i < count; i++)
{
RegexpMatch m = (RegexpMatch) _matches.get(i);
String expected = m.getExpectedString();
String actual = matches[i];
if (expected.equals(actual))
continue;
throw new ApplicationRuntimeException(
ScriptMessages.incorrectRegexpMatch(expected, m.getLocation(), actual),
m.getLocation(),
null);
}
if (matches.length != expectedCount)
throw new ApplicationRuntimeException(
ScriptMessages.incorrectRegexpMatchCount(
_regexp,
getLocation(),
expectedCount,
matches.length),
getLocation(),
null);
}
public void setRegexp(String string)
{
_regexp = string;
}
public void setSubgroup(int subgroup)
{
_subgroup = subgroup;
}
}
1.2 +4 -3 jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptParser.properties
Index: ScriptParser.properties
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptParser.properties,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ScriptParser.properties 5 May 2004 02:41:19 -0000 1.1
+++ ScriptParser.properties 17 Jun 2004 18:53:42 -0000 1.2
@@ -29,6 +29,7 @@
required.request.servlet=false
required.request.servlet-path=false
-
required.parameter.name=true
-required.parameter.value=true
\ No newline at end of file
+required.parameter.value=true
+
+required.assert-regexp.subgroup=false
\ No newline at end of file
1.2 +107 -10 jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptParser.java
Index: ScriptParser.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ScriptParser.java 5 May 2004 02:41:19 -0000 1.1
+++ ScriptParser.java 17 Jun 2004 18:53:42 -0000 1.2
@@ -23,11 +23,14 @@
import java.util.Properties;
import org.apache.hivemind.ApplicationRuntimeException;
+import org.apache.hivemind.ErrorHandler;
import org.apache.hivemind.Resource;
import org.apache.hivemind.parse.AbstractParser;
import org.apache.hivemind.parse.ElementParseInfo;
import org.apache.hivemind.sdl.SDLResourceParser;
import org.apache.tapestry.test.assertions.AssertOutput;
+import org.apache.tapestry.test.assertions.AssertRegexp;
+import org.apache.tapestry.test.assertions.RegexpMatch;
import org.apache.tapestry.util.xml.DocumentParseException;
/**
@@ -35,7 +38,6 @@
* a sequence of operations and assertions.
*
* @author Howard Lewis Ship
- * @version $Id$
*/
public class ScriptParser extends AbstractParser
{
@@ -175,6 +177,8 @@
private static final int STATE_INIT_PARAMETER = 3;
private static final int STATE_REQUEST = 4;
private static final int STATE_ASSERT_OUTPUT = 5;
+ private static final int STATE_ASSERT_REGEXP = 6;
+ private static final int STATE_MATCH = 7;
private static final int STATE_NO_CONTENT = 1000;
@@ -201,6 +205,10 @@
beginRequest();
break;
+ case STATE_ASSERT_REGEXP :
+ beginAssertRegexp();
+ break;
+
default :
unexpectedElement(_elementName);
}
@@ -217,6 +225,14 @@
endAssertOutput();
break;
+ case STATE_ASSERT_REGEXP :
+ endAssertRegexp();
+ break;
+
+ case STATE_MATCH :
+ endMatch();
+ break;
+
default :
break;
}
@@ -296,6 +312,12 @@
return;
}
+ if (_elementName.equals("assert-regexp"))
+ {
+ enterAssertRegexp();
+ return;
+ }
+
unexpectedElement(_elementName);
}
@@ -311,6 +333,45 @@
push(_elementName, ao, STATE_ASSERT_OUTPUT, false);
}
+ private void enterAssertRegexp()
+ {
+ validateAttributes();
+
+ int subgroup = getIntAttribute("subgroup", 0);
+
+ AssertRegexp ar = new AssertRegexp();
+ ar.setSubgroup(subgroup);
+
+ RequestDescriptor rd = (RequestDescriptor) peekObject();
+
+ rd.addAssertion(ar);
+
+ push(_elementName, ar, STATE_ASSERT_REGEXP, false);
+ }
+
+ private void beginAssertRegexp()
+ {
+ if (_elementName.equals("match"))
+ {
+ enterMatch();
+ return;
+ }
+
+ unexpectedElement(_elementName);
+ }
+
+ private void enterMatch()
+ {
+ validateAttributes();
+
+ RegexpMatch m = new RegexpMatch();
+ AssertRegexp ar = (AssertRegexp) peekObject();
+
+ ar.addMatch(m);
+
+ push(_elementName, m, STATE_MATCH, false);
+ }
+
private void endAssertOutput()
{
String content = peekContent();
@@ -319,6 +380,24 @@
ao.setExpectedSubstring(content);
}
+ private void endAssertRegexp()
+ {
+ String content = peekContent();
+
+ AssertRegexp ar = (AssertRegexp) peekObject();
+
+ ar.setRegexp(content);
+ }
+
+ private void endMatch()
+ {
+ String content = peekContent();
+
+ RegexpMatch m = (RegexpMatch) peekObject();
+
+ m.setExpectedString(content);
+ }
+
protected String peekContent()
{
String rawContent = super.peekContent();
@@ -422,7 +501,7 @@
if (!epi.isKnown(name))
throw new DocumentParseException(
- "Unknown attribute: " + name + " at " + getLocation() + ".",
+ ScriptMessages.unexpectedAttributeInElement(name, _elementName),
getLocation(),
null);
}
@@ -435,14 +514,8 @@
String name = (String) i.next();
if (!_attributes.containsKey(name))
- throw new ApplicationRuntimeException(
- "Missing required attribute: "
- + name
- + " at "
- + getElementPath()
- + " ("
- + getLocation()
- + ").",
+ throw new DocumentParseException(
+ ScriptMessages.missingRequiredAttribute(name, _elementName),
getLocation(),
null);
}
@@ -460,5 +533,29 @@
}
return result;
+ }
+
+ private int getIntAttribute(String name, int defaultValue)
+ {
+ String attributeValue = getAttribute(name);
+
+ if (attributeValue == null)
+ return defaultValue;
+
+ try
+ {
+ return Integer.parseInt(attributeValue);
+ }
+ catch (NumberFormatException ex)
+ {
+ throw new ApplicationRuntimeException(
+ ScriptMessages.invalidIntAttribute(
+ name,
+ _elementName,
+ getLocation(),
+ attributeValue),
+ getLocation(),
+ ex);
+ }
}
}
1.2 +9 -2 jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptedTestSession.java
Index: ScriptedTestSession.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptedTestSession.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ScriptedTestSession.java 5 May 2004 02:41:19 -0000 1.1
+++ ScriptedTestSession.java 17 Jun 2004 18:53:42 -0000 1.2
@@ -26,6 +26,7 @@
import org.apache.tapestry.test.mock.MockRequest;
import org.apache.tapestry.test.mock.MockResponse;
import org.apache.tapestry.test.mock.MockServletConfig;
+import org.apache.tapestry.util.RegexpMatcher;
/**
* Executes a series of requests and assertions specified by
@@ -40,7 +41,8 @@
private MockContext _context;
private MockRequest _request;
private MockResponse _response;
-
+ private RegexpMatcher _matcher = new RegexpMatcher();
+
public ScriptedTestSession(ScriptDescriptor descriptor)
{
_scriptDescriptor = descriptor;
@@ -192,6 +194,11 @@
public MockResponse getResponse()
{
return _response;
+ }
+
+ public RegexpMatcher getMatcher()
+ {
+ return _matcher;
}
}
1.1 jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptMessages.java
Index: ScriptMessages.java
===================================================================
//Copyright 2004 The Apache Software Foundation
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package org.apache.tapestry.test;
import org.apache.hivemind.Location;
import org.apache.hivemind.impl.MessageFormatter;
/**
* Container of static methods to format logging and exception messages, used
* within the org.apache.tapesty.test package (and a few sub-packages).
*
* @author Howard Lewis Ship
* @since 3.1
*/
public final class ScriptMessages
{
private static final MessageFormatter _formatter =
new MessageFormatter(ScriptMessages.class, "ScriptStrings");
public static String expectedSubstringMissing(String substring, Location location)
{
return _formatter.format("expected-substring-missing", substring, location);
}
public static String expectedRegexpMissing(String regexp, Location location)
{
return _formatter.format("expected-regexp-missing", regexp, location);
}
public static String unexpectedAttributeInElement(String attributeName, String elementName)
{
return _formatter.format("unexpected-attribute-in-element", attributeName, elementName);
}
public static String missingRequiredAttribute(String attributeName, String elementName)
{
return _formatter.format("missing-required-attribute", attributeName, elementName);
}
public static String invalidIntAttribute(
String attributeName,
String elementName,
Location location,
String attributeValue)
{
return _formatter.format(
"invalid-int-attribute",
new Object[] { attributeName, elementName, location, attributeValue });
}
public static String incorrectRegexpMatch(
String expectedMatch,
Location location,
String actualMatch)
{
return _formatter.format("incorrect-regexp-match", expectedMatch, location, actualMatch);
}
public static String incorrectRegexpMatchCount(
String pattern,
Location location,
int expectedCount,
int actualCount)
{
return _formatter.format(
"incorrect-regexp-match-count",
new Object[] {
pattern,
location,
new Integer(expectedCount),
new Integer(actualCount)});
}
}
1.1 jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptStrings.properties
Index: ScriptStrings.properties
===================================================================
# Copyright 2004 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
expected-substring-missing=Expected text ("{0}", at {1}) was not found in the response.
expected-regexp-missing=Expected regular expression ("{0}", at {1}) was not found in the response.
incorrect-regexp-match=Expected match was ''{0}'' (at {1}), but actual value matched was ''{2}''.
incorrect-regexp-match-count=Regular expression ''{0}'' (at {1}) should have generated {2,number,integer} matches, but generated {3,number,integer} instead.
unexpected-attribute-in-element=Unexpected attribute ''{0}'' (in element {1}).
missing-required-attribute=Required attribute ''{0}'' is not supplied for element {1}.
invalid-int-attribute=Attribute ''{0}'' (of element {1}, at {2}) is ''{3}'', which is not an integer value.
1.2 +1 -2 jakarta-tapestry/framework/src/org/apache/tapestry/parse/BeanSetPropertySetter.java
Index: BeanSetPropertySetter.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/parse/BeanSetPropertySetter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BeanSetPropertySetter.java 3 May 2004 16:03:45 -0000 1.1
+++ BeanSetPropertySetter.java 17 Jun 2004 18:53:42 -0000 1.2
@@ -24,7 +24,6 @@
* parsed.
*
* @author Howard Lewis Ship
- * @version $Id$
*/
class BeanSetPropertySetter extends BaseLocatable
{
@@ -44,7 +43,7 @@
void applyExpression(String expression)
{
- PropertyUtils.write(_initializer, "expression", expression, getLocation());
+ PropertyUtils.write(_initializer, "expression", expression);
_beanSpecification.setLocation(getLocation());
_beanSpecification.addInitializer(_initializer);
1.23 +3 -4 jakarta-tapestry/framework/src/org/apache/tapestry/parse/SpecificationParser.java
Index: SpecificationParser.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/parse/SpecificationParser.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- SpecificationParser.java 5 May 2004 02:41:20 -0000 1.22
+++ SpecificationParser.java 17 Jun 2004 18:53:42 -0000 1.23
@@ -66,7 +66,6 @@
* thread safety.
*
* @author Howard Lewis Ship
- * @version $Id$
*/
public class SpecificationParser extends AbstractParser
{
@@ -1415,8 +1414,8 @@
IBeanInitializer bi = _factory.createMessageBeanInitializer();
- PropertyUtils.write(bi, "propertyName", name, getLocation());
- PropertyUtils.write(bi, "key", key, getLocation());
+ PropertyUtils.write(bi, "propertyName", name);
+ PropertyUtils.write(bi, "key", key);
bi.setLocation(getLocation());
@@ -1434,7 +1433,7 @@
IBeanInitializer bi = _factory.createExpressionBeanInitializer();
- PropertyUtils.write(bi, "propertyName", name, getLocation());
+ PropertyUtils.write(bi, "propertyName", name);
IBeanSpecification bs = (IBeanSpecification) peekObject();
1.2 +1 -2 jakarta-tapestry/framework/src/org/apache/tapestry/parse/DescriptionSetter.java
Index: DescriptionSetter.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/parse/DescriptionSetter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DescriptionSetter.java 3 May 2004 16:03:45 -0000 1.1
+++ DescriptionSetter.java 17 Jun 2004 18:53:42 -0000 1.2
@@ -22,7 +22,6 @@
* <description> element.
*
* @author Howard Lewis Ship
- * @version $Id$
*/
class DescriptionSetter extends BaseLocatable
{
@@ -35,6 +34,6 @@
void apply(String description)
{
- PropertyUtils.write(_descriptionHolder, "description", description, getLocation());
+ PropertyUtils.write(_descriptionHolder, "description", description);
}
}
1.2 +49 -14 jakarta-tapestry/junit/src/org/apache/tapestry/test/TestScriptParser.java
Index: TestScriptParser.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/org/apache/tapestry/test/TestScriptParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestScriptParser.java 5 May 2004 02:41:19 -0000 1.1
+++ TestScriptParser.java 17 Jun 2004 18:53:42 -0000 1.2
@@ -17,14 +17,12 @@
import java.io.PrintWriter;
import java.util.List;
-import junit.framework.AssertionFailedError;
-
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.Resource;
import org.apache.hivemind.impl.DefaultClassResolver;
-import org.apache.hivemind.test.HiveMindTestCase;
import org.apache.hivemind.util.ClasspathResource;
+import org.apache.tapestry.junit.TapestryTestCase;
import org.apache.tapestry.test.mock.MockContext;
import org.apache.tapestry.test.mock.MockRequest;
import org.apache.tapestry.test.mock.MockResponse;
@@ -34,10 +32,9 @@
* Tests {@link org.apache.tapestry.test.ScriptParser}.
*
* @author Howard Lewis Ship
- * @version $Id$
* @since 3.1
*/
-public class TestScriptParser extends HiveMindTestCase
+public class TestScriptParser extends TapestryTestCase
{
private ClassResolver _resolver = new DefaultClassResolver();
@@ -73,7 +70,9 @@
}
catch (DocumentParseException ex)
{
- checkException(ex, "Missing required attribute: context");
+ checkException(
+ ex,
+ "Required attribute 'context' is not supplied for element test-script.");
}
}
@@ -86,7 +85,7 @@
}
catch (DocumentParseException ex)
{
- checkException(ex, "Unknown attribute: cntext");
+ checkException(ex, "Unexpected attribute 'cntext' (in element test-script).");
}
}
@@ -116,7 +115,6 @@
{
checkException(ex, "Servlet descriptor 'default'");
checkException(ex, "conflicts with prior instance");
- assertNotNull(ex.getLocation());
}
}
@@ -190,18 +188,55 @@
}
catch (ApplicationRuntimeException ex)
{
- assertEquals(
- "Expected output '<title>Away</title>' not found in response.",
+ assertRegexp(
+ "Expected text \\(\"<title>Away</title>\", at .*?\\) was not found in the response\\.",
ex.getMessage());
assertNotNull(ex.getLocation());
}
}
- private void checkException(Throwable ex, String substring)
+ public void testAssertRegexp() throws Exception
{
- if (ex.getMessage().indexOf(substring) < 0)
- throw new AssertionFailedError(
- "Exception '" + ex.getMessage() + "' does not contain '" + substring + "'.");
+ ScriptDescriptor sd = parse("AssertRegexp.sdl");
+
+ ScriptedTestSession ss = createSession();
+
+ RequestDescriptor rd = (RequestDescriptor) sd.getRequestDescriptors().get(0);
+
+ try
+ {
+ rd.executeAssertions(ss);
+ unreachable();
+ }
+ catch (ApplicationRuntimeException ex)
+ {
+ assertRegexp(
+ "Expected regular expression \\(\"<body>.*</body>\", at .*?\\) was not found in the response\\.",
+ ex.getMessage());
+ assertNotNull(ex.getLocation());
+ }
+ }
+
+ public void testAssertRegexpMatch() throws Exception
+ {
+ ScriptDescriptor sd = parse("AssertRegexpMatch.sdl");
+
+ ScriptedTestSession ss = createSession();
+
+ RequestDescriptor rd = (RequestDescriptor) sd.getRequestDescriptors().get(0);
+
+ try
+ {
+ rd.executeAssertions(ss);
+ unreachable();
+ }
+ catch (ApplicationRuntimeException ex)
+ {
+ assertRegexp(
+ "Regular expression '<\\(\\.\\*\\?\\)>' \\(at .*?\\) should have generated 4 matches, but generated 2 instead\\.",
+ ex.getMessage());
+ assertNotNull(ex.getLocation());
+ }
}
/**
1.2 +5 -5 jakarta-tapestry/junit/src/org/apache/tapestry/test/TestAssertOutput.java
Index: TestAssertOutput.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/org/apache/tapestry/test/TestAssertOutput.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestAssertOutput.java 5 May 2004 02:41:19 -0000 1.1
+++ TestAssertOutput.java 17 Jun 2004 18:53:42 -0000 1.2
@@ -15,7 +15,7 @@
package org.apache.tapestry.test;
import org.apache.hivemind.ApplicationRuntimeException;
-import org.apache.hivemind.test.HiveMindTestCase;
+import org.apache.tapestry.junit.TapestryTestCase;
import org.apache.tapestry.test.assertions.AssertOutput;
/**
@@ -25,7 +25,7 @@
* @author Howard Lewis Ship
* @version $Id$
*/
-public class TestAssertOutput extends HiveMindTestCase
+public class TestAssertOutput extends TapestryTestCase
{
public void testSuccess() throws Exception
{
@@ -53,8 +53,8 @@
}
catch (ApplicationRuntimeException ex)
{
- assertEquals(
- "Expected output '<title>Home</title>' not found in response.",
+ assertRegexp(
+ "Expected text \\(\"<title>Home</title>\", at .*?\\) was not found in the response\\.",
ex.getMessage());
}
}
1.1 jakarta-tapestry/junit/src/org/apache/tapestry/test/AssertRegexp.sdl
Index: AssertRegexp.sdl
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// $Id: AssertRegexp.sdl,v 1.1 2004/06/17 18:53:42 hlship Exp $
test-script (context=test-context)
{
request
{
assert-regexp { "<body>.*</body>" }
}
}
1.1 jakarta-tapestry/junit/src/org/apache/tapestry/test/AssertRegexpMatch.sdl
Index: AssertRegexpMatch.sdl
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// $Id: AssertRegexpMatch.sdl,v 1.1 2004/06/17 18:53:42 hlship Exp $
test-script (context=test-context)
{
request
{
assert-regexp (subgroup=1)
{
"<(.*?)>"
match { "title" }
match { "/title" }
match { "body" }
match { "/body" }
}
}
}
1.1 jakarta-tapestry/junit/src/org/apache/tapestry/test/TestAssertRegexp.java
Index: TestAssertRegexp.java
===================================================================
//Copyright 2004 The Apache Software Foundation
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package org.apache.tapestry.test;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.tapestry.junit.TapestryTestCase;
import org.apache.tapestry.test.assertions.AssertRegexp;
import org.apache.tapestry.test.assertions.RegexpMatch;
/**
* Tests for the {@link org.apache.tapestry.test.assertions.AssertRegexp}
* class.
*
* @author Howard Lewis Ship
* @version $Id: TestAssertRegexp.java,v 1.1 2004/06/17 18:53:42 hlship Exp $
*/
public class TestAssertRegexp extends TapestryTestCase
{
public void testSuccess() throws Exception
{
ScriptedTestSession ss = TestScriptParser.createSession();
AssertRegexp ar = new AssertRegexp();
ar.setRegexp("<title>.*</title>");
ar.execute(ss);
}
public void testFailure() throws Exception
{
ScriptedTestSession ss = TestScriptParser.createSession();
AssertRegexp ar = new AssertRegexp();
ar.setRegexp("<body>.*</body>");
try
{
ar.execute(ss);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertRegexp(
"Expected regular expression \\(\"<body>.*</body>\", at .*?\\) was not found in the response\\.",
ex.getMessage());
}
}
public void testMatchesSuccess() throws Exception
{
ScriptedTestSession ss = TestScriptParser.createSession();
AssertRegexp ar = new AssertRegexp();
ar.setRegexp("<.*?>");
addMatch(ar, "<title>");
addMatch(ar, "</title>");
ar.execute(ss);
}
public void testMatchesWrongCount() throws Exception
{
ScriptedTestSession ss = TestScriptParser.createSession();
AssertRegexp ar = new AssertRegexp();
ar.setRegexp("<.*?>");
addMatch(ar, "<title>");
try
{
ar.execute(ss);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertRegexp(
"Regular expression '<\\.\\*\\?>' \\(at .*?\\) should have generated 1 matches, but generated 2 instead\\.",
ex.getMessage());
}
}
public void testMatchesSubgroupSuccess() throws Exception
{
ScriptedTestSession ss = TestScriptParser.createSession();
AssertRegexp ar = new AssertRegexp();
ar.setRegexp("<(.*?)>");
ar.setSubgroup(1);
addMatch(ar, "title");
addMatch(ar, "/title");
ar.execute(ss);
}
public void testMatchesFailure() throws Exception
{
ScriptedTestSession ss = TestScriptParser.createSession();
AssertRegexp ar = new AssertRegexp();
ar.setRegexp("<.*?>");
addMatch(ar, "<little>");
addMatch(ar, "</title>");
try
{
ar.execute(ss);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertRegexp(
"Expected match was '<little>' \\(at .*?\\), but actual value matched was '<title>'\\.",
ex.getMessage());
}
}
private void addMatch(AssertRegexp ar, String matchValue)
{
RegexpMatch m = new RegexpMatch();
m.setExpectedString(matchValue);
ar.addMatch(m);
}
}
1.10 +33 -14 jakarta-tapestry/junit/src/org/apache/tapestry/junit/TapestryTestCase.java
Index: TapestryTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/org/apache/tapestry/junit/TapestryTestCase.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- TapestryTestCase.java 30 Apr 2004 15:17:56 -0000 1.9
+++ TapestryTestCase.java 17 Jun 2004 18:53:42 -0000 1.10
@@ -19,11 +19,11 @@
import java.util.Locale;
import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.Resource;
import org.apache.hivemind.impl.DefaultClassResolver;
+import org.apache.hivemind.test.HiveMindTestCase;
import org.apache.hivemind.util.ClasspathResource;
import org.apache.tapestry.IPage;
import org.apache.tapestry.Tapestry;
@@ -35,6 +35,7 @@
import org.apache.tapestry.spec.IComponentSpecification;
import org.apache.tapestry.spec.ILibrarySpecification;
import org.apache.tapestry.util.IPropertyHolder;
+import org.apache.tapestry.util.RegexpMatcher;
/**
* Base class for Tapestry test cases.
@@ -42,16 +43,17 @@
* @author Howard Lewis Ship
* @version $Id$
* @since 2.2
- *
- **/
+ */
-public class TapestryTestCase extends TestCase
+public class TapestryTestCase extends HiveMindTestCase
{
protected static final boolean IS_JDK13 =
System.getProperty("java.specification.version").equals("1.3");
private ClassResolver _resolver = new DefaultClassResolver();
+ private static RegexpMatcher _matcher;
+
protected IPage createPage(String specificationPath, Locale locale)
{
Resource specResource = new ClasspathResource(_resolver, specificationPath);
@@ -103,8 +105,7 @@
{
String adjustedClassName = "/" + getClass().getName().replace('.', '/') + ".class";
- Resource classResource =
- new ClasspathResource(_resolver, adjustedClassName);
+ Resource classResource = new ClasspathResource(_resolver, adjustedClassName);
return classResource.getRelativeResource(simpleName);
}
@@ -118,12 +119,12 @@
return parser.parseLibrarySpecification(location);
}
- protected void checkList(String propertyName, Object[] expected, Object[] actual)
+ public static void checkList(String propertyName, Object[] expected, Object[] actual)
{
checkList(propertyName, expected, Arrays.asList(actual));
}
- protected void checkList(String propertyName, Object[] expected, List actual)
+ public static void checkList(String propertyName, Object[] expected, List actual)
{
int count = Tapestry.size(actual);
@@ -135,12 +136,12 @@
}
}
- protected void checkProperty(IPropertyHolder h, String propertyName, String expectedValue)
+ public static void checkProperty(IPropertyHolder h, String propertyName, String expectedValue)
{
assertEquals("Property " + propertyName + ".", expectedValue, h.getProperty(propertyName));
}
- protected void checkException(Throwable ex, String string)
+ public static void checkException(Throwable ex, String string)
{
if (ex.getMessage().indexOf(string) >= 0)
return;
@@ -149,8 +150,26 @@
"Exception " + ex + " does not contain sub-string '" + string + "'.");
}
- protected void unreachable()
+ /**
+ * Tests to see if the provided value matches the regular expression.
+ *
+ * @throws AssertionFailedError if the input does not match
+ *
+ * @since 3.1
+ */
+ public static void assertRegexp(String regexpPattern, String value)
{
- throw new AssertionFailedError("This code should be unreachable.");
+ if (_matcher == null)
+ _matcher = new RegexpMatcher();
+
+ if (_matcher.matches(regexpPattern, value))
+ return;
+
+ throw new AssertionFailedError(
+ "Text \""
+ + value
+ + "\" does not contain regular expression \""
+ + regexpPattern
+ + "\".");
}
-}
+}
\ No newline at end of file
1.26 +11 -11 jakarta-tapestry/eclipse/Tapestry-Junit.launch
Index: Tapestry-Junit.launch
===================================================================
RCS file: /home/cvs/jakarta-tapestry/eclipse/Tapestry-Junit.launch,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- Tapestry-Junit.launch 3 May 2004 16:04:40 -0000 1.25
+++ Tapestry-Junit.launch 17 Jun 2004 18:53:42 -0000 1.26
@@ -2,17 +2,13 @@
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
- <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.apache.tapestry.junit.TapestrySuite"/>
+ <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/>
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
<listEntry value="<?xml version="1.0" encoding="UTF-8"?>
<runtimeClasspathEntry containerPath="JRE_LIB" path="2"
sourceAttachmentPath="JRE_SRC" sourceRootPath="" type="3"/>
"/>
<listEntry value="<?xml version="1.0" encoding="UTF-8"?>
-<runtimeClasspathEntry
- containerPath="org.eclipse.jdt.launching.JRE_CONTAINER" path="2" type="4"/>
-"/>
- <listEntry value="<?xml version="1.0" encoding="UTF-8"?>
<runtimeClasspathEntry internalArchive="/jakarta-tapestry/config"
path="3" type="2"/>
"/>
@@ -85,19 +81,23 @@
path="3" type="2"/>
"/>
<listEntry value="<?xml version="1.0" encoding="UTF-8"?>
-<runtimeClasspathEntry path="3" projectName="jakarta-hivemind" type="1"/>
-"/>
- <listEntry value="<?xml version="1.0" encoding="UTF-8"?>
<runtimeClasspathEntry
- containerPath="MAVEN_REPO/log4j/jars/log4j-1.2.7.jar" path="3" type="3"/>
+ internalArchive="/jakarta-tapestry/lib/ext/hivemind-1.0-beta-1-snapshot.jar"
+ path="3" type="2"/>
"/>
<listEntry value="<?xml version="1.0" encoding="UTF-8"?>
<runtimeClasspathEntry
- containerPath="MAVEN_REPO/werkz/jars/werkz-1.0-beta-10.jar" path="3" type="3"/>
+ internalArchive="/jakarta-tapestry/lib/ext/hivemind-lib-1.0-beta-1-snapshot.jar"
+ path="3" type="2"/>
"/>
<listEntry value="<?xml version="1.0" encoding="UTF-8"?>
<runtimeClasspathEntry containerPath="JYTHON_DIR/jython.jar" path="3" type="3"/>
"/>
+ <listEntry value="<?xml version="1.0" encoding="UTF-8"?>
+<runtimeClasspathEntry
+ internalArchive="/jakarta-tapestry/lib/runtime/log4j-1.2.6.jar"
+ path="3" type="2"/>
+"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="jakarta-tapestry"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dnet.sf.tapestry.enable-reset-service=true"/>
@@ -105,7 +105,7 @@
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
</listAttribute>
<stringAttribute key="org.eclipse.debug.ui.target_debug_perspective" value="perspective_none"/>
- <stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
+ <stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value="=jakarta-tapestry/junit/src"/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="jakarta-tapestry/junit"/>
<stringAttribute key="org.eclipse.debug.ui.target_run_perspective" value="perspective_none"/>
</launchConfiguration>
1.4 +0 -1 jakarta-tapestry/.project
Index: .project
===================================================================
RCS file: /home/cvs/jakarta-tapestry/.project,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- .project 30 Apr 2004 15:15:17 -0000 1.3
+++ .project 17 Jun 2004 18:53:42 -0000 1.4
@@ -3,7 +3,6 @@
<name>jakarta-tapestry</name>
<comment></comment>
<projects>
- <project>jakarta-hivemind</project>
</projects>
<buildSpec>
<buildCommand>
1.44 +2 -1 jakarta-tapestry/.classpath
Index: .classpath
===================================================================
RCS file: /home/cvs/jakarta-tapestry/.classpath,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- .classpath 3 May 2004 16:04:40 -0000 1.43
+++ .classpath 17 Jun 2004 18:53:42 -0000 1.44
@@ -23,6 +23,7 @@
<classpathentry kind="lib" path="lib/j2ee/geronimo-ejb.jar"/>
<classpathentry kind="lib" path="lib/j2ee/jsp-api.jar"/>
<classpathentry exported="true" kind="lib" path="lib/j2ee/servlet-api.jar"/>
- <classpathentry kind="src" path="/jakarta-hivemind"/>
+ <classpathentry kind="lib" path="lib/ext/hivemind-1.0-beta-1-snapshot.jar"/>
+ <classpathentry kind="lib" path="lib/ext/hivemind-lib-1.0-beta-1-snapshot.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1.42 +1 -2 jakarta-tapestry/framework/src/org/apache/tapestry/TapestryStrings.properties
Index: TapestryStrings.properties
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/TapestryStrings.properties,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -r1.41 -r1.42
--- TapestryStrings.properties 30 Mar 2004 18:45:25 -0000 1.41
+++ TapestryStrings.properties 17 Jun 2004 18:53:42 -0000 1.42
@@ -1,4 +1,3 @@
-# $Id$
# Copyright 2004 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -372,7 +371,7 @@
# org.apache.tapestry.script
-ScriptParser.unknown-public-id=Script uses unknown public indentifier {0}.
+ScriptParser.unknown-public-id=Script uses unknown public identifier {0}.
ScriptParser.invalid-key=''{0}'' is not a valid key. Symbol keys must be valid Java identifiers.
ScriptParser.unable-to-resolve-class=''{0}'' is not a resolvable class name.
1.38 +7 -28 jakarta-tapestry/junit/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/build.xml,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- build.xml 5 May 2004 02:41:20 -0000 1.37
+++ build.xml 17 Jun 2004 18:53:42 -0000 1.38
@@ -79,12 +79,13 @@
<macrodef name="run-junit" description="Executes the JUnit test suite with a configured classpath.">
<attribute name="classpathref"/>
<attribute name="log4j-configuration" default="log4j.properties"/>
+ <attribute name="fork" default="false"/>
<sequential>
<junit printsummary="no"
errorProperty="test.failed"
failureProperty="test.failed"
- fork="true">
+ fork="@{fork}">
<classpath refid="@{classpathref}"/>
<formatter type="xml"/>
<formatter type="brief" usefile="false"/>
@@ -123,36 +124,14 @@
<report format="frames" todir="${classes.dir}"/>
</junitreport>
</target>
-
- <target name="gui" depends="compile" description="Run the JUnit GUI.">
- <java fork="true" classname="junit.swingui.TestRunner">
- <classpath refid="junit.classpath"/>
- <arg value="org.apache.tapestry.junit.TapestrySuite"/>
- <sysproperty key="org.apache.tapestry.enable-reset-service" value="true"/>
- </java>
-
- </target>
-
<path id="clover.classpath">
<pathelement location="${clover-classes.dir}"/>
<pathelement location="${clover.dir}/lib/clover.jar"/>
<path refid="junit.classpath"/>
</path>
-
- <target name="clover" description="Run the entire Tapestry test suite and construct the Clover report.">
- <antcall target="run-clover">
- <param name="suite" value="org.apache.tapestry.junit.TapestrySuite"/>
- </antcall>
- </target>
-
- <target name="fast-clover" description="Run a subset of the Tapestry test suite (excluding mock unit tests), and construct the Clover report.">
- <antcall target="run-clover">
- <param name="suite" value="org.apache.tapestry.junit.BasicTestSuite"/>
- </antcall>
- </target>
-
- <target name="run-clover" depends="compile" description="Run tests and construct the Clover report.">
+
+ <target name="clover" depends="compile" description="Run tests and construct the Clover report.">
<mkdir dir="${clover-classes.dir}"/>
<mkdir dir="${clover-db.dir}"/>
@@ -171,7 +150,7 @@
</echo>
- <run-junit classpathref="clover.classpath"/>
+ <run-junit classpathref="clover.classpath" fork="true"/>
<echo>
@@ -179,7 +158,7 @@
</echo>
- <run-junit classpathref="clover.classpath" log4j-configuration="log4j-slow.properties"/>
+ <run-junit classpathref="clover.classpath" log4j-configuration="log4j-slow.properties" fork="true"/>
<antcall target="clover-report"/>
</target>
1.8 +36 -2 jakarta-tapestry/junit/src/org/apache/tapestry/junit/utils/TestRegexpMatcher.java
Index: TestRegexpMatcher.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/org/apache/tapestry/junit/utils/TestRegexpMatcher.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- TestRegexpMatcher.java 30 Apr 2004 15:17:57 -0000 1.7
+++ TestRegexpMatcher.java 17 Jun 2004 18:53:42 -0000 1.8
@@ -96,4 +96,38 @@
assertEquals(m.getEscapedPatternString("^\\d$"), "\\^\\\\d\\$");
}
-}
+
+ /** @since 3.1 */
+
+ public void testGetMatches()
+ {
+ RegexpMatcher m = new RegexpMatcher();
+
+ String[] matches = m.getMatches("\\d+", "57,232 89 147", 0);
+
+ assertListsEqual(new String[] { "57", "232", "89", "147" }, matches);
+ }
+
+ /** @since 3.1 */
+
+ public void testGetMatchesNoMatch()
+ {
+ RegexpMatcher m = new RegexpMatcher();
+
+ String[] matches = m.getMatches("A(B|C)", "aBCAaBA", 0);
+
+ assertEquals(0, matches.length);
+ }
+
+ /** @since 3.1 */
+
+ public void testGetMatchesSubgroup()
+ {
+ RegexpMatcher m = new RegexpMatcher();
+
+ String matches[] = m.getMatches("A(B|C|fred)", "AA AC AB Afred AA AC", 1);
+
+ assertListsEqual(new String[] { "C", "B", "fred", "C" }, matches);
+ }
+
+}
\ No newline at end of file
1.7 +36 -2 jakarta-tapestry/framework/src/org/apache/tapestry/util/RegexpMatcher.java
Index: RegexpMatcher.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/util/RegexpMatcher.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- RegexpMatcher.java 30 Apr 2004 15:16:31 -0000 1.6
+++ RegexpMatcher.java 17 Jun 2004 18:53:42 -0000 1.7
@@ -1,10 +1,10 @@
-// Copyright 2004 The Apache Software Foundation
+// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
-// http://www.apache.org/licenses/LICENSE-2.0
+// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,14 +14,18 @@
package org.apache.tapestry.util;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
+import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
@@ -118,6 +122,36 @@
}
return result;
+ }
+
+ /**
+ * Given an input string, finds all matches in an input string for the pattern.
+ *
+ * @param pattern the regexp pattern for matching
+ * @param input the string to search for matches within
+ * @param subgroup the group (sub-expression) within the pattern to return as a match
+ * @return array (possibly empty) of matching strings
+ *
+ */
+ public String[] getMatches(String pattern, String input, int subgroup)
+ {
+ Pattern compiledPattern = getCompiledPattern(pattern);
+
+ PatternMatcher matcher = getPatternMatcher();
+ PatternMatcherInput matcherInput = new PatternMatcherInput(input);
+
+ List matches = new ArrayList();
+
+ while (matcher.contains(matcherInput, compiledPattern))
+ {
+ MatchResult match = matcher.getMatch();
+
+ String matchedInput = match.group(subgroup);
+
+ matches.add(matchedInput);
+ }
+
+ return (String[]) matches.toArray(new String[matches.size()]);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org