Commits

Shuji Watanabe committed 65da9d7

8章のコードを追加

  • Participants
  • Parent commits be4598c

Comments (0)

Files changed (21)

File junit-examples/ch08_list.txt

+第6章のリスト一覧
+------------------
+リスト8.1   src/main/java/ch08/Members.java
+リスト8.2   src/main/java/ch08/Members.java
+リスト8.3   src/test/java/ch08/JankenTest.java
+リスト8.4   src/main/java/ch08/Janken.java
+リスト8.5   (なし)
+リスト8.6   (なし)
+リスト8.7   (なし)
+リスト8.8   src/test/java/ch08/ParameterizedTest.java
+リスト8.9   src/test/java/ch08/ParameterizedTypeTest.java
+リスト8.10  src/test/java/ch08/EnclosedParameterizedTypeTest.java
+リスト8.11  src/test/java/ch08/ParameterizedMultiParamsTest.java
+リスト8.12  src/test/java/ch08/ParameterizedMultiSameTypeParamsTest.java
+リスト8.13  src/test/java/ch08/CalculatorDataPointTest.java
+リスト8.14  src/test/java/ch08/CalculatorDataPointsTest.java
+リスト8.15  src/test/java/ch08/CalculatorDataPointsYamlTest.java
+リスト8.16  src/test/resources/ch08/params.yaml
+リスト8.17  src/test/java/ch08/MemberCombinedTest.java
+リスト8.18  src/test/java/ch08/AssmeTest.java
+リスト8.19  src/test/java/ch08/MemberTest.java
+リスト8.20  src/test/java/ch08/WindowsOnlyTest.java
+リスト8.21  src/test/java/ch08/MemberTest.java
+リスト8.A   src/main/java/ch08/Members.java
+リスト8.B   src/main/java/ch08/Age.java

File junit-examples/src/main/java/ch08/Age.java

+package ch08;
+
+// リスト8.B
+public class Age {
+    public final int value;
+
+    public Age(int value) {
+        if (value < 0 || 150 <= value) throw new IllegalArgumentException();
+        this.value = value;
+    }
+
+}

File junit-examples/src/main/java/ch08/Calculator.java

+package ch08;
+
+public class Calculator {
+    
+    public int add(int x, int y) {
+        return x + y;
+    }
+}

File junit-examples/src/main/java/ch08/Gender.java

+package ch08;
+
+public enum Gender {
+    MALE, FEMALE
+}

File junit-examples/src/main/java/ch08/Janken.java

+package ch08;
+
+// リスト8.4
+public class Janken {
+
+    public enum Hand {
+        GU, TYOKI, PA
+    }
+
+    public int judge(Hand h1, Hand h2) {
+        if (h1 == h2) return 0;
+        switch (h1) {
+        case GU:
+            return h2 == Hand.TYOKI ? 1 : 2;
+        case PA:
+            return h2 == Hand.GU ? 1 : 2;
+        default:
+            return h2 == Hand.PA ? 1 : 2;
+        }
+    }
+
+}

File junit-examples/src/main/java/ch08/Member.java

+package ch08;
+
+public class Member {
+
+    public static boolean canEntry(int age, Gender gender) {
+        if (gender == Gender.MALE) return false; 
+        if (25 < age) return false;
+        return true;
+    }
+
+}

File junit-examples/src/main/java/ch08/Members.java

+package ch08;
+
+// リスト8.1
+// リスト8.2
+public class Members {
+
+    /**
+     * 年齢を指定して、会員制サイトへ登録できるかを返す.
+     * @param age 年齢
+     * @return 18歳以上ならばtrueを、18歳未満ならばfalse
+     */
+    public static boolean canRegister(int age) {
+        return 18 <= age;
+    }
+
+    /**
+     * 優待会員かどうかを返す.
+     * @param age 年齢
+     * @param isRegisterMailMagazine メールマガジンに登録している場合にtrue
+     * @param usePastMonth 前月の利用回数
+     * @return 20歳以上であり、メールマガジンに登録してあり、かつ前月の利用回数が1回以上ならtrue
+     */
+    public static boolean isSpecialMember(int age, boolean isRegisterMailMagazine, int usePastMonth) {
+        if (age < 20) return false;
+        if (!isRegisterMailMagazine) return false;
+        if (usePastMonth < 1) return false;
+        return true;
+    }
+
+    /**
+     * 年齢を指定して、会員制サイトへ登録できるかを返す.
+     * @param age 年齢
+     * @return 18歳以上ならばtrueを、18歳未満ならばfalse
+     */
+    public static boolean canRegister(Age age) {
+        return 18 <= age.value;
+    }
+}

File junit-examples/src/test/java/ch08/AssmeTest.java

+package ch08;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+import org.junit.Test;
+
+// リスト8.18
+public class AssmeTest {
+    
+    @Test
+    public void assume() throws Exception {
+        assumeThat(1, is(0));
+        fail("この行は実行されない");
+    }
+}

