Skip to content

Commit

Permalink
Initial Bazel build definition
Browse files Browse the repository at this point in the history
  • Loading branch information
odisseus committed Feb 28, 2023
1 parent e5ecca2 commit 4ceae67
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 41 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/ant.yml

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ TEST-io.ipfs.api.APITest.*
.idea/*
api.iml

# Bazel
/bazel-*
/.ijwb/

10 changes: 10 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
java_library (
name = "maven_deps",
visibility = ["//visibility:public"],
exports = [
"@maven//:com_github_multiformats_java_multiaddr",
"@maven//:com_github_multiformats_java_multihash",
"@maven//:com_github_multiformats_java_multibase",
"@maven//:com_github_ipld_java_cid",
],
)
34 changes: 34 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "4.5"
RULES_JVM_EXTERNAL_SHA = "b17d7388feb9bfa7f2fa09031b32707df529f26c91ab9e5d909eb1676badd9a6"

http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()

load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")

rules_jvm_external_setup()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"junit:junit:4.13.2",
"org.hamcrest:hamcrest:2.2",
"com.github.multiformats:java-multiaddr:v1.4.10",
],
repositories = [
# Private repositories are supported through HTTP Basic auth
"https://jitpack.io",
"https://repo1.maven.org/maven2",
],
)
11 changes: 11 additions & 0 deletions src/main/java/io/ipfs/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
java_library (
name = "core",
visibility = ["//visibility:public"],
srcs = glob(["*.java"]),
deps = [
"//src/main/java/io/ipfs/api/cbor:cbor",
"//:maven_deps",
],
)

#TODO generate a distribution JAR with all the classes and the manifest
8 changes: 8 additions & 0 deletions src/main/java/io/ipfs/api/cbor/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
java_library (
name = "cbor",
visibility = ["//visibility:public"],
srcs = glob(["*.java"]),
deps = [
"//:maven_deps",
],
)
3 changes: 2 additions & 1 deletion src/main/java/io/ipfs/api/cbor/CborDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import java.io.*;
import java.util.Arrays;

import static io.ipfs.api.cbor.CborConstants.*;
import static io.ipfs.api.cbor.CborType.*;
Expand All @@ -30,7 +31,7 @@ public CborDecoder(InputStream is) {
}

private static void fail(String msg, Object... args) throws IOException {
throw new IOException(msg + args);
throw new IOException(msg + Arrays.toString(args));
}

private static String lengthToString(int len) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/ipfs/api/demo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
java_binary (
name = "demo",
srcs = glob(["*.java"]),
main_class = "io.ipfs.api.demo.UsageMFSFilesAPI",
deps = [
"//src/main/java/io/ipfs/api:core",
"//:maven_deps",
],
)
15 changes: 15 additions & 0 deletions src/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load(":test_utils.bzl", "junit_test")

filegroup(
name = "test-resources",
srcs = glob([
"resources/**",
]),
)

junit_test(test_class = "io.ipfs.api.AddTest")
junit_test(test_class = "io.ipfs.api.APITest")
junit_test(test_class = "io.ipfs.api.RecursiveAddTest")
junit_test(test_class = "io.ipfs.api.SimpleAddTest")
junit_test(test_class = "io.ipfs.api.VersionsTest")

39 changes: 21 additions & 18 deletions src/test/java/io/ipfs/api/APITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,17 @@ public void directoryTest() throws IOException {
throw new IllegalStateException("Different contents!");
}

// @Test
@Ignore
@Test
public void largeFileTest() throws IOException {
byte[] largerData = new byte[100*1024*1024];
new Random(1).nextBytes(largerData);
NamedStreamable.ByteArrayWrapper largeFile = new NamedStreamable.ByteArrayWrapper("nontrivial.txt", largerData);
fileTest(largeFile);
}

// @Test
@Ignore
@Test
public void hugeFileStreamTest() throws IOException {
byte[] hugeData = new byte[1000*1024*1024];
new Random(1).nextBytes(hugeData);
Expand Down Expand Up @@ -460,8 +462,8 @@ public void objectTest() throws IOException {
MerkleNode _new = ipfs.object._new(Optional.empty());
Multihash pointer = Multihash.fromBase58("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
MerkleNode object = ipfs.object.get(pointer);
List<MerkleNode> newPointer = ipfs.object.put(Arrays.asList(object.toJSONString().getBytes()));
List<MerkleNode> newPointer2 = ipfs.object.put("json", Arrays.asList(object.toJSONString().getBytes()));
List<MerkleNode> newPointer = ipfs.object.put(Collections.singletonList(object.toJSONString().getBytes()));
List<MerkleNode> newPointer2 = ipfs.object.put("json", Collections.singletonList(object.toJSONString().getBytes()));
MerkleNode links = ipfs.object.links(pointer);
byte[] data = ipfs.object.data(pointer);
Map stat = ipfs.object.stat(pointer);
Expand All @@ -472,7 +474,7 @@ public void blockTest() throws IOException {
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
Map stat = ipfs.block.stat(pointer.hash);
byte[] object = ipfs.block.get(pointer.hash);
List<MerkleNode> newPointer = ipfs.block.put(Arrays.asList("Some random data...".getBytes()));
List<MerkleNode> newPointer = ipfs.block.put(Collections.singletonList("Some random data...".getBytes()));
}

@Test
Expand Down Expand Up @@ -556,15 +558,15 @@ public void merkleLinkInMap() throws IOException {
Random r = new Random();
CborObject.CborByteArray target = new CborObject.CborByteArray(("g'day IPFS!").getBytes());
byte[] rawTarget = target.toByteArray();
MerkleNode targetRes = ipfs.block.put(Arrays.asList(rawTarget), Optional.of("cbor")).get(0);
MerkleNode targetRes = ipfs.block.put(Collections.singletonList(rawTarget), Optional.of("cbor")).get(0);

CborObject.CborMerkleLink link = new CborObject.CborMerkleLink(targetRes.hash);
Map<String, CborObject> m = new TreeMap<>();
m.put("alink", link);
m.put("arr", new CborObject.CborList(Collections.emptyList()));
CborObject.CborMap source = CborObject.CborMap.build(m);
byte[] rawSource = source.toByteArray();
MerkleNode sourceRes = ipfs.block.put(Arrays.asList(rawSource), Optional.of("cbor")).get(0);
MerkleNode sourceRes = ipfs.block.put(Collections.singletonList(rawSource), Optional.of("cbor")).get(0);

CborObject.fromByteArray(rawSource);

Expand All @@ -587,23 +589,23 @@ public void merkleLinkInMap() throws IOException {
public void recursiveRefs() throws IOException {
CborObject.CborByteArray leaf1 = new CborObject.CborByteArray(("G'day IPFS!").getBytes());
byte[] rawLeaf1 = leaf1.toByteArray();
MerkleNode leaf1Res = ipfs.block.put(Arrays.asList(rawLeaf1), Optional.of("cbor")).get(0);
MerkleNode leaf1Res = ipfs.block.put(Collections.singletonList(rawLeaf1), Optional.of("cbor")).get(0);

CborObject.CborMerkleLink link = new CborObject.CborMerkleLink(leaf1Res.hash);
Map<String, CborObject> m = new TreeMap<>();
m.put("link1", link);
CborObject.CborMap source = CborObject.CborMap.build(m);
MerkleNode sourceRes = ipfs.block.put(Arrays.asList(source.toByteArray()), Optional.of("cbor")).get(0);
MerkleNode sourceRes = ipfs.block.put(Collections.singletonList(source.toByteArray()), Optional.of("cbor")).get(0);

CborObject.CborByteArray leaf2 = new CborObject.CborByteArray(("G'day again, IPFS!").getBytes());
byte[] rawLeaf2 = leaf2.toByteArray();
MerkleNode leaf2Res = ipfs.block.put(Arrays.asList(rawLeaf2), Optional.of("cbor")).get(0);
MerkleNode leaf2Res = ipfs.block.put(Collections.singletonList(rawLeaf2), Optional.of("cbor")).get(0);

Map<String, CborObject> m2 = new TreeMap<>();
m2.put("link1", new CborObject.CborMerkleLink(sourceRes.hash));
m2.put("link2", new CborObject.CborMerkleLink(leaf2Res.hash));
CborObject.CborMap source2 = CborObject.CborMap.build(m2);
MerkleNode rootRes = ipfs.block.put(Arrays.asList(source2.toByteArray()), Optional.of("cbor")).get(0);
MerkleNode rootRes = ipfs.block.put(Collections.singletonList(source2.toByteArray()), Optional.of("cbor")).get(0);

List<Multihash> refs = ipfs.refs(rootRes.hash, false);
boolean correct = refs.contains(sourceRes.hash) && refs.contains(leaf2Res.hash) && refs.size() == 2;
Expand All @@ -625,14 +627,14 @@ public void rootMerkleLink() throws IOException {
Random r = new Random();
CborObject.CborByteArray target = new CborObject.CborByteArray(("g'day IPFS!" + r.nextInt()).getBytes());
byte[] rawTarget = target.toByteArray();
MerkleNode block1 = ipfs.block.put(Arrays.asList(rawTarget), Optional.of("cbor")).get(0);
MerkleNode block1 = ipfs.block.put(Collections.singletonList(rawTarget), Optional.of("cbor")).get(0);
Multihash block1Hash = block1.hash;
byte[] retrievedObj1 = ipfs.block.get(block1Hash);
Assert.assertTrue("get inverse of put", Arrays.equals(retrievedObj1, rawTarget));

CborObject.CborMerkleLink cbor2 = new CborObject.CborMerkleLink(block1.hash);
byte[] obj2 = cbor2.toByteArray();
MerkleNode block2 = ipfs.block.put(Arrays.asList(obj2), Optional.of("cbor")).get(0);
MerkleNode block2 = ipfs.block.put(Collections.singletonList(obj2), Optional.of("cbor")).get(0);
byte[] retrievedObj2 = ipfs.block.get(block2.hash);
Assert.assertTrue("get inverse of put", Arrays.equals(retrievedObj2, obj2));

Expand All @@ -655,7 +657,7 @@ public void rootMerkleLink() throws IOException {
public void rootNull() throws IOException {
CborObject.CborNull cbor = new CborObject.CborNull();
byte[] obj = cbor.toByteArray();
MerkleNode block = ipfs.block.put(Arrays.asList(obj), Optional.of("cbor")).get(0);
MerkleNode block = ipfs.block.put(Collections.singletonList(obj), Optional.of("cbor")).get(0);
byte[] retrievedObj = ipfs.block.get(block.hash);
Assert.assertTrue("get inverse of put", Arrays.equals(retrievedObj, obj));

Expand All @@ -676,12 +678,12 @@ public void merkleLinkInList() throws IOException {
Random r = new Random();
CborObject.CborByteArray target = new CborObject.CborByteArray(("g'day IPFS!" + r.nextInt()).getBytes());
byte[] rawTarget = target.toByteArray();
MerkleNode targetRes = ipfs.block.put(Arrays.asList(rawTarget), Optional.of("cbor")).get(0);
MerkleNode targetRes = ipfs.block.put(Collections.singletonList(rawTarget), Optional.of("cbor")).get(0);

CborObject.CborMerkleLink link = new CborObject.CborMerkleLink(targetRes.hash);
CborObject.CborList source = new CborObject.CborList(Arrays.asList(link));
CborObject.CborList source = new CborObject.CborList(Collections.singletonList(link));
byte[] rawSource = source.toByteArray();
MerkleNode sourceRes = ipfs.block.put(Arrays.asList(rawSource), Optional.of("cbor")).get(0);
MerkleNode sourceRes = ipfs.block.put(Collections.singletonList(rawSource), Optional.of("cbor")).get(0);

List<Multihash> add = ipfs.pin.add(sourceRes.hash);
ipfs.repo.gc();
Expand Down Expand Up @@ -930,7 +932,8 @@ public void addArgsTest() {
Assert.assertTrue("args toQueryString() format", queryStr.equals("inline=true&cid-version=1"));
}

// this api is disabled until deployment over IPFS is enabled
@Ignore("This api is disabled until deployment over IPFS is enabled")
@Test
public void updateTest() throws IOException {
Object check = ipfs.update.check();
Object update = ipfs.update();
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/io/ipfs/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
java_library (
name = "test-core",
visibility = ["//visibility:public"],
srcs = glob(["*.java"]),
deps = [
"//src/main/java/io/ipfs/api:core",
"//src/main/java/io/ipfs/api/cbor:cbor",
"//:maven_deps",
"@maven//:junit_junit",
"@maven//:org_hamcrest_hamcrest",
],
)
14 changes: 14 additions & 0 deletions src/test/test_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def junit_test(test_class):
native.java_test(
name = test_class,
test_class = test_class,
runtime_deps = [
"//src/test/java/io/ipfs/api:test-core",
],
data = [
"//src/test:test-resources",
],
jvm_flags = [
"-Djava.security.manager=allow",
]
)

0 comments on commit 4ceae67

Please sign in to comment.