Commits

Shuji Watanabe  committed ed4e888

9章のコードを追加

  • Participants
  • Parent commits 65da9d7

Comments (0)

Files changed (26)

File junit-examples/ch09_list.txt

+第9章
+------------------
+リスト9.1   src/test/java/ch09/TimeoutExampleTest.java
+リスト9.2   src/test/java/ch09/RulesExampleTest.java
+リスト9.3   src/test/java/ch09/TemporaryFolderExampleTest.java
+リスト9.4   src/test/java/ch09/SpecificTemporaryFolder.java
+リスト9.5   src/test/java/ch09/ExternalResourceExampleTest.java
+リスト9.6   src/test/java/ch09/VerifierExampleTest.java
+リスト9.7   src/test/java/ch09/VerifierExternalObjectExampleTest.java
+リスト9.8   src/test/java/ch09/ShopInfoTest.java
+リスト9.9   src/test/java/ch09/ExceptionTest.java
+リスト9.10  src/test/java/ch09/ExceptionTest.java
+リスト9.11  src/test/java/ch09/ExpectedExceptionExampleTest.java
+リスト9.12  src/test/java/ch09/TimeoutTest.java
+リスト9.13  src/test/java/ch09/TimeoutExampleTest.java
+リスト9.14  src/test/java/ch09/TestWatcherExampleTest.java
+リスト9.15  src/test/java/ch09/TestNameExampleTest.java
+リスト9.16  src/test/java/ch09/PreCondition.java
+リスト9.17  src/test/java/ch09/OSDependentTest.java
+リスト9.18  src/test/java/ch09/RunOn.java
+リスト9.19  src/test/java/ch09/OSDependent.java
+リスト9.20  src/test/java/ch09/RuleChainExampleTest.java
+リスト9.21  src/test/java/ch09/ClassRuleExampleTest.java
+
+
+

File junit-examples/src/main/java/ch09/ErrorLog.java

+package ch09;
+
+public class ErrorLog {
+    
+    public int size() {
+        return 0;
+    }
+}

File junit-examples/src/main/java/ch09/Server.java

+package ch09;
+
+public class Server {
+
+    public Server(int port) {
+    }
+
+    public void start() {
+    }
+
+    public void shutdown() {
+    }
+
+}

File junit-examples/src/main/java/ch09/ShopInfo.java

+package ch09;
+
+import java.net.URL;
+
+public class ShopInfo {
+    Integer id;
+    String name;
+    String address;
+    URL url;
+}

File junit-examples/src/main/java/ch09/TemporaryFolderExcample.java

+package ch09;
+
+import java.io.File;
+import java.io.IOException;
+
+public class TemporaryFolderExcample {
+
+    public static void mkDefaultFiles(File folder) throws IOException {
+        new File(folder, "UnitTest").createNewFile();
+        new File(folder, "readme.txt").createNewFile();
+    }
+
+}

File junit-examples/src/main/java/ch09/TimeoutExample.java

+package ch09;
+
+/**
+ * テストの失敗を確認する場合は、sleepメソッドに100以上の値を設定してください
+ * @author shuji.w6e
+ */
+public class TimeoutExample {
+    static void doLongTask() throws InterruptedException {
+        Thread.sleep(50L);
+    }
+}

File junit-examples/src/main/java/ch09/VerifierExample.java

+package ch09;
+
+public class VerifierExample {
+
+    int value = 0;
+
+    public void set(int value) {
+        this.value = value;
+    }
+
+    public void add(int value) {
+        this.value += value;
+    }
+
+    public void minus(int value) {
+        this.value -= value;
+    }
+
+    public int getValue() {
+        return this.value;
+    }
+
+}

File junit-examples/src/test/java/ch09/ClassRuleExampleTest.java

+package ch09;
+
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.ExternalResource;
+
+/**
+ * リスト9.21
+ * @author shuji.w6e
+ */
+public class ClassRuleExampleTest {
+
+    @ClassRule
+    public static ExternalServer externalServer = new ExternalServer();
+
+    @Test
+    public void test01() throws Exception {
+        System.out.println("test01");
+    }
+
+    @Test
+    public void test02() throws Exception {
+        System.out.println("test02");
+    }
+
+    static class ExternalServer extends ExternalResource {
+
+        @Override
+        protected void before() throws Throwable {
+            System.out.println("connect");
+        }
+
+        @Override
+        protected void after() {
+            System.out.println("disconnect");
+        }
+    }
+}

