Package org.apache.imperius.spl.tests

Source Code of org.apache.imperius.spl.tests.StringExpressionTest

/*
* 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.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.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.expressions.impl.BooleanConstant;
import org.apache.imperius.spl.parser.expressions.impl.Concatenate;
import org.apache.imperius.spl.parser.expressions.impl.Contains;
import org.apache.imperius.spl.parser.expressions.impl.EndsWith;
import org.apache.imperius.spl.parser.expressions.impl.IntegerConstant;
import org.apache.imperius.spl.parser.expressions.impl.LeftSubstring;
import org.apache.imperius.spl.parser.expressions.impl.MiddleSubstring;
import org.apache.imperius.spl.parser.expressions.impl.ReplaceSubstring;
import org.apache.imperius.spl.parser.expressions.impl.RightSubstring;
import org.apache.imperius.spl.parser.expressions.impl.StartsWith;
import org.apache.imperius.spl.parser.expressions.impl.StringConstant;
import org.apache.imperius.spl.parser.expressions.impl.StringLength;
import org.apache.imperius.spl.parser.expressions.impl.ToLower;
import org.apache.imperius.spl.parser.expressions.impl.ToString;
import org.apache.imperius.spl.parser.expressions.impl.ToUpper;
import org.apache.imperius.spl.parser.expressions.impl.Word;






public class StringExpressionTest extends TestCase {

  static final String CMVC_ID_StringExpressionTest = "$Header: /cvsroot/cimsplpegasus/trunk/modules/imperius-splcore/src/com/ibm/ac/spl/tests/StringExpressionTest.java,v 1.2 2007/12/20 13:07:37 prabalig Exp $";
  static final String VERSION_StringExpressionTest = "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");
  static  Expression propertySensor6 =null;
  //new PropertySensor("propertySensor6");
  static  Expression propertySensor7 =null;
  //new PropertySensor("propertySensor7");
  static  Expression propertySensor8 =null;
  //new PropertySensor("propertySensor8");
  static  Expression propertySensor9 =null;
  //new PropertySensor("propertySensor9");
  static  Expression propertySensor10 =null;
  //new PropertySensor("propertySensor10");

  public StringExpressionTest(String arg0) {
    super(arg0);
  }

  public static Test suite() {
    TestSuite suite = new TestSuite(StringExpressionTest.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 MiddleSubstring Expression
   */
  public void testMiddleSubstring() {
    String string = "\"AutonomicComputing\"";
    StringConstant lr = new StringConstant("\"LeftToRight\"");
    StringConstant rl = new StringConstant("\"RightToLeft\"");
    IntegerConstant ic4 = new IntegerConstant(4);
    IntegerConstant ic5 = new IntegerConstant(5);
    //HashMap map = new HashMap();
    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      Expression exp;
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(ic5);
      v.add(lr);
      Expression esnnl = new MiddleSubstring(v,true);

      v.clear();
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(ic5);
      v.add(rl);
      Expression esnnr = new MiddleSubstring(v,true);

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(ic5);
      v.add(lr);
      Expression essnl = new MiddleSubstring(v,true);

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(ic5);
      v.add(rl);
      Expression essnr = new MiddleSubstring(v,true);

      v.clear();
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      Expression esnsl = new MiddleSubstring(v,true);

      v.clear();
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      Expression esnsr = new MiddleSubstring(v,true);

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"on\""));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      Expression esssl = new MiddleSubstring(v,true);

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"on\""));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      Expression esssr = new MiddleSubstring(v,true);

      assertTrue(((String) esnnl.evaluate()).equals("nomic"));
      assertTrue(((String) esnnr.evaluate()).equals("ing"));
      assertTrue(((String) essnl.evaluate()).equals("icCom"));
      assertTrue(((String) essnr.evaluate()).equals("putin"));
      assertTrue(((String) esnsl.evaluate()).equals("n"));
      assertTrue(((String) esnsr.evaluate()).equals("pu"));
      assertTrue(((String) esssl.evaluate()).equals(""));
      assertTrue(((String) esssr.evaluate()).equals("Aut"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(4));
      v.add(new IntegerConstant(5));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("nomic"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(4));
      v.add(new IntegerConstant(5));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("ing"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(4));
      v.add(new IntegerConstant(-1));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("o"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(8));
      v.add(new IntegerConstant(-1));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("o"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new IntegerConstant(5));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("g"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new IntegerConstant(5));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("tonom"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new IntegerConstant(-1));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("n"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new IntegerConstant(-1));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("u"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(19));
      v.add(new IntegerConstant(5));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(19));
      v.add(new IntegerConstant(5));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("Auton"));
      //////////////////////////////////////////////////////
      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new IntegerConstant(5));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("icCom"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new IntegerConstant(5));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("putin"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new IntegerConstant(18));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("icComputing"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new IntegerConstant(18));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("puting"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new IntegerConstant(-1));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("n"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new IntegerConstant(-1));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("C"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new IntegerConstant(5));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new IntegerConstant(5));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new IntegerConstant(-1));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new IntegerConstant(-1));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new IntegerConstant(20));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new IntegerConstant(20));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));
      ///////////////////////////////////////////////

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(4));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("n"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(4));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("pu"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(7));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("icC"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(7));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("g"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new StringConstant("\"bz\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("g"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(-1));
      v.add(new StringConstant("\"bz\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(4));
      v.add(new StringConstant("\"bz\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(
          ((String) exp.evaluate()).equals("nomicComputing"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(4));
      v.add(new StringConstant("\"bz\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(20));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(20));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(20));
      v.add(new StringConstant("\"bz\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new IntegerConstant(20));
      v.add(new StringConstant("\"bz\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));
      /////////////////////////////////////
      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("icC"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("icC"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new StringConstant("\"bz\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("icComputing"));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new StringConstant("\"bz\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals("AutonomicC"));


      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new StringConstant("\"bz\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"bz\""));
      v.add(new StringConstant("\"bz\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new StringConstant("\"icC\""));
      v.add(lr);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(new StringConstant("\"icC\""));
      v.add(rl);
      exp = new MiddleSubstring(v,true);
      assertTrue(((String) exp.evaluate()).equals(""));


    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test RightSubstring Expression
   */
  public void testRightSubstring() {
    String string = "\"AutonomicComputing\"";
    StringConstant lr = new StringConstant("\"LeftToRight\"");
    StringConstant rl = new StringConstant("\"RightToLeft\"");
    IntegerConstant icmin1 = new IntegerConstant(-1);

    IntegerConstant ic4 = new IntegerConstant(4);
//    IntegerConstant ic5 = new IntegerConstant(5);
    //HashMap map = new HashMap();
    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(lr);
      Expression esnl = new RightSubstring(v,true);
      // Examples: RightSubstring("AutonomicComputing", 4, LeftToRight) = "ting".

      v.clear();
      v.add(new StringConstant(string));
      v.add(icmin1);
      v.add(lr);
      Expression esnlmin1 = new RightSubstring(v,true);
      // Examples: RightSubstring("AutonomicComputing", -1, LeftToRight) = "AutonomicComputing".

      v.clear();
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(rl);
      Expression esnr = new RightSubstring(v,true);
      // RightSubstring("AutonomicComputing", 4, RightToLeft) = "nomicComputing".<p>

      v.clear();
      v.add(new StringConstant(string));
      v.add(icmin1);
      v.add(rl);
      Expression esnrmin1 = new RightSubstring(v,true);
      // Examples: RightSubstring("AutonomicComputing", -1, RightToLeft) = "".

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      Expression essl = new RightSubstring(v,true);
      // Examples: RightSubstring("AutonomicComputing", "om", LeftToRight)="icComputing"

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"jj\""));
      v.add(lr);
      Expression esslne = new RightSubstring(v,true);
      // Examples: RightSubstring("AutonomicComputing", "jj", LeftToRight)=""

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      Expression essr = new RightSubstring(v,true);
      // RightSubstring("AutonomicComputing", "om", RightToLeft)="puting".

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"jj\""));
      v.add(rl);
      Expression essrne = new RightSubstring(v,true);
      // RightSubstring("AutonomicComputing", "jj", RightToLeft)="".

      assertTrue(((String) esnl.evaluate()).equals("ting"));
      assertTrue(
          ((String) esnlmin1.evaluate()).equals(
          "AutonomicComputing"));
      assertTrue(
          ((String) esnr.evaluate()).equals("nomicComputing"));
      assertTrue(((String) esnrmin1.evaluate()).equals(""));
      assertTrue(((String) essl.evaluate()).equals("icComputing"));
      assertTrue(((String) esslne.evaluate()).equals(""));
      assertTrue(((String) essr.evaluate()).equals("puting"));
      assertTrue(((String) essrne.evaluate()).equals(""));
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test LeftSubstring Expression
   */
  public void testLeftSubstring() {
    String string = "\"AutonomicComputing\"";
    StringConstant lr = new StringConstant("\"LeftToRight\"");
    StringConstant rl = new StringConstant("\"RightToLeft\"");
    IntegerConstant ic4 = new IntegerConstant(4);
//    IntegerConstant ic5 = new IntegerConstant(5);
    //HashMap map = new HashMap();
    //SensorLookup lookup = new SensorLookupImpl(map);
    IntegerConstant icmin1 = new IntegerConstant(-1);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(lr);
      Expression esnl = new LeftSubstring(v,true);
      // Examples: LeftSubstring("AutonomicComputing", 4, LeftToRight) = "Auto".

      v.clear();
      v.add(new StringConstant(string));
      v.add(icmin1);
      v.add(lr);
      Expression esnlmin1 = new LeftSubstring(v,true);
      // Examples: LeftSubstring("AutonomicComputing", -1, LeftToRight) = "AutonomicComputing".

      v.clear();
      v.add(new StringConstant(string));
      v.add(ic4);
      v.add(rl);
      Expression esnr = new LeftSubstring(v,true);
      // LeftSubstring("AutonomicComputing", 4, RightToLeft) = "AutonomicCompu".

      v.clear();
      v.add(new StringConstant(string));
      v.add(icmin1);
      v.add(rl);
      Expression esnrmin1 = new LeftSubstring(v,true);
      // Examples: LeftSubstring("AutonomicComputing", -1, RightToLeft) = "".

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(lr);
      Expression essl = new LeftSubstring(v,true);
      // Examples: LeftSubstring("AutonomicComputing", "om", LeftToRight)="Auton"

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"\""));
      v.add(lr);
      Expression esslempty = new LeftSubstring(v,true);
      // Examples: LeftSubstring("AutonomicComputing", "", LeftToRight)=""

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"om\""));
      v.add(rl);
      Expression essr = new LeftSubstring(v,true);
      // LeftSubstring("AutonomicComputing", "om", RightToLeft)="AutonomicC"

      v.clear();
      v.add(new StringConstant(string));
      v.add(new StringConstant("\"\""));
      v.add(lr);
      Expression essrempty = new LeftSubstring(v,true);
      // Examples: LeftSubstring("AutonomicComputing", "", RightToLeft)=""

      assertTrue(((String) esnl.evaluate()).equals("Auto"));
      assertTrue(
          ((String) esnlmin1.evaluate()).equals(
          "AutonomicComputing"));
      assertTrue(
          ((String) esnr.evaluate()).equals("AutonomicCompu"));
      assertTrue(((String) esnrmin1.evaluate()).equals(""));
      assertTrue(((String) essl.evaluate()).equals("Auton"));
      assertTrue(((String) esslempty.evaluate()).equals(""));
      assertTrue(((String) essr.evaluate()).equals("AutonomicC"));
      assertTrue(((String) essrempty.evaluate()).equals(""));
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test StringLength Expression
   */
  public void testStringLength() {
    String s1 = "\"AutonomicComputing\"";
    String s2 = "\"StringLength\"";
    String s3 = "\"PMAC1.2\"";

    //HashMap map = new HashMap();
    propertySensor1=new StringConstant(s1);
    propertySensor2=new StringConstant(s2);
    propertySensor3=new StringConstant(s3);

    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(s1));
      Expression e1 = new StringLength(v,true);

      v.clear();
      v.add(new StringConstant(s2));
      Expression e2 = new StringLength(v,true);

      v.clear();
      v.add(new StringConstant(s3));
      Expression e3 = new StringLength(v,true);

      v.clear();
      v.add(propertySensor1);
      Expression eps1 = new StringLength(v,true);

      v.clear();
      v.add(propertySensor2);
      Expression eps2 = new StringLength(v,true);

      v.clear();
      v.add(propertySensor3);
      Expression eps3 = new StringLength(v,true);
      s1 = s1.substring(1, s1.length()-1);
      s2 = s2.substring(1, s2.length()-1);
      s3 = s3.substring(1, s3.length()-1);

      assertTrue(
          new Integer(s1.length()).compareTo((Integer)e1.evaluate()) == 0);
      assertTrue(
          new Integer(s2.length()).compareTo((Integer)e2.evaluate()) == 0);
      assertTrue(
          new Integer(s3.length()).compareTo((Integer)e3.evaluate()) == 0);
      assertTrue(
          new Integer(s1.length()).compareTo((Integer)eps1.evaluate()) == 0);
      assertTrue(
          new Integer(s2.length()).compareTo((Integer)eps2.evaluate()) == 0);
      assertTrue(
          new Integer(s3.length()).compareTo((Integer)eps3.evaluate()) == 0);
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test ReplaceSubstring Expression
   */
  public void testReplaceSubstring() {
    String s1 = "\"ThisIsADogThisIsAPenThisIsATelevisionSetThisIsAHouse.\"";
    String s2 = "\"This\"";
    String s3 = "\"That\"";
    String s4 = "\"ThatIsADogThatIsAPenThatIsATelevisionSetThatIsAHouse.\"";

    //HashMap map = new HashMap();
    propertySensor1=new StringConstant(s1);
    propertySensor2=new StringConstant(s2);
    propertySensor3=new StringConstant(s3);

    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(s1));
      v.add(new StringConstant(s2));
      v.add(new StringConstant(s3));
      Expression e1 = new ReplaceSubstring(v,true);

      v.clear();
      v.add(propertySensor1);
      v.add(propertySensor2);
      v.add(propertySensor3);
      Expression eps1 = new ReplaceSubstring(v,true);
      s4 = s4.substring(1, s4.length()-1);
      assertTrue(s4.compareTo((String)e1.evaluate()) == 0);
      assertTrue(s4.compareTo((String)eps1.evaluate()) == 0);
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test StartsWith Expression
   */
  public void testStartsWith() {
    String s1 = "\"ThisIsADogThisIsAPenThisIsATelevisionSetThisIsAHouse.\"";
    String s2 = "\"This\"";

    //HashMap map = new HashMap();
    propertySensor1=new StringConstant(s1);
    propertySensor2=new StringConstant(s2);
    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(s1));
      v.add(new StringConstant(s2));
      Expression e1 = new StartsWith(v,true);

      v.clear();
      v.add(propertySensor1);
      v.add(propertySensor2);
      Expression eps1 = new StartsWith(v,true);

      assertTrue(((Boolean) (e1.evaluate())).booleanValue());
      assertTrue(((Boolean) (eps1.evaluate())).booleanValue());
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test Ends Expression
   */
  public void testEndsWith() {
    String s1 = "\"ThisIsADogThisIsAPenThisIsATelevisionSetThisIsAHouse.\"";
    String s2 = "\"ThisIsAHouse.\"";

    //HashMap map = new HashMap();
    propertySensor1=new StringConstant(s1);
    propertySensor2=new StringConstant(s2);
    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(s1));
      v.add(new StringConstant(s2));
      Expression e1 = new EndsWith(v,true);

      v.clear();
      v.add(propertySensor1);
      v.add(propertySensor2);
      Expression eps1 = new EndsWith(v,true);

      assertTrue(((Boolean) (e1.evaluate())).booleanValue());
      assertTrue(((Boolean) (eps1.evaluate())).booleanValue());
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test Concatenate Expression
   */
  public void testConcatenate() {
    String s1 = "\"ThisIsADog\"";
    String s2 = "\"ThisIsAPen\"";
    //String s3 = "ThisIsATelevisionSet";
    //String s4 = "ThisIsAHouse.";

    //HashMap map = new HashMap();
    propertySensor1=new StringConstant(s1);
    propertySensor2=new StringConstant(s2);
   
    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(s1));
      v.add(new StringConstant(s2));
     
      Expression e1 = new Concatenate(v,true);

      v.clear();
      v.add(propertySensor1);
      v.add(propertySensor2);
     
      Expression eps1 = new Concatenate(v,true);
      s1 = s1.substring(1, s1.length()-1);
      s2 = s2.substring(1, s2.length()-1);
      assertTrue(e1.evaluate().equals(s1 + s2));
      assertTrue(eps1.evaluate().equals(s1 + s2));
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test Word Expression
   */
  public void testWord() {
    String s1 = "\"ThisIsADogThisIsAPenThisIsATelevisionSetThisIsAHouse\"";
    String s2 = "\"This\"";
    int n = 3;

    //HashMap map = new HashMap();
    propertySensor1=new StringConstant(s1);
    propertySensor2=new StringConstant(s2);
    propertySensor3=new IntegerConstant(1);
    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(s1));
      v.add(new StringConstant(s2));
      v.add(new IntegerConstant(n));
      Expression e1 = new Word(v,true);

      v.clear();
      v.add(propertySensor1);
      v.add(propertySensor2);
      v.add(propertySensor3);
      Expression eps1 = new Word(v,true);

      assertTrue(
          (e1.evaluate()).equals(
          "ThisIsADogThisIsAPenThisIsATelevisionSet"));
      assertTrue((eps1.evaluate()).equals("ThisIsADog"));
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }

  /*
   * This will test Contains Expression
   */
  public void testContains() {
    String s1 = "\"ThisIsADogThisIsAPenThisIsATelevisionSetThisIsAHouse\"";
    String s2 = "\"ThisIsAPen\"";

    //HashMap map = new HashMap();
    propertySensor1=new StringConstant(s1);
    propertySensor2=new StringConstant(s2);
    //SensorLookup lookup = new SensorLookupImpl(map);

    try {
      Vector v = new Vector();
      v.add(new StringConstant(s1));
      v.add(new StringConstant(s2));
      Expression e1 = new Contains(v,true);

      v.clear();
      v.add(propertySensor1);
      v.add(propertySensor2);
      Expression eps1 = new Contains(v,true);

      assertTrue(((Boolean) (e1.evaluate())).booleanValue());
      assertTrue(((Boolean) (eps1.evaluate())).booleanValue());
    } catch (SPLException e) {
      fail("Received evaluation exception " + e.getMessage());
    } catch (Exception e) {
      fail("Received expression exception " + e.getMessage());
    }
  }
 
     public void testToLower() {
        String s1 = "\"HELLO\"";
          String s2 = "\"HellO\"";
          String s3 = "hello";

          //HashMap map = new HashMap();
          propertySensor1=new StringConstant(s1.toString());
          propertySensor2=new StringConstant(s2.toString());
         
          try {
              Vector v = new Vector();
              v.add(propertySensor1);
              Expression es1 = new ToLower(v,true);

              v.clear();
              v.add(propertySensor2);
              Expression es2 = new ToLower(v,true);

             
             
              String x=(String)es1.evaluate();
              assertTrue(x.equals(s3));
              x=(String)es2.evaluate();
              assertTrue(x.equals(s3));
                     
             
          } catch (SPLException e) {
              fail("Received evaluation exception " + e.getMessage());
          } catch (Exception e) {
              fail("Received expression exception " + e.getMessage());
          }
      }
    
     public void testToUpper() {
        String s1 = "\"hello\"";
          String s2 = "\"HellO\"";
          String s3 = "HELLO";

          //HashMap map = new HashMap();
          propertySensor1=new StringConstant(s1.toString());
          propertySensor2=new StringConstant(s2.toString());
         
          try {
              Vector v = new Vector();
              v.add(propertySensor1);
              Expression es1 = new ToUpper(v,true);

              v.clear();
              v.add(propertySensor2);
              Expression es2 = new ToUpper(v,true);

             
             
              String x=(String)es1.evaluate();
              assertTrue(x.equals(s3));
              x=(String)es2.evaluate();
              assertTrue(x.equals(s3));
                     
             
          } catch (SPLException e) {
              fail("Received evaluation exception " + e.getMessage());
          } catch (Exception e) {
              fail("Received expression exception " + e.getMessage());
          }
      }
    
     public void testToString() {
        propertySensor1=new IntegerConstant(845);
          propertySensor2=new BooleanConstant(true);
         
          try {
              Vector v = new Vector();
              v.add(propertySensor1);
              Expression es1 = new ToString(v,true);

              v.clear();
              v.add(propertySensor2);
              Expression es2 = new ToString(v,true);

             
             
              String x=(String)es1.evaluate();
              assertTrue(x.equals("845"));
              x=(String)es2.evaluate();
              assertTrue(x.equals("true"));
                     
             
          } catch (SPLException e) {
              fail("Received evaluation exception " + e.getMessage());
          } catch (Exception e) {
              fail("Received expression exception " + e.getMessage());
          }
      }
}
TOP

Related Classes of org.apache.imperius.spl.tests.StringExpressionTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.