/*
* *************************************************************************************
* 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.view.std;
import com.espertech.esper.client.EventType;
import com.espertech.esper.support.bean.SupportBean;
import com.espertech.esper.support.epl.SupportExprNodeFactory;
import com.espertech.esper.support.event.SupportEventTypeFactory;
import com.espertech.esper.support.view.SupportStatementContextFactory;
import com.espertech.esper.view.TestViewSupport;
import com.espertech.esper.view.ViewFactoryContext;
import com.espertech.esper.view.ViewParameterException;
import junit.framework.TestCase;
public class TestGroupByViewFactory extends TestCase
{
private GroupByViewFactory factory;
private ViewFactoryContext viewFactoryContext = new ViewFactoryContext(SupportStatementContextFactory.makeContext(), 1, 0, null, null);
public void setUp()
{
factory = new GroupByViewFactory();
}
public void testSetParameters() throws Exception
{
tryParameter(new Object[] {"doublePrimitive"}, new String[] {"doublePrimitive"});
tryParameter(new Object[] {"doublePrimitive", "longPrimitive"}, new String[] {"doublePrimitive", "longPrimitive"});
tryInvalidParameter(new Object[] {"theString", 1.1d});
tryInvalidParameter(new Object[] {1.1d});
tryInvalidParameter(new Object[] {new String[] {}});
tryInvalidParameter(new Object[] {new String[] {}, new String[] {}});
}
public void testCanReuse() throws Exception
{
factory.setViewParameters(viewFactoryContext, TestViewSupport.toExprListBean(new Object[] {"theString", "longPrimitive"}));
factory.attach(SupportEventTypeFactory.createBeanType(SupportBean.class), SupportStatementContextFactory.makeContext(), null, null);
assertFalse(factory.canReuse(new FirstElementView()));
assertFalse(factory.canReuse(new GroupByViewImpl(SupportStatementContextFactory.makeAgentInstanceViewFactoryContext(), SupportExprNodeFactory.makeIdentNodesBean("theString"), null)));
assertTrue(factory.canReuse(new GroupByViewImpl(SupportStatementContextFactory.makeAgentInstanceViewFactoryContext(), SupportExprNodeFactory.makeIdentNodesBean("theString", "longPrimitive"), null)));
factory.setViewParameters(viewFactoryContext, TestViewSupport.toExprListBean(new Object[] {SupportExprNodeFactory.makeIdentNodesBean("theString", "longPrimitive")}));
assertFalse(factory.canReuse(new GroupByViewImpl(SupportStatementContextFactory.makeAgentInstanceViewFactoryContext(), SupportExprNodeFactory.makeIdentNodesBean("theString"), null)));
assertTrue(factory.canReuse(new GroupByViewImpl(SupportStatementContextFactory.makeAgentInstanceViewFactoryContext(), SupportExprNodeFactory.makeIdentNodesBean("theString", "longPrimitive"), null)));
}
public void testAttaches() throws Exception
{
// Should attach to anything as long as the fields exists
EventType parentType = SupportEventTypeFactory.createBeanType(SupportBean.class);
factory.setViewParameters(viewFactoryContext, TestViewSupport.toExprListBean(new Object[] {"intBoxed"}));
factory.attach(parentType, SupportStatementContextFactory.makeContext(), null, null);
}
private void tryInvalidParameter(Object[] parameters) throws Exception
{
try
{
GroupByViewFactory factory = new GroupByViewFactory();
factory.setViewParameters(viewFactoryContext, TestViewSupport.toExprListBean(parameters));
factory.attach(SupportEventTypeFactory.createBeanType(SupportBean.class), SupportStatementContextFactory.makeContext(), null, null);
fail();
}
catch (ViewParameterException ex)
{
// expected
}
}
private void tryParameter(Object[] parameters, String[] fieldNames) throws Exception
{
GroupByViewFactory factory = new GroupByViewFactory();
factory.setViewParameters(viewFactoryContext, TestViewSupport.toExprListBean(parameters));
factory.attach(SupportEventTypeFactory.createBeanType(SupportBean.class), SupportStatementContextFactory.makeContext(), null, null);
GroupByView view = (GroupByView) factory.makeView(SupportStatementContextFactory.makeAgentInstanceViewFactoryContext());
assertEquals(fieldNames[0], view.getCriteriaExpressions()[0].toExpressionString());
}
}