@Test public void shouldBeAbleToReadFile() throws Exception { Asset asset = new FileAsset(new File(EXISTING_FILE)); InputStream io = asset.openStream(); Assert.assertNotNull(io); Assert.assertEquals("Should be able to read the content of the resource", "shrinkwrap=true", ApiTestUtils.convertToString(io)); }
/** * Read the byte size of a {@link Class}. * * @param clazz * The class * @return The byte size of the given {@link Class} * @throws Exception */ static int findLengthOfClass(Class<?> clazz) throws Exception { String classResourceName = getResourceNameForClass(clazz); InputStream in = SecurityActions.getThreadContextClassLoader().getResourceAsStream(classResourceName); return findLengthOfStream(in); }
@Test public void shouldBeAbleToReadThisClass() throws Exception { Class<?> clazz = ClassAssetTestCase.class; Asset asset = new ClassAsset(clazz); InputStream io = asset.openStream(); Assert.assertNotNull(io); Assert.assertEquals("Loaded class should have the same size", ApiTestUtils.findLengthOfStream(io), ApiTestUtils.findLengthOfClass(clazz)); }
/** * https://jira.jboss.org/jira/browse/TMPARCH-19 <br/> * <br/> * A {@link Class} loaded by the Bootstrap ClassLoader will return a null {@link ClassLoader}, should use * {@link Thread} current context {@link ClassLoader} instead. * * @throws Exception */ @Test public void shouldBeAbleAddBootstrapClass() throws Exception { Class<?> bootstrapClass = Class.class; Asset asset = new ClassAsset(bootstrapClass); InputStream io = asset.openStream(); Assert.assertNotNull(io); Assert.assertEquals("Loaded class should have the same size", ApiTestUtils.findLengthOfStream(io), ApiTestUtils.findLengthOfClass(bootstrapClass)); }
@Test public void shouldBeAbleToReadResource() throws Exception { Asset asset = new ClassLoaderAsset(EXISTING_RESOURCE); InputStream io = asset.openStream(); Assert.assertNotNull(io); Assert.assertEquals("Should be able to read the content of the resource", "shrinkwrap=true", ApiTestUtils.convertToString(io)); }
@Test public void shouldBeAbleToReadURL() throws Exception { Asset asset = new UrlAsset(getThreadContextClassLoader().getResource(EXISTING_RESOURCE)); InputStream io = asset.openStream(); Assert.assertNotNull(io); Assert.assertEquals("Should be able to read the content of the resource", "shrinkwrap=true", ApiTestUtils.convertToString(io)); }
@Test public void shouldCreateDefensiveCopyOfURLOnConstruction() throws Exception { URL mutableURL = getThreadContextClassLoader().getResource(EXISTING_RESOURCE); Asset asset = new UrlAsset(mutableURL); // mutate the URL - can't be sure that some malicious code or user won't do this? mutateURL(mutableURL); // now try to get a stream to read the asset InputStream io = null; try { io = asset.openStream(); } catch (Exception e) { Assert.fail("Mutated URL leaked into the UrlAsset"); } Assert.assertNotNull(io); Assert .assertEquals("Mutated URL leaked into the UrlAsset", "shrinkwrap=true", ApiTestUtils.convertToString(io)); }