Created
October 23, 2009 00:34
-
-
Save mraible/216497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.appfuse.gwt.service.client.service; | |
import org.appfuse.gwt.service.client.AbstractGwtTestCase; | |
import org.appfuse.gwt.service.client.event.CollectionLoadedEvent; | |
import org.appfuse.gwt.service.client.event.ResourceDeletedEvent; | |
import org.appfuse.gwt.service.client.event.ResourceLoadedEvent; | |
import org.appfuse.gwt.service.client.event.ResourceSavedEvent; | |
import org.appfuse.gwt.service.client.rest.Representation; | |
import org.appfuse.gwt.service.client.rest.jso.AbstractJSOModel; | |
import org.appfuse.gwt.service.client.rest.jso.JSOModel; | |
import com.google.gwt.user.client.Timer; | |
import net.customware.gwt.presenter.client.DefaultEventBus; | |
import net.customware.gwt.presenter.client.EventBus; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class GenericServiceGwtTest extends AbstractGwtTestCase | |
implements CollectionLoadedEvent.Handler, ResourceLoadedEvent.Handler, | |
ResourceSavedEvent.Handler, ResourceDeletedEvent.Handler { | |
FooService service; | |
ResourceLoadedEvent<Foo> loadedEvent; | |
ResourceSavedEvent<Foo> savedEvent; | |
ResourceDeletedEvent deletedEvent; | |
CollectionLoadedEvent<Foo> collectionLoadedEvent; | |
@Override | |
public void gwtSetUp() throws Exception { | |
super.gwtSetUp(); | |
EventBus eventBus = new DefaultEventBus(); | |
service = new FooServiceImpl(eventBus); | |
eventBus.addHandler(ResourceLoadedEvent.TYPE, this); | |
eventBus.addHandler(ResourceSavedEvent.TYPE, this); | |
eventBus.addHandler(ResourceDeletedEvent.TYPE, this); | |
eventBus.addHandler(CollectionLoadedEvent.TYPE, this); | |
} | |
public void testGetAllFoos() { | |
service.getAll(); | |
Timer t = new Timer() { | |
public void run() { | |
assertNotNull("CollectionLoadedEvent not received", collectionLoadedEvent); | |
List<Foo> foos = new ArrayList<Foo>(collectionLoadedEvent.getResources()); | |
assertEquals(3, foos.size()); | |
assertEquals("light", foos.get(2).getString("name")); | |
finishTest(); | |
} | |
}; | |
delayTestFinish(3000); | |
t.schedule(100); | |
} | |
public void testGetFoo() { | |
service.get("foo"); | |
Timer t = new Timer() { | |
public void run() { | |
assertNotNull("ResourceLoadedEvent not received", loadedEvent); | |
Foo foo = loadedEvent.getResource(); | |
assertEquals("bar", foo.getString("foo")); | |
convertToAndFromJson(foo); | |
finishTest(); | |
} | |
}; | |
delayTestFinish(3000); | |
t.schedule(100); | |
} | |
public void testSaveFoo() { | |
Foo foo = new Foo().setName("Test"); | |
assertTrue(foo.toJson().toString().contains("Test")); | |
service.save(foo); | |
Timer t = new Timer() { | |
public void run() { | |
assertNotNull("ResourceSavedEvent not received", savedEvent); | |
finishTest(); | |
} | |
}; | |
delayTestFinish(3000); | |
t.schedule(100); | |
} | |
public void testDeleteFoo() { | |
service.delete("1"); | |
Timer t = new Timer() { | |
public void run() { | |
assertNotNull("ResourceDeletedEvent not received", deletedEvent); | |
finishTest(); | |
} | |
}; | |
delayTestFinish(3000); | |
t.schedule(100); | |
} | |
private void convertToAndFromJson(Foo fromJsonModel) { | |
Representation json = fromJsonModel.toJson(); | |
assertNotNull("Cannot convert empty JSON", json.getData()); | |
System.out.println("foo.toJson(): " + json); | |
// change back into model | |
JSOModel data = JSOModel.fromJson(json.getData()); | |
Foo toJsonModel = new Foo(data); | |
System.out.println("foo.fromJson(): " + toJsonModel); | |
verifyModelBuiltCorrectly(toJsonModel); | |
} | |
private void verifyModelBuiltCorrectly(Foo model) { | |
assertEquals("Foo is incorrect", "bar", model.getString("foo")); | |
} | |
@SuppressWarnings("unchecked") | |
public void onLoad(ResourceLoadedEvent event) { | |
this.loadedEvent = event; | |
} | |
@SuppressWarnings("unchecked") | |
public void onLoad(CollectionLoadedEvent event) { | |
this.collectionLoadedEvent = event; | |
} | |
@SuppressWarnings("unchecked") | |
public void onSave(ResourceSavedEvent event) { | |
this.savedEvent = event; | |
} | |
public void onDelete(ResourceDeletedEvent event) { | |
this.deletedEvent = event; | |
} | |
/** | |
* Test class to verify GenericService works as expected | |
*/ | |
class Foo extends AbstractJSOModel { | |
public Foo(JSOModel model) { | |
super(model); | |
} | |
public Foo() { | |
super(); | |
} | |
public Foo setName(String name) { | |
data.set("name", name); | |
return this; | |
} | |
} | |
interface FooService extends GenericService<Foo, String> { | |
} | |
class FooServiceImpl extends GenericServiceImpl<Foo, String> implements FooService { | |
public FooServiceImpl(EventBus eventBus) { | |
super(eventBus, "/services/foo"); | |
} | |
@Override | |
protected Foo convertResultToModel(JSOModel model) { | |
return new Foo(model); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment