From adeef73a97cf191301a7b563883a075caed733b7 Mon Sep 17 00:00:00 2001 From: Damien Martin-Guillerez Date: Tue, 24 Mar 2015 09:05:08 +0000 Subject: Move all examples into Bazel workspace Those examples will be used for Bazel integration tests and their sources should be available directly to the Bazel workspace itself. -- MOS_MIGRATED_REVID=89380736 --- examples/java-native/README.md | 4 ++ .../src/main/java/com/example/myproject/BUILD | 24 +++++++++++ .../main/java/com/example/myproject/Greeter.java | 49 ++++++++++++++++++++++ examples/java-native/src/main/resources/BUILD | 6 +++ .../java-native/src/main/resources/greeting.txt | 1 + .../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 ++++++++++++++++ 9 files changed, 198 insertions(+) create mode 100644 examples/java-native/README.md create mode 100644 examples/java-native/src/main/java/com/example/myproject/BUILD create mode 100644 examples/java-native/src/main/java/com/example/myproject/Greeter.java create mode 100644 examples/java-native/src/main/resources/BUILD create mode 100644 examples/java-native/src/main/resources/greeting.txt create mode 100644 examples/java-native/src/test/java/com/example/myproject/BUILD create mode 100644 examples/java-native/src/test/java/com/example/myproject/Fail.java create mode 100644 examples/java-native/src/test/java/com/example/myproject/TestCustomGreeting.java create mode 100644 examples/java-native/src/test/java/com/example/myproject/TestHello.java (limited to 'examples/java-native') diff --git a/examples/java-native/README.md b/examples/java-native/README.md new file mode 100644 index 0000000000..fb37462bf3 --- /dev/null +++ b/examples/java-native/README.md @@ -0,0 +1,4 @@ +Java Examples +============= + +This directory contains examples for Java language rules. diff --git a/examples/java-native/src/main/java/com/example/myproject/BUILD b/examples/java-native/src/main/java/com/example/myproject/BUILD new file mode 100644 index 0000000000..717625343b --- /dev/null +++ b/examples/java-native/src/main/java/com/example/myproject/BUILD @@ -0,0 +1,24 @@ +package(default_visibility = ["//visibility:public"]) + +java_binary( + name = "hello-world", + main_class = "com.example.myproject.Greeter", + runtime_deps = [":hello-lib"], +) + +java_library( + name = "hello-lib", + srcs = glob(["*.java"]), +) + +java_binary( + name = "hello-resources", + main_class = "com.example.myproject.Greeter", + runtime_deps = [":custom-greeting"], +) + +java_library( + name = "custom-greeting", + srcs = ["Greeter.java"], + resources = ["//examples/java-native/src/main/resources:greeting"], +) diff --git a/examples/java-native/src/main/java/com/example/myproject/Greeter.java b/examples/java-native/src/main/java/com/example/myproject/Greeter.java new file mode 100644 index 0000000000..4123f2e202 --- /dev/null +++ b/examples/java-native/src/main/java/com/example/myproject/Greeter.java @@ -0,0 +1,49 @@ +package com.example.myproject; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + +/** + * Prints a greeting which can be customized by building with resources and/or passing in command- + * line arguments. + * + *

Building and running this file will print "Hello world". Build and run the + * //examples/java-native/src/main/java/com/example/myproject:hello-world target to demonstrate + * this.

+ * + *

If this is built with a greeting.txt resource included, it will replace "Hello" with the + * contents of greeting.txt. The + * //examples/java-native/src/main/java/com/example/myproject:hello-resources target demonstrates + * this.

+ * + *

If arguments are passed to the binary on the command line, the first argument will replace + * "world" in the output. See //examples/java-native/src/test/java/com/example/myproject:hello's + * argument test.

+ */ +public class Greeter { + static PrintStream out = System.out; + + public static String convertStreamToString(InputStream is) throws Exception { + try (Scanner s = new Scanner(is)) { + s.useDelimiter("\n"); + return s.hasNext() ? s.next() : ""; + } + } + + public void hello(String obj) throws Exception { + String greeting = "Hello"; + InputStream stream = Greeter.class.getResourceAsStream( + "/examples/java-native/src/main/resources/greeting.txt"); + if (stream != null) { + greeting = convertStreamToString(stream); + } + out.println(greeting + " " + obj); + } + + public static void main(String... args) throws Exception { + Greeter g = new Greeter(); + String obj = args.length > 0 ? args[0] : "world"; + g.hello(obj); + } +} diff --git a/examples/java-native/src/main/resources/BUILD b/examples/java-native/src/main/resources/BUILD new file mode 100644 index 0000000000..8347a3555a --- /dev/null +++ b/examples/java-native/src/main/resources/BUILD @@ -0,0 +1,6 @@ +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "greeting", + srcs = ["greeting.txt"], +) diff --git a/examples/java-native/src/main/resources/greeting.txt b/examples/java-native/src/main/resources/greeting.txt new file mode 100644 index 0000000000..09170083fc --- /dev/null +++ b/examples/java-native/src/main/resources/greeting.txt @@ -0,0 +1 @@ +Bye diff --git a/examples/java-native/src/test/java/com/example/myproject/BUILD b/examples/java-native/src/test/java/com/example/myproject/BUILD new file mode 100644 index 0000000000..838107650c --- /dev/null +++ b/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/examples/java-native/src/test/java/com/example/myproject/Fail.java b/examples/java-native/src/test/java/com/example/myproject/Fail.java new file mode 100644 index 0000000000..7b947c6e96 --- /dev/null +++ b/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/examples/java-native/src/test/java/com/example/myproject/TestCustomGreeting.java b/examples/java-native/src/test/java/com/example/myproject/TestCustomGreeting.java new file mode 100644 index 0000000000..f14b7d234a --- /dev/null +++ b/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/examples/java-native/src/test/java/com/example/myproject/TestHello.java b/examples/java-native/src/test/java/com/example/myproject/TestHello.java new file mode 100644 index 0000000000..d50e6e503f --- /dev/null +++ b/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