File junit-examples/src/test/java/ch09/ExceptionTest.java

+package ch09;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * リスト9.9
+ * リスト9.10
+ * @author shuji.w6e
+ */
+public class ExceptionTest {
+
+    @Test(expected = IllegalArgumentException.class)
+    public void 例外の発生を検証する標準的なテスト() throws Exception {
+        throw new IllegalArgumentException();
+    }
+
+    @Ignore("例外が発生しないため、テストは失敗する")
+    @Test
+    public void 例外の発生とメッセージを検証する標準的なテスト() throws Exception {
+        try {
+            throwNewIllegalArgumentException();
+            fail("例外が発生しない");
+        } catch (IllegalArgumentException e) {
+            assertThat(e.getMessage(), is("argument is null."));
+        }
+    }
+
+    private void throwNewIllegalArgumentException() {
+    }
+
+}

File junit-examples/src/test/java/ch09/ExpectedExceptionExampleTest.java

+package ch09;
+
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+/**
+ * リスト9.11
+ * @author shuji.w6e
+ */
+public class ExpectedExceptionExampleTest {
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Ignore("例外メッセージが一致しないため、テストは失敗する")
+    @Test
+    public void 例外の発生とメッセージを検証するテスト() throws Exception {
+        expectedException.expect(IllegalArgumentException.class);
+        expectedException.expectMessage("argument is null.");
+        throw new IllegalArgumentException();
+    }
+}

File junit-examples/src/test/java/ch09/ExternalResourceExampleTest.java

+package ch09;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExternalResource;
+
+/**
+ * リスト9.5
+ * @author shuji.w6e
+ */
+public class ExternalResourceExampleTest {
+
+    @Rule
+    public ServerResource resource = new ServerResource();
+
+    static class ServerResource extends ExternalResource {
+        Server server;
+
+        @Override
+        protected void before() throws Throwable {
+            server = new Server(8080);
+            server.start();
+        }
+
+        @Override
+        protected void after() {
+            server.shutdown();
+        }
+
+    }
+
+    @Test
+    public void testCase() throws Exception {
+    }
+
+}

File junit-examples/src/test/java/ch09/OSDependent.java

+package ch09;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import ch09.RunOn.OS;
+
+/**
+ * リスト9.19
+ * @author shuji.w6e
+ */
+public class OSDependent implements TestRule {
+
+    @Override
+    public Statement apply(final Statement base, final Description desc) {
+        return new Statement() {
+
+            @Override
+            public void evaluate() throws Throwable {
+                RunOn env = desc.getAnnotation(RunOn.class);
+                if (env == null || canEvaluate(env.value())) {
+                    base.evaluate();
+                } else {
+                    // don't evaluate
+                }
+            }
+
+            private boolean canEvaluate(OS os) {
+                String osName = System.getProperty("os.name");
+                if (osName == null) return false;
+                if (os == OS.WINDOWS && osName.startsWith("Windows")) return true;
+                if (os == OS.MAC && osName.startsWith("Mac OS X")) return true;
+                if (os == OS.LINUX && osName.startsWith("Linux")) return true;
+                return false;
+            }
+        };
+    }
+}

File junit-examples/src/test/java/ch09/OSDependentTest.java

+package ch09;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.io.File;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import ch09.RunOn.OS;
+
+/**
+ * リスト9.17
+ * @author shuji.w6e
+ */
+public class OSDependentTest {
+
+    @Rule
+    public OSDependent osDependent = new OSDependent();
+
+    @Test
+    @RunOn(OS.WINDOWS)
+    public void onlyWindows() throws Exception {
+        System.out.println("test: onlyWindows");
+        assertThat(File.separator, is("\\"));
+    }
+
+    @Test
+    @RunOn(OS.MAC)
+    public void onlyMac() throws Exception {
+        System.out.println("test: onlyMac");
+        assertThat(File.separator, is("/"));
+    }
+
+}

File junit-examples/src/test/java/ch09/PreCondition.java

+package ch09;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * リスト9.16
+ * @author shuji.w6e
+ */
+public abstract class PreCondition implements TestRule {
+
+    @Override
+    public Statement apply(final Statement base, Description description) {
+        return new Statement() {
+
+            @Override
+            public void evaluate() throws Throwable {
+                verify();
+                base.evaluate();
+            }
+        };
+    }
+
+    protected abstract void verify() throws Throwable;
+
+}

