発売は2009/9なので内容的には若干古いですが(メール受信に関する記述がない...リリースされたのが発売後なので当たり前ですが)、基本はしっかり押さえられていて GAE/J特有の制限について大変勉強になりました。
実際の開発では「Slim3」を使うことになるので直接扱う知識は多くはないけど、ベースをしっかり理解しておかないと何かトラブったときに困ってしまうので、この本でキチンと理解しようと勉強中です。
<c:forEach var="e" items="${tweetList}"> ${f:h(e.content)} <hr /> </c:forEach>
assertThat(tester.requestScope("tweetList"), is(notNullValue()));
ここで一つTips、eclipseに「moreUnit」プラグインがインストールされていれば"Ctrl+J"でテストクラスと対象クラスを行き来できるようになるので切り替えが楽チンです。修正後は下記のようになる。
public class IndexController extends Controller { private TwitterService service = new TwitterService(); @Override public Navigation run() throws Exception { List<Tweet> tweetList = service.getTweetList(); requestScope("tweetList", tweetList); return forward("index.jsp"); } }
@Test public void getTweetList() throws Exception { Tweet tweet = new Tweet(); tweet.setContent("Hello"); Datastore.put(tweet); List<Tweet> tweetList = service.getTweetList(); assertThat(tweetList.size(), is(1)); assertThat(tweetList.get(0).getContent(), is("Hello")); }
private TweetMeta t = new TweetMeta();getTweetListメソッドを次のように修正する
public List<Tweet> getTweetList() { return Datastore.query(t).sort(t.createdDate.desc).asList(); }
public Tweet tweet(Map<String, Object> input) { Tweet tweet = new Tweet(); BeanUtil.copy(input, tweet); Transaction tx = Datastore.beginTransaction(); Datastore.put(tweet); tx.commit(); return tweet; }
@Test public void run() throws Exception { tester.param("content", "Hello"); tester.start("/twitter/tweet"); TweetController controller = tester.getController(); assertThat(controller, is(notNullValue())); assertThat(tester.isRedirect(), is(true)); assertThat(tester.getDestinationPath(), is("/twitter/")); Tweet stored = Datastore.query(Tweet.class).asSingle(); assertThat(stored, is(notNullValue())); assertThat(stored.getContent(), is("Hello")); }
service.tweet(new RequestMap(request));
private String content; private Date createdDate = new Date();
@Test public void tweet() throws Exception { Map<string, object=""> input = new HashMap<string, object="">(); input.put("content", "Hello"); Tweet tweeted = service.tweet(input); assertThat(tweeted, is(notNullValue())); Tweet stored = Datastore.get(Tweet.class, tweeted.getKey()); assertThat(stored.getContent(), is("Hello")); }はい、ここで「あれ?」と気づいた人(^_^)/
public Tweet tweet(Map input) { Tweet tweet = new Tweet(); BeanUtil.copy(input, tweet); Transaction tx = Datastore.beginTransaction(); Datastore.put(tweet); tx.commit(); return tweet; }
<p>What are you doing?</p> <form method="post" action="tweet"> <textarea name="content"></textarea><br /> <input type="submit" value="tweet"/> </form>
assertThat(tester.isRedirect(), is(true));
assertThat(tester.getDestinationPath(), is("/twitter/"));
return redirect(basePath);
<context-param> <param-name>slim3.rootPackage</param-name> <param-value>tutorial</param-value> </context-param>
[引数]: Projects using App Engine 1.2.6 or later require a Java agent. Add this VM argument:というエラーが表示されたので、表示された通りに[引数]タブの"VM引数"に下記のように指定する。
-javaagent:(GAE SDKインストールパス)\com.google.appengine.eclipse.sdkbundle.1.3.1_1.3.1.v201002101412\appengine-java-sdk-1.3.1\lib\agent\appengine-agent.jar
警告: Your working directory, (C:\e\workspace\slim3-blank) is not equal to your web application root (C:\...\workspace\slim3-blank\war)「"working directory"が違ってるぞ」ということなので、「実行の構成」の[引数]タブの作業ディレクトリで"${workspace_loc:slim3-blank/war}"を指定して再度実行してみた。