Commits

Shuji Watanabe  committed 6233bd8

6章のコードを追加

  • Participants
  • Parent commits f1d4df9

Comments (0)

Files changed (15)

File junit-examples/ch06_list.txt

+第6章のリスト一覧
+------------------
+リスト6.1  src/main/java/ch06/ItemStock.java
+リスト6.2  src/test/java/ch06/ItemStockTest.java
+リスト6.3  src/main/java/ch06/UserDao.java
+リスト6.4  src/test/java/ch06/UserDaoTest.java
+リスト6.5  src/test/java/ch06/ArrayListFlatTest.java
+リスト6.6  src/test/java/ch06/ArrayListEnclosedTest.java
+リスト6.7  src/test/java/ch06/UserTest.java
+リスト6.8  src/test/java/ch06/withoutrule/UserDaoTest.java
+リスト6.9  src/test/java/ch06/rule/UserDaoTest.java
+
+
+
+

File junit-examples/src/main/java/ch06/ItemStock.java

+package ch06;
+
+// リスト6.1
+public interface ItemStock {
+    
+    /**
+     * 商品と数量を指定して追加する
+     * @param item 商品名
+     * @param num 追加する数量
+     */
+    void add(String item, int num);
+ 
+    /**
+     * 商品を指定して、商品の在庫数を返す
+     * @param item 商品名
+     * @return 在庫数、登録されていない場合は0
+     */
+    int size(String item);
+    
+    /**
+     * 商品の在庫が残っている場合にtrueを返す
+     * @param item 商品名
+     * @return 在庫が1以上の場合にtrue
+     */
+    boolean contains(String item);
+}

File junit-examples/src/main/java/ch06/ItemStockImpl.java

+package ch06;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ItemStockImpl implements ItemStock {
+    private final Map<String, Integer> list = new HashMap<String, Integer>();
+
+    public void add(String item, int num) {
+        int oldSize = size(item);
+        int newSize = oldSize + num;
+        list.put(item, newSize);
+    }
+
+    public int size(String item) {
+        Integer size = list.get(item);
+        return (size != null) ? size : 0;
+    }
+
+    public boolean contains(String item) {
+        return 0 < size(item);
+    }
+
+}

File junit-examples/src/main/java/ch06/User.java

+package ch06;
+
+public class User {
+
+    private String name = "nobody";
+    private boolean admin = false;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public boolean isAdmin() {
+        return admin;
+    }
+
+    public void setAdmin(boolean admin) {
+        this.admin = admin;
+    }
+
+}

File junit-examples/src/main/java/ch06/UserDao.java

+package ch06;
+
+import java.util.List;
+
+// リスト6.3
+public class UserDao {
+
+    public List<User> getList() {
+        throw new UnsupportedOperationException("未実装");
+    }
+}

File junit-examples/src/test/java/ch06/ArrayListEnclosedTest.java

+package ch06;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+// リスト6.6
+@RunWith(Enclosed.class)
+public class ArrayListEnclosedTest {
+
+    public static class listに1件追加してある場合 {
+        private ArrayList<String> sut;
+
+        @Before
+        public void setUp() throws Exception {
+            sut = new ArrayList<String>();
+            sut.add("A");
+        }
+
+        @Test
+        public void sizeは1を返す() throws Exception {
+            int actual = sut.size();
+            assertThat(actual, is(1));
+        }
+
+    }
+
+    public static class listに2件追加してある場合 {
+        private ArrayList<String> sut;
+
+        @Before
+        public void setUp() throws Exception {
+            sut = new ArrayList<String>();
+            sut.add("A");
+            sut.add("B");
+        }
+
+        @Test
+        public void sizeは2を返す() throws Exception {
+            int actual = sut.size();
+            assertThat(actual, is(2));
+        }
+    }
+}

File junit-examples/src/test/java/ch06/ArrayListFlatTest.java

+package ch06;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+
+import org.junit.Before;
+import org.junit.Test;
+
+// リスト6.5
+public class ArrayListFlatTest {
+
+    private ArrayList<String> sut;
+
+    @Before
+    public void setUp() throws Exception {
+        sut = new ArrayList<String>();
+    }
+
+    @Test
+    public void listに1件追加してある場合_sizeは1を返す() throws Exception {
+        sut.add("A");
+        int actual = sut.size();
+        assertThat(actual, is(1));
+    }
+
+    @Test
+    public void listに2件追加してある場合_sizeは2を返す() throws Exception {
+        sut.add("A");
+        sut.add("B");
+        int actual = sut.size();
+        assertThat(actual, is(2));
+    }
+
+}

File junit-examples/src/test/java/ch06/DbUtils.java

+package ch06;
+
+import java.net.URL;
+import java.sql.SQLException;
+
+/**
+ * データベースを操作するユーティリティクラス(のダミー).
+ * @author shuji.w6e
+ */
+public class DbUtils {
+
+    /**
+     * テーブル名を指定してデータベースからDROPする.
+     * @param tableName DROPするテーブルの名前
+     */
+    public static void drop(String tableName) throws SQLException {
+        // TODO
+    }
+
+    /**
+     * テーブルとデータを示すリソースのURLを指定して、テーブルにINSERTする
+     * @param tableName テーブル名
+     * @param resource データソース
+     */
+    public static void insert(String tableName, URL resource) throws SQLException  {
+        // TODO
+    }
+
+}