File junit-examples/src/test/java/ch09/RuleChainExampleTest.java

+package ch09;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExternalResource;
+import org.junit.rules.RuleChain;
+
+/**
+ * リスト9.20
+ * @author shuji.w6e
+ */
+public class RuleChainExampleTest {
+
+    @Rule
+    public RuleChain ruleChain = RuleChain
+                                        .outerRule(new DBServer())
+                                        .around(new AppServer());
+    
+    @Test
+    public void テスト() throws Exception {
+    }
+}
+
+class AppServer extends ExternalResource {
+    @Override
+    protected void before() throws Throwable {
+        System.out.println("start AP");
+    }
+
+    @Override
+    protected void after() {
+        System.out.println("shutdown AP.");
+    }
+}
+
+class DBServer extends ExternalResource {
+    @Override
+    protected void before() throws Throwable {
+        System.out.println("start DB");
+    }
+
+    @Override
+    protected void after() {
+        System.out.println("shutdown DB.");
+    }
+}

File junit-examples/src/test/java/ch09/RulesExampleTest.java

+package ch09;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TestName;
+
+/**
+ * リスト9.2
+ * @author shuji.w6e
+ */
+public class RulesExampleTest {
+    
+    @Rule
+    public TestName testName = new TestName();
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void test() throws Exception {
+    }
+
+}

File junit-examples/src/test/java/ch09/RunOn.java

+package ch09;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * リスト9.18
+ * @author shuji.w6e
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.METHOD })
+public @interface RunOn {
+
+    public enum OS {
+        WINDOWS, MAC, LINUX
+    }
+
+    public OS value();
+}

File junit-examples/src/test/java/ch09/ShopInfoTest.java

+package ch09;
+
+import static org.hamcrest.CoreMatchers.*;
+
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.rules.ErrorCollector;
+import org.junit.runner.RunWith;
+
+/**
+ * リスト9.6 ErrorCollectorを使ったインスタンス化テスト
+ * @author shuji.w6e
+ */
+@RunWith(Enclosed.class)
+public class ShopInfoTest {
+    public static class インスタンス化テスト {
+        @Rule
+        public ErrorCollector ec = new ErrorCollector();
+
+        @Ignore("初期値が不正であるためテストは失敗する")
+        @Test
+        public void デフォルトコンストラクタ() throws Exception {
+            // Exercise
+            ShopInfo instance = new ShopInfo();
+            // Verify
+            ec.checkThat(instance, is(notNullValue()));
+            ec.checkThat("id", instance.id, is(nullValue()));
+            ec.checkThat("name", instance.name, is(""));
+            ec.checkThat("address", instance.address, is(""));
+            ec.checkThat("url", instance.url, is(nullValue()));
+        }
+    }
+}

File junit-examples/src/test/java/ch09/SpecificTemporaryFolder.java

+package ch09;
+
+import java.io.File;
+
+import org.junit.rules.TemporaryFolder;
+
+/**
+ * リスト9.4
+ * @author shuji.w6e
+ */
+public class SpecificTemporaryFolder extends TemporaryFolder {
+
+    public File srcFolder;
+    public File binFolder;
+
+    @Override
+    protected void before() throws Throwable {
+        super.before();
+        srcFolder = newFolder("src");
+        binFolder = newFolder("bin");
+    }
+
+}

File junit-examples/src/test/java/ch09/TemporaryFolderExampleTest.java

+package ch09;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.util.Arrays;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+/**
+ * リスト9.3
+ * @author shuji.w6e
+ */
+public class TemporaryFolderExampleTest {
+    @Rule
+    public TemporaryFolder tempFolder = new TemporaryFolder();
+
+    @Test
+    public void mkDefaultFilesで2つのファイルが作成される() throws Exception {
+        File folder = tempFolder.getRoot();
+        TemporaryFolderExcample.mkDefaultFiles(folder);
+        String[] actualFiles = folder.list();
+        Arrays.sort(actualFiles);
+        assertThat(actualFiles.length, is(2));
+        assertThat(actualFiles[0], is("UnitTest"));
+        assertThat(actualFiles[1], is("readme.txt"));
+    }
+
+}

File junit-examples/src/test/java/ch09/TestNameExampleTest.java

+package ch09;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+
+/**
+ * リスト9.15
+ * @author shuji.w6e
+ */
+public class TestNameExampleTest {
+
+    @Rule
+    public TestName testName = new TestName();
+
+    @Test
+    public void テスト名() throws Exception {
+        assertThat(testName.getMethodName(), is("テスト名"));
+    }
+
+}

File junit-examples/src/test/java/ch09/TestWatcherExampleTest.java

+package ch09;
+
+import static org.junit.Assert.*;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+/**
+ * リスト9.14
+ * @author shuji.w6e
+ */
+public class TestWatcherExampleTest {
+    @Rule
+    public TestWatcher testWatcher = new TestWatcher() {
+
+        @Override
+        protected void starting(Description description) {
+            Logger.getAnonymousLogger().info("start: " + description.getMethodName());
+        }
+
+        @Override
+        protected void succeeded(Description description) {
+            Logger.getAnonymousLogger().info("succeeded: " + description.getMethodName());
+        }
+
+        @Override
+        protected void failed(Throwable e, Description description) {
+            Logger.getAnonymousLogger().log(Level.WARNING, "failed: " + description.getMethodName(), e);
+        }
+
+        @Override
+        protected void finished(Description description) {
+            Logger.getAnonymousLogger().info("finished: " + description.getMethodName());
+        }
+
+    };
+
+    @Test
+    public void 成功するテスト() throws Exception {
+    }
+
+    @Ignore("動作確認のためにはコメントアウトしてください")
+    @Test
+    public void 失敗するテスト() throws Exception {
+        fail("NG");
+    }
+
+}

File junit-examples/src/test/java/ch09/TimeoutExampleTest.java

+package ch09;
+
+import static ch09.TimeoutExample.*;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.Timeout;
+
+/**
+ * リスト9.1
+ * リスト9.13
+ * @author shuji.w6e
+ */
+public class TimeoutExampleTest {
+    @Rule
+    public Timeout timeout = new Timeout(100);
+
+    @Test
+    public void ルールを使ったタイムアウト() throws Exception {
+        doLongTask();
+    }
+}

File junit-examples/src/test/java/ch09/TimeoutTest.java

+package ch09;
+
+import static ch09.TimeoutExample.*;
+
+import org.junit.Test;
+
+/**
+ * リスト9.12
+ * @author shuji.w6e
+ */
+public class TimeoutTest {
+    @Test(timeout = 100L)
+    public void アノテーションを使ったタイムアウト() throws Exception {
+        doLongTask();
+    }
+}

File junit-examples/src/test/java/ch09/VerifierExampleTest.java

+package ch09;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.Verifier;
+
+/**
+ * リスト9.6
+ * @author shuji.w6e
+ */
+public class VerifierExampleTest {
+
+    @Rule
+    public Verifier verifier = new Verifier() {
+        protected void verify() throws Throwable {
+            assertThat("value should be 0.", sut.value, is(0));
+        }
+    };
+    VerifierExample sut;
+
+    @Before
+    public void setUp() throws Exception {
+        sut = new VerifierExample();
+    }
+
+    @Ignore("後処理としてsut.set(0)を実行していないため、テストは失敗する")
+    @Test
+    public void getValueで計算結果を取得する() throws Exception {
+        sut.set(200);
+        sut.add(100);
+        sut.minus(50);
+        int actual = sut.getValue();
+        assertThat(actual, is(250));
+    }
+}

File junit-examples/src/test/java/ch09/VerifierExternalObjectExampleTest.java

+package ch09;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.Verifier;
+
+/**
+ * リスト9.8
+ * @author shuji.w6e
+ */
+public class VerifierExternalObjectExampleTest {
+    ErrorLog log = new ErrorLog();
+
+    @Rule
+    public ErrorLogVerifier errorLogVerifier = new ErrorLogVerifier(log);
+
+    @Ignore("事後条件でErrorLogが空であるため、テストは失敗する")
+    @Test
+    public void testCase() throws Exception {
+    }
+}
+
+class ErrorLogVerifier extends Verifier {
+
+    final ErrorLog log;
+
+    ErrorLogVerifier(ErrorLog log) {
+        this.log = log;
+    }
+
+    @Override
+    protected void verify() throws Throwable {
+        assertThat(log.size(), is(not(0)));
+    }
+}