Skip to content

Commit 7572907

Browse files
committed
Add stubs for TestDocumentLoader methods
1 parent fcbba66 commit 7572907

1 file changed

Lines changed: 62 additions & 32 deletions

File tree

core/src/test/java/com/github/jsonldjava/core/JsonLdProcessorTest.java

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.Date;
24-
import java.util.Iterator;
2524
import java.util.LinkedHashMap;
2625
import java.util.List;
2726
import java.util.Map;
2827

2928
import org.junit.AfterClass;
29+
import org.junit.Before;
3030
import org.junit.BeforeClass;
31+
import org.junit.Rule;
3132
import org.junit.Test;
33+
import org.junit.rules.TemporaryFolder;
34+
import org.junit.rules.Timeout;
3235
import org.junit.runner.RunWith;
3336
import org.junit.runners.Parameterized;
3437
import org.junit.runners.Parameterized.Parameters;
@@ -38,6 +41,7 @@
3841
import com.github.jsonldjava.impl.TurtleTripleCallback;
3942
import com.github.jsonldjava.utils.JSONUtils;
4043
import com.github.jsonldjava.utils.Obj;
44+
import com.github.jsonldjava.utils.TestUtils;
4145

4246
@RunWith(Parameterized.class)
4347
public class JsonLdProcessorTest {
@@ -224,7 +228,7 @@ public boolean accept(File dir, String name) {
224228
// + name);
225229
// Remote-doc tests are not currently supported
226230
if (name.contains("remote-doc")) {
227-
//return false;
231+
// return false;
228232
}
229233
return true;
230234
}
@@ -278,22 +282,49 @@ public RemoteDocument loadDocument(String url) throws JsonLdError {
278282
if (url.contains(":")) {
279283
// check if the url is relative to the test base
280284
if (url.startsWith(this.base)) {
281-
url = url.substring(this.base.length());
282-
} else {
283-
// we can't load remote documents from the test suite
284-
throw new JsonLdError(JsonLdError.Error.NOT_IMPLEMENTED);
285+
String classpath = url.substring(this.base.length());
286+
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
287+
final InputStream inputStream = cl.getResourceAsStream(TEST_DIR + "/" + classpath);
288+
try {
289+
return new RemoteDocument(url, JSONUtils.fromInputStream(inputStream));
290+
} catch (final IOException e) {
291+
throw new JsonLdError(JsonLdError.Error.LOADING_DOCUMENT_FAILED);
292+
}
285293
}
286294
}
287-
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
288-
final InputStream inputStream = cl.getResourceAsStream(TEST_DIR + "/" + url);
289-
try {
290-
return new RemoteDocument(url, JSONUtils.fromInputStream(inputStream));
291-
} catch (final IOException e) {
292-
throw new JsonLdError(JsonLdError.Error.LOADING_DOCUMENT_FAILED);
293-
}
295+
// we can't load this remote document from the test suite
296+
throw new JsonLdError(JsonLdError.Error.NOT_IMPLEMENTED);
297+
}
298+
299+
public void setRedirectTo(String string) {
300+
// TODO Auto-generated method stub
301+
302+
}
303+
304+
public void setHttpStatus(Integer integer) {
305+
// TODO Auto-generated method stub
306+
307+
}
308+
309+
public void setContentType(String string) {
310+
// TODO Auto-generated method stub
311+
312+
}
313+
314+
public void addHttpLink(String nextLink) {
315+
// TODO Auto-generated method stub
316+
294317
}
295318
}
296319

320+
@Rule
321+
public Timeout timeout = new Timeout(10000);
322+
323+
@Rule
324+
public TemporaryFolder tempDir = new TemporaryFolder();
325+
326+
private File testDir;
327+
297328
private final String group;
298329
private final Map<String, Object> test;
299330

@@ -302,17 +333,9 @@ public JsonLdProcessorTest(final String group, final String id, final Map<String
302333
this.test = test;
303334
}
304335