File junit-examples/src/test/java/ch06/ItemStockTest.java

+package ch06;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+// リスト6.2
+@RunWith(Enclosed.class)
+public class ItemStockTest {
+
+    public static class 空の場合 {
+        ItemStock sut;
+
+        @Before
+        public void setUp() throws Exception {
+            sut = new ItemStockImpl();
+        }
+
+        @Test
+        public void size_Aが0を返す() throws Exception {
+            assertThat(sut.size("A"), is(0));
+        }
+
+        @Test
+        public void contains_Aはfalseを返す() throws Exception {
+            assertThat(sut.contains("A"), is(false));
+        }
+    }
+
+    public static class 商品Aを1件含む場合 {
+        ItemStock sut;
+
+        @Before
+        public void setUp() throws Exception {
+            sut = new ItemStockImpl();
+            sut.add("A", 1);
+        }
+
+        @Test
+        public void sizeが1を返す() throws Exception {
+            assertThat(sut.size("A"), is(1));
+        }
+
+        @Test
+        public void contains_Aはtrueを返す() throws Exception {
+            assertThat(sut.contains("A"), is(true));
+        }
+    }
+}

File junit-examples/src/test/java/ch06/UserDaoTest.java

+package ch06;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+// リスト6.4
+@RunWith(Enclosed.class)
+public class UserDaoTest {
+
+    public static class テーブルが空の場合 {
+
+        UserDao sut;
+
+        @Before
+        public void setUp() throws Exception {
+            DbUtils.drop("users");
+            sut = new UserDao();
+        }
+
+        @Ignore("未実装")
+        @Test
+        public void getListで0件取得できる() throws Exception {
+            List<User> actual = sut.getList();
+            assertThat(actual, is(notNullValue()));
+            assertThat(actual.size(), is(0));
+        }
+    }
+
+    public static class テーブルにサンプルデータが100件含まれる場合 {
+
+        UserDao sut;
+
+        @Before
+        public void setUp() throws Exception {
+            DbUtils.drop("users");
+            DbUtils.insert("users", getClass().getResource("users.yaml"));
+            sut = new UserDao();
+        }
+        
+        @Ignore("未実装")
+        @Test
+        public void getListで100件取得できる() throws Exception {
+            List<User> actual = sut.getList();
+            assertThat(actual, is(notNullValue()));
+            assertThat(actual.size(), is(100));
+        }
+    }
+}

File junit-examples/src/test/java/ch06/UserTest.java

+package ch06;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+// リスト6.7
+@RunWith(Enclosed.class)
+public class UserTest {
+
+    public static class インスタンス化テスト {
+        @Test
+        public void デフォルトコンストラクタ() throws Exception {
+            User instance = new User();
+            assertThat(instance.getName(), is("nobody"));
+            assertThat(instance.isAdmin(), is(false));
+        }
+    }
+}

File junit-examples/src/test/java/ch06/rule/InMemoryDbRule.java

+package ch06.rule;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import ch06.withoutrule.InMemoryDb;
+
+public class InMemoryDbRule implements TestRule {
+
+    InMemoryDb db = null;
+
+    @Override
+    public Statement apply(final Statement base, Description description) {
+        db = new InMemoryDb();
+        return new Statement() {
+
+            @Override
+            public void evaluate() throws Throwable {
+                db.start();
+                try {
+                    base.evaluate();
+                } finally {
+                    db.shutdownNow();
+                }
+            }
+        };
+    }
+
+}

File junit-examples/src/test/java/ch06/rule/UserDaoTest.java

+package ch06.rule;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import ch06.UserDao;
+
+// リスト6.9
+@SuppressWarnings("unused")
+public class UserDaoTest {
+    private UserDao sut;
+    @Rule
+    public InMemoryDbRule db = new InMemoryDbRule();
+
+    @Before
+    public void setUp() throws Exception {
+        sut = new UserDao();
+    }
+
+    @Test
+    public void getListは0を返す() throws Exception {
+        // 省略
+    }
+
+}

File junit-examples/src/test/java/ch06/withoutrule/InMemoryDb.java

+package ch06.withoutrule;
+
+public class InMemoryDb {
+
+    public void start() {
+    }
+
+    public void shutdownNow() {
+    }
+
+}

File junit-examples/src/test/java/ch06/withoutrule/UserDaoTest.java

+package ch06.withoutrule;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import ch06.UserDao;
+
+// リスト6.8
+@SuppressWarnings("unused")
+public class UserDaoTest {
+    private UserDao sut;
+    private InMemoryDb db;
+
+    @Before
+    public void setUp() throws Exception {
+        db = new InMemoryDb();
+        db.start();
+        sut = new UserDao();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        db.shutdownNow();
+    }
+
+    @Test
+    public void getListは0を返す() throws Exception {
+        // 省略
+    }
+
+}