/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
//
/**
* @author Prashant Baliga <prabalig@in.ibm.com>
*
*/
package org.apache.imperius.spl.tests;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.imperius.spl.core.Expression;
import org.apache.imperius.spl.core.TypeConstants;
import org.apache.imperius.spl.parser.compiler.symboltable.SPLSymbolTable;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.expression.primary.BasicCollectionExpression;
import org.apache.imperius.spl.parser.expressions.impl.BooleanConstant;
import org.apache.imperius.spl.parser.expressions.impl.CharConstant;
import org.apache.imperius.spl.parser.expressions.impl.ContainsOnlyDigits;
import org.apache.imperius.spl.parser.expressions.impl.ContainsOnlyLetters;
import org.apache.imperius.spl.parser.expressions.impl.ContainsOnlyLettersOrDigits;
import org.apache.imperius.spl.parser.expressions.impl.DateTimeConstant;
import org.apache.imperius.spl.parser.expressions.impl.DoubleConstant;
import org.apache.imperius.spl.parser.expressions.impl.FloatConstant;
import org.apache.imperius.spl.parser.expressions.impl.IntegerConstant;
import org.apache.imperius.spl.parser.expressions.impl.LongConstant;
import org.apache.imperius.spl.parser.expressions.impl.MatchesRegExp;
import org.apache.imperius.spl.parser.expressions.impl.ShortConstant;
import org.apache.imperius.spl.parser.expressions.impl.StringConstant;
import org.apache.imperius.spl.parser.expressions.impl.Substring;
public class NewExpressionTest extends TestCase {
static final String CMVC_ID_NewExpressionTest = "$Header: /cvsroot/cimsplpegasus/trunk/modules/imperius-splcore/src/com/ibm/ac/spl/tests/NewExpressionTest.java,v 1.2 2007/12/20 13:07:37 prabalig Exp $";
static final String VERSION_NewExpressionTest = "File version $Revision: 1.2 $, last changed $Date: 2007/12/20 13:07:37 $";
// private static Logger logger = SPLLogger.getCIMSPLLogger().getLogger();
static Expression propertySensor1 =null;
//new ConstantExpression("propertySensor1");
static Expression propertySensor2 =null;
//new PropertySensor("propertySensor2");
static Expression propertySensor3 =null;
//new PropertySensor("propertySensor3");
static Expression propertySensor4 =null;
//new PropertySensor("propertySensor4");
static Expression propertySensor5 =null;
//new PropertySensor("propertySensor5");
public NewExpressionTest(String arg0) {
super(arg0);
}
public static Test suite() {
TestSuite suite = new TestSuite(NewExpressionTest.class);
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() throws Exception {
oneTimeSetUp();
}
protected void tearDown() throws Exception {
oneTimeTearDown();
}
};
return wrapper;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
protected static void oneTimeSetUp() throws Exception {
}
protected static void oneTimeTearDown() throws Exception {
}
/*
* This will test ContainsOnlyDigits Expression
*/
public void testContainsOnlyDigits() {
String s1 = "\"containsNoDigits\"";
String s2 = "\"30 Sawmill River Rd, Hawthorne, NY 10532\"";
String s3 = "\"18007778888\"";
String s4 = "\"\"";
String s5 = "\"@#$%^&*(){}|\"";
//HashMap map = new HashMap();
propertySensor1=new StringConstant(s1);
propertySensor2=new StringConstant(s2);
propertySensor3=new StringConstant(s3);
propertySensor4=new StringConstant(s4);
propertySensor5=new StringConstant(s5);
//SensorLookup lookup = new SensorLookupImpl(map);
try {
Expression exp;
Vector v = new Vector();
v.add(new StringConstant(s1));
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor1);
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(new StringConstant(s2));
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor2);
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(new StringConstant(s3));
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(propertySensor3);
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s4));
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(propertySensor4);
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s5));
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor5);
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
/**
* This will test ContainsOnlyLetters Expression
*/
public void testContainsOnlyLetters() {
String s1 = "\"containsNoDigits\"";
String s2 = "\"30 Sawmill River Rd, Hawthorne, NY 10532\"";
String s3 = "\"1 800 777 8888\"";
String s4 = "\"\"";
String s5 = "\"@#$%^&*(){}|\"";
//HashMap map = new HashMap();
propertySensor1=new StringConstant(s1);
propertySensor2=new StringConstant(s2);
propertySensor3=new StringConstant(s3);
propertySensor4=new StringConstant(s4);
propertySensor5=new StringConstant(s5);
//SensorLookup lookup = new SensorLookupImpl(map);
try {
Expression exp;
Vector v = new Vector();
v.add(new StringConstant(s1));
exp = new ContainsOnlyLetters(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(propertySensor1);
exp = new ContainsOnlyLetters(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s2));
exp = new ContainsOnlyLetters(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor2);
exp = new ContainsOnlyLetters(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(new StringConstant(s3));
exp = new ContainsOnlyLetters(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor3);
exp = new ContainsOnlyLetters(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(new StringConstant(s4));
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(propertySensor4);
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s5));
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor5);
exp = new ContainsOnlyDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
/**
* This will test ContainsOnlyLettersOrDigits Expression
*/
public void testContainsOnlyLettersOrDigits() {
String s1 = "\"containsDigitsAndLetters12345\"";
String s2 = "\"30 Sawmill River Rd, Hawthorne, NY 10532\"";
String s3 = "\"TollFree18007778888\"";
String s4 = "\"\"";
String s5 = "\"@#$%^&*(){}|\"";
//HashMap map = new HashMap();
propertySensor1=new StringConstant(s1);
propertySensor2=new StringConstant(s2);
propertySensor3=new StringConstant(s3);
propertySensor4=new StringConstant(s4);
propertySensor5=new StringConstant(s5);
//SensorLookup lookup = new SensorLookupImpl(map);
try {
Expression exp;
Vector v = new Vector();
v.add(new StringConstant(s1));
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(propertySensor1);
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s2));
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor2);
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(new StringConstant(s3));
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(propertySensor3);
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s4));
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(propertySensor4);
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s5));
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
v.clear();
v.add(propertySensor5);
exp = new ContainsOnlyLettersOrDigits(v,true);
assertTrue(exp.evaluate().equals(Boolean.FALSE));
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
/**
* This will test Like Expression
*/
public void testMatchesRegExp() {
String s1 = "\"aaaaab\"";
String s2 = "\"a*b\"";
//HashMap map = new HashMap();
propertySensor1=new StringConstant(s1);
propertySensor2=new StringConstant(s2);
//SensorLookup lookup = new SensorLookupImpl(map);
try {
Expression exp;
Vector v = new Vector();
v.add(new StringConstant(s1));
v.add(new StringConstant(s2));
exp = new MatchesRegExp(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
v.clear();
v.add(new StringConstant(s1));
v.add(new StringConstant(s2));
exp = new MatchesRegExp(v,true);
assertTrue(exp.evaluate().equals(Boolean.TRUE));
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
/**
* This will test Substring Expression
*/
public void testSubstring() {
String s1 = "\"containsDigitsAndLetters12345\"";
//HashMap map = new HashMap();
propertySensor1=new StringConstant(s1);
//SensorLookup lookup = new SensorLookupImpl(map);
try {
Expression exp;
Vector v = new Vector();
v.add(new StringConstant(s1));
v.add(new IntegerConstant(8));
exp = new Substring(v,true);
assertTrue(exp.evaluate().equals("DigitsAndLetters12345"));
v.clear();
v.add(propertySensor1);
v.add(new IntegerConstant(8));
exp = new Substring(v,true);
assertTrue(exp.evaluate().equals("DigitsAndLetters12345"));
v.clear();
v.add(new StringConstant(s1));
v.add(new IntegerConstant(8));
v.add(new IntegerConstant(24));
exp = new Substring(v,true);
assertTrue(exp.evaluate().equals("DigitsAndLetters"));
v.clear();
v.add(propertySensor1);
v.add(new IntegerConstant(8));
v.add(new IntegerConstant(24));
exp = new Substring(v,true);
assertTrue(exp.evaluate().equals("DigitsAndLetters"));
} catch (SPLException e) {
e.printStackTrace();
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail("Received expression exception " + e.getMessage());
}
}
public void testStringConstant() {
String s1 = "\"Test String\"";
String s2 = "Test String";
//HashMap map = new HashMap();
propertySensor1=new StringConstant(s1);
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.stringType);
assertFalse(propertySensor1.isArray());
assertTrue(((String)propertySensor1.evaluate()).equals(s2));
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testIntegerConstant() {
Integer s1 = new Integer(1);
//HashMap map = new HashMap();
propertySensor1=new IntegerConstant(s1.intValue());
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.intType);
assertFalse(propertySensor1.isArray());
assertTrue(((Integer)propertySensor1.evaluate()).intValue()==s1.intValue());
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testFloatConstant() {
Float s1 = new Float(1.23);
//HashMap map = new HashMap();
propertySensor1=new FloatConstant(s1.floatValue());
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.floatType);
assertFalse(propertySensor1.isArray());
assertTrue(((Float)propertySensor1.evaluate()).floatValue()==s1.floatValue());
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testLongConstant() {
Long s1 = new Long(65000);
//HashMap map = new HashMap();
propertySensor1=new LongConstant(s1.longValue());
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.longType);
assertFalse(propertySensor1.isArray());
assertTrue(((Long)propertySensor1.evaluate()).longValue()==s1.longValue());
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testDoubleConstant() {
Double s1 = new Double(65000.65);
//HashMap map = new HashMap();
propertySensor1=new DoubleConstant(s1.doubleValue());
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.doubleType);
assertFalse(propertySensor1.isArray());
assertTrue(((Double)propertySensor1.evaluate()).doubleValue()==s1.doubleValue());
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testBooleanConstant() {
Boolean s1 = new Boolean(true);
//HashMap map = new HashMap();
propertySensor1=new BooleanConstant(s1.booleanValue());
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.booleanType);
assertFalse(propertySensor1.isArray());
assertTrue(((Boolean)propertySensor1.evaluate()).booleanValue()==s1.booleanValue());
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testCharConstant() {
Character s1 = new Character('a');
//HashMap map = new HashMap();
propertySensor1=new CharConstant(s1.charValue());
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.charType);
assertFalse(propertySensor1.isArray());
assertTrue(((Character)propertySensor1.evaluate()).equals(s1));
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testShortConstant() {
Short s1 = new Short((short)23);
//HashMap map = new HashMap();
propertySensor1=new ShortConstant(s1.shortValue());
//SensorLookup lookup = new SensorLookupImpl(map);
try {
assertTrue(propertySensor1.getType().getType()== TypeConstants.shortType);
assertFalse(propertySensor1.isArray());
assertTrue(((Short)propertySensor1.evaluate()).shortValue()==s1.shortValue());
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testDateTimeConstant() {
Calendar c1=Calendar.getInstance();
//HashMap map = new HashMap();
//SensorLookup lookup = new SensorLookupImpl(map);
try {
propertySensor1=new DateTimeConstant(c1);
assertTrue(propertySensor1.getType().getType()== TypeConstants.dateTime);
assertFalse(propertySensor1.isArray());
assertTrue(((Calendar)propertySensor1.evaluate())==c1);
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
public void testBasicCollectionExpression() {
int c1=10;
//HashMap map = new HashMap();
propertySensor1=new IntegerConstant(c1);
propertySensor2=new IntegerConstant(c1);
propertySensor3=new IntegerConstant(c1);
propertySensor4=new IntegerConstant(c1);
propertySensor5=new IntegerConstant(c1);
//SensorLookup lookup = new SensorLookupImpl(map);
try {
SPLSymbolTable t =new SPLSymbolTable();
Vector v = new Vector();
v.add(propertySensor1);
v.add(propertySensor2);
v.add(propertySensor3);
v.add(propertySensor4);
v.add(propertySensor5);
Expression e1=new BasicCollectionExpression(v,t);
assertTrue(e1.getType().getType()== TypeConstants.intType);
assertTrue(e1.isArray());
List resultList=(List)e1.evaluate();
for(int i=0;i<resultList.size();i++)
{
assertTrue(((Integer)resultList.get(i)).intValue()==c1);
}
} catch (SPLException e) {
fail("Received evaluation exception " + e.getMessage());
} catch (Exception e) {
fail("Received expression exception " + e.getMessage());
}
}
}