Commits

Shuji Watanabe committed ac5e1c1

13章のコードを追加

  • Participants
  • Parent commits acca6bf

Comments (0)

Files changed (11)

File hello-model/.classpath

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="test"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+	<classpathentry kind="lib" path="lib/mockito-all-1.9.0.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

File hello-model/.project

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>hello-model</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

File hello-model/.settings/org.eclipse.core.resources.prefs

+#Wed Jul 04 20:21:49 JST 2012
+eclipse.preferences.version=1
+encoding/<project>=utf-8

File hello-model/.settings/org.eclipse.jdt.core.prefs

+#Wed Jul 04 20:21:37 JST 2012
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

File hello-model/lib/mockito-all-1.9.0.jar

Binary file added.

File hello-model/src/examples/helloandroid/AuthService.java

+package examples.helloandroid;
+
+import java.io.IOException;
+
+/**
+ * リスト13.4 AuthServiceインタフェース
+ * @author shuji.w6e
+ */
+public interface AuthService {
+    
+    boolean login(AuthUser authUser) throws IOException;
+}

File hello-model/src/examples/helloandroid/AuthServiceImpl.java

+package examples.helloandroid;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * リスト13.10 ユーザ認証の実装クラス
+ * @author shuji.w6e
+ */
+public class AuthServiceImpl implements AuthService {
+
+    private static final String URL = "http://localhost/api/login";
+    // TODO 仕様確定後に実装
+    JsonHttpClient httpClient = null;
+
+    @Override
+    public boolean login(AuthUser authUser) throws IOException {
+        if (authUser == null) throw new IllegalArgumentException();
+        String json = httpClient.sendRequest(URL, toParams(authUser));
+        return json.toLowerCase().contains("success");
+    }
+
+    private Map<String, String> toParams(AuthUser authUser) {
+        Map<String, String> params = new HashMap<String, String>();
+        params.put("userId", authUser.userId);
+        params.put("password", authUser.password);
+        return params;
+    }
+
+}

File hello-model/src/examples/helloandroid/AuthUser.java

+package examples.helloandroid;
+
+/**
+ * リスト13.5 AuthUserクラス
+ * @author shuji.w6e
+ */
+public class AuthUser {
+
+    public final String userId;
+    public final String password;
+
+    public AuthUser(String userId, String password) {
+        if (userId == null || userId.length() < 4) {
+            throw new IllegalArgumentException("userId: " + userId); 
+        }
+        if (password == null || password.length() < 4) {
+            throw new IllegalArgumentException("password:" + password); 
+        }
+        this.userId = userId;
+        this.password = password;
+    }
+}

File hello-model/src/examples/helloandroid/JsonHttpClient.java

+package examples.helloandroid;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * リスト13.9 外部サービスを定義したインタフェース
+ * @author shuji.w6e
+ */
+public interface JsonHttpClient {
+
+    String sendRequest(String url, Map<String, String> params) throws IOException;
+}

File hello-model/test/examples/helloandroid/AuthServiceImplTest.java

+package examples.helloandroid;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * リスト13.11 AuthServiceImplのユニットテスト
+ * @author shuji.w6e
+ */
+public class AuthServiceImplTest {
+
+    AuthServiceImpl sut;
+    JsonHttpClient httpClient;
+    AuthUser authUser;
+    Map<String, String> params;
+
+    @Before
+    public void setup() throws Exception {
+        sut = new AuthServiceImpl();
+        httpClient = mock(JsonHttpClient.class);
+        sut.httpClient = httpClient;
+        authUser = new AuthUser("u000001", "123456");
+        params = new HashMap<String, String>();
+        params.put("userId", "u000001");
+        params.put("password", "123456");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void authUserがnullの時_例外() throws Exception {
+        sut.login(null);
+    }
+
+    @Test
+    public void httpClientがsucessを含むJSONを返す時にtrueを返すこと() throws Exception {
+        when(httpClient.sendRequest("http://localhost/api/login", params))
+                       .thenReturn("{ result: 'success'}");
+        assertThat(sut.login(authUser), is(true));
+    }
+
+    @Test
+    public void httpClientがfailを含むJSONを返す時にfalseを返すこと() throws Exception {
+        when(httpClient.sendRequest("http://localhost/api/login", params))
+                       .thenReturn("{ result: 'fail'}");
+        assertThat(sut.login(authUser), is(false));
+    }
+
+    @Test
+    public void httpClientがIOExceptionを送出する時にfalseを返すこと() throws Exception {
+        when(httpClient.sendRequest("http://localhost/api/login", params))
+                       .thenThrow(new IOException());
+        assertThat(sut.login(authUser), is(false));
+    }
+}

File hello-model/test/examples/helloandroid/AuthUserTest.java

+package examples.helloandroid;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * リスト13.8 AuthUserクラスのテスト
+ * @author shuji.w6e
+ */
+public class AuthUserTest {
+
+    @Test
+    public void userIdとpasswordが4文字以上の時_正しく設定されていること() throws Exception {
+        String userId = "userId";
+        String password = "password";
+        AuthUser instance = new AuthUser(userId, password);
+        assertThat(instance.userId, is(userId));
+        assertThat(instance.password, is(password));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void userIdがnullの場合に例外が発生すること() {
+        new AuthUser(null, "password");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void userIdが3文字の場合に例外が発生すること() {
+        new AuthUser("bad", "password");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void passwordがnullの場合に例外が発生すること() {
+        new AuthUser("userId", null);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void passwordが3文字の場合に例外が発生すること() {
+        new AuthUser("userId", "bad");
+    }
+}