aboutsummaryrefslogtreecommitdiffhomepage
path: root/base_workspace/examples/java-skylark
diff options
context:
space:
mode:
authorGravatar Kristina Chodorow <kchodorow@google.com>2015-02-20 15:45:07 +0000
committerGravatar Han-Wen Nienhuys <hanwen@google.com>2015-02-20 15:45:07 +0000
commit254aee40df78e79ac1b19fe6d20ae20bb05129a8 (patch)
tree2a94af911e09d62782086b56e1d63b501714b843 /base_workspace/examples/java-skylark
parent24e957158dad847c7a61fc40e49b7eae04335a67 (diff)
Improve Java examples
-- MOS_MIGRATED_REVID=86787893
Diffstat (limited to 'base_workspace/examples/java-skylark')
-rw-r--r--base_workspace/examples/java-skylark/README.md7
-rw-r--r--base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD21
-rw-r--r--base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java45
-rw-r--r--base_workspace/examples/java-skylark/src/main/resources/BUILD6
-rw-r--r--base_workspace/examples/java-skylark/src/main/resources/greeting.txt1
-rw-r--r--base_workspace/examples/java-skylark/src/test/java/com/example/myproject/BUILD16
-rw-r--r--base_workspace/examples/java-skylark/src/test/java/com/example/myproject/Fail.java7
-rw-r--r--base_workspace/examples/java-skylark/src/test/java/com/example/myproject/TestHello.java35
8 files changed, 138 insertions, 0 deletions
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().
+ *
+ * <p>With an empty args array, {@link Greeter} should print "Hello world". If there are one or more
+ * args, {@link Greeter} should print "Hello &lt;arg[0]&gt;".</p>
+ */
+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));
+ }
+
+}