/*
* *************************************************************************************
* 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.enummethod;
import com.espertech.esper.client.*;
import com.espertech.esper.client.scopetest.EPAssertionUtil;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.support.bean.SupportBean_ST0_Container;
import com.espertech.esper.support.bean.SupportCollection;
import com.espertech.esper.support.bean.lambda.LambdaAssertionUtil;
import com.espertech.esper.support.client.SupportConfigFactory;
import junit.framework.TestCase;
public class TestEnumMostLeastFrequent extends TestCase {
private EPServiceProvider epService;
private SupportUpdateListener listener;
public void setUp() {
Configuration config = SupportConfigFactory.getConfiguration();
config.addEventType("Bean", SupportBean_ST0_Container.class);
config.addEventType("SupportCollection", SupportCollection.class);
epService = EPServiceProviderManager.getDefaultProvider(config);
epService.initialize();
listener = new SupportUpdateListener();
}
protected void tearDown() throws Exception {
listener = null;
}
public void testMostLeastEvents() {
String[] fields = "val0,val1".split(",");
String eplFragment = "select " +
"contained.mostFrequent(x => p00) as val0," +
"contained.leastFrequent(x => p00) as val1 " +
"from Bean";
EPStatement stmtFragment = epService.getEPAdministrator().createEPL(eplFragment);
stmtFragment.addListener(listener);
LambdaAssertionUtil.assertTypes(stmtFragment.getEventType(), fields, new Class[]{Integer.class, Integer.class});
SupportBean_ST0_Container bean = SupportBean_ST0_Container.make2Value("E1,12", "E2,11", "E2,2", "E3,12");
epService.getEPRuntime().sendEvent(bean);
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{12, 11});
bean = SupportBean_ST0_Container.make2Value("E1,12");
epService.getEPRuntime().sendEvent(bean);
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{12, 12});
bean = SupportBean_ST0_Container.make2Value("E1,12", "E2,11", "E2,2", "E3,12", "E1,12", "E2,11", "E3,11");
epService.getEPRuntime().sendEvent(bean);
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{12, 2});
bean = SupportBean_ST0_Container.make2Value("E2,11", "E1,12", "E2,15", "E3,12", "E1,12", "E2,11", "E3,11");
epService.getEPRuntime().sendEvent(bean);
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{11, 15});
epService.getEPRuntime().sendEvent(SupportBean_ST0_Container.make2Value(null));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{null, null});
epService.getEPRuntime().sendEvent(SupportBean_ST0_Container.make2Value());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{null, null});
}
public void testScalar() {
String[] fields = "val0,val1".split(",");
String eplFragment = "select " +
"strvals.mostFrequent() as val0, " +
"strvals.leastFrequent() as val1 " +
"from SupportCollection";
EPStatement stmtFragment = epService.getEPAdministrator().createEPL(eplFragment);
stmtFragment.addListener(listener);
LambdaAssertionUtil.assertTypes(stmtFragment.getEventType(), fields, new Class[]{String.class, String.class});
epService.getEPRuntime().sendEvent(SupportCollection.makeString("E2,E1,E2,E1,E3,E3,E4,E3"));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E3", "E4"});
epService.getEPRuntime().sendEvent(SupportCollection.makeString("E1"));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E1", "E1"});
epService.getEPRuntime().sendEvent(SupportCollection.makeString(null));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{null, null});
epService.getEPRuntime().sendEvent(SupportCollection.makeString(""));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{null, null});
stmtFragment.destroy();
epService.getEPAdministrator().getConfiguration().addPlugInSingleRowFunction("extractNum", TestEnumMinMax.MyService.class.getName(), "extractNum");
String eplLambda = "select " +
"strvals.mostFrequent(v => extractNum(v)) as val0, " +
"strvals.leastFrequent(v => extractNum(v)) as val1 " +
"from SupportCollection";
EPStatement stmtLambda = epService.getEPAdministrator().createEPL(eplLambda);
stmtLambda.addListener(listener);
LambdaAssertionUtil.assertTypes(stmtLambda.getEventType(), fields, new Class[]{Integer.class, Integer.class});
epService.getEPRuntime().sendEvent(SupportCollection.makeString("E2,E1,E2,E1,E3,E3,E4,E3"));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{3, 4});
epService.getEPRuntime().sendEvent(SupportCollection.makeString("E1"));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{1, 1});
epService.getEPRuntime().sendEvent(SupportCollection.makeString(null));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{null, null});
epService.getEPRuntime().sendEvent(SupportCollection.makeString(""));
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{null, null});
}
}