/*
* *************************************************************************************
* 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.EPStatement;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.scopetest.EPAssertionUtil;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.support.bean.SupportMarketDataBean;
import com.espertech.esper.support.bean.SupportBeanString;
import com.espertech.esper.support.client.SupportConfigFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import junit.framework.TestCase;
public class TestGroupByEventPerRowHaving extends TestCase
{
private static String SYMBOL_DELL = "DELL";
private static String SYMBOL_IBM = "IBM";
private EPServiceProvider epService;
private SupportUpdateListener testListener;
public void setUp()
{
testListener = new SupportUpdateListener();
epService = EPServiceProviderManager.getDefaultProvider(SupportConfigFactory.getConfiguration());
epService.initialize();
}
protected void tearDown() throws Exception {
testListener = null;
}
public void testSumOneView()
{
// Every event generates a new row, this time we sum the price by symbol and output volume
String viewExpr = "select irstream symbol, volume, sum(price) as mySum " +
"from " + SupportMarketDataBean.class.getName() + ".win:length(3) " +
"where symbol='DELL' or symbol='IBM' or symbol='GE' " +
"group by symbol " +
"having sum(price) >= 50";
EPStatement selectTestView = epService.getEPAdministrator().createEPL(viewExpr);
selectTestView.addListener(testListener);
runAssertion(selectTestView);
}
public void testSumJoin()
{
// Every event generates a new row, this time we sum the price by symbol and output volume
String viewExpr = "select irstream symbol, volume, sum(price) as mySum " +
"from " + SupportBeanString.class.getName() + ".win:length(100) as one, " +
SupportMarketDataBean.class.getName() + ".win:length(3) as two " +
"where (symbol='DELL' or symbol='IBM' or symbol='GE') " +
" and one.theString = two.symbol " +
"group by symbol " +
"having sum(price) >= 50";
EPStatement selectTestView = epService.getEPAdministrator().createEPL(viewExpr);
selectTestView.addListener(testListener);
epService.getEPRuntime().sendEvent(new SupportBeanString(SYMBOL_DELL));
epService.getEPRuntime().sendEvent(new SupportBeanString(SYMBOL_IBM));
runAssertion(selectTestView);
}
private void runAssertion(EPStatement selectTestView)
{
// assert select result type
assertEquals(String.class, selectTestView.getEventType().getPropertyType("symbol"));
assertEquals(Long.class, selectTestView.getEventType().getPropertyType("volume"));
assertEquals(Double.class, selectTestView.getEventType().getPropertyType("mySum"));
String[] fields = "symbol,volume,mySum".split(",");
sendEvent(SYMBOL_DELL, 10000, 49);
assertFalse(testListener.isInvoked());
sendEvent(SYMBOL_DELL, 20000, 54);
EPAssertionUtil.assertProps(testListener.assertOneGetNewAndReset(), fields, new Object[]{SYMBOL_DELL, 20000L, 103d});
sendEvent(SYMBOL_IBM, 1000, 10);
assertFalse(testListener.isInvoked());
sendEvent(SYMBOL_IBM, 5000, 20);
EPAssertionUtil.assertProps(testListener.assertOneGetOldAndReset(), fields, new Object[]{SYMBOL_DELL, 10000L, 54d});
sendEvent(SYMBOL_IBM, 6000, 5);
assertFalse(testListener.isInvoked());
}
private void sendEvent(String symbol, long volume, double price)
{
SupportMarketDataBean bean = new SupportMarketDataBean(symbol, price, volume, null);
epService.getEPRuntime().sendEvent(bean);
}
private static final Log log = LogFactory.getLog(TestGroupByEventPerRowHaving.class);
}