final HttpResponse response = getHttpClient().execute(request); final int status = response.getStatusLine().getStatusCode(); if (status != 200 && status != 203) {
@Test(expected = IOException.class) public void jarCacheMissThreadCtx() throws Exception { final URLClassLoader findNothingCL = new URLClassLoader(new URL[] {}, null); Thread.currentThread().setContextClassLoader(findNothingCL); JsonUtils.fromURL(new URL("http://nonexisting.example.com/context"), documentLoader.getHttpClient()); }
return new RemoteDocument(url, JsonUtils.fromURL(new URL(url), getHttpClient())); } catch (final Exception e) { throw new JsonLdError(JsonLdError.Error.LOADING_REMOTE_CONTEXT_FAILED, url, e);
return new RemoteDocument(url, JsonUtils.fromURL(new URL(url), getHttpClient())); } catch (final Exception e) { throw new JsonLdError(JsonLdError.Error.LOADING_REMOTE_CONTEXT_FAILED, url, e);
@Test public void sharedHttpClient() throws Exception { // Should be the same instance unless explicitly set assertSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient()); }
@Test(expected = IOException.class) public void jarCacheMiss404() throws Exception { // Should fail-fast as nonexisting.example.com is not in DNS JsonUtils.fromURL(new URL("http://nonexisting.example.com/miss"), documentLoader.getHttpClient()); }
@Test public void differentHttpClient() throws Exception { // Custom http client try { documentLoader.setHttpClient(JsonUtils.createDefaultHttpClient()); assertNotSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient()); } finally { // Use default again documentLoader.setHttpClient(null); assertSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient()); } }
@Test public void jarCacheHitThreadCtx() throws Exception { final URL url = new URL("http://nonexisting.example.com/nested/hello"); final URL nestedJar = getClass().getResource("/nested.jar"); try { JsonUtils.fromURL(url, documentLoader.getHttpClient()); fail("Should not be able to find nested/hello yet"); } catch (final IOException ex) { // expected } Thread.currentThread().setContextClassLoader(null); try { JsonUtils.fromURL(url, documentLoader.getHttpClient()); fail("Should not be able to find nested/hello yet"); } catch (final IOException ex) { // expected } final ClassLoader cl = new URLClassLoader(new URL[] { nestedJar }); Thread.currentThread().setContextClassLoader(cl); final Object hello = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertTrue(hello instanceof Map); assertEquals("World!", ((Map) hello).get("Hello")); }
@Test public void jarCacheHit() throws Exception { // If no cache, should fail-fast as nonexisting.example.com is not in // DNS final Object context = JsonUtils.fromURL(new URL("http://nonexisting.example.com/context"), documentLoader.getHttpClient()); assertTrue(context instanceof Map); assertTrue(((Map) context).containsKey("@context")); }
@Test public void fromURLredirectHTTPSToHTTP() throws Exception { final URL url = new URL("https://w3id.org/bundle/context"); final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); // Should not fail because of // http://stackoverflow.com/questions/1884230/java-doesnt-follow-redirect-in-urlconnection // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571 assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
@Test public void fromURLredirect() throws Exception { final URL url = new URL("http://purl.org/wf4ever/ro-bundle/context.json"); final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
@Test public void fromURLBomTest0004() throws Exception { final URL contexttest = getClass().getResource("/custom/contexttest-0004.jsonld"); assertNotNull(contexttest); final Object context = JsonUtils.fromURL(contexttest, documentLoader.getHttpClient()); assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
@Test public void fromURLTest0001() throws Exception { final URL contexttest = getClass().getResource("/custom/contexttest-0001.jsonld"); assertNotNull(contexttest); final Object context = JsonUtils.fromURL(contexttest, documentLoader.getHttpClient()); assertTrue(context instanceof Map); final Map<String, Object> contextMap = (Map<String, Object>) context; assertEquals(1, contextMap.size()); final Map<String, Object> cont = (Map<String, Object>) contextMap.get("@context"); assertEquals(3, cont.size()); assertEquals("http://example.org/", cont.get("ex")); final Map<String, Object> term1 = (Map<String, Object>) cont.get("term1"); assertEquals("ex:term1", term1.get("@id")); }
@Ignore("Caching failed without any apparent cause on the client side") @Test public void fromURLCache() throws Exception { final URL url = new URL("https://json-ld.org/contexts/person.jsonld"); JsonUtils.fromURL(url, documentLoader.getHttpClient()); // Now try to get it again and ensure it is // cached final HttpClient clientCached = documentLoader.getHttpClient(); final HttpUriRequest getCached = new HttpGet(url.toURI()); getCached.setHeader("Accept", JsonUtils.ACCEPT_HEADER); final HttpCacheContext localContextCached = HttpCacheContext.create(); final HttpResponse respoCached = clientCached.execute(getCached, localContextCached); EntityUtils.consume(respoCached.getEntity()); // Check cache status // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html final CacheResponseStatus responseStatusCached = localContextCached .getCacheResponseStatus(); assertNotEquals(CacheResponseStatus.CACHE_MISS, responseStatusCached); }
@Test public void fromURLTest0002() throws Exception { final URL contexttest = getClass().getResource("/custom/contexttest-0002.jsonld"); assertNotNull(contexttest); final Object context = JsonUtils.fromURL(contexttest, documentLoader.getHttpClient()); assertTrue(context instanceof List); final List<Map<String, Object>> contextList = (List<Map<String, Object>>) context; final Map<String, Object> contextMap1 = contextList.get(0); assertEquals(1, contextMap1.size()); final Map<String, Object> cont1 = (Map<String, Object>) contextMap1.get("@context"); assertEquals(2, cont1.size()); assertEquals("http://example.org/", cont1.get("ex")); final Map<String, Object> term1 = (Map<String, Object>) cont1.get("term1"); assertEquals("ex:term1", term1.get("@id")); final Map<String, Object> contextMap2 = contextList.get(1); assertEquals(1, contextMap2.size()); final Map<String, Object> cont2 = (Map<String, Object>) contextMap2.get("@context"); assertEquals(1, cont2.size()); final Map<String, Object> term2 = (Map<String, Object>) cont2.get("term2"); assertEquals("ex:term2", term2.get("@id")); }
@Test public void fromURLCustomHandler() throws Exception { final AtomicInteger requests = new AtomicInteger(); final URLStreamHandler handler = new URLStreamHandler() { @Override protected URLConnection openConnection(URL u) throws IOException { return new URLConnection(u) { @Override public void connect() throws IOException { return; } @Override public InputStream getInputStream() throws IOException { requests.incrementAndGet(); return getClass().getResourceAsStream("/custom/contexttest-0001.jsonld"); } }; } }; final URL url = new URL(null, "jsonldtest:context", handler); assertEquals(0, requests.get()); final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertEquals(1, requests.get()); assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
documentLoader.setHttpClient(fakeHttpClient(httpRequest)); try { final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertTrue(context instanceof Map); } finally { documentLoader.setHttpClient(null); assertSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient());