From 254aee40df78e79ac1b19fe6d20ae20bb05129a8 Mon Sep 17 00:00:00 2001 From: Kristina Chodorow Date: Fri, 20 Feb 2015 15:45:07 +0000 Subject: Improve Java examples -- MOS_MIGRATED_REVID=86787893 --- .../src/test/java/com/example/myproject/BUILD | 33 ++++++++++++++++++++ .../src/test/java/com/example/myproject/Fail.java | 14 +++++++++ .../com/example/myproject/TestCustomGreeting.java | 32 ++++++++++++++++++++ .../test/java/com/example/myproject/TestHello.java | 35 ++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 base_workspace/examples/java-native/src/test/java/com/example/myproject/BUILD create mode 100644 base_workspace/examples/java-native/src/test/java/com/example/myproject/Fail.java create mode 100644 base_workspace/examples/java-native/src/test/java/com/example/myproject/TestCustomGreeting.java create mode 100644 base_workspace/examples/java-native/src/test/java/com/example/myproject/TestHello.java (limited to 'base_workspace/examples/java-native/src/test/java/com/example/myproject') diff --git a/base_workspace/examples/java-native/src/test/java/com/example/myproject/BUILD b/base_workspace/examples/java-native/src/test/java/com/example/myproject/BUILD new file mode 100644 index 0000000000..838107650c --- /dev/null +++ b/base_workspace/examples/java-native/src/test/java/com/example/myproject/BUILD @@ -0,0 +1,33 @@ +java_test( + name = "hello", + srcs = ["TestHello.java"], + deps = [ + "//examples/java-native/src/main/java/com/example/myproject:hello-lib", + "//third_party:junit4", + ], +) + +java_test( + name = "custom", + srcs = ["TestCustomGreeting.java"], + deps = [ + "//examples/java-native/src/main/java/com/example/myproject:custom-greeting", + "//third_party:junit4", + ], +) + +java_test( + name = "fail", + srcs = ["Fail.java"], + deps = ["//third_party:junit4"], +) + +# This attempts to run TestCustomGreeting.java without any resources, so fails. +java_test( + name = "resource-fail", + srcs = glob(["Test*.java"]), + deps = [ + "//examples/java-native/src/main/java/com/example/myproject:hello-lib", + "//third_party:junit4", + ], +) diff --git a/base_workspace/examples/java-native/src/test/java/com/example/myproject/Fail.java b/base_workspace/examples/java-native/src/test/java/com/example/myproject/Fail.java new file mode 100644 index 0000000000..7b947c6e96 --- /dev/null +++ b/base_workspace/examples/java-native/src/test/java/com/example/myproject/Fail.java @@ -0,0 +1,14 @@ +package com.example.myproject; + +import org.junit.Assert; +import org.junit.Test; + +/** + * A test that always fails. + */ +public class Fail { + @Test + public void testFail() { + Assert.fail("This is an expected test failure."); + } +} diff --git a/base_workspace/examples/java-native/src/test/java/com/example/myproject/TestCustomGreeting.java b/base_workspace/examples/java-native/src/test/java/com/example/myproject/TestCustomGreeting.java new file mode 100644 index 0000000000..f14b7d234a --- /dev/null +++ b/base_workspace/examples/java-native/src/test/java/com/example/myproject/TestCustomGreeting.java @@ -0,0 +1,32 @@ +package com.example.myproject; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; + +/** + * Tests using a resource file to replace "Hello" in the output. + */ +public class TestCustomGreeting { + + @Test + public void testNoArgument() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Greeter.out = new PrintStream(out); + Greeter.main(); + assertEquals("Bye world\n", new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + + @Test + public void testWithArgument() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Greeter.out = new PrintStream(out); + Greeter.main("toto"); + assertEquals("Bye toto\n", new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + +} diff --git a/base_workspace/examples/java-native/src/test/java/com/example/myproject/TestHello.java b/base_workspace/examples/java-native/src/test/java/com/example/myproject/TestHello.java new file mode 100644 index 0000000000..d50e6e503f --- /dev/null +++ b/base_workspace/examples/java-native/src/test/java/com/example/myproject/TestHello.java @@ -0,0 +1,35 @@ +package com.example.myproject; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; + +/** + * Tests different numbers of arguments to main(). + * + *

With an empty args array, {@link Greeter} should print "Hello world". If there are one or more + * args, {@link Greeter} should print "Hello <arg[0]>".

+ */ +public class TestHello { + + @Test + public void testNoArgument() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Greeter.out = new PrintStream(out); + Greeter.main(); + assertEquals("Hello world\n", new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + + @Test + public void testWithArgument() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Greeter.out = new PrintStream(out); + Greeter.main("toto"); + assertEquals("Hello toto\n", new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + +} -- cgit v1.2.3