@Test public void testAddResourceWithMultipleContent() throws Exception { // Add a resource from XML RepositoryReader reader = getRepositoryReader("xml/repository-testB.xml"); storage.addResource(reader.nextResource()); Requirement req = new DefaultRequirementBuilder(IdentityNamespace.IDENTITY_NAMESPACE, "resA") .getRequirement(); Collection<Capability> providers = storage.findProviders(req); Assert.assertNotNull(providers); Assert.assertEquals(1, providers.size()); Capability cap = providers.iterator().next(); Assert.assertEquals("resA", cap.getAttribute(IdentityNamespace.IDENTITY_NAMESPACE)); Resource resource = cap.getResource(); verifyDefaultContent(resource); InputStream input = resource.adapt(ResourceContent.class).getContent(); String digest = RepositoryContentHelper.getDigest(input); Assert.assertNotNull("RepositoryContent not null", input); input.close(); List<Capability> ccaps = resource.getCapabilities(ContentNamespace.CONTENT_NAMESPACE); Assert.assertEquals(2, ccaps.size()); ContentCapability ccap = ccaps.get(0).adapt(ContentCapability.class); Assert.assertEquals(digest, ccap.getDigest()); Assert.assertEquals("application/java-archive", ccap.getMimeType()); Assert.assertEquals(new Long(392), ccap.getSize()); File contentFile = new File(ccap.getContentURL().getPath()).getCanonicalFile(); Assert.assertTrue("File exists: " + contentFile, contentFile.exists()); Assert.assertTrue( "Path starts with: " + storageDir.getPath(), contentFile.getPath().startsWith(storageDir.getPath())); ccap = ccaps.get(1).adapt(ContentCapability.class); Assert.assertFalse(digest.equals(ccap.getDigest())); Assert.assertEquals("text/plain", ccap.getMimeType()); Assert.assertEquals(new Long("some text".length()), ccap.getSize()); contentFile = new File(ccap.getContentURL().getPath()).getCanonicalFile(); Assert.assertTrue("File exists: " + contentFile, contentFile.exists()); Assert.assertTrue( "Path starts with: " + storageDir.getPath(), contentFile.getPath().startsWith(storageDir.getPath())); BufferedReader br = new BufferedReader(new FileReader(contentFile)); Assert.assertEquals("some text", br.readLine()); br.close(); }
private void verifyDefaultContent(Resource resource) throws Exception { InputStream input = resource.adapt(ResourceContent.class).getContent(); String digest = RepositoryContentHelper.getDigest(input); Assert.assertNotNull("RepositoryContent not null", input); input.close(); Capability cap = resource.getCapabilities(ContentNamespace.CONTENT_NAMESPACE).get(0); ContentCapability ccap = cap.adapt(ContentCapability.class); Assert.assertEquals(digest, ccap.getDigest()); Assert.assertEquals(digest, cap.getAttribute(ContentNamespace.CONTENT_NAMESPACE)); Assert.assertEquals("application/java-archive", ccap.getMimeType()); Assert.assertEquals( "application/java-archive", cap.getAttribute(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE)); Assert.assertEquals(new Long(392), ccap.getSize()); Assert.assertEquals( new Long(392), cap.getAttribute(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE)); URL contentURL = (URL) ccap.getAttribute(ContentNamespace.CAPABILITY_URL_ATTRIBUTE); File contentFile = new File(contentURL.getPath()).getCanonicalFile(); Assert.assertTrue("File exists: " + contentFile, contentFile.exists()); Assert.assertTrue( "Path starts with: " + storageDir.getPath(), contentFile.getPath().startsWith(storageDir.getPath())); }