Skip to content

Commit 8865c06

Browse files
committed
Add tests for Link class
1 parent b415f91 commit 8865c06

File tree

3 files changed

+99
-11
lines changed

3 files changed

+99
-11
lines changed

build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ ext {
3333
camelVersion = '2.19.3'
3434

3535
/* Testing */
36-
junitVersion = '4.12'
36+
junitVersion = '5.0.1'
37+
junitPlatformVersion = '1.0.1'
3738
jaxbVersion = '2.3.0'
3839
jacocoVersion = '0.7.9'
3940
activationVersion = '1.1.1'
4041
logbackVersion = '1.2.3'
42+
apiguardianVersion = '1.0.0'
4143

4244
/* OSGi */
4345
projectOsgiVersion = project.version.replaceAll("-SNAPSHOT", ".SNAPSHOT")
@@ -57,7 +59,9 @@ jacocoTestReport {
5759
dependencies {
5860
api group: 'org.apache.camel', name: 'camel-core', version: camelVersion
5961

60-
testImplementation group: 'junit', name: 'junit', version: junitVersion
62+
testImplementation group: 'org.apiguardian', name: 'apiguardian-api', version: apiguardianVersion
63+
testImplementation group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion
64+
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion
6165
testImplementation group: 'org.apache.camel', name: 'camel-test', version: camelVersion
6266
testImplementation group: 'javax.xml.bind', name: 'jaxb-api', version: jaxbVersion
6367
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: logbackVersion

src/main/java/org/trellisldp/camel/Link.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ public class Link {
3333
*/
3434
public Link(final String value) {
3535
final String[] parts = value.split(";");
36-
if (parts.length >= 1) {
37-
final String val = parts[0].trim();
38-
this.uri = val.startsWith("<") && val.endsWith(">") ? val.substring(1, val.length() - 1) : null;
39-
for (int i = 1; i < parts.length; i++) {
40-
final String[] p = parts[1].trim().split("=");
41-
if (p.length == 2) {
42-
if (!params.containsKey(p[0])) {
36+
final String val = parts[0].trim();
37+
this.uri = val.startsWith("<") && val.endsWith(">") ? val.substring(1, val.length() - 1) : null;
38+
for (int i = 1; i < parts.length; i++) {
39+
final String[] p = parts[i].trim().split("=");
40+
if (p.length == 2) {
41+
if (!params.containsKey(p[0])) {
42+
if (p[1].startsWith("\"") && p[1].endsWith("\"")) {
43+
params.put(p[0], p[1].substring(1, p[1].length() - 1));
44+
} else {
4345
params.put(p[0], p[1]);
4446
}
4547
}
4648
}
47-
} else {
48-
this.uri = null;
4949
}
5050
}
5151

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.trellisldp.camel;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNull;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.platform.runner.JUnitPlatform;
21+
import org.junit.runner.RunWith;
22+
23+
/**
24+
* @author acoburn
25+
*/
26+
@RunWith(JUnitPlatform.class)
27+
public class LinkTest {
28+
29+
@Test
30+
public void testLinkWithParams() {
31+
final Link link = new Link("<http://www.w3.org/ns/ldp#Container>; rel=\"type\";" +
32+
"title=\"some title\"; type=\"text/turtle\"; other=\"param\"");
33+
assertEquals("http://www.w3.org/ns/ldp#Container", link.getUri());
34+
assertEquals("type", link.getRel());
35+
assertEquals("some title", link.getTitle());
36+
assertEquals("text/turtle", link.getType());
37+
assertEquals("param", link.getParams().get("other"));
38+
assertEquals(4L, link.getParams().size());
39+
}
40+
41+
@Test
42+
public void testLinkRelQuotes() {
43+
final Link link = new Link("<http://www.w3.org/ns/ldp#Container>; rel=\"type\"");
44+
assertEquals("http://www.w3.org/ns/ldp#Container", link.getUri());
45+
assertEquals("type", link.getRel());
46+
assertNull(link.getTitle());
47+
assertNull(link.getType());
48+
assertEquals(1L, link.getParams().size());
49+
}
50+
51+
@Test
52+
public void testLinkRelNoQuotes() {
53+
final Link link = new Link("<http://www.w3.org/ns/ldp#Container>;rel=type");
54+
assertEquals("http://www.w3.org/ns/ldp#Container", link.getUri());
55+
assertEquals("type", link.getRel());
56+
assertNull(link.getTitle());
57+
assertNull(link.getType());
58+
assertEquals(1L, link.getParams().size());
59+
}
60+
61+
@Test
62+
public void testNoURI() {
63+
final Link link = new Link("");
64+
assertNull(link.getUri());
65+
}
66+
67+
@Test
68+
public void testBadURI() {
69+
final Link link = new Link("<blah");
70+
assertNull(link.getUri());
71+
}
72+
73+
@Test
74+
public void testBadParam() {
75+
final Link link = new Link("<uri>; rel");
76+
assertNull(link.getRel());
77+
}
78+
79+
@Test
80+
public void testMultipleParams() {
81+
final Link link = new Link("<uri>; rel=one; rel=two; rel=three");
82+
assertEquals("one", link.getRel());
83+
}
84+
}

0 commit comments

Comments
 (0)