surinder-insonix / datanucleus-appengine

Automatically exported from code.google.com/p/datanucleus-appengine
0 stars 0 forks source link

GWT/GAE/JPA / test #230

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I am developing a GWT / GAE / JPA / maven application.
When testing the data layer directly. All is well.

package com.avpanasovich;

import com.google.appengine.api.datastore.dev.LocalDatastoreService;
import com.google.appengine.tools.development.ApiProxyLocal;

import com.google.apphosting.api.ApiProxy;

public class LocalDatastoreTestCase extends LocalServiceTestCase {

   @Override
   public void setUp() throws Exception {
       super.setUp();
       ApiProxyLocal proxy = (ApiProxyLocal) ApiProxy.getDelegate();
       proxy.setProperty(LocalDatastoreService.NO_STORAGE_PROPERTY,
Boolean.TRUE.toString());
   }

   @Override
   public void tearDown() throws Exception {
       ApiProxyLocal proxy = (ApiProxyLocal) ApiProxy.getDelegate();
       LocalDatastoreService datastoreService =
(LocalDatastoreService) proxy.getService("datastore_v3");
       datastoreService.clearProfiles();
       super.tearDown();
   }

}

package com.avpanasovich;
import com.avpanasovich.model.ItemType;
import com.avpanasovich.server.ItemTypeRepository;
import java.util.Collection;
import org.apache.log4j.Logger;
import org.junit.Ignore;
import org.junit.Test;

public class ContactInfoDAOTestCase extends LocalDatastoreTestCase {
   private Logger
logger=Logger.getLogger(ContactInfoDAOTestCase.class);

 @Test
   public void testgetAllItemType() {
 ItemType itemType=new ItemType();
     itemType.setNameItemType("gggg");
ItemTypeRepository itemTypeRepository=new ItemTypeRepository();
Long iLong=itemTypeRepository.create(itemType);
assertNotNull(iLong);
       Collection<ItemType> itemtypes = itemTypeRepository.getAll();
       assertNotNull(itemtypes);
       logger.info(itemtypes);
   }
}

But when testing asynchronous calls in GWT are having problems.

package com.avpanasovich.client;
import com.avpanasovich.model.ItemType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.Timer;
import org.junit.Test;
/**
 *
 * @author Panason
 */
public class GwtTestMyFirstTestCase extends GWTTestCase {
  ItemTypeServiceAsync itemTypeServiceAsync ;
 AsyncCallback<Long> callback;
       @Override
   public String getModuleName() {
       return "com.avpanasovich.itemManager";
     //      return null;
   }
   @Override
 public void gwtSetUp() {
        itemTypeServiceAsync = GWT.create(ItemTypeService.class);
       callback = new AsyncCallback<Long>() {
        @Override
        public void onFailure(Throwable caught) {
             GWT.log(caught.getMessage());

        }
        @Override
        public void onSuccess(Long result) {
               GWT.log("###############"+result);
        }
    };
  }
@Test
   public void testSomething() {
       String moduleBaseURL = GWT.getModuleBaseURL();
 Timer timer = new Timer() {
  public void run()
  {
      ItemType itemType=new ItemType();
      itemType.setNameItemType("dcccccccccccc");
  itemTypeServiceAsync.create(itemType, callback);
   finishTest();
  }
 };
 timer.schedule(200);
 delayTestFinish(10000);
   }
}

ItemManager.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.2.0//
EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.2.0/distro-
source/core/src/gwt-module.dtd">
<module rename-to="itemManager">
 <inherits name="com.google.gwt.user.User"/>
 <inherits name="com.google.gwt.user.theme.standard.Standard"/>
   <inherits name="com.google.gwt.junit.JUnit"/>
   <source path="client"/>
 <source path="model"/>
 <servlet class="com.avpanasovich.web.ItemTypeServiceImpl" path="/
itemType"/>
 <entry-point class="com.avpanasovich.client.ItemManager"/>
</module>

Получаю следущее

logging for HtmlUnit thread
  [WARN] Expected content type of 'application/javascript' or
'application/ecmascript' for remotely loaded JavaScript element at
'http://192.168.241.1:53571/com.avpanasovich.itemManager.JUnit/
com.avpanasovich.itemManager.JUnit.nocache.js', but got 'application/x-
javascript'.
Starting Jetty on port 0
  [WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method
'public abstract java.lang.Long
com.avpanasovich.model.ItemTypes.create(com.avpanasovich.model.ItemType)'
threw an unexpected exception: java.lang.NullPointerException: No API
environment is registered for this thread.
       at
com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:
385)
       at
com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:
588)
       at
com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceSer
vlet.java:
208)
       at
com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceSer
vlet.java:
248)
       at
com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemot
eServiceServlet.java:
62)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
       at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:
487)
       at
org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:
362)
       at
org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:
216)
       at
org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:
181)
       at
org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:
729)
       at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:
405)
       at
org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:
152)
       at
org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:
49)
       at
org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:
152)
       at org.mortbay.jetty.Server.handle(Server.java:324)
       at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:
505)
       at org.mortbay.jetty.HttpConnection
$RequestHandler.content(HttpConnection.java:843)
       at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
       at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
       at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
       at
org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:
395)
       at org.mortbay.thread.QueuedThreadPool
$PoolThread.run(QueuedThreadPool.java:488)
Caused by: java.lang.NullPointerException: No API environment is
registered for this thread.
       at
com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreA
piHelper.java:
108)
       at
com.google.appengine.api.datastore.BaseDatastoreServiceImpl.beginTransactionInte
rnal(BaseDatastoreServiceImpl.java:
121)
       at
com.google.appengine.api.datastore.DatastoreServiceImpl.beginTransaction(Datasto
reServiceImpl.java:
222)
       at
org.datanucleus.store.appengine.RuntimeExceptionWrappingDatastoreService.beginTr
ansaction(RuntimeExceptionWrappingDatastoreService.java:
210)
       at
org.datanucleus.store.appengine.jpa.DatastoreEntityTransactionImpl.begin(Datasto
reEntityTransactionImpl.java:
49)
       at
com.avpanasovich.server.ItemTypeRepository.create(ItemTypeRepository.java:
26)
       at
com.avpanasovich.web.ItemTypeServiceImpl.create(ItemTypeServiceImpl.java:
32)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
       at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:
25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at
com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:
569)
       ... 22 more
[ERROR] 500 - POST /com.avpanasovich.itemManager.JUnit/itemType
(192.168.241.1) 57 bytes

Original issue reported on code.google.com by panason2...@gmail.com on 28 Apr 2011 at 4:54

GoogleCodeExporter commented 8 years ago
Likely not the place to register this problem (i.e more likely GWT, or 
AppEngine libs). Nothing that can be addressed in this plugin without a 
selfcontained test for just this plugin

Original comment by googleco...@yahoo.co.uk on 27 Sep 2011 at 6:44

GoogleCodeExporter commented 8 years ago
First, I strongly encourage you to use LocalDatastoreServiceTestConfig to 
manage the behavior of the local datastore service rather than the way you're 
doing it.  Example here:
http://code.google.com/appengine/docs/java/tools/localunittesting.html

Second, the reason you're getting this exception is that the App Engine 
environment needs to be configured per-thread.  You are configuring it for the 
thread that is executing your test's setUp() method but then your async 
processing is running on a different thread.  This thread doesn't have the App 
Engine environment set up.

In order for this test to work you'll need to find a way to configure the App 
Engine environment on every thread that is doing work.

Original comment by max.r...@gmail.com on 3 Oct 2011 at 4:48