Skip to content

Commit 8a0939e

Browse files
committed
Add Bazel rules for Java
1 parent e996f27 commit 8a0939e

File tree

7 files changed

+117
-33
lines changed

7 files changed

+117
-33
lines changed

WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ http_archive(
2222
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories")
2323

2424
closure_repositories()
25+

java/BUILD

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
java_library(
3+
name = "openlocationcode",
4+
srcs = [
5+
"com/google/openlocationcode/OpenLocationCode.java",
6+
],
7+
)
8+
9+
java_test(
10+
name = "encoding_Test",
11+
srcs = [
12+
"com/google/openlocationcode/tests/EncodingTest.java",
13+
],
14+
test_class = "com.google.openlocationcode.tests.EncodingTest",
15+
size = "small",
16+
deps = [
17+
":openlocationcode",
18+
],
19+
data = [
20+
"//test_data:test_data"
21+
],
22+
)
23+
24+
java_test(
25+
name = "precision_test",
26+
srcs = [
27+
"com/google/openlocationcode/tests/PrecisionTest.java",
28+
],
29+
test_class = "com.google.openlocationcode.tests.PrecisionTest",
30+
size = "small",
31+
deps = [
32+
":openlocationcode",
33+
],
34+
data = [
35+
"//test_data:test_data"
36+
],
37+
)
38+
39+
java_test(
40+
name = "shortening_test",
41+
srcs = [
42+
"com/google/openlocationcode/tests/ShorteningTest.java",
43+
],
44+
test_class = "com.google.openlocationcode.tests.ShorteningTest",
45+
size = "small",
46+
deps = [
47+
":openlocationcode",
48+
],
49+
data = [
50+
"//test_data:test_data"
51+
],
52+
)
53+
54+
java_test(
55+
name = "validity_test",
56+
srcs = [
57+
"com/google/openlocationcode/tests/ValidityTest.java",
58+
],
59+
test_class = "com.google.openlocationcode.tests.ValidityTest",
60+
size = "small",
61+
deps = [
62+
":openlocationcode",
63+
],
64+
data = [
65+
"//test_data:test_data"
66+
],
67+
)

java/README.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
# Java Open Location Code library
22

3-
## Building
3+
## Building and Testing
44

5-
Create an empty `build` directory:
5+
Included is a `BUILD` file that uses the [Bazel](https://bazel.build/) build system to produce a JAR file and to run tests. You will need to install Bazel on your system to run the tests.
66

7-
```shell
8-
mkdir build
9-
```
7+
### Building the JAR file
108

11-
Compile the `com/google/openlocationcode/OpenLocationCode.java` file and then
12-
package it as a jar file:
9+
To build a JAR file, run:
1310

14-
```shell
15-
javac -d build com/google/openlocationcode/OpenLocationCode.java
16-
cd build
17-
jar cvf OpenLocationCode.jar com/google/openlocationcode/OpenLocationCode*.class
18-
cd ..
1911
```
20-
That's it - you'll have a jar file in `build/OpenLocationCode.jar`.
12+
$ bazel build java:openlocationcode
13+
INFO: Found 1 target...
14+
Target //java:openlocationcode up-to-date:
15+
bazel-bin/java/libopenlocationcode.jar
16+
INFO: Elapsed time: 3.107s, Critical Path: 0.22s
17+
$
18+
```
2119

22-
## Testing
20+
The JAR file is accessable using the path shown in the output.
2321

24-
Download the `junit` and `hamcrest` jar files from [their repository](https://github.com/junit-team/junit4/wiki/Download-and-Install)
25-
and place them somewhere.
22+
If you cannot install Bazel, you can build the JAR file manually with:
2623

27-
(This will assume you downloaded `junit-41.2.jar` and `hamcrest-core-1.3.jar`)
24+
```
25+
mkdir build
26+
javac -d build com/google/openlocationcode/OpenLocationCode.java
27+
```
2828

29-
Build the `OpenLocationCode.jar` file as above.
29+
This will create a JAR file in the `build` directory. Change that to a suitable location.
3030

31-
Add all three files to your `CLASSPATH` variable (obviously use the real paths to the files):
31+
### Running tests
3232

33-
```shell
34-
CLASSPATH=$CLASSPATH:build/OpenLocationCode.jar:/path/to/junit-4.12.jar:/path/to/hamcrest-core-1.3.jar
35-
```
33+
The tests read their data from the [`test_data`](https://github.com/google/open-location-code/tree/master/test_data) directory.
3634

37-
Compile the test classes:
35+
Run the tests from the top-level github directory. This command will build the JAR file and test classes, and execute them:
3836

39-
```shell
40-
javac -cp $CLASSPATH -d build com/google/openlocationcode/tests/*java
4137
```
42-
43-
Run the tests. Note that we need to use the `-cp` argument to give the location of the test classes and the test data files:
44-
45-
```shell
46-
java -cp $CLASSPATH:build:../test_data: org.junit.runner.JUnitCore com.google.openlocationcode.tests.EncodingTest com.google.openlocationcode.tests.PrecisionTest com.google.openlocationcode.tests.ShorteningTest com.google.openlocationcode.tests.ValidityTest
38+
$ bazel test java:all
39+
INFO: Found 1 target and 4 test targets...
40+
INFO: Elapsed time: 0.657s, Critical Path: 0.46s
41+
//java:encoding_Test PASSED in 0.4s
42+
//java:precision_test PASSED in 0.4s
43+
//java:shortening_test PASSED in 0.4s
44+
//java:validity_test PASSED in 0.4s
45+
46+
Executed 4 out of 4 tests: 4 tests pass.
47+
$
4748
```
49+

java/com/google/openlocationcode/tests/EncodingTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.junit.Test;
88

99
import java.io.BufferedReader;
10+
import java.io.File;
11+
import java.io.FileInputStream;
1012
import java.io.InputStream;
1113
import java.io.InputStreamReader;
1214
import java.util.ArrayList;
@@ -46,7 +48,8 @@ public TestData(String line) {
4648

4749
@Before
4850
public void setUp() throws Exception {
49-
InputStream testDataStream = ClassLoader.getSystemResourceAsStream("encodingTests.csv");
51+
File testFile = new File(System.getenv("JAVA_RUNFILES"), "openlocationcode/test_data/encodingTests.csv");
52+
InputStream testDataStream = new FileInputStream(testFile);
5053
BufferedReader reader = new BufferedReader(new InputStreamReader(testDataStream));
5154
String line;
5255
while ((line = reader.readLine()) != null) {

java/com/google/openlocationcode/tests/ShorteningTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.junit.Test;
88

99
import java.io.BufferedReader;
10+
import java.io.File;
11+
import java.io.FileInputStream;
1012
import java.io.InputStream;
1113
import java.io.InputStreamReader;
1214
import java.util.ArrayList;
@@ -38,7 +40,8 @@ public TestData(String line) {
3840

3941
@Before
4042
public void setUp() throws Exception {
41-
InputStream testDataStream = ClassLoader.getSystemResourceAsStream("shortCodeTests.csv");
43+
File testFile = new File(System.getenv("JAVA_RUNFILES"), "openlocationcode/test_data/shortCodeTests.csv");
44+
InputStream testDataStream = new FileInputStream(testFile);
4245
BufferedReader reader = new BufferedReader(new InputStreamReader(testDataStream));
4346
String line;
4447
while ((line = reader.readLine()) != null) {

java/com/google/openlocationcode/tests/ValidityTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.junit.Test;
88

99
import java.io.BufferedReader;
10+
import java.io.File;
11+
import java.io.FileInputStream;
1012
import java.io.InputStream;
1113
import java.io.InputStreamReader;
1214
import java.util.ArrayList;
@@ -42,7 +44,8 @@ public TestData(String line) {
4244

4345
@Before
4446
public void setUp() throws Exception {
45-
InputStream testDataStream = ClassLoader.getSystemResourceAsStream("validityTests.csv");
47+
File testFile = new File(System.getenv("JAVA_RUNFILES"), "openlocationcode/test_data/validityTests.csv");
48+
InputStream testDataStream = new FileInputStream(testFile);
4649
BufferedReader reader = new BufferedReader(new InputStreamReader(testDataStream));
4750
String line;
4851
while ((line = reader.readLine()) != null) {

test_data/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filegroup(
2+
name = "test_data",
3+
srcs = glob(["*.csv"]),
4+
visibility = ["//visibility:public"],
5+
)

0 commit comments

Comments
 (0)