/*
* *************************************************************************************
* 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.view;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.scopetest.EPAssertionUtil;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.client.soda.EPStatementFormatter;
import com.espertech.esper.client.soda.EPStatementObjectModel;
import com.espertech.esper.support.bean.bookexample.*;
import com.espertech.esper.support.bean.word.SentenceEvent;
import com.espertech.esper.support.client.SupportConfigFactory;
import junit.framework.TestCase;
public class TestFilterPropertySimple extends TestCase
{
private String NEWLINE = System.getProperty("line.separator");
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;
}
// Assures that the events inserted into the named window are preemptive to events generated by contained-event syntax.
// This example generates 3 contained-events: One for each book.
// It then inserts them into a named window to determine the highest price among all.
// The named window updates first becoming useful to subsequent events (versus last and not useful).
public void testNamedWindowPremptive()
{
String[] fields = "bookId".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
epService.getEPAdministrator().getConfiguration().addEventType("BookDesc", BookDesc.class);
String stmtText = "insert into BookStream select * from OrderEvent[books]";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
EPStatement stmtNW = epService.getEPAdministrator().createEPL("create window MyWindow.std:lastevent() as BookDesc");
epService.getEPAdministrator().createEPL("insert into MyWindow select * from BookStream bs where not exists (select * from MyWindow mw where mw.price > bs.price)");
epService.getEPRuntime().sendEvent(TestFilterPropertySimple.makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10020"}, {"10021"}, {"10022"}});
listener.reset();
// higest price (27 is the last value)
EventBean theEvent = stmtNW.iterator().next();
assertEquals(35.0, theEvent.get("price"));
}
public void testUnidirectionalJoin()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select * from " +
"OrderEvent as orderEvent unidirectional, " +
"OrderEvent[select * from books] as book, " +
"OrderEvent[select * from orderdetail.items] as item " +
"where book.bookId = item.productId " +
"order by book.bookId, item.amount";
String stmtTextFormatted = "select *" + NEWLINE +
"from OrderEvent as orderEvent unidirectional," + NEWLINE +
"OrderEvent[select * from books] as book," + NEWLINE +
"OrderEvent[select * from orderdetail.items] as item" + NEWLINE +
"where book.bookId = item.productId" + NEWLINE +
"order by book.bookId, item.amount";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
runAssertion();
stmt.destroy();
EPStatementObjectModel model = epService.getEPAdministrator().compileEPL(stmtText);
assertEquals(stmtText, model.toEPL());
assertEquals(stmtTextFormatted, model.toEPL(new EPStatementFormatter(true)));
stmt = epService.getEPAdministrator().create(model);
stmt.addListener(listener);
runAssertion();
}
private void runAssertion()
{
String[] fields = "orderEvent.orderdetail.orderId,book.bookId,book.title,item.amount".split(",");
epService.getEPRuntime().sendEvent(makeEventOne());
assertEquals(3, listener.getLastNewData().length);
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"PO200901", "10020", "Enders Game", 10}, {"PO200901", "10020", "Enders Game", 30}, {"PO200901", "10021", "Foundation 1", 25}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventTwo());
assertEquals(1, listener.getLastNewData().length);
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"PO200902", "10022", "Stranger in a Strange Land", 5}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventThree());
assertEquals(1, listener.getLastNewData().length);
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"PO200903", "10021", "Foundation 1", 50}});
}
public void testUnidirectionalJoinCount()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select count(*) from " +
"OrderEvent orderEvent unidirectional, " +
"OrderEvent[books] as book, " +
"OrderEvent[orderdetail.items] item " +
"where book.bookId = item.productId order by book.bookId asc, item.amount asc";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{3L});
epService.getEPRuntime().sendEvent(makeEventTwo());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{1L});
epService.getEPRuntime().sendEvent(makeEventThree());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{1L});
epService.getEPRuntime().sendEvent(makeEventFour());
assertFalse(listener.isInvoked());
}
public void testJoinCount()
{
String[] fields = "count(*)".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select count(*) from " +
"OrderEvent[books].std:unique(bookId) book, " +
"OrderEvent[orderdetail.items].win:keepall() item " +
"where book.bookId = item.productId";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{3L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{3L}});
epService.getEPRuntime().sendEvent(makeEventTwo());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{4L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{4L}});
epService.getEPRuntime().sendEvent(makeEventThree());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{5L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{5L}});
epService.getEPRuntime().sendEvent(makeEventFour());
assertFalse(listener.isInvoked());
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{8L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{8L}});
}
public void testJoin()
{
String[] fields = "book.bookId,item.itemId,amount".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select book.bookId,item.itemId,amount from " +
"OrderEvent[books].std:firstunique(bookId) book, " +
"OrderEvent[orderdetail.items].win:keepall() item " +
"where book.bookId = item.productId " +
"order by book.bookId, item.itemId";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventTwo());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10022", "B001", 5}});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}, {"10022", "B001", 5}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventThree());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10021", "C001", 50}});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}, {"10021", "C001", 50}, {"10022", "B001", 5}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventFour());
assertFalse(listener.isInvoked());
}
public void testAloneCount()
{
String[] fields = "count(*)".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select count(*) from OrderEvent[books]";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{3L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{3L}});
epService.getEPRuntime().sendEvent(makeEventFour());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{5L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{5L}});
}
public void testPropertyAccess()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
EPStatement stmtOne = epService.getEPAdministrator().createEPL("select bookId from OrderEvent[books]");
stmtOne.addListener(listener);
EPStatement stmtTwo = epService.getEPAdministrator().createEPL("select books[0].author as val from OrderEvent(books[0].bookId = '10020')");
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10020"}, {"10021"}, {"10022"}});
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmtOne.iterator(), "bookId".split(","), new Object[][]{{"10020"}, {"10021"}, {"10022"}});
EPAssertionUtil.assertPropsPerRow(stmtTwo.iterator(), "val".split(","), new Object[][]{{"Orson Scott Card"}});
epService.getEPRuntime().sendEvent(makeEventFour());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10031"}, {"10032"}});
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmtOne.iterator(), "bookId".split(","), new Object[][]{{"10031"}, {"10032"}});
EPAssertionUtil.assertPropsPerRow(stmtTwo.iterator(), "val".split(","), new Object[][]{{"Orson Scott Card"}});
// add where clause
stmtOne.destroy();
stmtTwo.destroy();
stmtOne = epService.getEPAdministrator().createEPL("select bookId from OrderEvent[books where author='Orson Scott Card']");
stmtOne.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10020"}});
listener.reset();
}
public void testIRStreamArrayItem()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select irstream bookId from OrderEvent[books[0]]";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10020"}});
assertNull(listener.getLastOldData());
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), "bookId".split(","), new Object[][]{{"10020"}});
epService.getEPRuntime().sendEvent(makeEventFour());
assertNull(listener.getLastOldData());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10031"}});
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), "bookId".split(","), new Object[][]{{"10031"}});
}
public void testSplitWords() {
epService.getEPAdministrator().getConfiguration().addEventType(SentenceEvent.class);
String stmtText = "insert into WordStream select * from SentenceEvent[words]";
String[] fields = "word".split(",");
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(new SentenceEvent("I am testing this"));
EPAssertionUtil.assertPropsPerRow(listener.getAndResetLastNewData(), fields, new Object[][]{{"I"}, {"am"}, {"testing"}, {"this"}});
}
public static OrderBean makeEventOne()
{
Order order = new Order("PO200901",
new OrderItem[] {
new OrderItem("A001", "10020", 10, 11.95),
new OrderItem("A002", "10021", 25, 7.50),
new OrderItem("A003", "10020", 30, 10),
});
return new OrderBean(order, getBookDesc(), new GameDesc[0]);
}
public static OrderBean makeEventTwo()
{
Order order = new Order("PO200902",
new OrderItem[] {new OrderItem("B001", "10022", 5, 99.50)});
return new OrderBean(order, getBookDesc(), new GameDesc[0]);
}
public static OrderBean makeEventThree()
{
Order order = new Order("PO200903",
new OrderItem[] {
new OrderItem("C001", "10025", 52, 99.50),
new OrderItem("C001", "10024", 51, 41.50),
new OrderItem("C001", "10021", 50, 30.50)
});
return new OrderBean(order, getBookDesc(),
new GameDesc[] {new GameDesc("GA01", "Castlevania", "Eidos",
new Review[] {
new Review(100, "best game ever"),
new Review(101, "good platformer")
})
});
}
public static OrderBean makeEventFour()
{
Order order = new Order("PO200904",
new OrderItem[0]);
return new OrderBean(order, new BookDesc[] {
new BookDesc("10031", "Foundation 2", "Isaac Asimov", 15.00d,
new Review[] {
new Review(201, "great book")
}),
new BookDesc("10032", "Red Planet", "Robert A Heinlein", 13.00d, new Review[0]),
}, new GameDesc[0]);
}
private static BookDesc[] getBookDesc()
{
return new BookDesc[] {
new BookDesc("10020", "Enders Game", "Orson Scott Card", 24.00d,
new Review[] {
new Review(1, "best book ever"),
new Review(2, "good science fiction")
}),
new BookDesc("10021", "Foundation 1", "Isaac Asimov", 35.00d,
new Review[] {
new Review(10, "great book")
}),
new BookDesc("10022", "Stranger in a Strange Land", "Robert A Heinlein", 27.00d, new Review[0])
};
}
}