|
| 1 | +package com.github.jsonldjava.core; |
| 2 | + |
| 3 | +import com.github.jsonldjava.utils.JsonUtils; |
| 4 | +import org.junit.AfterClass; |
| 5 | +import org.junit.BeforeClass; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | + |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | +import static org.junit.Assert.fail; |
| 12 | + |
| 13 | + |
| 14 | +public class ContextRecursionTest { |
| 15 | + |
| 16 | + @BeforeClass |
| 17 | + public static void setup() { |
| 18 | + System.setProperty(DocumentLoader.DISALLOW_REMOTE_CONTEXT_LOADING, "true"); |
| 19 | + } |
| 20 | + |
| 21 | + @AfterClass |
| 22 | + public static void tearDown() { |
| 23 | + System.setProperty(DocumentLoader.DISALLOW_REMOTE_CONTEXT_LOADING, "false"); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testIssue302_allowedRecursion() throws IOException { |
| 28 | + |
| 29 | + final String contextB = "{\"@context\": [\"http://localhost/d\", {\"b\": \"http://localhost/b\"} ] }"; |
| 30 | + final String contextC = "{\"@context\": [\"http://localhost/d\", {\"c\": \"http://localhost/c\"} ] }"; |
| 31 | + final String contextD = "{\"@context\": [\"http://localhost/e\", {\"d\": \"http://localhost/d\"} ] }"; |
| 32 | + final String contextE = "{\"@context\": {\"e\": \"http://localhost/e\"} }"; |
| 33 | + |
| 34 | + final DocumentLoader dl = new DocumentLoader(); |
| 35 | + dl.addInjectedDoc("http://localhost/b", contextB); |
| 36 | + dl.addInjectedDoc("http://localhost/c", contextC); |
| 37 | + dl.addInjectedDoc("http://localhost/d", contextD); |
| 38 | + dl.addInjectedDoc("http://localhost/e", contextE); |
| 39 | + final JsonLdOptions options = new JsonLdOptions(); |
| 40 | + options.setDocumentLoader(dl); |
| 41 | + |
| 42 | + final String jsonString = "{\"@context\": [\"http://localhost/d\", \"http://localhost/b\", \"http://localhost/c\", {\"a\": \"http://localhost/a\"} ], \"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\"}"; |
| 43 | + final Object json = JsonUtils.fromString(jsonString); |
| 44 | + final Object expanded = JsonLdProcessor.expand(json, options); |
| 45 | + assertEquals( |
| 46 | + "[{http://localhost/a=[{@value=A}], http://localhost/b=[{@value=B}], http://localhost/c=[{@value=C}], http://localhost/d=[{@value=D}]}]", |
| 47 | + expanded.toString()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testCyclicRecursion() throws IOException { |
| 52 | + |
| 53 | + final String contextC = "{\"@context\": [\"http://localhost/d\", {\"c\": \"http://localhost/c\"} ] }"; |
| 54 | + final String contextD = "{\"@context\": [\"http://localhost/e\", {\"d\": \"http://localhost/d\"} ] }"; |
| 55 | + final String contextE = "{\"@context\": [\"http://localhost/c\", {\"e\": \"http://localhost/e\"} ] }"; |
| 56 | + |
| 57 | + final DocumentLoader dl = new DocumentLoader(); |
| 58 | + dl.addInjectedDoc("http://localhost/c", contextC); |
| 59 | + dl.addInjectedDoc("http://localhost/d", contextD); |
| 60 | + dl.addInjectedDoc("http://localhost/e", contextE); |
| 61 | + final JsonLdOptions options = new JsonLdOptions(); |
| 62 | + options.setDocumentLoader(dl); |
| 63 | + |
| 64 | + final String jsonString = "{\"@context\": [\"http://localhost/c\", {\"a\": \"http://localhost/a\"} ]}"; |
| 65 | + final Object json = JsonUtils.fromString(jsonString); |
| 66 | + try { |
| 67 | + JsonLdProcessor.expand(json, options); |
| 68 | + fail("it should throw"); |
| 69 | + } catch(JsonLdError err) { |
| 70 | + assertEquals(JsonLdError.Error.RECURSIVE_CONTEXT_INCLUSION, err.getType()); |
| 71 | + assertEquals("recursive context inclusion: http://localhost/c", err.getMessage()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments