/* * ************************************************************************************* * Copyright (C) 2008 EsperTech, Inc. All rights reserved. * * http://esper.codehaus.org * * http://www.espertech.com * * ---------------------------------------------------------------------------------- * * The software in this package is published under the terms of the GPL license * * a copy of which has been included with this distribution in the license.txt file. * * ************************************************************************************* */ package com.espertech.esper.regression.epl; import com.espertech.esper.client.scopetest.SupportUpdateListener; import junit.framework.TestCase; import com.espertech.esper.client.*; import com.espertech.esper.client.EPStatementSyntaxException; import com.espertech.esper.support.bean.SupportBean; import com.espertech.esper.support.bean.SupportMarketDataBean; import com.espertech.esper.support.client.SupportConfigFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class TestInvalidEPL extends TestCase { private EPServiceProvider epService; private SupportUpdateListener listener; public void setUp() { epService = EPServiceProviderManager.getDefaultProvider(SupportConfigFactory.getConfiguration()); epService.initialize(); listener = new SupportUpdateListener(); } protected void tearDown() throws Exception { listener = null; } public void testInvalidSyntax() { String exceptionText = getSyntaxExceptionEPL("select * from *"); assertEquals("Incorrect syntax near '*' at line 1 column 14, please check the from clause [select * from *]", exceptionText); exceptionText = getSyntaxExceptionEPL("select * from SupportBean a where a.intPrimitive between r.start and r.end"); assertEquals("Incorrect syntax near 'start' (a reserved keyword) at line 1 column 59, please check the where clause [select * from SupportBean a where a.intPrimitive between r.start and r.end]", exceptionText); } public void testLongTypeConstant() { String stmtText = "select 2512570244 as value from " + SupportBean.class.getName(); EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText); stmt.addListener(listener); epService.getEPRuntime().sendEvent(new SupportBean()); assertEquals(2512570244L, listener.assertOneGetNewAndReset().get("value")); } public void testDifferentJoins() { String streamDef = "select * from " + SupportBean.class.getName() + ".win:length(3) as sa," + SupportBean.class.getName() + ".win:length(3) as sb" + " where "; String streamDefTwo = "select * from " + SupportBean.class.getName() + ".win:length(3)," + SupportMarketDataBean.class.getName() + ".win:length(3)" + " where "; tryInvalid(streamDef + "sa.intPrimitive = sb.theString"); tryValid(streamDef + "sa.intPrimitive = sb.intBoxed"); tryValid(streamDef + "sa.intPrimitive = sb.intPrimitive"); tryValid(streamDef + "sa.intPrimitive = sb.longBoxed"); tryInvalid(streamDef + "sa.intPrimitive = sb.intPrimitive and sb.intBoxed = sa.boolPrimitive"); tryValid(streamDef + "sa.intPrimitive = sb.intPrimitive and sb.boolBoxed = sa.boolPrimitive"); tryInvalid(streamDef + "sa.intPrimitive = sb.intPrimitive and sb.intBoxed = sa.intPrimitive and sa.theString=sX.theString"); tryValid(streamDef + "sa.intPrimitive = sb.intPrimitive and sb.intBoxed = sa.intPrimitive and sa.theString=sb.theString"); tryInvalid(streamDef + "sa.intPrimitive = sb.intPrimitive or sa.theString=sX.theString"); tryValid(streamDef + "sa.intPrimitive = sb.intPrimitive or sb.intBoxed = sa.intPrimitive"); // try constants tryValid(streamDef + "sa.intPrimitive=5"); tryValid(streamDef + "sa.theString='4'"); tryValid(streamDef + "sa.theString=\"4\""); tryValid(streamDef + "sa.boolPrimitive=false"); tryValid(streamDef + "sa.longPrimitive=-5L"); tryValid(streamDef + "sa.doubleBoxed=5.6d"); tryValid(streamDef + "sa.floatPrimitive=-5.6f"); tryInvalid(streamDef + "sa.intPrimitive='5'"); tryInvalid(streamDef + "sa.theString=5"); tryInvalid(streamDef + "sa.boolBoxed=f"); tryInvalid(streamDef + "sa.intPrimitive=x"); tryValid(streamDef + "sa.intPrimitive=5.5"); // try addition and subtraction tryValid(streamDef + "sa.intPrimitive=sa.intBoxed + 5"); tryValid(streamDef + "sa.intPrimitive=2*sa.intBoxed - sa.intPrimitive/10 + 1"); tryValid(streamDef + "sa.intPrimitive=2*(sa.intBoxed - sa.intPrimitive)/(10 + 1)"); tryInvalid(streamDef + "sa.intPrimitive=2*(sa.intBoxed"); // try comparison tryValid(streamDef + "sa.intPrimitive > sa.intBoxed and sb.doublePrimitive < sb.doubleBoxed"); tryValid(streamDef + "sa.intPrimitive >= sa.intBoxed and sa.doublePrimitive <= sa.doubleBoxed"); tryValid(streamDef + "sa.intPrimitive > (sa.intBoxed + sb.doublePrimitive)"); tryInvalid(streamDef + "sa.intPrimitive >= sa.theString"); tryInvalid(streamDef + "sa.boolBoxed >= sa.boolPrimitive"); // Try some nested tryValid(streamDef + "(sa.intPrimitive=3) or (sa.intBoxed=3 and sa.intPrimitive=1)"); tryValid(streamDef + "((sa.intPrimitive>3) or (sa.intBoxed<3)) and sa.boolBoxed=false"); tryValid(streamDef + "(sa.intPrimitive<=3 and sa.intPrimitive>=1) or (sa.boolBoxed=false and sa.boolPrimitive=true)"); tryInvalid(streamDef + "sa.intPrimitive=3 or (sa.intBoxed=2"); tryInvalid(streamDef + "sa.intPrimitive=3 or sa.intBoxed=2)"); tryInvalid(streamDef + "sa.intPrimitive=3 or ((sa.intBoxed=2)"); // Try some without stream name tryInvalid(streamDef + "intPrimitive=3"); tryValid(streamDefTwo + "intPrimitive=3"); // Try invalid outer join criteria String outerJoinDef = "select * from " + SupportBean.class.getName() + ".win:length(3) as sa " + "left outer join " + SupportBean.class.getName() + ".win:length(3) as sb "; tryValid(outerJoinDef + "on sa.intPrimitive = sb.intBoxed"); tryInvalid(outerJoinDef + "on sa.intPrimitive = sb.XX"); tryInvalid(outerJoinDef + "on sa.XX = sb.XX"); tryInvalid(outerJoinDef + "on sa.XX = sb.intBoxed"); tryInvalid(outerJoinDef + "on sa.boolBoxed = sb.intBoxed"); tryValid(outerJoinDef + "on sa.boolPrimitive = sb.boolBoxed"); tryInvalid(outerJoinDef + "on sa.boolPrimitive = sb.theString"); tryInvalid(outerJoinDef + "on sa.intPrimitive <= sb.intBoxed"); tryInvalid(outerJoinDef + "on sa.intPrimitive = sa.intBoxed"); tryInvalid(outerJoinDef + "on sb.intPrimitive = sb.intBoxed"); tryValid(outerJoinDef + "on sb.intPrimitive = sa.intBoxed"); } private void tryInvalid(String eplInvalidEPL) { try { epService.getEPAdministrator().createEPL(eplInvalidEPL); fail(); } catch (EPException ex) { // Expected exception } } private void tryValid(String invalidEPL) { epService.getEPAdministrator().createEPL(invalidEPL); } private String getSyntaxExceptionEPL(String expression) { String exceptionText = null; try { epService.getEPAdministrator().createEPL(expression); fail(); } catch (EPStatementSyntaxException ex) { exceptionText = ex.getMessage(); log.debug(".getSyntaxExceptionEPL epl=" + expression, ex); // Expected exception } return exceptionText; } private static Log log = LogFactory.getLog(TestInvalidEPL.class); }