File junit-examples/src/test/java/ch08/CalculatorDataPointTest.java

+package ch08;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.13
+@RunWith(Theories.class)
+public class CalculatorDataPointTest {
+    @DataPoint
+    public static Fixture PARAM_1 = new Fixture(3, 4, 7);
+    @DataPoint
+    public static Fixture PARAM_2 = new Fixture(0, 5, 5);
+    @DataPoint
+    public static Fixture PARAM_3 = new Fixture(-3, 1, -2);
+
+    @Theory
+    public void add(Fixture p) throws Exception {
+        Calculator sut = new Calculator();
+        assertThat(sut.add(p.x, p.y), is(p.expected));
+    }
+
+    static class Fixture {
+        int x;
+        int y;
+        int expected;
+
+        Fixture(int x, int y, int expected) {
+            this.x = x;
+            this.y = y;
+            this.expected = expected;
+        }
+    }
+}

File junit-examples/src/test/java/ch08/CalculatorDataPointsTest.java

+package ch08;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.14
+@RunWith(Theories.class)
+public class CalculatorDataPointsTest {
+    @DataPoints
+    public static Fixture[] PARAMs = { new Fixture(3, 4, 7), new Fixture(0, 5, 5), new Fixture(-3, 1, -2), };
+
+    @Theory
+    public void add(Fixture p) throws Exception {
+        Calculator sut = new Calculator();
+        assertThat(sut.add(p.x, p.y), is(p.expected));
+    }
+
+    static class Fixture {
+        int x;
+        int y;
+        int expected;
+
+        Fixture(int x, int y, int expected) {
+            this.x = x;
+            this.y = y;
+            this.expected = expected;
+        }
+    }
+}

File junit-examples/src/test/java/ch08/CalculatorDataPointsYamlTest.java

+package ch08;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.io.InputStream;
+import java.util.List;
+
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+import org.yaml.snakeyaml.Yaml;
+
+// リスト8.15
+@RunWith(Theories.class)
+public class CalculatorDataPointsYamlTest {
+
+    @SuppressWarnings("unchecked")
+    @DataPoints
+    public static Fixture[] getParams() {
+        InputStream in = CalculatorDataPointsYamlTest.class.getResourceAsStream("params.yaml");
+        return ((List<Fixture>) new Yaml().load(in)).toArray(new Fixture[0]);
+    }
+
+    @Theory
+    public void add(Fixture p) throws Exception {
+        Calculator sut = new Calculator();
+        assertThat(sut.add(p.x, p.y), is(p.expected));
+    }
+
+    public static class Fixture {
+        public int x;
+        public int y;
+        public int expected;
+    }
+}

File junit-examples/src/test/java/ch08/EnclosedParameterizedTypeTest.java

+package ch08;
+
+import org.junit.experimental.runners.Enclosed;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.6
+@RunWith(Enclosed.class)
+public class EnclosedParameterizedTypeTest {
+    @RunWith(Theories.class)
+    public static class intのパラメータ化テスト {
+        @DataPoint
+        public static int INT_PARAM_1 = 3;
+        @DataPoint
+        public static int INT_PARAM_2 = 4;
+
+        @Theory
+        public void 引数がint型のテストメソッド(int param) throws Exception {
+            System.out.println("引数がint型のテストメソッド(" + param + ")");
+        }
+    }
+
+    @RunWith(Theories.class)
+    public static class Stringのパラメータ化テスト {
+        @DataPoint
+        public static String STRING_PARAM_1 = "Hello";
+        @DataPoint
+        public static String STRING_PARAM_2 = "World";
+
+        @Theory
+        public void 引数がString型のテストメソッド(String param) throws Exception {
+            System.out.println("引数がString型のテストメソッド(" + param + ")");
+        }
+    }
+}

File junit-examples/src/test/java/ch08/JankenTest.java

+package ch08;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+// リスト8.3
+public class JankenTest {
+
+    private Janken sut;
+
+    @Before
+    public void setUp() throws Exception {
+        sut = new Janken();
+    }
+
+    @Test
+    public void グーとチョキなら前者が勝利() throws Exception {
+        assertThat(sut.judge(Janken.Hand.GU, Janken.Hand.TYOKI), is(1));
+    }
+
+    @Test
+    public void グーとパーなら後者が勝利() throws Exception {
+        assertThat(sut.judge(Janken.Hand.GU, Janken.Hand.PA), is(2));
+    }
+
+    @Test
+    public void グーとグーなら引き分け() throws Exception {
+        assertThat(sut.judge(Janken.Hand.GU, Janken.Hand.GU), is(0));
+    }
+
+    @Test
+    public void チョキとパーなら前者が勝利() throws Exception {
+        assertThat(sut.judge(Janken.Hand.TYOKI, Janken.Hand.PA), is(1));
+    }
+}

File junit-examples/src/test/java/ch08/MemberCombinedTest.java

