antelder 2002/09/20 01:46:54
Modified: java/test/util WSIFTestRunner.java
java/src/org/apache/wsif/util WSIFUtils.java
Added: java/test/util WildcardTest.java
Log:
Add a new WSIFUtils method (and testcase) to compare strings allowing for a
wildcard.
This will be used when adding proxy support to the apache soap provider for the
http.nonProxyHosts system property.
Revision Changes Path
1.15 +1 -0 xml-axis-wsif/java/test/util/WSIFTestRunner.java
Index: WSIFTestRunner.java
===================================================================
RCS file: /home/cvs/xml-axis-wsif/java/test/util/WSIFTestRunner.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- WSIFTestRunner.java 7 Aug 2002 12:04:43 -0000 1.14
+++ WSIFTestRunner.java 20 Sep 2002 08:46:53 -0000 1.15
@@ -124,6 +124,7 @@
suite.addTest(new TestSuite(PlugableProvidersTest.class));
suite.addTest(new TestSuite(InputPartsTest.class));
suite.addTest(new TestSuite(OutputPartsTest.class));
+ suite.addTest(new TestSuite(WildcardTest.class));
if (TestUtilities.areWeTesting("jms"))
suite.addTest(new TestSuite(JmsTest.class));
1.1 xml-axis-wsif/java/test/util/WildcardTest.java
Index: WildcardTest.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2002 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "WSIF" 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 [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* 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 and was
* originally based on software copyright (c) 2001, 2002, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package util;
import java.util.StringTokenizer;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.wsif.util.WSIFUtils;
/**
* Junit test for the WSIFUtils wildcardCompare method.
*
* @author Ant Elder <[EMAIL PROTECTED]>
*/
public class WildcardTest extends TestCase {
public WildcardTest(String name) {
super(name);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(WildcardTest.class);
}
public void testWildcardCompare() throws Throwable {
assertTrue( "t1", cmp( "foo.com", "foo.com" ) );
assertTrue( "t2", !cmp( "xxx.com", "foo.com" ) );
assertTrue( "t3", !cmp( "foo.com", "xxx.com" ) );
assertTrue( "t4", cmp( "*.com", "foo.com" ) );
assertTrue( "t5", cmp( "foo.*", "foo.com" ) );
assertTrue( "t6", !cmp( "*.com", "foo.cxm" ) );
assertTrue( "t7", !cmp( "foo.*", "fxo.com" ) );
assertTrue( "t8", cmp( "hur.*.com", "hur.ibm.com" ) );
assertTrue( "t9", !cmp( "hur.*.com", "hxr.ibm.com" ) );
assertTrue( "t10", !cmp( "hur.*.com", "hur.ibm.cxm" ) );
assertTrue( "t11", cmp( "hur*y.*.com", "hursley.ibm.com" ) );
assertTrue( "t12", !cmp( "hur*y.*.com", "xursley.ibm.com" ) );
assertTrue( "t13", !cmp( "hur*y.*.com", "hurslex.ibm.com" ) );
assertTrue( "t14", !cmp( "hur*y.*.com", "hursley.ibm.cxm" ) );
assertTrue( "t15", !cmp( "foo.com", "" ) );
assertTrue( "t16", !cmp( "", "foo.com" ) );
assertTrue( "t17", cmp( "*", "foo.com" ) );
assertTrue( "t18", !cmp( "hjkj", null ) );
assertTrue( "t19", !cmp( "*", null ) );
assertTrue( "t20", !cmp( null, "foo.com" ) );
assertTrue( "t21", !cmp( null, null ) );
}
private boolean cmp(String s1, String s2) {
return WSIFUtils.wildcardCompare( s1, s2, '*' );
}
}
1.15 +45 -1 xml-axis-wsif/java/src/org/apache/wsif/util/WSIFUtils.java
Index: WSIFUtils.java
===================================================================
RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/util/WSIFUtils.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- WSIFUtils.java 18 Sep 2002 15:38:19 -0000 1.14
+++ WSIFUtils.java 20 Sep 2002 08:46:54 -0000 1.15
@@ -1223,5 +1223,49 @@
// continue goto package synchronized
// null true false assert
}
-
+
+ /**
+ * Compares two strings taking acount of a wildcard.
+ * The first string is compared to the second string taking
+ * account of a wildcard character in the first string. For
+ * example, wildcardCompare( "*.ibm.com", "hursley.ibm.com", '*')
+ * would return true.
+ */
+ public static boolean wildcardCompare(String s1, String s2, char wild) {
+ if ( s1 == null ) {
+ return false;
+ }
+ String w = wild + "";
+ return cmp( new StringTokenizer( s1, w, true ), s2, w );
+ }
+
+ private static boolean cmp(StringTokenizer st, String s, String wild) {
+ if ( s == null || s.equals( "" ) ) {
+ return !st.hasMoreTokens();
+ }
+ if ( st.hasMoreTokens() ) {
+ String s2 = st.nextToken();
+ if ( wild.equals( s2 ) ) {
+ if ( !st.hasMoreTokens() ) {
+ return true; // a trailing wildcard matches anything
+ }
+ s2 = st.nextToken();
+ if ( s.equals( s2 ) ) {
+ return false; // wildcard must be at least 1 character
+ }
+ }
+ int i = s.indexOf( s2 );
+ if ( i < 0 ) {
+ return false; // prefix not in s
+ }
+ i += s2.length();
+ if ( i < s.length() ) {
+ return cmp( st, s.substring( i ), wild );
+ } else {
+ return cmp( st, "", wild );
+ }
+ }
+ return false; // no more tokens but still some s
+ }
+
}