1 package eu.fbk.knowledgestore.runtime;
2
3 import java.io.InputStream;
4 import java.util.Arrays;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.annotation.Nullable;
9
10 import com.google.common.base.Objects;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.openrdf.model.Statement;
17 import org.openrdf.model.URI;
18 import org.openrdf.model.impl.URIImpl;
19 import org.openrdf.rio.RDFFormat;
20
21 import eu.fbk.knowledgestore.internal.rdf.RDFUtil;
22
23 public class FactoryTest {
24
25 private static final Person ALICE = Person.create("Alice", 84);
26
27 private static final Person JACK = Person.create("Jack", 0, ALICE);
28
29 private static final Person JOHN = Person.create("John", 30, JACK, ALICE);
30
31 private static final String BASE = "java:eu.fbk.knowledgestore.runtime.FactoryTest$Person";
32
33 @Test
34 public void testConstructor() {
35 Assert.assertEquals(ALICE, Factory.instantiate(
36 ImmutableMap.<String, Object>of("name", "Alice", "age", 84),
37 new URIImpl(FactoryTest.BASE), Object.class));
38 }
39
40 @Test
41 public void testMethod() {
42 Assert.assertEquals(ALICE, Factory.instantiate(
43 ImmutableMap.<String, Object>of("name", "Alice", "age", 84),
44 new URIImpl(FactoryTest.BASE + "#create"), Object.class));
45 }
46
47 @Test
48 public void testBuilder() {
49 Assert.assertEquals(ALICE, Factory.instantiate(
50 ImmutableMap.<String, Object>of("name", "Alice", "age", 84),
51 new URIImpl(FactoryTest.BASE + "#builder"), Object.class));
52 }
53
54 @Test
55 public void testRDF() throws Throwable {
56 final InputStream stream = FactoryTest.class.getResourceAsStream("FactoryTest.ttl");
57 final List<Statement> statements = RDFUtil.readRDF(stream, RDFFormat.TURTLE, null, null,
58 false).toList();
59 stream.close();
60 final Map<URI, Object> map = Factory.instantiate(statements);
61 Assert.assertEquals(ALICE, map.get(new URIImpl("example:alice")));
62 Assert.assertEquals(JACK, map.get(new URIImpl("example:jack")));
63 Assert.assertEquals(JOHN, map.get(new URIImpl("example:john")));
64 }
65
66 public static class Person {
67
68 private final String name;
69
70 private final int age;
71
72 private final List<Person> knows;
73
74 public static Person create(final String name, final int age,
75 @Nullable final Person... knows) {
76 return new Person(name, age, 0, knows == null ? null : Arrays.asList(knows));
77 }
78
79 public Person(final String name, final int age, final int unused,
80 @Nullable final Iterable<? extends Person> knows) {
81 this.name = name;
82 this.age = age;
83 this.knows = knows == null ? ImmutableList.<Person>of() : ImmutableList.copyOf(knows);
84 }
85
86 @Override
87 public boolean equals(final Object object) {
88 if (object == this) {
89 return true;
90 }
91 if (!(object instanceof Person)) {
92 return false;
93 }
94 final Person other = (Person) object;
95 return Objects.equal(this.name, other.name) && this.age == other.age
96 && Objects.equal(this.knows, other.knows);
97 }
98
99 @Override
100 public int hashCode() {
101 return Objects.hashCode(this.name, this.age, this.knows);
102 }
103
104 @Override
105 public String toString() {
106 final StringBuilder builder = new StringBuilder();
107 builder.append(this.name).append(", ").append(this.age).append(" years old");
108 if (!this.knows.isEmpty()) {
109 builder.append(", knows ");
110 String separator = "";
111 for (final Person p : this.knows) {
112 builder.append(separator).append(p.name);
113 separator = ", ";
114 }
115 }
116 return builder.toString();
117 }
118
119 public static Builder builder() {
120 return new Builder();
121 }
122
123 public static class Builder {
124
125 @Nullable
126 private String name;
127
128 private int age;
129
130 @Nullable
131 private Iterable<? extends Person> knows;
132
133 public Builder name(final String name) {
134 this.name = name;
135 return this;
136 }
137
138 public Builder withAge(final int age) {
139 this.age = age;
140 return this;
141 }
142
143 public Builder setUnused(final int unused) {
144 return this;
145 }
146
147 public Builder setKnows(@Nullable final Iterable<? extends Person> knows) {
148 this.knows = knows;
149 return this;
150 }
151
152 public Person build() {
153 return new Person(this.name, this.age, 0, this.knows);
154 }
155
156 }
157
158 }
159
160 }