001: /*
002: * $Id: TestMatchesFunction.java,v 1.1 2005/06/18 01:03:44 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functions;
042:
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: import org.axiondb.AxionException;
050: import org.axiondb.ColumnIdentifier;
051: import org.axiondb.Function;
052: import org.axiondb.RowDecorator;
053: import org.axiondb.Selectable;
054: import org.axiondb.engine.rows.SimpleRow;
055:
056: /**
057: * @version $Revision: 1.1 $ $Date: 2005/06/18 01:03:44 $
058: * @author Jonathan Giron
059: */
060: public class TestMatchesFunction extends BaseFunctionTest {
061:
062: private static final String REGEX_ZIP_PLUS_4 = "^[0-9]{5}([ \\-]?[0-9]{4})?$";
063: private static final String REGEX_US_SSN = "^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$";
064:
065: //------------------------------------------------------------ Conventional
066: public TestMatchesFunction(String testName) {
067: super (testName);
068: }
069:
070: public static Test suite() {
071: TestSuite suite = new TestSuite(TestMatchesFunction.class);
072: return suite;
073: }
074:
075: public static void main(String args[]) {
076: String[] testCaseName = { TestMatchesFunction.class.getName() };
077: junit.textui.TestRunner.main(testCaseName);
078: }
079:
080: //--------------------------------------------------------------- Lifecycle
081:
082: public void setUp() throws Exception {
083: super .setUp();
084: }
085:
086: public void tearDown() throws Exception {
087: super .tearDown();
088: }
089:
090: //--------------------------------------------------------------- Framework
091:
092: protected ConcreteFunction makeFunction() {
093: return new MatchesFunction();
094: }
095:
096: //------------------------------------------------------------------- Tests
097: public void testEval() throws Exception {
098: Function f = makeFunction();
099: doTestEvaluate(f, "90041-2417", REGEX_ZIP_PLUS_4, Boolean.TRUE);
100: doTestEvaluate(f, "90041 2417", REGEX_ZIP_PLUS_4, Boolean.TRUE);
101:
102: doTestEvaluate(f, "94112*1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
103: doTestEvaluate(f, "94112_1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
104: doTestEvaluate(f, "94112=1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
105: doTestEvaluate(f, "94112+1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
106: doTestEvaluate(f, "94112#1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
107: doTestEvaluate(f, "94112/1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
108: doTestEvaluate(f, "94112\\1234", REGEX_ZIP_PLUS_4,
109: Boolean.FALSE);
110: doTestEvaluate(f, "94112[1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
111: doTestEvaluate(f, "94112]1234", REGEX_ZIP_PLUS_4, Boolean.FALSE);
112: doTestEvaluate(f, "V5P 1B2", REGEX_ZIP_PLUS_4, Boolean.FALSE);
113:
114: doTestEvaluate(f, "123-45-6789", REGEX_US_SSN, Boolean.TRUE);
115: doTestEvaluate(f, "123456789", REGEX_US_SSN, Boolean.TRUE);
116:
117: doTestEvaluate(f, "123-cc-41bc", REGEX_US_SSN, Boolean.FALSE);
118: doTestEvaluate(f, "123_45_6789", REGEX_US_SSN, Boolean.FALSE);
119: doTestEvaluate(f, "123+45+6789", REGEX_US_SSN, Boolean.FALSE);
120: doTestEvaluate(f, "123=45=6789", REGEX_US_SSN, Boolean.FALSE);
121: doTestEvaluate(f, "12-3456789", REGEX_US_SSN, Boolean.FALSE);
122: doTestEvaluate(f, "12-abcdefg", REGEX_US_SSN, Boolean.FALSE);
123: doTestEvaluate(f, "123=cc=41bc", REGEX_US_SSN, Boolean.FALSE);
124:
125: doTestEvaluate(f, "A123", null, null);
126: doTestEvaluate(f, null, ".*", null);
127: doTestEvaluate(f, null, null, null);
128: }
129:
130: public void testNegative() throws Exception {
131: Function f = makeFunction();
132:
133: try {
134: doTestEvaluate(f, "A123", "A{2", null);
135: fail("Expected AxionException(2201B)");
136: } catch (AxionException e) {
137: if (!"2201B".equals(e.getSQLState())) {
138: fail("Expected AxionException(2201B)");
139: }
140: }
141: }
142:
143: private void doTestEvaluate(Function f, String source,
144: String regexp, Boolean shouldMatch) throws AxionException {
145: Selectable sel1 = new ColumnIdentifier("arg1");
146: Selectable sel2 = new ColumnIdentifier("arg2");
147: f.addArgument(sel1);
148: f.addArgument(sel2);
149: Map map = new HashMap();
150: map.put(sel1, new Integer(0));
151: map.put(sel2, new Integer(1));
152: RowDecorator dec = new RowDecorator(map);
153:
154: dec.setRow(new SimpleRow(new Object[] { source, regexp }));
155: assertEquals("Matches function failed to evaluate to "
156: + shouldMatch + " for " + source + " using regexp '"
157: + regexp + "'", shouldMatch, f.evaluate(dec));
158: }
159: }
|