+package ch08;
+
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.17
+@RunWith(Theories.class)
+public class MemberCombinedTest {
+
+    @DataPoints
+    public static int[] AGES = { 15, 20, 25, 30 };
+    @DataPoints
+    public static Gender[] GENDERS = Gender.values();
+
+    @Theory
+    public void canEntry(int age, Gender gender) throws Exception {
+        System.out.println("canEntry(" + age + ", " + gender + ")");
+    }
+}

File junit-examples/src/test/java/ch08/MemberTest.java

+package ch08;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.19
+// リスト8.21
+@RunWith(Theories.class)
+public class MemberTest {
+
+    @DataPoints
+    public static int[] AGES = { 15, 20, 25, 30 };
+    @DataPoints
+    public static Gender[] GENDERS = Gender.values();
+
+    @Theory
+    public void canEntryは25歳以下の女性の場合にtrueを返す(int age, Gender gender) throws Exception {
+        assumeTrue(age <= 25);
+        assumeThat(gender, is(Gender.FEMALE));
+        assertThat(Member.canEntry(age, gender), is(true));
+    }
+
+    @Theory
+    public void canEntryは25歳以下の女性でない場合にfalseを返す(int age, Gender gender) throws Exception {
+        assumeTrue(25 < age || gender != Gender.FEMALE);
+        String msg = "When age=" + age + ", gender=" + gender;
+        assertThat(msg, Member.canEntry(age, gender), is(false));
+    }
+}

File junit-examples/src/test/java/ch08/ParameterizedMultiParamsTest.java

+package ch08;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.11
+@RunWith(Theories.class)
+public class ParameterizedMultiParamsTest {
+    @DataPoint
+    public static int INT_PARAM_1 = 3;
+    @DataPoint
+    public static int INT_PARAM_2 = 4;
+    @DataPoint
+    public static String STRING_PARAM_1 = "Hello";
+    @DataPoint
+    public static String STRING_PARAM_2 = "World";
+    
+    @Theory
+    public void テストメソッド(int intParam, String strParam) throws Exception {
+        System.out.println("テストメソッド(" + intParam + ", " + strParam + ")");
+    }
+}

File junit-examples/src/test/java/ch08/ParameterizedMultiSameTypeParamsTest.java

+package ch08;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.12
+@RunWith(Theories.class)
+public class ParameterizedMultiSameTypeParamsTest {
+    @DataPoint
+    public static int INT_PARAM_1 = 3;
+    @DataPoint
+    public static int INT_PARAM_2 = 4;
+    
+    @Theory
+    public void テストメソッド(int x, int y) throws Exception {
+        System.out.println("テストメソッド(" + x + ", " + y + ")");
+    }
+}

File junit-examples/src/test/java/ch08/ParameterizedTest.java

+package ch08;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.8
+@RunWith(Theories.class)
+public class ParameterizedTest {
+    @DataPoint
+    public static int INT_PARAM_1 = 3;
+    @DataPoint
+    public static int INT_PARAM_2 = 4;
+    
+    public ParameterizedTest() {
+        System.out.println("初期化");
+    }
+
+    @Theory
+    public void 引数を持つテストメソッド(int param) throws Exception {
+        System.out.println("引数を持つテストメソッド(" + param + ")");
+    }
+}

File junit-examples/src/test/java/ch08/ParameterizedTypeTest.java

+package ch08;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト8.9
+@RunWith(Theories.class)
+public class ParameterizedTypeTest {
+    @DataPoint
+    public static int INT_PARAM_1 = 3;
+    @DataPoint
+    public static int INT_PARAM_2 = 4;
+
+    @DataPoint
+    public static String STRING_PARAM_1 = "Hello";
+    @DataPoint
+    public static String STRING_PARAM_2 = "World";
+
+    @Theory
+    public void 引数がint型のテストメソッド(int param) throws Exception {
+        System.out.println("引数がint型のテストメソッド(" + param + ")");
+    }
+
+    @Theory
+    public void 引数がString型のテストメソッド(String param) throws Exception {
+        System.out.println("引数がString型のテストメソッド(" + param + ")");
+    }
+}

File junit-examples/src/test/java/ch08/WindowsOnlyTest.java

+package ch08;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+import org.junit.Test;
+
+// リスト8.20
+public class WindowsOnlyTest {
+
+    @Test
+    public void windows環境では改行はrn() throws Exception {
+        assumeTrue(System.getProperty("os.name").contains("Windows"));
+        assertThat(System.getProperty("line.separator"), is("\r\n"));
+    }
+}

File junit-examples/src/test/resources/ch08/params.yaml

+!!seq [ 
+!!ch08.CalculatorDataPointsYamlTest$Fixture
+  { x: 3, y: 4, expected: 7 }, 
+!!ch08.CalculatorDataPointsYamlTest$Fixture
+  { x: 0, y: 5, expected: 5 }, 
+!!ch08.CalculatorDataPointsYamlTest$Fixture
+  { x: -3, y: 1, expected: -2 },
+]