Skip to content
Browse files

Initial refactoring for new split-artifact structure.

- Extracted SPI to new halbuilder-api artifact
- Extracted test resources to halbuilder-test-resources
- Extracted Guava based API methods to halbuilder-java
- Reworked implementation/tests for new API
  • Loading branch information...
1 parent 7986955 commit 1de125f0e00cbda64e87c4a9965df3fafb966859 @talios talios committed Sep 17, 2012
Showing with 357 additions and 805 deletions.
  1. +37 −7 pom.xml
  2. +1 −1 src/it/clojure-hal/pom.xml
  3. +1 −1 src/it/clojure-hal/src/test/clojure/haltest.clj
  4. +22 −15 .../com/theoryinpractise/halbuilder/{RepresentationFactory.java → DefaultRepresentationFactory.java}
  5. +1 −1 src/main/java/com/theoryinpractise/halbuilder/impl/api/RepresentationReader.java
  6. +2 −2 src/main/java/com/theoryinpractise/halbuilder/impl/bytecode/InterfaceContract.java
  7. +12 −17 src/main/java/com/theoryinpractise/halbuilder/impl/bytecode/InterfaceRenderer.java
  8. +18 −18 src/main/java/com/theoryinpractise/halbuilder/impl/json/JsonRenderer.java
  9. +16 −13 src/main/java/com/theoryinpractise/halbuilder/impl/json/JsonRepresentationReader.java
  10. +42 −60 src/main/java/com/theoryinpractise/halbuilder/impl/representations/BaseRepresentation.java
  11. +14 −10 src/main/java/com/theoryinpractise/halbuilder/impl/representations/ImmutableRepresentation.java
  12. +1 −1 src/main/java/com/theoryinpractise/halbuilder/impl/representations/LinkPredicate.java
  13. +16 −61 src/main/java/com/theoryinpractise/halbuilder/impl/representations/MutableRepresentation.java
  14. +18 −19 src/main/java/com/theoryinpractise/halbuilder/impl/xml/XmlRenderer.java
  15. +12 −17 src/main/java/com/theoryinpractise/halbuilder/impl/xml/XmlRepresentationReader.java
  16. +0 −19 src/main/java/com/theoryinpractise/halbuilder/spi/Contract.java
  17. +0 −124 src/main/java/com/theoryinpractise/halbuilder/spi/Link.java
  18. +0 −164 src/main/java/com/theoryinpractise/halbuilder/spi/ReadableRepresentation.java
  19. +0 −21 src/main/java/com/theoryinpractise/halbuilder/spi/Renderer.java
  20. +0 −38 src/main/java/com/theoryinpractise/halbuilder/spi/Representation.java
  21. +0 −11 src/main/java/com/theoryinpractise/halbuilder/spi/RepresentationException.java
  22. +0 −15 src/main/java/com/theoryinpractise/halbuilder/spi/Serializable.java
  23. +8 −8 src/test/java/com/theoryinpractise/halbuilder/CollatedLinksTest.java
  24. +4 −2 src/test/java/com/theoryinpractise/halbuilder/{impl → }/ContentTypeTest.java
  25. +4 −3 src/test/java/com/theoryinpractise/halbuilder/CurrieOptimizationTest.java
  26. +30 −33 src/test/java/com/theoryinpractise/halbuilder/InterfaceSatisfactionTest.java
  27. +16 −28 src/test/java/com/theoryinpractise/halbuilder/{spi → }/LinkTest.java
  28. +35 −40 src/test/java/com/theoryinpractise/halbuilder/RenderingTest.java
  29. +8 −7 src/test/java/com/theoryinpractise/halbuilder/RepresentationFactoryTest.java
  30. +2 −2 src/test/java/com/theoryinpractise/halbuilder/ResourceBasicMethodsTest.java
  31. +27 −27 src/test/java/com/theoryinpractise/halbuilder/ResourceReaderTest.java
  32. +3 −2 src/test/java/com/theoryinpractise/halbuilder/ResourceTest.java
  33. +7 −18 src/test/java/com/theoryinpractise/halbuilder/ValidationTest.java
