@Test public void testWriteToJson() { final File file = new File(NamespacesJsonContext.class.getResource(nsDoc).getPath()); final String filename = file.getParent() + "/" + randomFilename(); try { System.setProperty(NamespacesJsonContext.CONFIG_NAMESPACES_PATH, filename); final NamespacesJsonContext svc1 = new NamespacesJsonContext(); assertEquals(15, svc1.getNamespaces().size(), "Incorrect namespace mapping count!"); assertFalse(svc1.getNamespaces().containsKey("jsonld"), "jsonld prefix unexpectedly found!"); assertTrue(svc1.setPrefix("jsonld", JSONLD.getNamespace()), "unable to set jsonld mapping!"); assertEquals(16, svc1.getNamespaces().size(), "Namespace count was not incremented!"); assertTrue(svc1.getNamespaces().containsKey("jsonld"), "jsonld prefix not found in mapping!"); final NamespacesJsonContext svc2 = new NamespacesJsonContext(); assertEquals(16, svc2.getNamespaces().size(), "Incorrect namespace count when reloading from file!"); assertFalse(svc2.setPrefix("jsonld", JSONLD.getNamespace()), "unexpected response when trying to re-set jsonld mapping!"); } finally { System.getProperties().remove(NamespacesJsonContext.CONFIG_NAMESPACES_PATH); } }
@Test public void testReadFromJson() { final URL res = NamespacesJsonContext.class.getResource(nsDoc); try { System.setProperty(NamespacesJsonContext.CONFIG_NAMESPACES_PATH, res.getPath()); final NamespacesJsonContext svc = new NamespacesJsonContext(); assertEquals(2, svc.getNamespaces().size(), "Namespace mapping count is incorrect!"); } finally { System.clearProperty(NamespacesJsonContext.CONFIG_NAMESPACES_PATH); } }
@Test public void testWriteError() { final File file = new File(getClass().getResource(nsDoc).getFile()); final File nonexistent = new File(file.getParentFile(), "nonexistent/dir/file.json"); assertThrows(UncheckedIOException.class, () -> new NamespacesJsonContext(nonexistent.getAbsolutePath()), "Loaded namespaces from invalid file!"); }
@Override public boolean setPrefix(final String prefix, final String namespace) { requireNonNull(prefix, "The prefix value may not be null!"); requireNonNull(namespace, "The namespce value may not be null!"); if (dataRev.containsKey(namespace)) { LOGGER.warn("A prefix already exists for the namespace: {}", namespace); return false; } data.put(prefix, namespace); dataRev.put(namespace, prefix); write(filePath, data); return true; }
private static IOService buildIoService(final AppConfiguration config) { final long cacheSize = config.getJsonld().getCacheSize(); final long hours = config.getJsonld().getCacheExpireHours(); final Cache<String, String> cache = newBuilder().maximumSize(cacheSize).expireAfterAccess(hours, HOURS).build(); final TrellisCache<String, String> profileCache = new TrellisCache<>(cache); final NamespaceService namespaceService = new NamespacesJsonContext(config.getNamespaces()); final RDFaWriterService htmlSerializer = new HtmlSerializer(namespaceService, config.getAssets().getTemplate(), config.getAssets().getCss(), config.getAssets().getJs(), config.getAssets().getIcon()); return new JenaIOService(namespaceService, htmlSerializer, profileCache, config.getJsonld().getContextWhitelist(), config.getJsonld().getContextDomainWhitelist()); }