Commits

Shuji Watanabe committed be4598c

7章のコードを追加

Comments (0)

Files changed (19)

junit-examples/.classpath

 <classpath>
 	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
 	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry kind="src" path="src/test/resources"/>
 	<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.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
 	<classpathentry kind="output" path="target/classes"/>

junit-examples/ch07_list.txt

+第6章のリスト一覧
+------------------
+リスト7.1  src/test/java/ch07/CalculatorTest.java
+リスト7.2  src/test/java/ch07/BookStoreComplexSetupTest.java
+リスト7.3  src/test/java/ch07/StringUtilTest.java
+リスト7.4  src/test/java/ch07/LruCacheTest.java
+リスト7.5  src/test/java/ch07/BookStoreTest.java
+リスト7.6  src/test/java/ch07/BookStoreTestHelper.java
+リスト7.7  src/test/resources/ch07/book_fixtures.yaml
+リスト7.8  src/test/java/ch07/BookStoreYamlTest.java
+リスト7.A  src/test/java/ch07/BookStoreDeclarativeTestHelper.java
+リスト7.B  src/test/groovy/ch07/BookStoreGroovyTestHelper.groovy
+
+
+
+
+

junit-examples/src/main/java/ch07/Author.java

+package ch07;
+
+public class Author {
+    String firstName;
+    String lastName;
+
+    public String getFirstName() {
+        return firstName;
+    }
+
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+
+    public String getLastName() {
+        return lastName;
+    }
+
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
+        result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) return true;
+        if (obj == null) return false;
+        if (getClass() != obj.getClass()) return false;
+        Author other = (Author) obj;
+        if (firstName == null) {
+            if (other.firstName != null) return false;
+        } else if (!firstName.equals(other.firstName)) return false;
+        if (lastName == null) {
+            if (other.lastName != null) return false;
+        } else if (!lastName.equals(other.lastName)) return false;
+        return true;
+    }
+
+}

junit-examples/src/main/java/ch07/Book.java

+package ch07;
+
+public class Book {
+
+    String title;
+    int price;
+    Author author;
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public int getPrice() {
+        return price;
+    }
+
+    public void setPrice(int price) {
+        this.price = price;
+    }
+
+    public Author getAuthor() {
+        return author;
+    }
+
+    public void setAuthor(Author author) {
+        this.author = author;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((author == null) ? 0 : author.hashCode());
+        result = prime * result + price;
+        result = prime * result + ((title == null) ? 0 : title.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) return true;
+        if (obj == null) return false;
+        if (getClass() != obj.getClass()) return false;
+        Book other = (Book) obj;
+        if (author == null) {
+            if (other.author != null) return false;
+        } else if (!author.equals(other.author)) return false;
+        if (price != other.price) return false;
+        if (title == null) {
+            if (other.title != null) return false;
+        } else if (!title.equals(other.title)) return false;
+        return true;
+    }
+
+}

junit-examples/src/main/java/ch07/BookStore.java

+package ch07;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class BookStore {
+
+    List<Book> cart = new ArrayList<Book>();
+
+    public void addToCart(Book book, int num) {
+        for (int i = 0; i < num; i++) {
+            cart.add(book);
+        }
+    }
+
+    public int getTotalPrice() {
+        int result = 0;
+        for (Book book : cart) {
+            result += book.getPrice();
+        }
+        return result;
+    }
+
+    public Book get(int idx) {
+        return cart.get(idx);
+    }
+}

junit-examples/src/main/java/ch07/Calculator.java

+package ch07;
+
+public class Calculator {
+
+    public int multiply(int i, int j) {
+        return i * j;
+    }
+
+}

junit-examples/src/main/java/ch07/LruCache.java

+package ch07;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+public class LruCache<E> {
+
+    final ArrayList<String> keys = new ArrayList<String>();
+    private final Map<String, E> entries = new HashMap<String, E>();
+
+    public void put(String key, E item) {
+        entries.put(key, item);
+        keys.remove(key);
+        keys.add(key);
+    }
+
+    public E get(String key) {
+        if (entries.containsKey(key)) {
+            keys.remove(key);
+            keys.add(key);
+        }
+        return entries.get(key);
+    }
+
+    public int size() {
+        return entries.size();
+    }
+}

junit-examples/src/main/java/ch07/StringUtil.java

+package ch07;
+
+public class StringUtil {
+
+    public static boolean isEmptyOrNull(String text) {
+        return text == null || text.isEmpty();
+    }
+
+}

junit-examples/src/test/groovy/ch07/BookStoreGroovyTestHelper.groovy

+package ch07
+
+// リスト7.B
+class BookStoreGroovyTestHelper {
+
+    static Book Bookオブジェクトの作成_MartinFowlerのRefactoring() {
+        new Book(
+                title: "Refactoring",
+                price: 4500,
+                author: new Author(
+                    firstName: "Martin",
+                    lastName: "Fowler",
+                ),
+        )
+    }
+}

junit-examples/src/test/java/ch07/BookStoreComplexSetupTest.java

+package ch07;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+// リスト7.2
+public class BookStoreComplexSetupTest {
+
+    @Test
+    public void getTotalPrice() throws Exception {
+        // SetUp
+        Book book = new Book();
+        book.setTitle("Refactoring");
+        book.setPrice(4500);
+        Author author = new Author();
+        author.setFirstName("Martin");
+        author.setLastName("Fowler");
+        book.setAuthor(author);
+        BookStore sut = new BookStore();
+        sut.addToCart(book, 1);
+        // Exercise & Verify
+        assertThat(sut.getTotalPrice(), is(4500));
+    }
+}

junit-examples/src/test/java/ch07/BookStoreDeclarativeTestHelper.java

+package ch07;
+
+// リスト7.A
+public class BookStoreDeclarativeTestHelper {
+
+    public static Book Bookオブジェクトの作成_MartinFowlerのRefactoring() {
+        return new Book() {
+            {
+                title = "Refactoring";
+                price = 4500;
+                author = new Author() {
+                    {
+                        firstName = "Martin";
+                        lastName = "Fowler";
+                    }
+                };
+            }
+        };
+    }
+
+}

junit-examples/src/test/java/ch07/BookStoreGroovyTestHelper.groovy

+package ch07
+
+class BookStoreGroovyTestHelper {
+
+    static Book Bookオブジェクトの作成_KentBeckのTestDrivenDevelopment() {
+        new Book(
+                title: "Test Driven Development",
+                price: 4500,
+                author: new Author(
+                    firstName: "Beck",
+                    lastName: "Kent",
+                ),
+        )
+    }
+}

junit-examples/src/test/java/ch07/BookStoreTest.java

+package ch07;
+
+import static ch07.BookStoreTestHelper.*;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+// リスト7.5
+public class BookStoreTest {
+    
+    @Test
+    public void getTotalPrice() throws Exception {
+        // SetUp
+        BookStore sut = new BookStore();
+        Book book = Bookオブジェクトの作成_MartinFowlerのRefactoring();
+        sut.addToCart(book, 1);
+        // Exercise & Verify
+        assertThat(sut.getTotalPrice(), is(4500));
+    }
+
+    @Test
+    public void get_0() throws Exception {
+        // SetUp
+        BookStore sut = new BookStore();
+        Book book = Bookオブジェクトの作成_MartinFowlerのRefactoring();
+        sut.addToCart(book, 1);
+        // Exercise & Verify
+        assertThat(sut.get(0), is(sameInstance(book)));
+    }
+}

junit-examples/src/test/java/ch07/BookStoreTestHelper.java

+package ch07;
+
+// リスト7.6
+public class BookStoreTestHelper {
+
+    public static Book Bookオブジェクトの作成_MartinFowlerのRefactoring() {
+        Book book = new Book();
+        book.setTitle("Refactoring");
+        book.setPrice(4500);
+        Author author = new Author();
+        author.setFirstName("Martin");
+        author.setLastName("Fowler");
+        book.setAuthor(author);
+        return book;
+    }
+
+}

junit-examples/src/test/java/ch07/BookStoreYamlTest.java

