#眉標=GAE/J、Maven、Eclipse #副標=直上Google App Engine雲端運算平台(2) #大標=架構師如何掌握GAE/J專案? #作者=文/圖 盧建州 ============= 程式1:DemoTest.java package demo.gae.test; public class DemoTest extends BaseGaeTestCase { public void testEmailGetsSent() { ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate(); LocalMailService mailService = (LocalMailService) proxy.getService("mail"); mailService.clearSentMessages(); assertEquals( 0,mailService.getSentMessages().size()); } } ================ ============= 程式2:HelloWorldGwtTest.java package demo.gae.example.helloworld.client; import com.google.gwt.junit.client.GWTTestCase; public class HelloWorldGwtTest extends GWTTestCase { @Override public String getModuleName() { return "demo.gae.example.helloworld.HelloWorld"; } public void testUI1() { HelloWorld app = new HelloWorld(); app.onModuleLoad(); } } ================ ============= 測試報告1 --------------------- Test set: … HelloWorldGwtTest --------------------- Tests run: …, Errors: … : <<< FAILURE! testUI1(….HelloWorldGwtTest) : <<< ERROR! java.lang.NullPointerException at…HelloWorld.onModuleLoad(HelloWorld.java:50) at…testUI1(HelloWorldGwtTest.java:14) ================ ============= 程式3:HelloWorld.java package demo.gae.example.helloworld.client; …中間省略… public class HelloWorld implements EntryPoint { …中間省略… public void onModuleLoad() { // 設定垂直佈局面板 VerticalPanel verticalPanel = new VerticalPanel(); verticalPanel.setHorizontalAlignment( HasHorizontalAlignment.ALIGN_CENTER); verticalPanel.setSize("100%", "50%"); // 設定UI標題與提示訊息 final Label titleLabel = new Label("Web Application Starter Project"); final Label enterMsgLabel = new Label("Please enter your name:"); //設定UI輸入框與送出按鈕 final TextBox nameField = new TextBox(); nameField.setText("GWT User"); nameField.setFocus(true); nameField.selectAll(); final Button sendButton = new Button("Send"); sendButton.addStyleName("sendButton"); //將UI元件加入佈局面板 verticalPanel.add(titleLabel); verticalPanel.add(enterMsgLabel); verticalPanel.add(nameField); verticalPanel.add(sendButton); // 將佈局面板加入至父面板 RootPanel rootPanel = RootPanel.get(); rootPanel.add(verticalPanel, 0, 0); …中間省略-建立彈出式對話框… …中間省略-建立MyHandler事件處理類別… //宣告事件處理者並加入於指定的UI中 MyHandler handler = new MyHandler(); /* 以下的二個新增事件處理者函式,可能在GWT Designer 進行佈局時被移到handler宣告之上造成無法編譯的錯誤。 */ sendButton.addClickHandler(handler); nameField.addKeyUpHandler(handler); } } ================ ============= 參考資源 ●Google整理的開放原始碼相容性清單:http://groups.google.com/group/google-appengine-java/web/will-it-play-in-app-engine pli=1。 ●JRE於GAE可用的白名單:http://code.google.com/intl/zh-TW/appengine/docs/java/jrewhitelist.html。 ================