[jboss-svn-commits] JBoss Common SVN: r3488 - declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Aug 26 03:42:53 EDT 2009
Author: aslak
Date: 2009-08-26 03:42:53 -0400 (Wed, 26 Aug 2009)
New Revision: 3488
Modified:
declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/ClassAssetTestCase.java
declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/TestUtils.java
Log:
TMPARCH-19 Added byte size check of read stream
Modified: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/ClassAssetTestCase.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/ClassAssetTestCase.java 2009-08-26 07:42:19 UTC (rev 3487)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/ClassAssetTestCase.java 2009-08-26 07:42:53 UTC (rev 3488)
@@ -36,13 +36,40 @@
@Test
public void shouldBeAbleToReadThisClass() throws Exception
{
- Asset asset = new ClassAsset(ClassAssetTestCase.class);
+ Class<?> clazz = ClassAssetTestCase.class;
+ Asset asset = new ClassAsset(clazz);
InputStream io = asset.getStream();
Assert.assertNotNull(io);
+ Assert.assertEquals(
+ "Loaded class should have the same size",
+ TestUtils.findLengthOfStream(io),
+ TestUtils.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.getStream();
+
+ Assert.assertNotNull(io);
+ Assert.assertEquals(
+ "Loaded class should have the same size",
+ TestUtils.findLengthOfStream(io),
+ TestUtils.findLengthOfClass(bootstrapClass));
+ }
+
+ @Test
public void shouldThrowExceptionOnNullClass() throws Exception
{
try
@@ -52,23 +79,10 @@
}
catch (Exception e)
{
- Assert.assertEquals("A null clazz argument should result in a IllegalArgumentException",
- IllegalArgumentException.class, e.getClass());
+ Assert.assertEquals(
+ "A null clazz argument should result in a IllegalArgumentException",
+ IllegalArgumentException.class,
+ e.getClass());
}
}
-
- /**
- * 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 shouldSwitchToTCCLIfClassIsLoadedByBootstrapClassLoader() throws Exception
- {
- Asset asset = new ClassAsset(Class.class);
- asset.getStream();
- }
}
Modified: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/TestUtils.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/TestUtils.java 2009-08-26 07:42:19 UTC (rev 3487)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/asset/TestUtils.java 2009-08-26 07:42:53 UTC (rev 3488)
@@ -20,8 +20,10 @@
import java.io.InputStream;
/**
+ * TestUtils
+ *
* @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
- *
+ * @version $Revision: $
*/
class TestUtils
{
@@ -30,12 +32,14 @@
}
/**
- * Convert a inputstream to a UTF-8 string.
- *
+ * Convert a {@link InputStream} to a UTF-8 string.
+ * <br/>
* Helper for testing the content of loaded resources.
+ * <br/>
+ * This method will close the stream when done.
*
- * @param in Open inputstream
- * @return The inputstream as a String
+ * @param in Open InputStream
+ * @return The InputStream as a String
* @throws Exception
*/
static String convertToString(InputStream in) throws Exception
@@ -50,4 +54,54 @@
in.close();
return new String(out.toByteArray(), "UTF-8");
}
+
+ /**
+ * Read the byte size of a {@link InputStream}.
+ * <br/>
+ * This method will close the stream when done.
+ *
+ * @param in Stream to get the size of.
+ * @return The byte size of the stream
+ * @throws Exception
+ */
+ static int findLengthOfStream(InputStream in) throws Exception
+ {
+ int length = 0;
+ while(in.read() != -1)
+ {
+ length++;
+ }
+ in.close();
+ return length;
+ }
+
+ /**
+ * 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 = Thread.currentThread().getContextClassLoader().getResourceAsStream(classResourceName);
+ return findLengthOfStream(in);
+ }
+
+ /**
+ * Get a resourceName for a {@link Class} so that it can be found in the {@link ClassLoader}.
+ * <br/>
+ * class.getName.relace( . -> / ) + ".class"
+ *
+ * @param clazz The class to lookup
+ * @return
+ */
+ static String getResourceNameForClass(Class<?> clazz)
+ {
+ String classResourceDelimiter = clazz.getName()
+ .replaceAll("\\.", "/");
+ String classFullPath = classResourceDelimiter + ".class";
+ return classFullPath;
+ }
}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list