305-
public static String join(Collection<String> list, String delim) {
306-
final StringBuilder builder = new StringBuilder();
307-
final Iterator<String> iter = list.iterator();
308-
while (iter.hasNext()) {
309-
builder.append(iter.next());
310-
if (!iter.hasNext()) {
311-
break;
312-
}
313-
builder.append(delim);
314-
}
315-
return builder.toString();
336+
@Before
337+
public void setUp() throws Exception {
338+
testDir = tempDir.newFolder("jsonld");
316339
}
317340

318341
@Test
@@ -344,7 +367,7 @@ public void runTest() throws URISyntaxException, IOException, JsonLdError {
344367
inputLines.add(line);
345368
}
346369
// Collections.sort(inputLines);
347-
input = join(inputLines, "\n");
370+
input = TestUtils.join(inputLines, "\n");
348371
}
349372
Object expect = null;
350373
String sparql = null;
@@ -377,7 +400,7 @@ public void runTest() throws URISyntaxException, IOException, JsonLdError {
377400
expectLines.add(line);
378401
}
379402
Collections.sort(expectLines);
380-
expect = join(expectLines, "\n");
403+
expect = TestUtils.join(expectLines, "\n");
381404
} else {
382405
expect = "";
383406
assertFalse("Unknown expect type: " + expectType, true);
@@ -403,7 +426,8 @@ public void runTest() throws URISyntaxException, IOException, JsonLdError {
403426
// OPTIONS SETUP
404427
final JsonLdOptions options = new JsonLdOptions("http://json-ld.org/test-suite/tests/"
405428
+ test.get("input"));
406-
options.documentLoader = new TestDocumentLoader("http://json-ld.org/test-suite/tests/");
429+
TestDocumentLoader testLoader = new TestDocumentLoader("http://json-ld.org/test-suite/tests/");
430+
options.documentLoader = testLoader;
407431
if (test.containsKey("option")) {
408432
final Map<String, Object> test_opts = (Map<String, Object>) test.get("option");
409433
if (test_opts.containsKey("base")) {
@@ -427,16 +451,22 @@ public void runTest() throws URISyntaxException, IOException, JsonLdError {
427451
options.setProduceGeneralizedRdf((Boolean) test_opts.get("produceGeneralizedRdf"));
428452
}
429453
if (test_opts.containsKey("redirectTo")) {
430-
// TODO: Handle redirectTo for remote-doc tests
454+
testLoader.setRedirectTo((String)test_opts.get("redirectTo"));
431455
}
432456
if (test_opts.containsKey("httpStatus")) {
433-
// TODO: Handle httpStatus for remote-doc tests
457+
testLoader.setHttpStatus((Integer)test_opts.get("httpStatus"));
434458
}
435459
if (test_opts.containsKey("contentType")) {
436-
// TODO: Handle contentType for remote-doc tests
460+
testLoader.setContentType((String)test_opts.get("contentType"));
437461
}
438462
if (test_opts.containsKey("httpLink")) {
439-
// TODO: Handle httpLink for remote-doc tests
463+
if(test_opts.get("httpLink") instanceof List) {
464+
for(String nextLink : (List<String>)test_opts.get("httpLink")) {
465+
testLoader.addHttpLink(nextLink);
466+
}
467+
} else {
468+
testLoader.addHttpLink((String)test_opts.get("httpLink"));
469+
}
440470
}
441471
}
442472

@@ -520,7 +550,7 @@ public void runTest() throws URISyntaxException, IOException, JsonLdError {
520550
});
521551
put("earl:subject", new LinkedHashMap<String, Object>() {
522552
{
523-
put("@id", "http://github.com/jsonld-java/jsonld-java");
553+
put("@id", "https://github.com/jsonld-java/jsonld-java");
524554
}
525555
});
526556
put("earl:test", new LinkedHashMap<String, Object>() {

0 commit comments

Comments
 (0)