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 --- base_workspace/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 ++++ base_workspace/examples/java-skylark/README.md | 7 + .../src/main/java/com/example/myproject/BUILD | 21 ++ .../main/java/com/example/myproject/Greeter.java | 45 ++++ .../examples/java-skylark/src/main/resources/BUILD | 6 + .../java-skylark/src/main/resources/greeting.txt | 1 + .../src/test/java/com/example/myproject/BUILD | 16 ++ .../src/test/java/com/example/myproject/Fail.java | 7 + .../test/java/com/example/myproject/TestHello.java | 35 ++++ base_workspace/examples/java/BUILD | 25 --- base_workspace/examples/java/greeting.txt | 1 - base_workspace/examples/java/test/Greeter.java | 31 --- base_workspace/examples/java_oss/BUILD | 33 --- base_workspace/examples/java_oss/Fail.java | 7 - base_workspace/examples/java_oss/Greeter.java | 38 ---- base_workspace/examples/java_oss/greeting.txt | 1 - base_workspace/examples/javatests/BUILD | 32 --- base_workspace/examples/javatests/test/Fail.java | 11 - .../examples/javatests/test/TestBye.java | 29 --- .../examples/javatests/test/TestHello.java | 29 --- src/test/java/BUILD | 2 +- tools/build_rules/java_rules_oss.bzl | 233 --------------------- tools/build_rules/java_rules_skylark.bzl | 233 +++++++++++++++++++++ 31 files changed, 570 insertions(+), 471 deletions(-) create mode 100644 base_workspace/examples/java-native/README.md create mode 100644 base_workspace/examples/java-native/src/main/java/com/example/myproject/BUILD create mode 100644 base_workspace/examples/java-native/src/main/java/com/example/myproject/Greeter.java create mode 100644 base_workspace/examples/java-native/src/main/resources/BUILD create mode 100644 base_workspace/examples/java-native/src/main/resources/greeting.txt 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 create mode 100644 base_workspace/examples/java-skylark/README.md create mode 100644 base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD create mode 100644 base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java create mode 100644 base_workspace/examples/java-skylark/src/main/resources/BUILD create mode 100644 base_workspace/examples/java-skylark/src/main/resources/greeting.txt create mode 100644 base_workspace/examples/java-skylark/src/test/java/com/example/myproject/BUILD create mode 100644 base_workspace/examples/java-skylark/src/test/java/com/example/myproject/Fail.java create mode 100644 base_workspace/examples/java-skylark/src/test/java/com/example/myproject/TestHello.java delete mode 100644 base_workspace/examples/java/BUILD delete mode 100644 base_workspace/examples/java/greeting.txt delete mode 100644 base_workspace/examples/java/test/Greeter.java delete mode 100644 base_workspace/examples/java_oss/BUILD delete mode 100644 base_workspace/examples/java_oss/Fail.java delete mode 100644 base_workspace/examples/java_oss/Greeter.java delete mode 100644 base_workspace/examples/java_oss/greeting.txt delete mode 100644 base_workspace/examples/javatests/BUILD delete mode 100644 base_workspace/examples/javatests/test/Fail.java delete mode 100644 base_workspace/examples/javatests/test/TestBye.java delete mode 100644 base_workspace/examples/javatests/test/TestHello.java delete mode 100644 tools/build_rules/java_rules_oss.bzl create mode 100644 tools/build_rules/java_rules_skylark.bzl diff --git a/base_workspace/examples/java-native/README.md b/base_workspace/examples/java-native/README.md new file mode 100644 index 0000000000..fb37462bf3 --- /dev/null +++ b/base_workspace/examples/java-native/README.md @@ -0,0 +1,4 @@ +Java Examples +============= + +This directory contains examples for Java language rules. diff --git a/base_workspace/examples/java-native/src/main/java/com/example/myproject/BUILD b/base_workspace/examples/java-native/src/main/java/com/example/myproject/BUILD new file mode 100644 index 0000000000..717625343b --- /dev/null +++ b/base_workspace/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/base_workspace/examples/java-native/src/main/java/com/example/myproject/Greeter.java b/base_workspace/examples/java-native/src/main/java/com/example/myproject/Greeter.java new file mode 100644 index 0000000000..4123f2e202 --- /dev/null +++ b/base_workspace/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/base_workspace/examples/java-native/src/main/resources/BUILD b/base_workspace/examples/java-native/src/main/resources/BUILD new file mode 100644 index 0000000000..8347a3555a --- /dev/null +++ b/base_workspace/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/base_workspace/examples/java-native/src/main/resources/greeting.txt b/base_workspace/examples/java-native/src/main/resources/greeting.txt new file mode 100644 index 0000000000..09170083fc --- /dev/null +++ b/base_workspace/examples/java-native/src/main/resources/greeting.txt @@ -0,0 +1 @@ +Bye 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)); + } + +} diff --git a/base_workspace/examples/java-skylark/README.md b/base_workspace/examples/java-skylark/README.md new file mode 100644 index 0000000000..970a859f54 --- /dev/null +++ b/base_workspace/examples/java-skylark/README.md @@ -0,0 +1,7 @@ +Skylark Java Examples +===================== + +Use the native Java rules (see the examples in +_base_workspace/examples/java-native_) for building Java, not these. These +files are examples of how Skylark rules can be used (see +_tools/build_rules/java_rules_skylark.bzl_ for the rule definitions). diff --git a/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD new file mode 100644 index 0000000000..ed69d7f2b3 --- /dev/null +++ b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD @@ -0,0 +1,21 @@ +load("tools/build_rules/java_rules_skylark", "java_library", "java_binary", "java_test") + +package(default_visibility = ["//visibility:public"]) + +java_binary( + name = "hello-world", + srcs = ["Greeter.java"], + main_class = "com.example.myproject.Greeter", +) + +java_binary( + name = "hello-data", + main_class = "com.example.myproject.Greeter", + deps = [":hello-lib"], +) + +java_library( + name = "hello-lib", + srcs = ["Greeter.java"], + data = ["//examples/java-skylark/src/main/resources:greeting"], +) diff --git a/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java new file mode 100644 index 0000000000..b4d8516dc9 --- /dev/null +++ b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java @@ -0,0 +1,45 @@ +package com.example.myproject; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + +/** + * Prints a greeting which can be customized by building with data and/or passing in command- + * line arguments. + */ +public class Greeter { + static PrintStream out = System.out; + + public static String getRunfiles() { + String path = System.getenv("JAVA_RUNFILES"); + if (path == null) { + path = System.getenv("TEST_SRCDIR"); + } + return path; + } + + public static String convertStreamToString(InputStream is) throws Exception { + Scanner s = new Scanner(is).useDelimiter("\n"); + return s.hasNext() ? s.next() : ""; + } + + public void hello(String obj) throws Exception { + String greeting = "Hello"; + try { + String greetFile = getRunfiles() + "/examples/java-skylark/src/main/resources/greeting.txt"; + greeting = convertStreamToString(new FileInputStream(greetFile)); + } catch (FileNotFoundException e) { + // use default. + } + 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/base_workspace/examples/java-skylark/src/main/resources/BUILD b/base_workspace/examples/java-skylark/src/main/resources/BUILD new file mode 100644 index 0000000000..8347a3555a --- /dev/null +++ b/base_workspace/examples/java-skylark/src/main/resources/BUILD @@ -0,0 +1,6 @@ +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "greeting", + srcs = ["greeting.txt"], +) diff --git a/base_workspace/examples/java-skylark/src/main/resources/greeting.txt b/base_workspace/examples/java-skylark/src/main/resources/greeting.txt new file mode 100644 index 0000000000..17654db5a1 --- /dev/null +++ b/base_workspace/examples/java-skylark/src/main/resources/greeting.txt @@ -0,0 +1 @@ +Heyo diff --git a/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/BUILD b/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/BUILD new file mode 100644 index 0000000000..90e71f97ac --- /dev/null +++ b/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/BUILD @@ -0,0 +1,16 @@ +load("tools/build_rules/java_rules_skylark", "java_test") + +java_test( + name = "pass", + srcs = ["TestHello.java"], + deps = [ + "//examples/java-skylark/src/main/java/com/example/myproject:hello-lib", + "//third_party:junit4", + ], +) + +java_test( + name = "fail", + srcs = ["Fail.java"], + main_class = "com.example.myproject.Fail", +) diff --git a/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/Fail.java b/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/Fail.java new file mode 100644 index 0000000000..079e1e9f04 --- /dev/null +++ b/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/Fail.java @@ -0,0 +1,7 @@ +package com.example.myproject; + +class Fail { + public static void main(String []args) { + System.exit(1); + } +} diff --git a/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/TestHello.java b/base_workspace/examples/java-skylark/src/test/java/com/example/myproject/TestHello.java new file mode 100644 index 0000000000..d50e6e503f --- /dev/null +++ b/base_workspace/examples/java-skylark/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)); + } + +} diff --git a/base_workspace/examples/java/BUILD b/base_workspace/examples/java/BUILD deleted file mode 100644 index 09e671be81..0000000000 --- a/base_workspace/examples/java/BUILD +++ /dev/null @@ -1,25 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -java_library( - name = "bye-lib", - srcs = ["test/Greeter.java"], - resources = ["greeting.txt"], -) - -java_library( - name = "hello-lib", - srcs = ["test/Greeter.java"], -) - -java_binary( - name = "bye-world", - main_class = "test.Greeter", - runtime_deps = [":bye-lib"], -) - -java_binary( - name = "hello-world", - main_class = "test.Greeter", - runtime_deps = [":hello-lib"], -) - diff --git a/base_workspace/examples/java/greeting.txt b/base_workspace/examples/java/greeting.txt deleted file mode 100644 index 09170083fc..0000000000 --- a/base_workspace/examples/java/greeting.txt +++ /dev/null @@ -1 +0,0 @@ -Bye diff --git a/base_workspace/examples/java/test/Greeter.java b/base_workspace/examples/java/test/Greeter.java deleted file mode 100644 index e1eebe7960..0000000000 --- a/base_workspace/examples/java/test/Greeter.java +++ /dev/null @@ -1,31 +0,0 @@ -package test; - -import java.io.InputStream; -import java.io.PrintStream; -import java.util.Scanner; - -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("/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/base_workspace/examples/java_oss/BUILD b/base_workspace/examples/java_oss/BUILD deleted file mode 100644 index a4acd5ce5f..0000000000 --- a/base_workspace/examples/java_oss/BUILD +++ /dev/null @@ -1,33 +0,0 @@ -load("tools/build_rules/java_rules_oss", "java_library", "java_binary", "java_test") - -java_library( - name = "hello-lib", - srcs = ["Greeter.java"], - data = ["greeting.txt"], -) - -java_binary( - name = "hello-world", - main_class = "test.Greeter", - deps = [":hello-lib"], -) - -java_binary( - name = "hello-main", - srcs = ["Greeter.java"], - main_class = "test.Greeter", -) - -# Testing. - -java_test( - name = "pass_test", - main_class = "test.Greeter", - deps = [":hello-lib"], -) - -java_test( - name = "fail_test", - srcs = ["Fail.java"], - main_class = "test.Fail", -) diff --git a/base_workspace/examples/java_oss/Fail.java b/base_workspace/examples/java_oss/Fail.java deleted file mode 100644 index 61e253930c..0000000000 --- a/base_workspace/examples/java_oss/Fail.java +++ /dev/null @@ -1,7 +0,0 @@ -package test; - -class Fail { - public static void main(String []args) { - System.exit(1); - } -} diff --git a/base_workspace/examples/java_oss/Greeter.java b/base_workspace/examples/java_oss/Greeter.java deleted file mode 100644 index 39aba02c97..0000000000 --- a/base_workspace/examples/java_oss/Greeter.java +++ /dev/null @@ -1,38 +0,0 @@ -package test; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.Scanner; - -class Greeter { - public static String getRunfiles() { - String path = System.getenv("JAVA_RUNFILES"); - if (path == null) { - path = System.getenv("TEST_SRCDIR"); - } - return path; - } - - public static String convertStreamToString(InputStream is) throws Exception { - Scanner s = new Scanner(is).useDelimiter("\n"); - return s.hasNext() ? s.next() : ""; - } - - public void hello(String obj) throws Exception { - String greeting = "Hello"; - try { - String greetFile = getRunfiles() + "/examples/java_oss/greeting.txt"; - greeting = convertStreamToString(new FileInputStream(greetFile)); - } catch (FileNotFoundException e) { - // use default. - } - System.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/base_workspace/examples/java_oss/greeting.txt b/base_workspace/examples/java_oss/greeting.txt deleted file mode 100644 index 3f453d002f..0000000000 --- a/base_workspace/examples/java_oss/greeting.txt +++ /dev/null @@ -1 +0,0 @@ -Hail diff --git a/base_workspace/examples/javatests/BUILD b/base_workspace/examples/javatests/BUILD deleted file mode 100644 index c504b1c7a0..0000000000 --- a/base_workspace/examples/javatests/BUILD +++ /dev/null @@ -1,32 +0,0 @@ -java_test( - name = "hello", - srcs = ["test/TestHello.java"], - deps = [ - "//examples/java:hello-lib", - "//third_party:junit4", - ], -) - -java_test( - name = "bye", - srcs = ["test/TestBye.java"], - deps = [ - "//examples/java:bye-lib", - "//third_party:junit4", - ], -) - -java_test( - name = "fail", - srcs = ["test/Fail.java"], - deps = ["//third_party:junit4"], -) - -java_test( - name = "should_fail", - srcs = glob(["test/Test*.java"]), - deps = [ - "//examples/java:hello-lib", - "//third_party:junit4", - ], -) diff --git a/base_workspace/examples/javatests/test/Fail.java b/base_workspace/examples/javatests/test/Fail.java deleted file mode 100644 index 10e99210ff..0000000000 --- a/base_workspace/examples/javatests/test/Fail.java +++ /dev/null @@ -1,11 +0,0 @@ -package test; - -import org.junit.Assert; -import org.junit.Test; - -public class Fail { - @Test - public void testFail() { - Assert.fail("This is an expected test failure."); - } -} diff --git a/base_workspace/examples/javatests/test/TestBye.java b/base_workspace/examples/javatests/test/TestBye.java deleted file mode 100644 index 2c00ce6e6c..0000000000 --- a/base_workspace/examples/javatests/test/TestBye.java +++ /dev/null @@ -1,29 +0,0 @@ -package test; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; - -public class TestBye { - - @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/javatests/test/TestHello.java b/base_workspace/examples/javatests/test/TestHello.java deleted file mode 100644 index d121f96c27..0000000000 --- a/base_workspace/examples/javatests/test/TestHello.java +++ /dev/null @@ -1,29 +0,0 @@ -package test; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; - -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)); - } - -} diff --git a/src/test/java/BUILD b/src/test/java/BUILD index 1dfc4af8fc..51b5ab5d92 100644 --- a/src/test/java/BUILD +++ b/src/test/java/BUILD @@ -86,7 +86,7 @@ java_test( "vfs/*.java", "vfs/inmemoryfs/*.java", ]], - # java_rules_oss doesn't support resource loading with + # java_rules_skylark doesn't support resource loading with # qualified paths. exclude = [ test_prefix + f diff --git a/tools/build_rules/java_rules_oss.bzl b/tools/build_rules/java_rules_oss.bzl deleted file mode 100644 index dcd2329ebc..0000000000 --- a/tools/build_rules/java_rules_oss.bzl +++ /dev/null @@ -1,233 +0,0 @@ -# Copyright 2014 Google Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -java_filetype = FileType([".java"]) -jar_filetype = FileType([".jar"]) -srcjar_filetype = FileType([".jar", ".srcjar"]) - -JAVA_PATH='tools/jdk/jdk/bin/' - -def is_windows(config): - return config.fragment(cpp).compiler.startswith("windows_") - -def path_separator(ctx): - if is_windows(ctx.configuration): - return ";" - else: - return ":" - -# This is a quick and dirty rule to make Bazel compile itself. It's not -# production ready. - -def java_library_impl(ctx): - class_jar = ctx.outputs.class_jar - compile_time_jars = set(order="link") - runtime_jars = set(order="link") - for dep in ctx.targets.deps: - compile_time_jars += dep.compile_time_jars - runtime_jars += dep.runtime_jars - - jars = jar_filetype.filter(ctx.files.jars) - compile_time_jars += jars - runtime_jars += jars - compile_time_jars_list = list(compile_time_jars) # TODO: This is weird. - - build_output = class_jar.path + ".build_output" - sources = ctx.files.srcs - - sources_param_file = ctx.new_file( - ctx.configuration.bin_dir, class_jar, "-2.params") - ctx.file_action( - output = sources_param_file, - content = cmd_helper.join_paths("\n", set(sources)), - executable = False) - - # Cleaning build output directory - cmd = "set -e;rm -rf " + build_output + ";mkdir " + build_output + "\n" - if ctx.files.srcs: - cmd += JAVA_PATH + "javac" - if compile_time_jars: - cmd += " -classpath '" + cmd_helper.join_paths(path_separator(ctx), compile_time_jars) + "'" - cmd += " -d " + build_output + " @" + sources_param_file.path + "\n" - - # We haven't got a good story for where these should end up, so - # stick them in the root of the jar. - for r in ctx.files.resources: - cmd += "cp %s %s\n" % (r.path, build_output) - cmd += (JAVA_PATH + "jar cf " + class_jar.path + " -C " + build_output + " .\n" + - "touch " + build_output + "\n") - ctx.action( - inputs = (sources + compile_time_jars_list + [sources_param_file] + - ctx.files.resources), - outputs = [class_jar], - mnemonic='Javac', - command=cmd, - use_default_shell_env=True) - - runfiles = ctx.runfiles(collect_data = True) - - return struct(files = set([class_jar]), - compile_time_jars = compile_time_jars + [class_jar], - runtime_jars = runtime_jars + [class_jar], - runfiles = runfiles) - - -def java_binary_impl(ctx): - library_result = java_library_impl(ctx) - - deploy_jar = ctx.outputs.deploy_jar - manifest = ctx.outputs.manifest - build_output = deploy_jar.path + ".build_output" - main_class = ctx.attr.main_class - ctx.file_action( - output = manifest, - content = "Main-Class: " + main_class + "\n", - executable = False) - - # Cleaning build output directory - cmd = "set -e;rm -rf " + build_output + ";mkdir " + build_output + "\n" - for jar in library_result.runtime_jars: - cmd += "unzip -qn " + jar.path + " -d " + build_output + "\n" - cmd += (JAVA_PATH + "jar cmf " + manifest.path + " " + - deploy_jar.path + " -C " + build_output + " .\n" + - "touch " + build_output + "\n") - - ctx.action( - inputs=list(library_result.runtime_jars) + [manifest], - outputs=[deploy_jar], - mnemonic='Deployjar', - command=cmd, - use_default_shell_env=True) - - # Write the wrapper. - executable = ctx.outputs.executable - ctx.file_action( - output = executable, - content = '\n'.join([ - "#!/bin/bash", - "# autogenerated - do not edit.", - "case \"$0\" in", - "/*) self=\"$0\" ;;", - "*) self=\"$PWD/$0\";;", - "esac", - "", - "if [[ -z \"$JAVA_RUNFILES\" ]]; then", - " if [[ -e \"${self}.runfiles\" ]]; then", - " export JAVA_RUNFILES=\"${self}.runfiles\"", - " fi", - " if [[ -n \"$JAVA_RUNFILES\" ]]; then", - " export TEST_SRCDIR=${TEST_SRCDIR:-$JAVA_RUNFILES}", - " fi", - "fi", - "", - - "jvm_bin=%s" % (ctx.file.javabin.path), - "if [[ ! -x ${jvm_bin} ]]; then", - " jvm_bin=$(which java)", - "fi", - - # We extract the .so into a temp dir. If only we could mmap - # directly from the zip file. - "DEPLOY=$(dirname $self)/$(basename %s)" % deploy_jar.path, - - # This works both on Darwin and Linux, with the darwin path - # looking like tmp.XXXXXXXX.{random} - "SO_DIR=$(mktemp -d -t tmp.XXXXXXXXX)", - "function cleanup() {", - " rm -rf ${SO_DIR}", - "}", - "trap cleanup EXIT", - "unzip -q -d ${SO_DIR} ${DEPLOY} \"*.so\" \"*.dll\" \"*.dylib\" >& /dev/null", - ("${jvm_bin} -Djava.library.path=${SO_DIR} %s -jar $DEPLOY \"$@\"" - % ' '.join(ctx.attr.jvm_flags)) , - "", - ]), - executable = True) - - runfiles = ctx.runfiles(files = [ - deploy_jar, executable] + ctx.files.jvm, collect_data = True) - files_to_build = set([deploy_jar, manifest, executable]) - files_to_build += library_result.files - - return struct(files = files_to_build, runfiles = runfiles) - - -def java_import_impl(ctx): - # TODO(bazel-team): Why do we need to filter here? The attribute - # already says only jars are allowed. - jars = set(jar_filetype.filter(ctx.files.jars)) - runfiles = ctx.runfiles(collect_data = True) - return struct(files = jars, - compile_time_jars = jars, - runtime_jars = jars, - runfiles = runfiles) - - -java_library_attrs = { - "data": attr.label_list(allow_files=True, cfg=DATA_CFG), - "resources": attr.label_list(allow_files=True), - "srcs": attr.label_list(allow_files=java_filetype), - "jars": attr.label_list(allow_files=jar_filetype), - "deps": attr.label_list( - allow_files=False, - providers = ["compile_time_jars", "runtime_jars"]), - } - -java_library = rule( - java_library_impl, - attrs = java_library_attrs, - outputs = { - "class_jar": "lib%{name}.jar", - }) - -java_binary_attrs_common = java_library_attrs + { - "jvm_flags": attr.string_list(), - "jvm": attr.label(default=Label("//tools/jdk:jdk"), allow_files=True), - "javabin": attr.label(default=Label("//tools/jdk:java"), single_file=True), - "args": attr.string_list(), -} - -java_binary_attrs = java_binary_attrs_common + { - "main_class": attr.string(mandatory=True), -} - -java_binary_outputs = { - "class_jar": "lib%{name}.jar", - "deploy_jar": "%{name}_deploy.jar", - "manifest": "%{name}_MANIFEST.MF" -} - -java_binary = rule(java_binary_impl, - executable = True, - attrs = java_binary_attrs, - outputs = java_binary_outputs) - -java_test = rule(java_binary_impl, - executable = True, - attrs = java_binary_attrs_common + { - "main_class": attr.string(default="org.junit.runner.JUnitCore"), - # TODO(bazel-team): it would be better if we could offer a - # test_class attribute, but the "args" attribute is hard - # coded in the bazel infrastructure. - }, - outputs = java_binary_outputs, - test = True, -) - -java_import = rule( - java_import_impl, - attrs = { - "jars": attr.label_list(allow_files=jar_filetype), - "srcjar": attr.label(allow_files=srcjar_filetype), - }) diff --git a/tools/build_rules/java_rules_skylark.bzl b/tools/build_rules/java_rules_skylark.bzl new file mode 100644 index 0000000000..dcd2329ebc --- /dev/null +++ b/tools/build_rules/java_rules_skylark.bzl @@ -0,0 +1,233 @@ +# Copyright 2014 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +java_filetype = FileType([".java"]) +jar_filetype = FileType([".jar"]) +srcjar_filetype = FileType([".jar", ".srcjar"]) + +JAVA_PATH='tools/jdk/jdk/bin/' + +def is_windows(config): + return config.fragment(cpp).compiler.startswith("windows_") + +def path_separator(ctx): + if is_windows(ctx.configuration): + return ";" + else: + return ":" + +# This is a quick and dirty rule to make Bazel compile itself. It's not +# production ready. + +def java_library_impl(ctx): + class_jar = ctx.outputs.class_jar + compile_time_jars = set(order="link") + runtime_jars = set(order="link") + for dep in ctx.targets.deps: + compile_time_jars += dep.compile_time_jars + runtime_jars += dep.runtime_jars + + jars = jar_filetype.filter(ctx.files.jars) + compile_time_jars += jars + runtime_jars += jars + compile_time_jars_list = list(compile_time_jars) # TODO: This is weird. + + build_output = class_jar.path + ".build_output" + sources = ctx.files.srcs + + sources_param_file = ctx.new_file( + ctx.configuration.bin_dir, class_jar, "-2.params") + ctx.file_action( + output = sources_param_file, + content = cmd_helper.join_paths("\n", set(sources)), + executable = False) + + # Cleaning build output directory + cmd = "set -e;rm -rf " + build_output + ";mkdir " + build_output + "\n" + if ctx.files.srcs: + cmd += JAVA_PATH + "javac" + if compile_time_jars: + cmd += " -classpath '" + cmd_helper.join_paths(path_separator(ctx), compile_time_jars) + "'" + cmd += " -d " + build_output + " @" + sources_param_file.path + "\n" + + # We haven't got a good story for where these should end up, so + # stick them in the root of the jar. + for r in ctx.files.resources: + cmd += "cp %s %s\n" % (r.path, build_output) + cmd += (JAVA_PATH + "jar cf " + class_jar.path + " -C " + build_output + " .\n" + + "touch " + build_output + "\n") + ctx.action( + inputs = (sources + compile_time_jars_list + [sources_param_file] + + ctx.files.resources), + outputs = [class_jar], + mnemonic='Javac', + command=cmd, + use_default_shell_env=True) + + runfiles = ctx.runfiles(collect_data = True) + + return struct(files = set([class_jar]), + compile_time_jars = compile_time_jars + [class_jar], + runtime_jars = runtime_jars + [class_jar], + runfiles = runfiles) + + +def java_binary_impl(ctx): + library_result = java_library_impl(ctx) + + deploy_jar = ctx.outputs.deploy_jar + manifest = ctx.outputs.manifest + build_output = deploy_jar.path + ".build_output" + main_class = ctx.attr.main_class + ctx.file_action( + output = manifest, + content = "Main-Class: " + main_class + "\n", + executable = False) + + # Cleaning build output directory + cmd = "set -e;rm -rf " + build_output + ";mkdir " + build_output + "\n" + for jar in library_result.runtime_jars: + cmd += "unzip -qn " + jar.path + " -d " + build_output + "\n" + cmd += (JAVA_PATH + "jar cmf " + manifest.path + " " + + deploy_jar.path + " -C " + build_output + " .\n" + + "touch " + build_output + "\n") + + ctx.action( + inputs=list(library_result.runtime_jars) + [manifest], + outputs=[deploy_jar], + mnemonic='Deployjar', + command=cmd, + use_default_shell_env=True) + + # Write the wrapper. + executable = ctx.outputs.executable + ctx.file_action( + output = executable, + content = '\n'.join([ + "#!/bin/bash", + "# autogenerated - do not edit.", + "case \"$0\" in", + "/*) self=\"$0\" ;;", + "*) self=\"$PWD/$0\";;", + "esac", + "", + "if [[ -z \"$JAVA_RUNFILES\" ]]; then", + " if [[ -e \"${self}.runfiles\" ]]; then", + " export JAVA_RUNFILES=\"${self}.runfiles\"", + " fi", + " if [[ -n \"$JAVA_RUNFILES\" ]]; then", + " export TEST_SRCDIR=${TEST_SRCDIR:-$JAVA_RUNFILES}", + " fi", + "fi", + "", + + "jvm_bin=%s" % (ctx.file.javabin.path), + "if [[ ! -x ${jvm_bin} ]]; then", + " jvm_bin=$(which java)", + "fi", + + # We extract the .so into a temp dir. If only we could mmap + # directly from the zip file. + "DEPLOY=$(dirname $self)/$(basename %s)" % deploy_jar.path, + + # This works both on Darwin and Linux, with the darwin path + # looking like tmp.XXXXXXXX.{random} + "SO_DIR=$(mktemp -d -t tmp.XXXXXXXXX)", + "function cleanup() {", + " rm -rf ${SO_DIR}", + "}", + "trap cleanup EXIT", + "unzip -q -d ${SO_DIR} ${DEPLOY} \"*.so\" \"*.dll\" \"*.dylib\" >& /dev/null", + ("${jvm_bin} -Djava.library.path=${SO_DIR} %s -jar $DEPLOY \"$@\"" + % ' '.join(ctx.attr.jvm_flags)) , + "", + ]), + executable = True) + + runfiles = ctx.runfiles(files = [ + deploy_jar, executable] + ctx.files.jvm, collect_data = True) + files_to_build = set([deploy_jar, manifest, executable]) + files_to_build += library_result.files + + return struct(files = files_to_build, runfiles = runfiles) + + +def java_import_impl(ctx): + # TODO(bazel-team): Why do we need to filter here? The attribute + # already says only jars are allowed. + jars = set(jar_filetype.filter(ctx.files.jars)) + runfiles = ctx.runfiles(collect_data = True) + return struct(files = jars, + compile_time_jars = jars, + runtime_jars = jars, + runfiles = runfiles) + + +java_library_attrs = { + "data": attr.label_list(allow_files=True, cfg=DATA_CFG), + "resources": attr.label_list(allow_files=True), + "srcs": attr.label_list(allow_files=java_filetype), + "jars": attr.label_list(allow_files=jar_filetype), + "deps": attr.label_list( + allow_files=False, + providers = ["compile_time_jars", "runtime_jars"]), + } + +java_library = rule( + java_library_impl, + attrs = java_library_attrs, + outputs = { + "class_jar": "lib%{name}.jar", + }) + +java_binary_attrs_common = java_library_attrs + { + "jvm_flags": attr.string_list(), + "jvm": attr.label(default=Label("//tools/jdk:jdk"), allow_files=True), + "javabin": attr.label(default=Label("//tools/jdk:java"), single_file=True), + "args": attr.string_list(), +} + +java_binary_attrs = java_binary_attrs_common + { + "main_class": attr.string(mandatory=True), +} + +java_binary_outputs = { + "class_jar": "lib%{name}.jar", + "deploy_jar": "%{name}_deploy.jar", + "manifest": "%{name}_MANIFEST.MF" +} + +java_binary = rule(java_binary_impl, + executable = True, + attrs = java_binary_attrs, + outputs = java_binary_outputs) + +java_test = rule(java_binary_impl, + executable = True, + attrs = java_binary_attrs_common + { + "main_class": attr.string(default="org.junit.runner.JUnitCore"), + # TODO(bazel-team): it would be better if we could offer a + # test_class attribute, but the "args" attribute is hard + # coded in the bazel infrastructure. + }, + outputs = java_binary_outputs, + test = True, +) + +java_import = rule( + java_import_impl, + attrs = { + "jars": attr.label_list(allow_files=jar_filetype), + "srcjar": attr.label(allow_files=srcjar_filetype), + }) -- cgit v1.2.3