View
44 pom.xml
@@ -1,8 +1,9 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.theoryinpractise</groupId>
- <artifactId>halbuilder</artifactId>
+ <artifactId>halbuilder-core</artifactId>
<version>2.0.1-SNAPSHOT</version>
<description>Java based builder for the Hal specification http://stateless.co/hal_specification.html</description>
<packaging>jar</packaging>
@@ -13,8 +14,8 @@
<scm>
<developerConnection>scm:git:git@github.com:talios/halbuilder.git</developerConnection>
<url>http://github.com/talios/halbuilder</url>
- <tag>HEAD</tag>
- </scm>
+ <tag>HEAD</tag>
+ </scm>
<developers>
<developer>
@@ -81,6 +82,23 @@
<build>
<plugins>
<plugin>
+ <artifactId>maven-remote-resources-plugin</artifactId>
+ <version>1.3</version>
+ <executions>
+ <execution>
+ <id>process-remote-resources</id>
+ <goals>
+ <goal>process</goal>
+ </goals>
+ <configuration>
+ <resourceBundles>
+ <resourceBundle>com.theoryinpractise:halbuilder-test-resources:2.0.1-SNAPSHOT</resourceBundle>
+ </resourceBundles>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
<configuration>
@@ -119,16 +137,23 @@
</build>
<dependencies>
+
+ <dependency>
+ <groupId>com.theoryinpractise</groupId>
+ <artifactId>halbuilder-api</artifactId>
+ <version>2.0.1-SNAPSHOT</version>
+ </dependency>
+
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
- <version>2.0M2</version>
+ <version>2.0M7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
- <version>6.5.2</version>
+ <version>6.8</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -146,7 +171,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
- <version>12.0</version>
+ <version>13.0</version>
</dependency>
<dependency>
@@ -164,5 +189,10 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.google.code.findbugs</groupId>
+ <artifactId>jsr305</artifactId>
+ <version>1.3.9</version>
+ </dependency>
</dependencies>
</project>
View
2 src/it/clojure-hal/pom.xml
@@ -33,7 +33,7 @@
</dependency>
<dependency>
<groupId>com.theoryinpractise</groupId>
- <artifactId>halbuilder</artifactId>
+ <artifactId>halbuilder-core</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
View
2 src/it/clojure-hal/src/test/clojure/haltest.clj
@@ -2,7 +2,7 @@
(:use [clojure.test]))
(deftest haltest
- (let [representation-factory (com.theoryinpractise.halbuilder.RepresentationFactory.)
+ (let [representation-factory (com.theoryinpractise.halbuilder.DefaultRepresentationFactory.)
representation (doto (.newRepresentation representation-factory "/foo")
(.withProperty "name" "Mark")
(.withLink "home" "/home"))]
View
37 ...ise/halbuilder/RepresentationFactory.java → ...builder/DefaultRepresentationFactory.java
@@ -1,18 +1,21 @@
package com.theoryinpractise.halbuilder;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Renderer;
+import com.theoryinpractise.halbuilder.api.Representation;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.impl.ContentType;
import com.theoryinpractise.halbuilder.impl.api.RepresentationReader;
import com.theoryinpractise.halbuilder.impl.json.JsonRenderer;
import com.theoryinpractise.halbuilder.impl.json.JsonRepresentationReader;
import com.theoryinpractise.halbuilder.impl.representations.MutableRepresentation;
import com.theoryinpractise.halbuilder.impl.xml.XmlRenderer;
import com.theoryinpractise.halbuilder.impl.xml.XmlRepresentationReader;
-import com.theoryinpractise.halbuilder.spi.*;
import java.io.BufferedReader;
import java.io.Reader;
@@ -24,67 +27,71 @@
import static java.lang.String.format;
-public class RepresentationFactory {
- public static final String HAL_XML = "application/hal+xml";
- public static final String HAL_JSON = "application/hal+json";
+public class DefaultRepresentationFactory implements RepresentationFactory {
private Map<ContentType, Class<? extends Renderer>> contentRenderers = Maps.newHashMap();
private Map<ContentType, Class<? extends RepresentationReader>> representationReaders = Maps.newHashMap();
private TreeMap<String, String> namespaces = Maps.newTreeMap(Ordering.usingToString());
private List<Link> links = Lists.newArrayList();
private String baseHref;
- public RepresentationFactory() {
+ public DefaultRepresentationFactory() {
this("http://localhost");
}
- public RepresentationFactory(URI baseUri) {
+ public DefaultRepresentationFactory(URI baseUri) {
this(baseUri.toASCIIString());
}
- public RepresentationFactory(String baseHref) {
+ public DefaultRepresentationFactory(String baseHref) {
this.baseHref = baseHref;
this.contentRenderers.put(new ContentType(HAL_XML), XmlRenderer.class);
this.contentRenderers.put(new ContentType(HAL_JSON), JsonRenderer.class);
this.representationReaders.put(new ContentType(HAL_XML), XmlRepresentationReader.class);
this.representationReaders.put(new ContentType(HAL_JSON), JsonRepresentationReader.class);
}
+ @Override
public String getBaseHref() {
return baseHref;
}
- public RepresentationFactory withRenderer(String contentType, Class<? extends Renderer<String>> rendererClass) {
+ public DefaultRepresentationFactory withRenderer(String contentType, Class<? extends Renderer<String>> rendererClass) {
contentRenderers.put(new ContentType(contentType), rendererClass);
return this;
}
- public RepresentationFactory withReader(String contentType, Class<? extends RepresentationReader> readerClass) {
+ public DefaultRepresentationFactory withReader(String contentType, Class<? extends RepresentationReader> readerClass) {
representationReaders.put(new ContentType(contentType), readerClass);
return this;
}
- public RepresentationFactory withNamespace(String namespace, String url) {
+ @Override
+ public DefaultRepresentationFactory withNamespace(String namespace, String url) {
if (namespaces.containsKey(namespace)) {
throw new RepresentationException(format("Duplicate namespace '%s' found for representation factory", namespace));
}
namespaces.put(namespace, url);
return this;
}
- public RepresentationFactory withLink(String url, String rel) {
+ @Override
+ public DefaultRepresentationFactory withLink(String url, String rel) {
links.add(new Link(this, url, rel));
return this;
}
+ @Override
public Representation newRepresentation(URI uri) {
return newRepresentation(uri.toASCIIString());
}
+ @Override
public Representation newRepresentation() {
return newRepresentation((String) null);
}
+ @Override
public Representation newRepresentation(String href) {
MutableRepresentation representation = new MutableRepresentation(this, href);
@@ -95,13 +102,13 @@ public Representation newRepresentation(String href) {
// Add factory standard links
for (Link link : links) {
- representation.withLink(link.getRel(), link.getHref(),
- Optional.<Predicate<ReadableRepresentation>>absent(), link.getName(), link.getTitle(), link.getHreflang());
+ representation.withLink(link.getRel(), link.getHref(), link.getName(), link.getTitle(), link.getHreflang());
}
return representation;
}
+ @Override
public ReadableRepresentation readRepresentation(Reader reader) {
try {
Reader bufferedReader = new BufferedReader(reader);
View
2 src/main/java/com/theoryinpractise/halbuilder/impl/api/RepresentationReader.java
@@ -1,6 +1,6 @@
package com.theoryinpractise.halbuilder.impl.api;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
import java.io.Reader;
View
4 src/main/java/com/theoryinpractise/halbuilder/impl/bytecode/InterfaceContract.java
@@ -1,8 +1,8 @@
package com.theoryinpractise.halbuilder.impl.bytecode;
import com.google.common.base.Preconditions;
-import com.theoryinpractise.halbuilder.spi.Contract;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Contract;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
import java.lang.reflect.Method;
View
29 src/main/java/com/theoryinpractise/halbuilder/impl/bytecode/InterfaceRenderer.java
@@ -1,11 +1,9 @@
package com.theoryinpractise.halbuilder.impl.bytecode;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
-import com.theoryinpractise.halbuilder.spi.Renderer;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
-import java.io.Writer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@@ -15,49 +13,46 @@
/**
* Java Interface based "renderer", this will render the resource as a Proxy to a Java interface.
*/
-public class InterfaceRenderer<T> implements Renderer<T> {
+public class InterfaceRenderer<T> {
private Class<T> anInterface;
- public static InterfaceRenderer newInterfaceRenderer(Class<?> anInterface) {
- return new InterfaceRenderer(anInterface);
+ public static <I> InterfaceRenderer<I> newInterfaceRenderer(Class<I> anInterface) {
+ return new InterfaceRenderer<I>(anInterface);
}
private InterfaceRenderer(Class<T> anInterface) {
- Preconditions.checkArgument(anInterface.isInterface(), "Renderable class MUST be an interface.");
+ Preconditions.checkArgument(anInterface.isInterface(), "Provided class MUST be an interface.");
this.anInterface = anInterface;
}
- public Optional<T> render(final ReadableRepresentation representation, Writer writer) {
- Preconditions.checkArgument(writer == null, "Writer argument should be null for " + InterfaceRenderer.class.getName());
+ public T render(final ReadableRepresentation representation) {
if (representation.isSatisfiedBy(InterfaceContract.newInterfaceContract(anInterface))) {
T proxy = (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{anInterface}, new InvocationHandler() {
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
String propertyName = derivePropertyNameFromMethod(method);
- Optional<Object> propertyOptional = representation.getProperties().get(propertyName);
+ Object propertyValue = representation.getProperties().get(propertyName);
Class<?> returnType = method.getReturnType();
Object returnValue;
- if(propertyOptional.isPresent()) {
- Object propertyValue = propertyOptional.get();
+ if(propertyValue != null) {
returnValue = returnType.getConstructor(propertyValue.getClass()).newInstance(propertyValue);
- }
- else {
+ } else {
// In this case, we have a null property.
returnValue = null;
}
return returnValue;
}
});
- return Optional.of(proxy);
+ return proxy;
} else {
- return Optional.absent();
+ throw new RepresentationException("Unable to render representation to " + anInterface.getName());
}
View
36 src/main/java/com/theoryinpractise/halbuilder/impl/json/JsonRenderer.java
@@ -4,13 +4,13 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.google.common.base.Function;
-import com.google.common.base.Optional;
+import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
-import com.theoryinpractise.halbuilder.spi.Link;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
-import com.theoryinpractise.halbuilder.spi.Renderer;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Renderer;
import javax.annotation.Nullable;
import java.io.IOException;
@@ -31,7 +31,7 @@
public class JsonRenderer<T> implements Renderer<T> {
- public Optional<T> render(ReadableRepresentation representation, Writer writer) {
+ public void render(ReadableRepresentation representation, Writer writer) {
JsonFactory f = new JsonFactory();
f.enable(JsonGenerator.Feature.QUOTE_FIELD_NAMES);
@@ -46,7 +46,7 @@
} catch (IOException e) {
throw new RuntimeException(e);
}
- return Optional.absent();
+
}
private void renderJson(JsonGenerator g, ReadableRepresentation representation, boolean embedded) throws IOException {
@@ -59,7 +59,7 @@ private void renderJson(JsonGenerator g, ReadableRepresentation representation,
// Include namespaces as links when not embedded
if (!embedded) {
for (Map.Entry<String, String> entry : representation.getNamespaces().entrySet()) {
- links.add(new Link(null, entry.getValue(), CURIE, Optional.of(entry.getKey()), Optional.<String>absent(), Optional.<String>absent()));
+ links.add(new Link(null, entry.getValue(), CURIE, entry.getKey(), null, null));
}
}
@@ -92,9 +92,9 @@ public String apply(@Nullable Link link) {
g.writeEndObject();
}
- for (Map.Entry<String, Optional<Object>> entry : representation.getProperties().entrySet()) {
- if(entry.getValue().isPresent()) {
- g.writeObjectField(entry.getKey(), entry.getValue().get());
+ for (Map.Entry<String, Object> entry : representation.getProperties().entrySet()) {
+ if(entry.getValue() != null) {
+ g.writeObjectField(entry.getKey(), entry.getValue());
}
else {
g.writeNullField(entry.getKey());
@@ -104,9 +104,9 @@ public String apply(@Nullable Link link) {
if (!representation.getResources().isEmpty()) {
g.writeObjectFieldStart(EMBEDDED);
- Multimap<String, ReadableRepresentation> resourceMap = representation.getResources();
+ Map<String, Collection<ReadableRepresentation>> resourceMap = representation.getResourceMap();
- for (Map.Entry<String, Collection<ReadableRepresentation>> resourceEntry : resourceMap.asMap().entrySet()) {
+ for (Map.Entry<String, Collection<ReadableRepresentation>> resourceEntry : resourceMap.entrySet()) {
if (resourceEntry.getValue().size() == 1) {
g.writeObjectFieldStart(resourceEntry.getKey());
ReadableRepresentation subRepresentation = resourceEntry.getValue().iterator().next();
@@ -128,14 +128,14 @@ public String apply(@Nullable Link link) {
private void writeJsonLinkContent(JsonGenerator g, Link link) throws IOException {
g.writeStringField(HREF, link.getHref());
- if (link.getName().isPresent()) {
- g.writeStringField(NAME, link.getName().get());
+ if (!Strings.isNullOrEmpty(link.getName())) {
+ g.writeStringField(NAME, link.getName());
}
- if (link.getTitle().isPresent()) {
- g.writeStringField(TITLE, link.getTitle().get());
+ if (!Strings.isNullOrEmpty(link.getTitle())) {
+ g.writeStringField(TITLE, link.getTitle());
}
- if (link.getHreflang().isPresent()) {
- g.writeStringField(HREFLANG, link.getHreflang().get());
+ if (!Strings.isNullOrEmpty(link.getHreflang())) {
+ g.writeStringField(HREFLANG, link.getHreflang());
}
if (link.hasTemplate()) {
g.writeBooleanField(TEMPLATED, true);
View
29 src/main/java/com/theoryinpractise/halbuilder/impl/json/JsonRepresentationReader.java
@@ -2,19 +2,23 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.theoryinpractise.halbuilder.RepresentationFactory;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.impl.api.RepresentationReader;
import com.theoryinpractise.halbuilder.impl.representations.MutableRepresentation;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
-import com.theoryinpractise.halbuilder.spi.RepresentationException;
import java.io.Reader;
import java.util.Iterator;
import java.util.Map;
-import static com.theoryinpractise.halbuilder.impl.api.Support.*;
+import static com.theoryinpractise.halbuilder.impl.api.Support.CURIE;
+import static com.theoryinpractise.halbuilder.impl.api.Support.EMBEDDED;
+import static com.theoryinpractise.halbuilder.impl.api.Support.HREF;
+import static com.theoryinpractise.halbuilder.impl.api.Support.HREFLANG;
+import static com.theoryinpractise.halbuilder.impl.api.Support.LINKS;
+import static com.theoryinpractise.halbuilder.impl.api.Support.NAME;
+import static com.theoryinpractise.halbuilder.impl.api.Support.TITLE;
public class JsonRepresentationReader implements RepresentationReader {
private RepresentationFactory representationFactory;
@@ -90,17 +94,16 @@ private void readLinks(MutableRepresentation resource, JsonNode rootNode) {
private void withJsonLink(MutableRepresentation resource, Map.Entry<String, JsonNode> keyNode, JsonNode valueNode) {
String rel = keyNode.getKey();
String href = valueNode.get(HREF).asText();
- Optional<String> name = optionalNodeValueAsText(valueNode, NAME);
- Optional<String> title = optionalNodeValueAsText(valueNode, TITLE);
- Optional<String> hreflang = optionalNodeValueAsText(valueNode, HREFLANG);
- Optional<Predicate<ReadableRepresentation>> predicate = Optional.<Predicate<ReadableRepresentation>>absent();
+ String name = optionalNodeValueAsText(valueNode, NAME);
+ String title = optionalNodeValueAsText(valueNode, TITLE);
+ String hreflang = optionalNodeValueAsText(valueNode, HREFLANG);
- resource.withLink(rel, href, predicate, name, title, hreflang);
+ resource.withLink(rel, href, name, title, hreflang);
}
- Optional<String> optionalNodeValueAsText(JsonNode node, String key) {
+ String optionalNodeValueAsText(JsonNode node, String key) {
JsonNode value = node.get(key);
- return value != null ? Optional.of(value.asText()) : Optional.<String>absent();
+ return value != null ? value.asText() : null;
}
private void readProperties(MutableRepresentation resource, JsonNode rootNode) {
View
102 src/main/java/com/theoryinpractise/halbuilder/impl/representations/BaseRepresentation.java
@@ -2,7 +2,6 @@
import com.google.common.base.Function;
import com.google.common.base.Joiner;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
@@ -16,30 +15,28 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.common.collect.Table;
-import com.theoryinpractise.halbuilder.RepresentationFactory;
+import com.theoryinpractise.halbuilder.api.Contract;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Renderer;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.impl.api.Support;
import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceContract;
import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceRenderer;
-import com.theoryinpractise.halbuilder.spi.Contract;
-import com.theoryinpractise.halbuilder.spi.Link;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
-import com.theoryinpractise.halbuilder.spi.Renderer;
-import com.theoryinpractise.halbuilder.spi.Representation;
-import com.theoryinpractise.halbuilder.spi.RepresentationException;
import javax.annotation.Nullable;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
+import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
-import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Pattern;
-import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Ordering.usingToString;
@@ -58,7 +55,7 @@ public int compare(Link l1, Link l2) {
protected Map<String, String> namespaces = Maps.newTreeMap(usingToString());
protected List<Link> links = Lists.newArrayList();
- protected Map<String, Optional<Object>> properties = Maps.newTreeMap(usingToString());
+ protected Map<String, Object> properties = Maps.newTreeMap(usingToString());
protected Multimap<String,ReadableRepresentation> resources = ArrayListMultimap.create();
protected RepresentationFactory representationFactory;
@@ -69,8 +66,8 @@ protected BaseRepresentation(RepresentationFactory representationFactory) {
this.representationFactory = representationFactory;
}
- public Optional<Link> getResourceLink() {
- return Iterables.tryFind(getLinks(), LinkPredicate.newLinkPredicate(Support.SELF));
+ public Link getResourceLink() {
+ return Iterables.find(getLinks(), LinkPredicate.newLinkPredicate(Support.SELF), null);
}
public Map<String, String> getNamespaces() {
@@ -81,8 +78,8 @@ protected BaseRepresentation(RepresentationFactory representationFactory) {
return ImmutableList.copyOf(links);
}
- public Optional<Link> getLinkByRel(String rel) {
- return fromNullable(Iterables.getFirst(getLinksByRel(rel), null));
+ public Link getLinkByRel(String rel) {
+ return Iterables.getFirst(getLinksByRel(rel), null);
}
public List<Link> getLinksByRel(final String rel) {
@@ -113,23 +110,18 @@ protected BaseRepresentation(RepresentationFactory representationFactory) {
return ImmutableList.copyOf(Iterables.filter(resources.values(), predicate));
}
- public Optional<Object> get(String name) {
+ public Object getValue(String name) {
if (properties.containsKey(name)) {
return properties.get(name);
} else {
- return Optional.absent();
+ throw new RepresentationException("Resource does not contain " + name);
}
}
- public Object getValue(String name) {
- return getValue(name, null);
- }
-
public Object getValue(String name, Object defaultValue) {
- Optional<Object> property = get(name);
- if (property.isPresent()) {
- return property.get();
- } else {
+ try {
+ return getValue(name);
+ } catch (RepresentationException e) {
return defaultValue;
}
}
@@ -170,29 +162,29 @@ public String apply(@Nullable String relType) {
String titles = joiner.join(ordering.sortedCopy(transform(hrefLinks, new Function<Link, Object>() {
public Object apply(@Nullable Link link) {
- return link.getTitle().orNull();
+ return link.getTitle();
}
})));
String names = joiner.join(ordering.sortedCopy(transform(hrefLinks, new Function<Link, Object>() {
public Object apply(@Nullable Link link) {
- return link.getName().orNull();
+ return link.getName();
}
})));
String hreflangs = joiner.join(ordering.sortedCopy(transform(hrefLinks, new Function<Link, Object>() {
public Object apply(@Nullable Link link) {
- return link.getHreflang().orNull();
+ return link.getHreflang();
}
})));
String curiedHref = currieHref(href);
collatedLinks.add(new Link(representationFactory, curiedHref, rels,
- fromNullable(emptyToNull(names)),
- fromNullable(emptyToNull(titles)),
- fromNullable(emptyToNull(hreflangs))));
+ emptyToNull(names),
+ emptyToNull(titles),
+ emptyToNull(hreflangs)));
}
return RELATABLE_ORDERING.sortedCopy(collatedLinks);
@@ -209,19 +201,23 @@ private String currieHref(String href) {
return href;
}
- public Map<String, Optional<Object>> getProperties() {
- return ImmutableMap.copyOf(properties);
+ public Map<String, Object> getProperties() {
+ return Collections.unmodifiableMap(properties);
+ }
+
+ public Collection<Map.Entry<String, ReadableRepresentation>> getResources() {
+ return ImmutableMultimap.copyOf(resources).entries();
}
- public Multimap<String, ReadableRepresentation> getResources() {
- return ImmutableMultimap.copyOf(resources);
+ public Map<String, Collection<ReadableRepresentation>> getResourceMap() {
+ return ImmutableMap.copyOf(resources.asMap());
}
protected void validateNamespaces(ReadableRepresentation representation) {
for (Link link : representation.getCanonicalLinks()) {
validateNamespaces(link.getRel());
}
- for (Map.Entry<String, ReadableRepresentation> aResource : representation.getResources().entries()) {
+ for (Map.Entry<String, ReadableRepresentation> aResource : representation.getResources()) {
validateNamespaces(aResource.getKey());
validateNamespaces(aResource.getValue());
}
@@ -241,26 +237,16 @@ private void validateNamespaces(String sourceRel) {
/**
* Test whether the Representation in its current state satisfies the provided interface.
*
- * @param anInterface The interface we wish to check
- * @return Is that Representation structurally like the interface?
+ * @param contract The interface we wish to check
+ * @return Is that Representation satisfied by the supplied contract?
*/
public boolean isSatisfiedBy(Contract contract) {
return contract.isSatisfiedBy(this);
}
- public <T, V> Optional<V> ifSatisfiedBy(Class<T> anInterface, Function<T, V> function) {
- if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
- Optional<T> proxy = InterfaceRenderer.newInterfaceRenderer(anInterface).render(this, null);
- if (proxy.isPresent()) {
- return Optional.of(function.apply(proxy.get()));
- }
- }
- return Optional.absent();
- }
-
public String resolveRelativeHref(String href) {
- if (getResourceLink().isPresent()) {
- return resolveRelativeHref(getResourceLink().get().getHref(), href);
+ if (getResourceLink() != null) {
+ return resolveRelativeHref(getResourceLink().getHref(), href);
} else {
throw new IllegalStateException("Unable to resolve relative href with missing resource href.");
}
@@ -301,11 +287,11 @@ public ImmutableRepresentation toImmutableResource() {
* @param anInterface The interface we wish to proxy the resource as
* @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
*/
- public <T> Optional<T> renderClass(Class<T> anInterface) {
+ public <T> T toClass(Class<T> anInterface) {
if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
- return InterfaceRenderer.newInterfaceRenderer(anInterface).render(this, null);
+ return InterfaceRenderer.newInterfaceRenderer(anInterface).render(this);
} else {
- return Optional.absent();
+ throw new RepresentationException("Unable to render representation to " + anInterface.getName());
}
}
@@ -314,10 +300,6 @@ public String renderContent(String contentType) {
return renderAsString(renderer);
}
- public <T> Optional<T> resolveClass(Function<ReadableRepresentation, Optional<T>> resolver) {
- return resolver.apply(this);
- }
-
private String renderAsString(final Renderer renderer) {
validateNamespaces(this);
StringWriter sw = new StringWriter();
@@ -355,9 +337,9 @@ public boolean equals(Object obj) {
@Override
public String toString() {
- Optional<Link> href = getLinkByRel("self");
- if (href.isPresent()) {
- return "<Representation: " + href.get().getHref() + ">";
+ Link href = getLinkByRel("self");
+ if (href != null) {
+ return "<Representation: " + href.getHref() + ">";
} else {
return "<Representation: @" + Integer.toHexString(hashCode()) + ">";
}
View
24 ...in/java/com/theoryinpractise/halbuilder/impl/representations/ImmutableRepresentation.java
@@ -1,32 +1,36 @@
package com.theoryinpractise.halbuilder.impl.representations;
-import com.google.common.base.Optional;
-import com.google.common.collect.Multimap;
-import com.theoryinpractise.halbuilder.RepresentationFactory;
-import com.theoryinpractise.halbuilder.spi.Link;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
+import com.google.common.collect.ImmutableMultimap;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
public class ImmutableRepresentation extends BaseRepresentation {
- private final Optional<Link> resourceLink;
+ private final Link resourceLink;
public ImmutableRepresentation(RepresentationFactory representationFactory,
- Map<String, String> namespaces, List<Link> links, Map<String, Optional<Object>> properties, Multimap<String, ReadableRepresentation> resources, boolean hasNullProperties) {
+ Map<String, String> namespaces, List<Link> links, Map<String, Object> properties, Collection<Map.Entry<String, ReadableRepresentation>> resources, boolean hasNullProperties) {
super(representationFactory);
this.namespaces = namespaces;
this.links = links;
this.properties = properties;
- this.resources = resources;
- this.resourceLink = super.getResourceLink();
+ ImmutableMultimap.Builder<String, ReadableRepresentation> resourceBuilder = ImmutableMultimap.builder();
+ for (Map.Entry<String, ReadableRepresentation> entry : resources) {
+ resourceBuilder.putAll(entry.getKey(), entry.getValue());
+ }
+ this.resources = resourceBuilder.build();
+ this.resourceLink = super.getResourceLink();
this.hasNullProperties = hasNullProperties;
}
- public Optional<Link> getResourceLink() {
+ public Link getResourceLink() {
return resourceLink;
}
View
2 src/main/java/com/theoryinpractise/halbuilder/impl/representations/LinkPredicate.java
@@ -2,7 +2,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
-import com.theoryinpractise.halbuilder.spi.Link;
+import com.theoryinpractise.halbuilder.api.Link;
import javax.annotation.Nullable;
View
77 ...main/java/com/theoryinpractise/halbuilder/impl/representations/MutableRepresentation.java
@@ -1,11 +1,12 @@
package com.theoryinpractise.halbuilder.impl.representations;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.theoryinpractise.halbuilder.RepresentationFactory;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Representation;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
+import com.theoryinpractise.halbuilder.api.Serializable;
import com.theoryinpractise.halbuilder.impl.api.Support;
-import com.theoryinpractise.halbuilder.spi.*;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
@@ -40,71 +41,25 @@ public MutableRepresentation(RepresentationFactory representationFactory) {
* @return
*/
public MutableRepresentation withLink(String rel, String href) {
- withLink(rel, href,
- Optional.of(Predicates.<ReadableRepresentation>alwaysTrue()),
- Optional.<String>absent(),
- Optional.<String>absent(),
- Optional.<String>absent());
+ withLink(rel, href, null, null, null);
return this;
}
/**
* Add a link to this resource
*
* @param rel
- * @param uri The target URI for the link, possibly relative to the href of
- * this resource.
- * @return
- */
- public MutableRepresentation withLink(String rel, URI uri) {
- return withLink(rel, uri.toASCIIString());
- }
-
- /**
- * Add a link to this resource
- *
- * @param rel
* @param href The target href for the link, relative to the href of this resource.
- * @return
*/
- public MutableRepresentation withLink(String rel, String href, Predicate<ReadableRepresentation> predicate) {
- withLink(rel, href,
- Optional.of(predicate),
- Optional.<String>absent(),
- Optional.<String>absent(),
- Optional.<String>absent());
- return this;
- }
-
- /**
- * Add a link to this resource
- *
- * @param rel
- * @param uri The target URI for the link, possibly relative to the href of
- * this resource.
- * @return
- */
- public MutableRepresentation withLink(String rel, URI uri, Predicate<ReadableRepresentation> predicate) {
- return withLink(rel, uri.toASCIIString(), predicate);
- }
-
- /**
- * Add a link to this resource
- *
- * @param rel
- * @param href The target href for the link, relative to the href of this resource.
- * @return
- */
- public MutableRepresentation withLink(String rel, String href, Optional<Predicate<ReadableRepresentation>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang) {
+ public MutableRepresentation withLink(String rel, String href, String name, String title, String hreflang) {
Support.checkRelType(rel);
- if (predicate.or(Predicates.<ReadableRepresentation>alwaysTrue()).apply(this)) {
- String resolvedHref = resolvableUri.matcher(href).matches() ? resolveRelativeHref(href) : href;
- for (String reltype : WHITESPACE_SPLITTER.split(rel)) {
- String resolvedRelType = resolvableUri.matcher(reltype).matches() ? resolveRelativeHref(reltype) : reltype;
- links.add(new Link(representationFactory, resolvedHref, resolvedRelType, name, title, hreflang));
- }
+ String resolvedHref = resolvableUri.matcher(href).matches() ? resolveRelativeHref(href) : href;
+ for (String reltype : WHITESPACE_SPLITTER.split(rel)) {
+ String resolvedRelType = resolvableUri.matcher(reltype).matches() ? resolveRelativeHref(reltype) : reltype;
+
+ links.add(new Link(representationFactory, resolvedHref, resolvedRelType, name, title, hreflang));
}
return this;
@@ -118,8 +73,8 @@ public MutableRepresentation withLink(String rel, String href, Optional<Predicat
* this resource.
* @return
*/
- public MutableRepresentation withLink(String rel, URI uri, Optional<Predicate<ReadableRepresentation>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang) {
- return withLink(rel, uri.toASCIIString(), predicate, name, title, hreflang);
+ public MutableRepresentation withLink(String rel, URI uri) {
+ return withLink(rel, uri.toASCIIString());
}
public Representation withProperty(String name, Object value) {
@@ -129,7 +84,7 @@ public Representation withProperty(String name, Object value) {
if (null == value) {
this.hasNullProperties = true;
}
- properties.put(name, Optional.fromNullable(value));
+ properties.put(name, value);
return this;
}
View
37 src/main/java/com/theoryinpractise/halbuilder/impl/xml/XmlRenderer.java
@@ -1,9 +1,9 @@
package com.theoryinpractise.halbuilder.impl.xml;
-import com.google.common.base.Optional;
-import com.theoryinpractise.halbuilder.spi.Link;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
-import com.theoryinpractise.halbuilder.spi.Renderer;
+import com.google.common.base.Strings;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Renderer;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.Text;
@@ -28,7 +28,7 @@
public class XmlRenderer<T> implements Renderer<T> {
- public Optional<T> render(ReadableRepresentation representation, Writer writer) {
+ public void render(ReadableRepresentation representation, Writer writer) {
final Element element = renderElement("self", representation, false);
final XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
try {
@@ -37,17 +37,16 @@
throw new RuntimeException(e);
}
- return Optional.absent();
}
private Element renderElement(String rel, ReadableRepresentation representation, boolean embedded) {
- final Optional<Link> resourceLink = representation.getResourceLink();
+ final Link resourceLink = representation.getResourceLink();
// Create the root element
final Element resourceElement = new Element("resource");
- if (resourceLink.isPresent()) {
- resourceElement.setAttribute("href", resourceLink.get().getHref());
+ if (resourceLink != null) {
+ resourceElement.setAttribute("href", resourceLink.getHref());
}
if (!rel.equals("self")) {
@@ -77,14 +76,14 @@ private Element renderElement(String rel, ReadableRepresentation representation,
if (!link.getRel().equals(SELF)) {
linkElement.setAttribute(REL, link.getRel());
linkElement.setAttribute(HREF, link.getHref());
- if (link.getName().isPresent()) {
- linkElement.setAttribute(NAME, link.getName().get());
+ if (!Strings.isNullOrEmpty(link.getName())) {
+ linkElement.setAttribute(NAME, link.getName());
}
- if (link.getTitle().isPresent()) {
- linkElement.setAttribute(TITLE, link.getTitle().get());
+ if (!Strings.isNullOrEmpty(link.getTitle())) {
+ linkElement.setAttribute(TITLE, link.getTitle());
}
- if (link.getHreflang().isPresent()) {
- linkElement.setAttribute(HREFLANG, link.getHreflang().get());
+ if (!Strings.isNullOrEmpty(link.getHreflang())) {
+ linkElement.setAttribute(HREFLANG, link.getHreflang());
}
if (link.hasTemplate()) {
linkElement.setAttribute(TEMPLATED, "true");
@@ -94,18 +93,18 @@ private Element renderElement(String rel, ReadableRepresentation representation,
}
// add properties
- for (Map.Entry<String, Optional<Object>> entry : representation.getProperties().entrySet()) {
+ for (Map.Entry<String, Object> entry : representation.getProperties().entrySet()) {
Element propertyElement = new Element(entry.getKey());
- if (entry.getValue().isPresent()) {
- propertyElement.setContent(new Text(entry.getValue().get().toString()));
+ if (entry.getValue() != null) {
+ propertyElement.setContent(new Text(entry.getValue().toString()));
} else {
propertyElement.setAttribute("nil", "true", XSI_NAMESPACE);
}
resourceElement.addContent(propertyElement);
}
// add subresources
- for (Map.Entry<String, ReadableRepresentation> halResource : representation.getResources().entries()) {
+ for (Map.Entry<String, ReadableRepresentation> halResource : representation.getResources()) {
Element subResourceElement = renderElement(halResource.getKey(), halResource.getValue(), true);
resourceElement.addContent(subResourceElement);
}
View
29 src/main/java/com/theoryinpractise/halbuilder/impl/xml/XmlRepresentationReader.java
@@ -1,13 +1,11 @@
package com.theoryinpractise.halbuilder.impl.xml;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.theoryinpractise.halbuilder.RepresentationFactory;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Representation;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.impl.api.RepresentationReader;
import com.theoryinpractise.halbuilder.impl.representations.MutableRepresentation;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
-import com.theoryinpractise.halbuilder.spi.Representation;
-import com.theoryinpractise.halbuilder.spi.RepresentationException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
@@ -18,7 +16,10 @@
import java.io.Reader;
import java.util.List;
-import static com.theoryinpractise.halbuilder.impl.api.Support.*;
+import static com.theoryinpractise.halbuilder.impl.api.Support.HREFLANG;
+import static com.theoryinpractise.halbuilder.impl.api.Support.NAME;
+import static com.theoryinpractise.halbuilder.impl.api.Support.TITLE;
+import static com.theoryinpractise.halbuilder.impl.api.Support.XSI_NAMESPACE;
public class XmlRepresentationReader implements RepresentationReader {
private RepresentationFactory representationFactory;
@@ -64,21 +65,15 @@ private void readLinks(Representation resource, Element element) {
for (Element link : links) {
String rel = link.getAttributeValue("rel");
String href = link.getAttributeValue("href");
- Optional<String> name = optionalElementValueAsText(link, NAME);
- Optional<String> title = optionalElementValueAsText(link, TITLE);
- Optional<String> hreflang = optionalElementValueAsText(link, HREFLANG);
- Optional<Predicate<ReadableRepresentation>> predicate = Optional.<Predicate<ReadableRepresentation>>absent();
+ String name = link.getAttributeValue( NAME);
+ String title = link.getAttributeValue( TITLE);
+ String hreflang = link.getAttributeValue( HREFLANG);
- resource.withLink(rel, href, predicate, name, title, hreflang);
+ resource.withLink(rel, href, name, title, hreflang);
}
}
- Optional<String> optionalElementValueAsText(Element node, String key) {
- String value = node.getAttributeValue(key);
- return value != null ? Optional.of(value) : Optional.<String>absent();
- }
-
private void readProperties(Representation resource, Element element) {
List<Element> properties = element.getChildren();
for (Element property : properties) {
View
19 src/main/java/com/theoryinpractise/halbuilder/spi/Contract.java
@@ -1,19 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-/**
- * A Contract is used to assert that a given ReadableRepresentation conforms
- * to a specific set of requirements.
- *
- * Implementing this interface allows an object to assert the validatity of a resources
- * structure (required fields), business data (duplicate data) or any other contractual
- * concept.
- */
-public interface Contract {
-
- /**
- * Returns whether the representation is satisfied by this contract.
- * @param representation The representation needing satisfaction
- * @return A boolean.
- */
- boolean isSatisfiedBy(ReadableRepresentation representation);
-}
View
124 src/main/java/com/theoryinpractise/halbuilder/spi/Link.java
@@ -1,124 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-import com.google.common.base.Optional;
-import com.theoryinpractise.halbuilder.RepresentationFactory;
-import java.util.regex.Pattern;
-
-/**
- * A Link to an external resource.
- */
-public class Link {
- /** Pattern that will hit an RFC 6570 URI template. */
- private static final Pattern URI_TEMPLATE_PATTERN = Pattern.compile("\\{.+\\}");
-
- private RepresentationFactory representationFactory;
-
- private String href;
- private String rel;
- private Optional<String> name = Optional.absent();
- private Optional<String> title = Optional.absent();
- private Optional<String> hreflang = Optional.absent();
- private boolean hasTemplate = false;
-
- public Link(RepresentationFactory representationFactory, String href, String rel) {
- this.representationFactory = representationFactory;
- this.href = href;
- this.rel = rel;
- if(hasTemplate(href)) {
- this.hasTemplate = true;
- }
- }
-
- public Link(RepresentationFactory representationFactory, String href, String rel, Optional<String> name, Optional<String> title, Optional<String> hreflang) {
- this(representationFactory, href, rel);
- this.name = name;
- this.title = title;
- this.hreflang = hreflang;
- }
-
- public String getHref() {
- return href;
- }
-
- public String getRel() {
- return rel;
- }
-
- public Optional<String> getName() {
- return name;
- }
-
- public Optional<String> getTitle() {
- return title;
- }
-
- public Optional<String> getHreflang() {
- return hreflang;
- }
-
- public boolean hasTemplate() {
- return hasTemplate;
- }
-
- /**
- * Determine whether the argument href contains at least one URI template,
- * as defined in RFC 6570.
- * @param href Href to check.
- * @return True if the href contains a template, false if not (or if the
- * argument is null).
- */
- private boolean hasTemplate(String href) {
- if(href == null) {
- return false;
- }
- return URI_TEMPLATE_PATTERN.matcher(href).find();
- }
-
- @Override
- public int hashCode() {
- int h = href.hashCode();
- h += rel.hashCode();
- h += name.hashCode();
- h += title.hashCode();
- h += hreflang.hashCode();
- return h;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (!(obj instanceof Link)) {
- return false;
- }
- Link that = (Link) obj;
- boolean e = this.href.equals(that.href);
- e &= this.rel.equals(that.rel);
- e &= this.name.equals(that.name);
- e &= this.title.equals(that.title);
- e &= this.hreflang.equals(that.hreflang);
- return e;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("<link rel=\"").append(rel).append("\" href=\"").append(href).append("\"");
- if (name.isPresent()) {
- sb.append(" name=\"").append(name.get()).append("\"");
- }
- if (title.isPresent()) {
- sb.append(" title=\"").append(title.get()).append("\"");
- }
- if (hreflang.isPresent()) {
- sb.append(" hreflang=\"").append(hreflang.get()).append("\"");
- }
- sb.append("/>");
-
- return sb.toString();
- }
-}
View
164 src/main/java/com/theoryinpractise/halbuilder/spi/ReadableRepresentation.java
@@ -1,164 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Multimap;
-
-import java.util.List;
-import java.util.Map;
-
-/**
- * A ReadableRepresentation is a read-only, immutable HAL Representation object.
- */
-public interface ReadableRepresentation {
-
- /**
- * Returns a Link with a rel of "self".
- * @return A Link
- */
- Optional<Link> getResourceLink();
-
- /**
- * Returns an ImmutableMap of the currently defined resource namespaces
- * @return A Map
- */
- Map<String, String> getNamespaces();
-
- /**
- * Returns an ImmutableList of individual Link instances on this resource.
- * @return A List of Links
- */
- List<Link> getCanonicalLinks();
-
- /**
- * Returns an ImmutableList of collated Link instances on this resource.
- *
- * Multiple links to the same resolved HREF are collated into a single Link
- * instance with a space separated combined rel attribute.
- * @return A List of Links
- */
- List<Link> getLinks();
-
- /**
- * Returns the first link matching the given rel by searching this, then
- * any embedded resource instance.
- * @param rel The rel type to search for.
- * @return A Guava Optional Link
- */
- Optional<Link> getLinkByRel(String rel);
-
- /**
- * Returns all links matching the given rel by searching this, then
- * any embedded resource instance.
- * @param rel The rel type to search for.
- * @return An Immutable List of Links
- */
- List<Link> getLinksByRel(String rel);
-
- /**
- * Returns all embedded resources matching the given rel by searching this, then
- * any embedded resource instance.
- * @param rel The rel type to search for.
- * @return An Immutable List of Resources
- */
- List<? extends ReadableRepresentation> getResourcesByRel(String rel);
-
- /**
- * Returns all embedded resources from the Representation that match the predicate
- * @param predicate The predicate to check against in the embedded resources
- * @return A List of matching objects (properties, links, resource)
- */
- List<? extends ReadableRepresentation> getResources(Predicate<ReadableRepresentation> predicate);
-
- /**
- * Returns a property from the Representation.
- * @param name The property to return
- * @return A Guava Optional for the property
- */
- Optional<Object> get(String name);
-
- /**
- * Returns a property from the Representation
- * @param name The property to return
- * @return An Object of the property value, or null if absent
- */
- Object getValue(String name);
-
- /**
- * Returns a property from the Representation
- * @param name The property to return
- * @return An Object of the property value, or a user supplied default value
- */
- Object getValue(String name, Object defaultValue);
-
- /**
- * Returns an ImmutableMap of the resources properties.
- * @return A Map
- */
- Map<String, Optional<Object>> getProperties();
-
- /**
- * Return an indication of whether this resource, or subresources of this
- * resource, contain null properties.
- * @return True if this resource, or subresources of this resource,
- * contain null properties. False if not.
- */
- boolean hasNullProperties();
-
- /**
- * Returns an Immutable Multimap of the resources currently embedded resources.
- * @return A Map
- */
- Multimap<String, ReadableRepresentation> getResources();
-
- /**
- * Returns whether this resource is satisfied by the provided Contact.
- * @param contract The contract to check.
- * @return A boolean
- */
- boolean isSatisfiedBy(Contract contract);
-
- /**
- * Returns an optional result of applying the provided function if the resource
- * is satisified by the provided interface.
- *
- * Interface satisfaction is determined by all interface methods being resolvable to
- * properties on the resource.
- *
- * @param anInterface The interface to check
- * @param function The function to apply
- * @param <T> The resource "interface"
- * @param <V> A return value
- * @return A Guava Optional of the functions return value.
- */
- <T, V> Optional<V> ifSatisfiedBy(Class<T> anInterface, Function<T, V> function);
-
- /**
- * Returns an optional proxy to the given interface mirroring the resource.
- *
- * @param anInterface An interface to mirror
- * @return A Guava Optional Representation Proxy
- */
- <T> Optional<T> renderClass(Class<T> anInterface);
-
- /**
- * Returns the resource in the request content-type.
- *
- * application/hal+xml and application/hal+json are provided by default,
- * additional Renderers can be added to a RepresentationFactory.
- *
- * @param contentType The content type requested
- * @return A String
- */
- String renderContent(String contentType);
-
- /**
- * Returns an optional proxy to the given interface mirroring the resource.
- *
- * @param anInterface An interface to mirror
- * @return A Guava Optional Representation Proxy
- */
- <T> Optional<T> resolveClass(Function<ReadableRepresentation, Optional<T>> resolver);
-
-}
View
21 src/main/java/com/theoryinpractise/halbuilder/spi/Renderer.java
@@ -1,21 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-import com.google.common.base.Optional;
-
-import java.io.Writer;
-
-/**
- * A Renderer takes a ReadableRepresentation and renders it to the provided Writer, returning an
- * Optional value.
- * @param <T> A class to return.
- */
-public interface Renderer<T> {
-
- /**
- * Returns an Optional value after writing the representation to the provided Writer.
- * @param representation The representation to render
- * @param writer The Writer to write to
- * @return An optional value
- */
- Optional<T> render(ReadableRepresentation representation, Writer writer);
-}
View
38 src/main/java/com/theoryinpractise/halbuilder/spi/Representation.java
@@ -1,38 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-
-import java.net.URI;
-
-public interface Representation extends ReadableRepresentation {
-
- Representation withLink(String rel, String href);
-
- Representation withLink(String rel, URI uri);
-
- Representation withLink(String rel, String href, Predicate<ReadableRepresentation> predicate);
-
- Representation withLink(String rel, URI uri, Predicate<ReadableRepresentation> predicate);
-
- Representation withLink(String rel, String href, Optional<Predicate<ReadableRepresentation>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang);
-
- Representation withLink(String rel, URI uri, Optional<Predicate<ReadableRepresentation>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang);
-
- Representation withProperty(String name, Object value);
-
- Representation withBean(Object value);
-
- Representation withFields(Object value);
-
- Representation withSerializable(Serializable serializable);
-
- Representation withFieldBasedRepresentation(String rel, String href, Object o);
-
- Representation withBeanBasedRepresentation(String rel, String href, Object o);
-
- Representation withNamespace(String namespace, String href);
-
- Representation withRepresentation(String rel, ReadableRepresentation resource);
-
-}
View
11 src/main/java/com/theoryinpractise/halbuilder/spi/RepresentationException.java
@@ -1,11 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-public class RepresentationException extends RuntimeException {
- public RepresentationException(String s) {
- super(s);
- }
-
- public RepresentationException(Throwable throwable) {
- super(throwable);
- }
-}
View
15 src/main/java/com/theoryinpractise/halbuilder/spi/Serializable.java
@@ -1,15 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-/**
- * Implementers of this interface can provide customized "serialization"
- * to a Representation.
- */
-public interface Serializable {
-
- /**
- * "Serializes" data to the given resource.
- * @param resource The resource to serialize into.
- */
- void serializeResource(Representation resource);
-
-}
View
16 src/test/java/com/theoryinpractise/halbuilder/CollatedLinksTest.java
@@ -1,7 +1,7 @@
package com.theoryinpractise.halbuilder;
-import com.theoryinpractise.halbuilder.spi.Link;
-import com.theoryinpractise.halbuilder.spi.Representation;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.Representation;
import org.fest.assertions.core.Condition;
import org.testng.annotations.Test;
@@ -15,7 +15,7 @@
@Test
public void testCollatedLinks() {
- Representation resource = new RepresentationFactory().newRepresentation("/foo")
+ Representation resource = new DefaultRepresentationFactory().newRepresentation("/foo")
.withLink("bar", "/bar")
.withLink("foo", "/bar");
@@ -35,7 +35,7 @@ public void testCollatedLinks() {
@Test
public void testSpacedRelsSeparateLinks() {
- Representation representation = new RepresentationFactory().newRepresentation("/foo");
+ Representation representation = new DefaultRepresentationFactory().newRepresentation("/foo");
try {
Representation resource = representation.withLink("bar foo", "/bar");
@@ -49,7 +49,7 @@ public void testSpacedRelsSeparateLinks() {
@Test
public void testMultiSpacedRelsSeparateLinks() {
- Representation representation = new RepresentationFactory().newRepresentation("/foo");
+ Representation representation = new DefaultRepresentationFactory().newRepresentation("/foo");
try {
Representation resource = representation.withLink("bar foo", "/bar");
fail("We should fail to add a space separated link rel.");
@@ -61,23 +61,23 @@ public void testMultiSpacedRelsSeparateLinks() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testRelLookupsWithNullFail() {
- Representation resource = new RepresentationFactory().newRepresentation("/foo")
+ Representation resource = new DefaultRepresentationFactory().newRepresentation("/foo")
.withLink("bar foo", "/bar");
resource.getLinkByRel(null);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testRelLookupsWithEmptyRelFail() {
- Representation resource = new RepresentationFactory().newRepresentation("/foo")
+ Representation resource = new DefaultRepresentationFactory().newRepresentation("/foo")
.withLink("bar", "/bar");
resource.getLinkByRel("");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testRelLookupsWithSpacesFail() {
- Representation resource = new RepresentationFactory().newRepresentation("/foo")
+ Representation resource = new DefaultRepresentationFactory().newRepresentation("/foo")
.withLink("bar", "/bar");
resource.getLinkByRel("test fail");
View
6 ...tise/halbuilder/impl/ContentTypeTest.java → ...npractise/halbuilder/ContentTypeTest.java
@@ -1,5 +1,7 @@
-package com.theoryinpractise.halbuilder.impl;
+package com.theoryinpractise.halbuilder;
+import com.theoryinpractise.halbuilder.impl.ContentType;
+import org.fest.assertions.api.Assertions;
import org.testng.annotations.Test;
import static org.fest.assertions.api.Assertions.assertThat;
@@ -8,7 +10,7 @@
@Test
public void testContentTypeCreation() {
- assertThat(new ContentType("application/xml").getType()).isEqualTo("application");
+ Assertions.assertThat(new ContentType("application/xml").getType()).isEqualTo("application");
assertThat(new ContentType("application/xml").getSubType()).isEqualTo("xml");
}
View
7 src/test/java/com/theoryinpractise/halbuilder/CurrieOptimizationTest.java
@@ -1,14 +1,15 @@
package com.theoryinpractise.halbuilder;
-import com.theoryinpractise.halbuilder.spi.Link;
-import com.theoryinpractise.halbuilder.spi.Representation;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.Representation;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import org.testng.annotations.Test;
import static org.fest.assertions.api.Assertions.assertThat;
public class CurrieOptimizationTest {
- RepresentationFactory representationFactory = new RepresentationFactory().withNamespace("app", "/api/applications/")
+ RepresentationFactory representationFactory = new DefaultRepresentationFactory().withNamespace("app", "/api/applications/")
.withNamespace("rel", "/api/rels/");
Representation resource = representationFactory.newRepresentation("/api/1")
View
63 src/test/java/com/theoryinpractise/halbuilder/InterfaceSatisfactionTest.java
@@ -1,21 +1,21 @@
package com.theoryinpractise.halbuilder;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
+import com.theoryinpractise.halbuilder.api.Contract;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceContract;
-import com.theoryinpractise.halbuilder.spi.Contract;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
-import javax.annotation.Nullable;
import java.io.InputStreamReader;
import static org.fest.assertions.api.Assertions.assertThat;
+import static org.fest.assertions.api.Assertions.fail;
public class InterfaceSatisfactionTest {
- private RepresentationFactory representationFactory = new RepresentationFactory();
+ private RepresentationFactory representationFactory = new DefaultRepresentationFactory();
public static interface IPerson {
Integer getAge();
@@ -56,14 +56,14 @@
@DataProvider
public Object[][] provideSatisfactionResources() {
return new Object[][]{
- {representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml"))), representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("exampleWithNullProperty.xml")))},
- {representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.json"))), representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("exampleWithNullProperty.json")))}};
+ {representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("/example.xml"))), representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("/exampleWithNullProperty.xml")))},
+ {representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("/example.json"))), representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("/exampleWithNullProperty.json")))}};
}
@Test(dataProvider = "providerSatisfactionData")
public void testSimpleInterfaceSatisfaction(Class<?> aClass, boolean shouldBeSatisfied) {
- ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
+ ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("/example.xml")));
assertThat(representation.isSatisfiedBy(InterfaceContract.newInterfaceContract(aClass))).isEqualTo(shouldBeSatisfied);
}
@@ -85,13 +85,13 @@ public boolean isSatisfiedBy(ReadableRepresentation resource) {
Contract contractHasOptionalFalse = new Contract() {
public boolean isSatisfiedBy(ReadableRepresentation resource) {
- return resource.getProperties().containsKey("optional") && resource.getProperties().get("optional").get().equals("false");
+ return resource.getProperties().containsKey("optional") && resource.getProperties().get("optional").equals("false");
}
};
Contract contractHasNullProperty = new Contract() {
public boolean isSatisfiedBy(ReadableRepresentation resource) {
- return resource.getProperties().containsKey("nullprop") && resource.getProperties().get("nullprop").equals(Optional.absent());
+ return resource.getProperties().containsKey("nullprop") && resource.getProperties().get("nullprop") == null;
}
};
@@ -105,36 +105,33 @@ public boolean isSatisfiedBy(ReadableRepresentation resource) {
@Test
public void testClassRendering() {
- ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
+ ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("/example.xml")));
+
+ assertThat(representation.toClass(INamed.class).name()).isEqualTo("Example Resource");
+ assertThat(representation.toClass(IPerson.class).getName()).isEqualTo("Example Resource");
+ try {
+ representation.toClass(ISimpleJob.class);
+ fail("RepresentationException expected");
+ } catch (RepresentationException e) {
+ //
+ }
+ try {
+ representation.toClass(IJob.class);
+ fail("RepresentationException expected");
+ } catch (RepresentationException e) {
+ //
+ }
- assertThat(representation.renderClass(INamed.class).get().name()).isEqualTo("Example Resource");
- assertThat(representation.renderClass(IPerson.class).get().getName()).isEqualTo("Example Resource");
- assertThat(representation.renderClass(ISimpleJob.class).isPresent()).isFalse();
- assertThat(representation.renderClass(IJob.class).isPresent()).isFalse();
}
@Test
public void testNullPropertyClassRendering() {
- ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("exampleWithNullProperty.xml")));
+ ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("/exampleWithNullProperty.xml")));
- assertThat(representation.renderClass(INullprop.class).isPresent()).isTrue();
- assertThat(representation.renderClass(INullprop.class).get().nullprop() == null);
+ assertThat(representation.toClass(INullprop.class)).isNotNull();
+ assertThat(representation.toClass(INullprop.class).nullprop() == null);
}
- @Test
- public void testFunctionalInterfaceSatisfaction() {
-
- ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
-
- String name = representation.ifSatisfiedBy(IPerson.class, new Function<IPerson, String>() {
- public String apply(@Nullable IPerson iPerson) {
- return iPerson.getName();
- }
- }).get();
-
- assertThat(name).isEqualTo("Example Resource");
-
- }
}
View
44 ...ryinpractise/halbuilder/spi/LinkTest.java → ...theoryinpractise/halbuilder/LinkTest.java
@@ -1,91 +1,79 @@
-package com.theoryinpractise.halbuilder.spi;
+package com.theoryinpractise.halbuilder;
-import com.google.common.base.Optional;
-import com.theoryinpractise.halbuilder.RepresentationFactory;
+import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import org.testng.annotations.Test;
import static org.fest.assertions.api.Assertions.assertThat;
public class LinkTest {
- private RepresentationFactory representationFactory = new RepresentationFactory("http://localhost/");
+ private RepresentationFactory representationFactory = new DefaultRepresentationFactory("http://localhost/");
- private Link link = new Link(representationFactory, "http://example.com/", "rel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ private Link link = new Link(representationFactory, "http://example.com/", "rel", "name", "title", "hreflang");
@Test
public void equalLinksHaveEqualHashCodes() {
- Link otherLink = new Link(representationFactory, "http://example.com/", "rel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/", "rel", "name", "title", "hreflang");
assertThat(link.hashCode()).isEqualTo(otherLink.hashCode());
}
@Test
public void testHashCodeIsDependentOnHref() {
- Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", "name", "title", "hreflang");
assertThat(otherLink.hashCode()).isNotEqualTo(link.hashCode());
}
@Test
public void testHashCodeIsDependentOnRel() {
- Link otherLink = new Link(representationFactory, "http://example.com/", "otherrel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/", "otherrel", "name", "title", "hreflang");
assertThat(otherLink.hashCode()).isNotEqualTo(link.hashCode());
}
@Test
public void testHashCodeIsDependentOnName() {
- Link otherLink = new Link(representationFactory, "http://example.com/", "rel", Optional.of("othername"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/", "rel", "othername", "title", "hreflang");
assertThat(otherLink.hashCode()).isNotEqualTo(link.hashCode());
}
@Test
public void testHashCodeIsDependentOnTitle() {
- Link otherLink = new Link(representationFactory, "http://example.com/", "rel", Optional.of("name"),
- Optional.of("othertitle"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/", "rel", "name", "othertitle", "hreflang");
assertThat(otherLink.hashCode()).isNotEqualTo(link.hashCode());
}
@Test
public void testHashCodeIsDependentOnHreflang() {
- Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", "name", "title", "hreflang");
assertThat(otherLink.hashCode()).isNotEqualTo(link.hashCode());
}
@Test
public void testEqualsIsDependentOnHref() {
- Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", "name", "title", "hreflang");
assertThat(otherLink).isNotEqualTo(link);
}
@Test
public void testEqualsIsDependentOnRel() {
- Link otherLink = new Link(representationFactory, "http://example.com/", "otherrel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/", "otherrel", "name", "title", "hreflang");
assertThat(otherLink).isNotEqualTo(link);
}
@Test
public void testEqualsIsDependentOnName() {
- Link otherLink = new Link(representationFactory, "http://example.com/", "rel", Optional.of("othername"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/", "rel", "othername", "title", "hreflang");
assertThat(otherLink).isNotEqualTo(link);
}
@Test
public void testEqualsIsDependentOnTitle() {
- Link otherLink = new Link(representationFactory, "http://example.com/", "rel", Optional.of("name"),
- Optional.of("othertitle"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/", "rel", "name", "othertitle", "hreflang");
assertThat(otherLink).isNotEqualTo(link);
}
@Test
public void testEqualsIsDependentOnHreflang() {
- Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", Optional.of("name"),
- Optional.of("title"), Optional.of("hreflang"));
+ Link otherLink = new Link(representationFactory, "http://example.com/other", "rel", "name", "title", "hreflang");
assertThat(otherLink).isNotEqualTo(link);
}
View
75 src/test/java/com/theoryinpractise/halbuilder/RenderingTest.java
@@ -2,16 +2,15 @@
import com.damnhandy.uri.template.UriTemplate;
import com.google.common.base.Charsets;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.io.Resources;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
+import com.theoryinpractise.halbuilder.api.Representation;
+import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.api.RepresentationFactory;
+import com.theoryinpractise.halbuilder.api.Serializable;
import com.theoryinpractise.halbuilder.impl.representations.MutableRepresentation;
-import com.theoryinpractise.halbuilder.spi.ReadableRepresentation;
-import com.theoryinpractise.halbuilder.spi.Representation;
-import com.theoryinpractise.halbuilder.spi.RepresentationException;
-import com.theoryinpractise.halbuilder.spi.Serializable;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@@ -23,7 +22,7 @@
public class RenderingTest {
- private RepresentationFactory representationFactory = new RepresentationFactory("https://example.com/api/")
+ private RepresentationFactory representationFactory = new DefaultRepresentationFactory("https://example.com/api/")
.withNamespace("ns", "/apidocs/accounts")
.withNamespace("role", "/apidocs/roles");
@@ -48,51 +47,47 @@
@BeforeMethod
public void setup() throws IOException {
- exampleXmlWithoutHref = Resources.toString(RenderingTest.class.getResource("exampleWithoutHref.xml"), Charsets.UTF_8)
+ exampleXmlWithoutHref = Resources.toString(RenderingTest.class.getResource("/exampleWithoutHref.xml"), Charsets.UTF_8)
.trim().replaceAll("\n", "\r\n");
- exampleJsonWithoutHref = Resources.toString(RenderingTest.class.getResource("exampleWithoutHref.json"), Charsets.UTF_8)
+ exampleJsonWithoutHref = Resources.toString(RenderingTest.class.getResource("/exampleWithoutHref.json"), Charsets.UTF_8)
.trim();
- exampleXml = Resources.toString(RenderingTest.class.getResource("example.xml"), Charsets.UTF_8)
+ exampleXml = Resources.toString(RenderingTest.class.getResource("/example.xml"), Charsets.UTF_8)
.trim().replaceAll("\n", "\r\n");
- exampleJson = Resources.toString(RenderingTest.class.getResource("example.json"), Charsets.UTF_8)
+ exampleJson = Resources.toString(RenderingTest.class.getResource("/example.json"), Charsets.UTF_8)
.trim();
- exampleWithSubresourceXml = Resources.toString(RenderingTest.class.getResource("exampleWithSubresource.xml"), Charsets.UTF_8)
+ exampleWithSubresourceXml = Resources.toString(RenderingTest.class.getResource("/exampleWithSubresource.xml"), Charsets.UTF_8)
.trim().replaceAll("\n", "\r\n");
- exampleWithSubresourceJson = Resources.toString(RenderingTest.class.getResource("exampleWithSubresource.json"), Charsets.UTF_8)
+ exampleWithSubresourceJson = Resources.toString(RenderingTest.class.getResource("/exampleWithSubresource.json"), Charsets.UTF_8)
.trim();
- exampleWithSubresourceLinkingToItselfXml = Resources.toString(RenderingTest.class.getResource("exampleWithSubresourceLinkingToItself.xml"), Charsets.UTF_8)
+ exampleWithSubresourceLinkingToItselfXml = Resources.toString(RenderingTest.class.getResource("/exampleWithSubresourceLinkingToItself.xml"), Charsets.UTF_8)
.trim().replaceAll("\n", "\r\n");
- exampleWithSubresourceLinkingToItselfJson = Resources.toString(RenderingTest.class.getResource("exampleWithSubresourceLinkingToItself.json"), Charsets.UTF_8)
+ exampleWithSubresourceLinkingToItselfJson = Resources.toString(RenderingTest.class.getResource("/exampleWithSubresourceLinkingToItself.json"), Charsets.UTF_8)
.trim();
- exampleWithMultipleSubresourcesXml = Resources.toString(RenderingTest.class.getResource("exampleWithMultipleSubresources.xml"), Charsets.UTF_8)
+ exampleWithMultipleSubresourcesXml = Resources.toString(RenderingTest.class.getResource("/exampleWithMultipleSubresources.xml"), Charsets.UTF_8)
.trim().replaceAll("\n", "\r\n