Created
April 8, 2012 02:11
-
-
Save mike-neck/2333740 to your computer and use it in GitHub Desktop.
@ruleアノテーションを利用したAppengineTestCase
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.mikeneck.gae.slim3.sample.util; | |
import org.junit.rules.ExternalResource; | |
import org.slim3.tester.AppEngineTester; | |
/** | |
* @author : mike | |
* @since : 12/04/08 | |
*/ | |
public class AppengineExternalResource extends ExternalResource { | |
private AppEngineTester tester = new AppEngineTester(); | |
@Override | |
protected void after() throws RuntimeException{ | |
try { | |
tester.tearDown(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
protected void before() throws Throwable { | |
tester.setUp(); | |
} | |
} |
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.junit.rules; | |
import org.junit.runner.Description; | |
import org.junit.runners.model.Statement; | |
/** | |
* A base class for Rules (like TemporaryFolder) that set up an external | |
* resource before a test (a file, socket, server, database connection, etc.), | |
* and guarantee to tear it down afterward: | |
* | |
* <pre> | |
* public static class UsesExternalResource { | |
* Server myServer= new Server(); | |
* | |
* @Rule | |
* public ExternalResource resource= new ExternalResource() { | |
* @Override | |
* protected void before() throws Throwable { | |
* myServer.connect(); | |
* }; | |
* | |
* @Override | |
* protected void after() { | |
* myServer.disconnect(); | |
* }; | |
* }; | |
* | |
* @Test | |
* public void testFoo() { | |
* new Client().run(myServer); | |
* } | |
* } | |
* </pre> | |
*/ | |
public abstract class ExternalResource implements TestRule { | |
public Statement apply(Statement base, Description description) { | |
return statement(base); | |
} | |
private Statement statement(final Statement base) { | |
return new Statement() { | |
@Override | |
public void evaluate() throws Throwable { | |
before(); | |
try { | |
base.evaluate(); | |
} finally { | |
after(); | |
} | |
} | |
}; | |
} | |
/** | |
* Override to set up your specific external resource. | |
* @throws if setup fails (which will disable {@code after} | |
*/ | |
protected void before() throws Throwable { | |
// do nothing | |
} | |
/** | |
* Override to tear down your specific external resource. | |
*/ | |
protected void after() { | |
// do nothing | |
} | |
} |
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.mikeneck.gae.slim3.sample; | |
import com.google.appengine.api.datastore.Key; | |
import org.slim3.datastore.Attribute; | |
import org.slim3.datastore.Model; | |
import java.util.Date; | |
/** | |
* @author : mike | |
* @since : 12/04/08 | |
*/ | |
@Model (schemaVersion = 1) | |
public class Message { | |
@Attribute(primaryKey = true) | |
Key key; | |
@Attribute | |
String message; | |
@Attribute | |
Date createdAt; | |
@Attribute | |
Date expiredAt; | |
public Date getCreatedAt() { | |
return createdAt; | |
} | |
public void setCreatedAt(Date createdAt) { | |
this.createdAt = createdAt; | |
} | |
public Date getExpiredAt() { | |
return expiredAt; | |
} | |
public void setExpiredAt(Date expiredAt) { | |
this.expiredAt = expiredAt; | |
} | |
public Key getKey() { | |
return key; | |
} | |
public void setKey(Key key) { | |
this.key = key; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
} |
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.mikeneck.gae.slim3.sample; | |
import com.google.appengine.api.datastore.Key; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.ExternalResource; | |
import org.mikeneck.gae.slim3.sample.util.AppengineExternalResource; | |
import org.slim3.datastore.Datastore; | |
import java.util.Date; | |
import java.util.List; | |
import static org.hamcrest.Matchers.is; | |
import static org.junit.Assert.assertThat; | |
/** | |
* @author : mike | |
* @since : 12/04/08 | |
*/ | |
public class MessageTest { | |
@Rule | |
public static ExternalResource resource = new AppengineExternalResource(); | |
private static final MessageMeta meta = MessageMeta.get(); | |
@Test | |
public void countData () { | |
Message message = new Message(); | |
message.setCreatedAt(new Date()); | |
message.setMessage("test"); | |
Key key = Datastore.put(message); | |
Message stored = Datastore.get(meta, key); | |
assertThat(stored.getMessage(), is("test")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment