context: ///////////////////////////////////////////////////////////////////////////////////////
  // Copyright (C) 2006-2015 Esper Team. All rights reserved.                           /
  // http://esper.codehaus.org                                                          /
  // ---------------------------------------------------------------------------------- /
  // 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.  /
  ///////////////////////////////////////////////////////////////////////////////////////
  
  using System;
  using System.Collections.Generic;
  
  using com.espertech.esper.common.client.scopetest;
  using com.espertech.esper.common.client.soda;
  using com.espertech.esper.common.client.util;
  using com.espertech.esper.common.@internal.support;
  using com.espertech.esper.compat;
  using com.espertech.esper.compat.collections;
  using com.espertech.esper.regressionlib.framework;
  using com.espertech.esper.regressionlib.support.bean;
  using com.espertech.esper.regressionlib.support.util;
  
  using NUnit.Framework;
  
  using static com.espertech.esper.common.@internal.util.CollectionUtil;
  using static com.espertech.esper.regressionlib.framework.SupportMessageAssertUtil;
  
  using SupportBean_A = com.espertech.esper.regressionlib.support.bean.SupportBean_A;
  
  namespace com.espertech.esper.regressionlib.suite.infra.namedwindow
  {
      /// <summary>
      ///     NOTE: More namedwindow-related tests in "nwtable"
      /// </summary>
      public class InfraNamedWindowInsertFrom
      {
          public static IList<RegressionExecution> Executions()
          {
              var execs = new List<RegressionExecution>();
              WithCreateNamedAfterNamed(execs);
              WithInsertWhereTypeAndFilter(execs);
              WithInsertWhereOMStaggered(execs);
              WithInvalid(execs);
              WithVariantStream(execs);
              return execs;
          }
  
          public static IList<RegressionExecution> WithVariantStream(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new InfraVariantStream());
              return execs;
          }
  
          public static IList<RegressionExecution> WithInvalid(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new InfraInvalid());
              return execs;
          }
  
          public static IList<RegressionExecution> WithInsertWhereOMStaggered(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new InfraInsertWhereOMStaggered());
              return execs;
          }
  
          public static IList<RegressionExecution> WithInsertWhereTypeAndFilter(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new InfraInsertWhereTypeAndFilter());
              return execs;
          }
  
          public static IList<RegressionExecution> WithCreateNamedAfterNamed(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new InfraCreateNamedAfterNamed());
              return execs;
          }
  
          private static long GetCount(
              RegressionEnvironment env,
              RegressionPath path,
              string statementName,
              string windowName)
          {
              if (env.IsHA) {
                  return env
                      .CompileExecuteFAF("select count(*) as cnt from " + windowName, path)
                      .Array[0]
                      .Get("cnt")
                      .AsInt64();
              }
  
              return SupportInfraUtil.GetDataWindowCountNoContext(env, statementName, windowName);
          }
  
          internal class InfraCreateNamedAfterNamed : RegressionExecution
          {
              public void Run(RegressionEnvironment env)
              {
                  var epl = "@Name('windowOne') create window MyWindow#keepall as SupportBean;\n" +
                            "@Name('windowTwo')create window MyWindowTwo#keepall as MyWindow;\n" +
                            "insert into MyWindow select * from SupportBean;\n" +
                            "@Name('selectOne') select TheString from MyWindow;\n";
                  env.CompileDeploy(epl).AddListener("selectOne").AddListener("windowOne");
  
                  env.SendEventBean(new SupportBean("E1", 1));
                  string[] fields = {"TheString"};
                  EPAssertionUtil.AssertProps(
                      env.Listener("windowOne").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E1"});
                  EPAssertionUtil.AssertProps(
                      env.Listener("selectOne").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E1"});
  
                  env.UndeployAll();
              }
          }
  
          internal class InfraInsertWhereTypeAndFilter : RegressionExecution
          {
              public void Run(RegressionEnvironment env)
              {
                  string[] fields = {"TheString"};
                  var path = new RegressionPath();
  
                  var epl = "@Name('window') create window MyWindowIWT#keepall as SupportBean;\n" +
                            "insert into MyWindowIWT select * from SupportBean(IntPrimitive > 0);\n";
                  env.CompileDeploy(epl, path).AddListener("window");
  
                  env.Milestone(0);
  
                  // populate some data
                  Assert.AreEqual(0, GetCount(env, path, "window", "MyWindowIWT"));
                  env.SendEventBean(new SupportBean("A1", 1));
                  Assert.AreEqual(1, GetCount(env, path, "window", "MyWindowIWT"));
                  env.SendEventBean(new SupportBean("B2", 1));
  
                  env.Milestone(1);
  
                  env.SendEventBean(new SupportBean("C3", 1));
                  env.SendEventBean(new SupportBean("A4", 4));
                  env.SendEventBean(new SupportBean("C5", 4));
                  Assert.AreEqual(5, GetCount(env, path, "window", "MyWindowIWT"));
                  env.Listener("window").Reset();
  
                  env.Milestone(2);
  
                  // create window with keep-all
                  var stmtTextCreateTwo = "@Name('windowTwo') create window MyWindowTwo#keepall as MyWindowIWT insert";
                  env.CompileDeploy(stmtTextCreateTwo, path).AddListener("windowTwo");
                  EPAssertionUtil.AssertPropsPerRow(
                      env.GetEnumerator("windowTwo"),
                      fields,
                      new[] {
                          new object[] {"A1"}, new object[] {"B2"}, new object[] {"C3"}, new object[] {"A4"},
                          new object[] {"C5"}
                      });
                  var eventTypeTwo = env.GetEnumerator("windowTwo").Advance().EventType;
                  Assert.IsFalse(env.Listener("windowTwo").IsInvoked);
                  Assert.AreEqual(5, GetCount(env, path, "windowTwo", "MyWindowTwo"));
                  Assert.AreEqual(
                      StatementType.CREATE_WINDOW,
                      env.Statement("windowTwo").GetProperty(StatementProperty.STATEMENTTYPE));
                  Assert.AreEqual(
                      "MyWindowTwo", 
                      env.Statement("windowTwo").GetProperty(StatementProperty.CREATEOBJECTNAME));
  
                  // create window with keep-all and filter
                  var stmtTextCreateThree =
                      "@Name('windowThree') create window MyWindowThree#keepall as MyWindowIWT insert where TheString like 'A%'";
                  env.CompileDeploy(stmtTextCreateThree, path).AddListener("windowThree");
                  EPAssertionUtil.AssertPropsPerRow(
                      env.GetEnumerator("windowThree"),
                      fields,
                      new[] {new object[] {"A1"}, new object[] {"A4"}});
                  var eventTypeThree = env.GetEnumerator("windowThree").Advance().EventType;
                  Assert.IsFalse(env.Listener("windowThree").IsInvoked);
  
                  env.Milestone(3);
  
                  Assert.AreEqual(2, GetCount(env, path, "windowThree", "MyWindowThree"));
  
                  // create window with last-per-id
                  var stmtTextCreateFour =
                      "@Name('windowFour') create window MyWindowFour#unique(IntPrimitive) as MyWindowIWT insert";
                  env.CompileDeploy(stmtTextCreateFour, path).AddListener("windowFour");
                  EPAssertionUtil.AssertPropsPerRow(
                      env.GetEnumerator("windowFour"),
                      fields,
                      new[] {new object[] {"C3"}, new object[] {"C5"}});
                  var eventTypeFour = env.GetEnumerator("windowFour").Advance().EventType;
                  Assert.IsFalse(env.Listener("windowFour").IsInvoked);
  
                  env.Milestone(4);
  
                  Assert.AreEqual(2, GetCount(env, path, "windowFour", "MyWindowFour"));
  
                  env.CompileDeploy("insert into MyWindowIWT select * from SupportBean(TheString like 'A%')", path);
                  env.CompileDeploy("insert into MyWindowTwo select * from SupportBean(TheString like 'B%')", path);
                  env.CompileDeploy("insert into MyWindowThree select * from SupportBean(TheString like 'C%')", path);
                  env.CompileDeploy("insert into MyWindowFour select * from SupportBean(TheString like 'D%')", path);
                  Assert.IsFalse(
                      env.Listener("window").IsInvoked ||
                      env.Listener("windowTwo").IsInvoked ||
                      env.Listener("windowThree").IsInvoked ||
                      env.Listener("windowFour").IsInvoked);
  
                  env.SendEventBean(new SupportBean("B9", -9));
                  var received = env.Listener("windowTwo").AssertOneGetNewAndReset();
                  EPAssertionUtil.AssertProps(
                      received,
                      fields,
                      new object[] {"B9"});
                  if (!env.IsHA) {
                      Assert.AreSame(eventTypeTwo, received.EventType);
                  }
  
                  Assert.IsFalse(
                      env.Listener("window").IsInvoked ||
                      env.Listener("windowThree").IsInvoked ||
                      env.Listener("windowFour").IsInvoked);
                  Assert.AreEqual(6, GetCount(env, path, "windowTwo", "MyWindowTwo"));
  
                  env.Milestone(5);
  
                  env.SendEventBean(new SupportBean("A8", -8));
                  received = env.Listener("window").AssertOneGetNewAndReset();
                  EPAssertionUtil.AssertProps(
                      received,
                      fields,
                      new object[] {"A8"});
                  Assert.AreSame(env.Statement("window").EventType, received.EventType);
                  Assert.IsFalse(
                      env.Listener("windowTwo").IsInvoked ||
                      env.Listener("windowThree").IsInvoked ||
                      env.Listener("windowFour").IsInvoked);
  
                  env.Milestone(6);
  
                  env.SendEventBean(new SupportBean("C7", -7));
                  received = env.Listener("windowThree").AssertOneGetNewAndReset();
                  EPAssertionUtil.AssertProps(
                      received,
                      fields,
                      new object[] {"C7"});
                  if (!env.IsHA) {
                      Assert.AreSame(eventTypeThree, received.EventType);
                  }
  
                  Assert.IsFalse(
                      env.Listener("windowTwo").IsInvoked ||
                      env.Listener("window").IsInvoked ||
                      env.Listener("windowFour").IsInvoked);
  
                  env.SendEventBean(new SupportBean("D6", -6));
                  received = env.Listener("windowFour").AssertOneGetNewAndReset();
                  EPAssertionUtil.AssertProps(
                      received,
                      fields,
  
gt:                     new object[] {"D6"});