+package ch07;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.yaml.snakeyaml.Yaml;
+
+// リスト7.8
+public class BookStoreYamlTest {
+
+    @Test
+    public void getTotalPrice() throws Exception {
+        // SetUp
+        BookStore sut = new BookStore();
+        Book book = (Book) new Yaml().load(getClass().getResourceAsStream("book_fixtures.yaml"));
+        sut.addToCart(book, 1);
+        // Exercise & Verify
+        assertThat(sut.getTotalPrice(), is(4500));
+    }
+
+    @Test
+    public void get_0() throws Exception {
+        // SetUp
+        BookStore sut = new BookStore();
+        Book book = (Book) new Yaml().load(getClass().getResourceAsStream("book_fixtures.yaml"));
+        sut.addToCart(book, 1);
+        // Exercise & Verify
+        assertThat(sut.get(0), is(sameInstance(book)));
+    }
+}

junit-examples/src/test/java/ch07/CalculatorTest.java

+package ch07;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+// リスト7.1
+public class CalculatorTest {
+
+    @Test
+    public void multiplyで3と4の乗算結果が取得できること() throws Exception {
+        Calculator calc = new Calculator();
+        int expected = 12;
+        int actual = calc.multiply(3, 4);
+        assertThat(actual, is(expected));
+    }
+}

junit-examples/src/test/java/ch07/LruCacheTest.java

+package ch07;
+
+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;
+
+// リスト7.4
+@RunWith(Enclosed.class)
+public class LruCacheTest {
+
+    public static class AとBを追加している場合 {
+        LruCache<String> sut;
+
+        @Before
+        public void setUp() throws Exception {
+            sut = new LruCache<String>();
+            sut.put("A", "valueA");
+            sut.put("B", "valueB");
+        }
+
+        @Test
+        public void sizeは2() throws Exception {
+            assertThat(sut.size(), is(2));
+        }
+
+        @Test
+        public void get_AでvalueAが取得されkeysはBA() throws Exception {
+            assertThat(sut.get("A"), is("valueA"));
+            assertThat(sut.keys.size(), is(2));
+            assertThat(sut.keys.get(0), is("B"));
+            assertThat(sut.keys.get(1), is("A"));
+        }
+
+        @Test
+        public void get_BでvalueBが取得されkeysはAB() throws Exception {
+            assertThat(sut.get("B"), is("valueB"));
+            assertThat(sut.keys.size(), is(2));
+            assertThat(sut.keys.get(0), is("A"));
+            assertThat(sut.keys.get(1), is("B"));
+        }
+
+        @Test
+        public void get_Cでnullが取得されkeysはAB() throws Exception {
+            assertThat(sut.get("C"), is(nullValue()));
+            assertThat(sut.keys.size(), is(2));
+            assertThat(sut.keys.get(0), is("A"));
+            assertThat(sut.keys.get(1), is("B"));
+        }
+
+        @Test
+        public void put_Cでsizeは3_keysはABCとなる() throws Exception {
+            // Set up
+            String key = "C";
+            String item = "valueC";
+            // Exercise
+            sut.put(key, item);
+            // verify
+            assertThat(sut.size(), is(3));
+            assertThat(sut.keys.size(), is(3));
+            assertThat(sut.keys.get(0), is("A"));
+            assertThat(sut.keys.get(1), is("B"));
+            assertThat(sut.keys.get(2), is("C"));
+        }
+    }
+}

junit-examples/src/test/java/ch07/StringUtilTest.java

+package ch07;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+// リスト7.3
+public class StringUtilTest {
+
+    @Test
+    public void isEmptyOrNullは空文字列でtrueを返す() throws Exception {
+        // SetUp
+        String input = "";
+        boolean expected = true;
+        // Exercise
+        boolean actual = StringUtil.isEmptyOrNull(input);
+        // Verify
+        assertThat(actual, is(expected));
+    }
+
+    @Test
+    public void isEmptyOrNullはAAAでfalseを返す() throws Exception {
+        // SetUp
+        String input = "AAA";
+        boolean expected = false;
+        // Exercise
+        boolean actual = StringUtil.isEmptyOrNull(input);
+        // Verify
+        assertThat(actual, is(expected));
+    }
+
+}

junit-examples/src/test/resources/ch07/book_fixtures.yaml

+!!ch07.Book
+title: Refactoring
+price: 4500
+author: !!ch07.Author
+  firstName: Martin
+  lastName: Fowler