Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How can I test Criteria/Criterion objects?

    Is there any way I can apply a Criteria/Criterion object to a sample data value in a unit test to verify that I have constructed it correctly?

    I'm building a SmartGWT (2.5) UI for an application that uses its own filter string grammar on the server. I'm translating these filter strings into Criteria/Criterion objects in the client and setting them on a ListGrid. When the ListGrid calls to its DataSource, I intercept the Criteria in transformRequest() and translate it back to our grammar to send to the server.

    But when the ListGrid has downloaded the full data set and switches to client-side filtering, I don't always get the result I expect because I have constructed the Criteria object incorrectly.

    I want to build unit tests that construct Criteria objects and then evaluate them against individual test values to make sure they match the ones we expect.

    Is there a way to do this without constructing a full data-driven ListGrid?

    James

    #2
    There's a direct method in SmartClient, not wrapped with JSNI yet, called DataSource.applyFilter(). The quickest way in SmartGWT at the moment is to use a ResultSet, populate it with the one record, setCriteria() and check length.

    Comment


      #3
      Thanks. That's pretty straightforward, but I must still be missing something. Here's my code:


      @Test
      public void testFilterSample() {
      Record[] records = new Record[2];
      records[0] = new Record();
      records[0].setAttribute("name", "Fred");
      records[1] = new Record();
      records[1].setAttribute("name", "Barney");

      ResultSet resultSet = new ResultSet();
      resultSet.setAllRows(records);
      assertEquals(2, resultSet.getLength());

      Criterion isFred = new Criterion("name", OperatorId.EQUALS, "Fred");
      resultSet.setCriteria(isFred);
      assertEquals(1, resultSet.getLength());
      }



      This results in an exception:


      java.lang.RuntimeException: Remote test failed at 15.98.147.42 / Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19
      at com.google.gwt.junit.JUnitShell.processTestResult(JUnitShell.java:1288)
      at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1408)
      at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1309)
      at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:650)
      at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:441)
      at junit.framework.TestCase.runBare(TestCase.java:134)
      at junit.framework.TestResult$1.protect(TestResult.java:110)
      at junit.framework.TestResult.runProtected(TestResult.java:128)
      at junit.framework.TestResult.run(TestResult.java:113)
      at junit.framework.TestCase.run(TestCase.java:124)
      at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:296)
      at junit.framework.TestSuite.runTest(TestSuite.java:232)
      at junit.framework.TestSuite.run(TestSuite.java:227)
      at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      Caused by: java.lang.Exception: com.google.gwt.core.client.JavaScriptException: (null): null
      at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:237)
      at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
      at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
      at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
      at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
      at com.smartgwt.client.data.ResultSet.create(ResultSet.java)
      at com.smartgwt.client.core.BaseClass.getOrCreateJsObj(BaseClass.java:112)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
      at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
      at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
      at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
      at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
      at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
      at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
      at com.google.gwt.dev.shell.ModuleSpace.invokeNativeInt(ModuleSpace.java:245)
      at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeInt(JavaScriptHost.java:75)
      at com.smartgwt.client.data.ResultSet.getLength(ResultSet.java)
      at com.hp.cp.pm.gwt.ui.client.data.SmartGWTFilterBuilderTest.testFilterSample(SmartGWTFilterBuilderTest.java:174)
      at com.hp.cp.pm.gwt.ui.client.data.__SmartGWTFilterBuilderTest_unitTestImpl.doRunTest(__SmartGWTFilterBuilderTest_unitTestImpl.java:28)
      at junit.framework.TestCase.runTest(TestCase.java:62)
      at com.google.gwt.junit.client.GWTTestCase.runBare(GWTTestCase.java:188)
      at com.google.gwt.junit.client.GWTTestCase.__doRunTest(GWTTestCase.java:129)
      at com.google.gwt.junit.client.impl.GWTRunner.runTest(GWTRunner.java:389)
      at com.google.gwt.junit.client.impl.GWTRunner.doRunTest(GWTRunner.java:318)
      at com.google.gwt.junit.client.impl.GWTRunner.access$9(GWTRunner.java:312)
      at com.google.gwt.junit.client.impl.GWTRunner$TestBlockListener.onSuccess(GWTRunner.java:107)
      at com.google.gwt.junit.client.impl.GWTRunner$TestBlockListener.onSuccess(GWTRunner.java:1)
      at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:232)
      at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
      at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
      at sun.reflect.GeneratedMethodAccessor27.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
      at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
      at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
      at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
      at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
      at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
      at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
      at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
      at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
      at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
      at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:221)
      at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
      at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
      at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
      at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:281)
      at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:531)
      at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
      at java.lang.Thread.run(Thread.java:662)

      Comment


        #4
        Seems like you may be running this through GWTTestCase or JUnit - try running it directly instead, you should find it works, then you can separately troubleshoot what's wrong with your test environment.

        Comment


          #5
          I moved the code into an otherwise-working SmartGWT client application, and it's failing at the same point, but perhaps with a better error message:


          15:57:49.849 [ERROR] [com.XXXXXX] 15:57:49.847:WARN:ResultSet:isc_ResultSet_0 (created by: undefined):can't getOperation for type: fetch, no fetchOperation specified, and no dataSource to create an auto-operation

          com.smartgwt.client.core.JsObject$SGWT_WARN: 15:57:49.847:WARN:ResultSet:isc_ResultSet_0 (created by: undefined):can't getOperation for type: fetch, no fetchOperation specified, and no dataSource to create an auto-operation
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
          at java.lang.reflect.Constructor.newInstance(Unknown Source)
          at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
          at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
          at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
          at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
          at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
          at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
          at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
          at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
          at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
          at com.smartgwt.client.data.ResultSet.create(ResultSet.java)
          at com.smartgwt.client.core.BaseClass.getOrCreateJsObj(BaseClass.java:112)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
          at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
          at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
          at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
          at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
          at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
          at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
          at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
          at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
          at com.smartgwt.client.data.ResultSet.lengthIsKnown(ResultSet.java)
          at com.XXXXXX.testFilterSample(XXXXXX.java:128)
          at com.XXXXXX.start(XXXXXX.java:33)
          at com.XXXXXX.onModuleLoad(XXXXXX.java:13)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)
          at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:193)
          at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:510)
          at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
          at java.lang.Thread.run(Unknown Source)


          Here's the new code. This method is called from onModuleLoad():

          public void testFilterSample() {
          Record[] records = new Record[2];
          records[0] = new Record();
          records[0].setAttribute("name", "Fred");
          records[1] = new Record();
          records[1].setAttribute("name", "Barney");

          ResultSet resultSet = new ResultSet();
          resultSet.setAllRows(records);

          GWT.log("Before: " + resultSet.getLength());
          //assertEquals(2, resultSet.getLength());

          Criterion isFred = new Criterion("name", OperatorId.EQUALS, "Fred");
          resultSet.setCriteria(isFred);

          GWT.log("After: " + resultSet.getLength());
          //assertEquals(1, resultSet.getLength());
          }

          Comment


            #6
            Oops. Here's the correct stack trace for that code:


            16:00:59.125 [ERROR] [com.XXXXXX] 16:00:59.122:WARN:ResultSet:isc_ResultSet_0 (created by: undefined):can't getOperation for type: fetch, no fetchOperation specified, and no dataSource to create an auto-operation

            com.smartgwt.client.core.JsObject$SGWT_WARN: 16:00:59.122:WARN:ResultSet:isc_ResultSet_0 (created by: undefined):can't getOperation for type: fetch, no fetchOperation specified, and no dataSource to create an auto-operation
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
            at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
            at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
            at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
            at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
            at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
            at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
            at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
            at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
            at com.smartgwt.client.data.ResultSet.create(ResultSet.java)
            at com.smartgwt.client.core.BaseClass.getOrCreateJsObj(BaseClass.java:112)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
            at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
            at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
            at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
            at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
            at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
            at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
            at com.google.gwt.dev.shell.ModuleSpace.invokeNativeInt(ModuleSpace.java:245)
            at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeInt(JavaScriptHost.java:75)
            at com.smartgwt.client.data.ResultSet.getLength(ResultSet.java)
            at com.XXXXXX.testFilterSample(ProductionManager.java:123)
            at com.XXXXXX.start(ProductionManager.java:33)
            at com.XXXXXX.onModuleLoad(PMLinker.java:13)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)
            at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:193)
            at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:510)
            at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
            at java.lang.Thread.run(Unknown Source)

            Comment


              #7
              Ah, right. As the error message says, a ResultSet does require a DataSource - in general a DataSource is required to correctly execute criteria because it provides type information for the fields.

              Comment


                #8
                Thank you. That got me on the right track.

                It turns out that in addition to defining a data source with the appropriate columns, I also had to call filterLocalData() after setting the criteria to get it to apply the filter.

                Here's the final code:

                Code:
                    public void testFilterSample() {
                		DataSource ds = new DataSource();
                		ds.setFields(new DataSourceField("name", FieldType.TEXT));
                
                		Record[] records = new Record[2];
                		records[0] = new Record();
                		records[0].setAttribute("name", "Fred");
                		records[1] = new Record();
                		records[1].setAttribute("name", "Barney");
                
                		ResultSet resultSet = new ResultSet(ds);
                		resultSet.setAllRows(records);
                
                		GWT.log("Before: " + resultSet.getLength());
                		// assertEquals(2, resultSet.getLength());
                
                		Criterion isFred = new Criterion("name", OperatorId.EQUALS, "Fred");
                		resultSet.setCriteria(isFred);
                		resultSet.filterLocalData();
                
                		GWT.log("After: " + resultSet.getLength());
                		// assertEquals(1, resultSet.getLength());
                    }

                Comment

                Working...
                X