Index: impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/asset/StringAssetTestCase.java =================================================================== --- impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/asset/StringAssetTestCase.java (revision 0) +++ impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/asset/StringAssetTestCase.java (revision 0) @@ -0,0 +1,82 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2009, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.shrinkwrap.impl.base.asset; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.util.logging.Logger; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * Test Cases for the {@link StringAsset} + * + * @author ALR + * @author Dan Allen + * @version $Revision: $ + */ +public class StringAssetTestCase +{ + + //-------------------------------------------------------------------------------------|| + // Class Members ----------------------------------------------------------------------|| + //-------------------------------------------------------------------------------------|| + + /** + * Logger + */ + private static final Logger log = Logger.getLogger(StringAssetTestCase.class.getName()); + + //-------------------------------------------------------------------------------------|| + // Instance Members -------------------------------------------------------------------|| + //-------------------------------------------------------------------------------------|| + + //-------------------------------------------------------------------------------------|| + // Tests ------------------------------------------------------------------------------|| + //-------------------------------------------------------------------------------------|| + + /** + * Ensures that the contents of the asset match that which was passed in. + */ + @Test + public void testRoundtrip() throws Exception + { + // Log + log.info("testRoundtrip"); + + // Make contents + String contents = StringAsset.class.getSimpleName(); + + // Make Asset + final StringAsset asset = new StringAsset(contents); + + // Get the contents back out of the asset + final InputStream stream = asset.openStream(); + final ByteArrayOutputStream out = new ByteArrayOutputStream(contents.length()); + int read; + while ((read = stream.read()) != -1) + { + out.write(read); + } + String roundtrip = new String(out.toByteArray()); + log.info("Roundtrip contents: " + roundtrip); + + Assert.assertEquals("Roundtrip did not equal passed in contents", contents, roundtrip); + } +} Index: impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/StringAsset.java =================================================================== --- impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/StringAsset.java (revision 0) +++ impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/StringAsset.java (revision 0) @@ -0,0 +1,97 @@ +package org.jboss.shrinkwrap.impl.base.asset; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.jboss.shrinkwrap.api.Asset; +import org.jboss.shrinkwrap.impl.base.Validate; +import org.jboss.shrinkwrap.impl.base.io.IOUtil; + +/** + * Implementation of a {@link Asset} backed by a String + * + * @author Dan Allen + * @version $Revision: $ + */ +public class StringAsset implements Asset +{ + //-------------------------------------------------------------------------------------|| + // Class Members ----------------------------------------------------------------------|| + //-------------------------------------------------------------------------------------|| + + /** + * Logger + */ + private static final Logger log = Logger.getLogger(StringAsset.class.getName()); + + //-------------------------------------------------------------------------------------|| + // Instance Members -------------------------------------------------------------------|| + //-------------------------------------------------------------------------------------|| + + /** + * Underlying content. + */ + private final String content; + + //-------------------------------------------------------------------------------------|| + // Constructor ------------------------------------------------------------------------|| + //-------------------------------------------------------------------------------------|| + + /** + * Creates a new {@link Asset} instance backed by the specified String + * + * @param content The content represented as a String + * @throws IllegalArgumentException If the contents were not specified + */ + public StringAsset(final String content) + { + // Precondition check + Validate.notNull(content, "content must be specified"); + // don't need to copy since String is immutable + this.content = content; + if (log.isLoggable(Level.FINER)) + { + log.finer("Created " + this + " with backing String of size " + content.length() + "b"); + } + } + + /** + * Creates a new {@link Asset} instance backed by the String + * contained in the the specified {@link InputStream} + * + * @param stream + * @throws IllegalArgumentException If the stream is not specified + */ + public StringAsset(final InputStream stream) + { + // Delegate + this(IOUtil.asString(stream)); + } + + //-------------------------------------------------------------------------------------|| + // Required Implementations -----------------------------------------------------------|| + //-------------------------------------------------------------------------------------|| + + /** + * @see org.jboss.shrinkwrap.api.Asset#openStream() + */ + + @Override + public InputStream openStream() + { + return new ByteArrayInputStream(content.getBytes()); + } + + /** + * {@inheritDoc} + * @see java.lang.Object#toString() + */ + @Override + public String toString() + { + return "StringAsset [content size=" + content.length() + " bytes]"; + } + +} Index: impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java =================================================================== --- impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java (revision 4330) +++ impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java (working copy) @@ -16,10 +16,12 @@ */ package org.jboss.shrinkwrap.impl.base.io; +import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; @@ -124,7 +126,52 @@ // Return return content; } + + /** + * Obtains the contents of the specified stream + * as a String + * + * @param in + * @throws IllegalArgumentException If the stream was not specified + */ + public static String asString(InputStream in) + { + // Precondition check + Validate.notNull(in, "Stream must be specified"); + StringBuilder buffer = new StringBuilder(); + String line; + + try + { + BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); + while ((line = reader.readLine()) != null) + { + buffer.append(line).append(Character.LINE_SEPARATOR); + } + } + catch (IOException ioe) + { + throw new RuntimeException("Error in obtaining string from " + in, ioe); + } + finally + { + try + { + in.close(); + } + catch (IOException ignore) + { + if (log.isLoggable(Level.FINER)) + { + log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring"); + } + } + } + + return buffer.toString(); + } + /** * Copies the contents from an InputStream to an OutputStream. It is the * responsibility of the caller to close the streams passed in when done,