[jboss-svn-commits] JBoss Common SVN: r3474 - in declarchive/trunk: impl-base/src/main/java/org/jboss/declarchive/impl/base and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Aug 21 14:55:50 EDT 2009
Author: johnbailey
Date: 2009-08-21 14:55:49 -0400 (Fri, 21 Aug 2009)
New Revision: 3474
Added:
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveBase.java
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveImpl.java
declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/unit/
declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/unit/MemoryMapArchiveTestCase.java
declarchive/trunk/impl-base/src/test/resources/org/jboss/declarchive/impl/base/asset/Test2.properties
declarchive/trunk/spi/src/main/java/org/jboss/declarchive/spi/MemoryMapArchive.java
Modified:
declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/Archive.java
Log:
[TMPARCH-9] - Introduced the MemoryMapArchive impl
Modified: declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/Archive.java
===================================================================
--- declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/Archive.java 2009-08-21 18:53:39 UTC (rev 3473)
+++ declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/Archive.java 2009-08-21 18:55:49 UTC (rev 3474)
@@ -39,24 +39,15 @@
String getName();
/**
- * Adds the specified assets to the archive and returns this reference
- *
- * @param assets
- * @return
- * @throws IllegalArgumentException If no assets were specified
- */
- T add(Asset... assets) throws IllegalArgumentException;
-
- /**
- * Adds the specified assets under the specified path into the
+ * Adds the specified asset under the specified path into the
* target context
*
* @param target The context under which to add the assets
- * @param assets
+ * @param asset
* @return
* @throws IllegalArgumentException If no target or assets were specified
*/
- T add(Path target, Asset... assets) throws IllegalArgumentException;
+ T add(Path target, Asset asset) throws IllegalArgumentException;
/**
* Adds the specified asset under the specified target (directory)
@@ -137,7 +128,7 @@
* @return
* @throws IllegalArgumentException If the path or archive are not specified
*/
- T add(Path path, Archive<?> arhive);
+ T add(Path path, Archive<?> archive);
/**
* Add the contents from an existing archive without
Added: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveBase.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveBase.java (rev 0)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveBase.java 2009-08-21 18:55:49 UTC (rev 3474)
@@ -0,0 +1,319 @@
+/*
+ * 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.declarchive.impl.base;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.jboss.declarchive.api.Archive;
+import org.jboss.declarchive.api.Asset;
+import org.jboss.declarchive.api.AssetNotFoundException;
+import org.jboss.declarchive.api.Path;
+import org.jboss.declarchive.impl.base.path.BasicPath;
+import org.jboss.declarchive.spi.MemoryMapArchive;
+
+/**
+ * MemoryMapArchiveBase
+ *
+ * A base implementation for all MemoryMap archives
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ * @param <T>
+ */
+public abstract class MemoryMapArchiveBase<T extends MemoryMapArchive> implements Archive<MemoryMapArchive>
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(MemoryMapArchiveBase.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Storage for the {@link Asset}s.
+ */
+ private final Map<Path, Asset> content = new ConcurrentHashMap<Path, Asset>();
+
+ /**
+ * The file name for this {@link Archive}.
+ */
+ private final String archiveName;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Constructor
+ *
+ * This constructor will generate a
+ * unique {@link Archive#getName()} per instance.
+ *
+ * @param actualType The {@link Archive} type.
+ */
+ public MemoryMapArchiveBase()
+ {
+ this("Archive-" + UUID.randomUUID().toString() + ".jar");
+ }
+
+ /**
+ * Constructor
+ *
+ * This constructor will generate an {@link Archive} with the provided name.
+ *
+ * @param archiveName
+ */
+ public MemoryMapArchiveBase(String archiveName)
+ {
+ super();
+ Validate.notNull(archiveName, "Archive name is required");
+
+ this.archiveName = archiveName;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations - Archive -------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#getName()
+ */
+ @Override
+ public String getName()
+ {
+ return archiveName;
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#add(org.jboss.declarchive.api.Path, org.jboss.declarchive.api.Asset[])
+ */
+ @Override
+ public T add(Path path, Asset asset)
+ {
+ Validate.notNull(path, "No path was specified");
+ Validate.notNull(asset, "No asset was specified");
+
+ content.put(path, asset);
+ return covariantReturn();
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#add(org.jboss.declarchive.api.Path, java.lang.String, org.jboss.declarchive.api.Asset)
+ */
+ @Override
+ public T add(Path path, String name, Asset asset)
+ {
+ Validate.notNull(path, "No path path was specified");
+ Validate.notNull(name, "No asset name was specified");
+ Validate.notNull(asset, "No asset was was specified");
+
+ content.put(new BasicPath(path, name), asset);
+ return covariantReturn();
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#add(java.lang.String, org.jboss.declarchive.api.Asset)
+ */
+ @Override
+ public T add(String name, Asset asset)
+ {
+ throw new UnsupportedOperationException("Remove when API updated");
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#contains(org.jboss.declarchive.api.Path)
+ */
+ @Override
+ public boolean contains(Path path)
+ {
+ Validate.notNull(path, "No path was specified");
+ return content.containsKey(path);
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#delete(org.jboss.declarchive.api.Path)
+ */
+ @Override
+ public boolean delete(Path path)
+ {
+ Validate.notNull(path, "No path was specified");
+ return content.remove(path) != null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#get(org.jboss.declarchive.api.Path)
+ */
+ @Override
+ public Asset get(Path path)
+ {
+ Validate.notNull(path, "No path was specified");
+ return content.get(path);
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#get(java.lang.String)
+ */
+ @Override
+ public Asset get(String path) throws AssetNotFoundException, IllegalArgumentException
+ {
+ Validate.notNull(path, "No path was specified");
+ return get(new BasicPath(path));
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#getContent()
+ */
+ @Override
+ public Map<Path, Asset> getContent()
+ {
+ return Collections.unmodifiableMap(content);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#add(org.jboss.declarchive.api.Path, org.jboss.declarchive.api.Archive)
+ */
+ @Override
+ public T add(Path path, Archive<?> archive)
+ {
+ Validate.notNull(path, "No path was specified");
+ Validate.notNull(archive, "No archive was specified");
+
+ final Path contentPath = new BasicPath(path, archive.getName());
+
+ return addContents(contentPath, archive);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#addContents(org.jboss.declarchive.api.Archive)
+ */
+ @Override
+ public T addContents(Archive<?> source) throws IllegalArgumentException
+ {
+ return addContents(new BasicPath(""), source);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#addContents(org.jboss.declarchive.api.Path, org.jboss.declarchive.api.Archive)
+ */
+ @Override
+ public T addContents(Path path, Archive<?> source) throws IllegalArgumentException
+ {
+ Validate.notNull(path, "No path was specified");
+ Validate.notNull(source, "No source archive was specified");
+
+ // Get existing contents from source archive
+ final Map<Path, Asset> sourceContent = source.getContent();
+ Validate.notNull(sourceContent, "Source archive content can not be null.");
+
+ // Add each asset from the source archive
+ for (Entry<Path, Asset> contentEntry : sourceContent.entrySet())
+ {
+ final Asset asset = contentEntry.getValue();
+ Path assetPath = contentEntry.getKey();
+ if (path != null)
+ {
+ assetPath = new BasicPath(path, assetPath);
+ }
+ add(assetPath, asset);
+ }
+ return covariantReturn();
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.declarchive.api.Archive#toString(boolean)
+ */
+ @Override
+ public String toString(boolean verbose)
+ {
+ StringBuilder sb = new StringBuilder();
+ List<Path> paths = new ArrayList<Path>(content.keySet());
+ // TODO: Make sure Paths impl Comparable
+ Collections.sort(paths, new Comparator<Path>()
+ {
+ @Override
+ public int compare(Path path1, Path path2)
+ {
+ return path1.get().compareTo(path2.get());
+ }
+
+ });
+
+ for (Path path : paths)
+ {
+ sb.append(path.get()).append('\n');
+ }
+ return sb.toString();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return toString(false);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Provides typesafe covariant return of this instance
+ */
+ protected final T covariantReturn()
+ {
+ try
+ {
+ return getActualClass().cast(this);
+ }
+ catch (final ClassCastException cce)
+ {
+ log.log(Level.SEVERE,
+ "The class specified by getActualClass is not a valid assignment target for this instance;"
+ + " developer error");
+ throw cce;
+ }
+ }
+
+ /**
+ *
+ * @return actual MemoryMapArchive type
+ */
+ protected abstract Class<T> getActualClass();
+}
Added: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveImpl.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveImpl.java (rev 0)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/MemoryMapArchiveImpl.java 2009-08-21 18:55:49 UTC (rev 3474)
@@ -0,0 +1,72 @@
+/*
+ * 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.declarchive.impl.base;
+
+import org.jboss.declarchive.api.Archive;
+import org.jboss.declarchive.spi.MemoryMapArchive;
+
+/**
+ * MemoryMapArchiveImpl
+ *
+ * A default implementation for all MemoryMap archives.
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * @version $Revision: $
+ * @param <T>
+ */
+public class MemoryMapArchiveImpl extends MemoryMapArchiveBase<MemoryMapArchive> implements MemoryMapArchive
+{
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Constructor
+ *
+ * This constructor will generate a
+ * unique {@link Archive#getName()} per instance.
+ *
+ * @param actualType The {@link Archive} type.
+ */
+ public MemoryMapArchiveImpl()
+ {
+ super();
+ }
+
+ /**
+ * Constructor
+ *
+ * This constructor will generate an {@link Archive} with the provided name.
+ *
+ * @param archiveName
+ */
+ public MemoryMapArchiveImpl(String archiveName)
+ {
+ super(archiveName);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.declarchive.impl.base.MemoryMapArchiveBase#getActualClass()
+ */
+ @Override
+ protected Class<MemoryMapArchive> getActualClass()
+ {
+ return MemoryMapArchive.class;
+ }
+
+}
Added: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/unit/MemoryMapArchiveTestCase.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/unit/MemoryMapArchiveTestCase.java (rev 0)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/unit/MemoryMapArchiveTestCase.java 2009-08-21 18:55:49 UTC (rev 3474)
@@ -0,0 +1,454 @@
+/*
+ * 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.declarchive.impl.base.unit;
+
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.jboss.declarchive.api.Archive;
+import org.jboss.declarchive.api.Asset;
+import org.jboss.declarchive.api.Path;
+import org.jboss.declarchive.impl.base.MemoryMapArchiveImpl;
+import org.jboss.declarchive.impl.base.asset.ClassLoaderAsset;
+import org.jboss.declarchive.impl.base.path.BasicPath;
+import org.jboss.declarchive.spi.MemoryMapArchive;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * MemoryMapArchiveTestCase
+ *
+ * TestCase to ensure that the MemoryMapArchive works as expected.
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class MemoryMapArchiveTestCase
+{
+ private MemoryMapArchive archive;
+
+ @Before
+ public void createMemoryArchive() throws Exception
+ {
+ archive = new MemoryMapArchiveImpl();
+ }
+
+ @After
+ public void ls()
+ {
+ System.out.println("test at jboss:/$ ls -l " + archive.getName());
+ System.out.println(archive.toString(true));
+ }
+
+ /**
+ * Test to ensure MemoryMap archives can be created with a name
+ * @throws Exception
+ */
+ @Test
+ public void testConstructorWithName() throws Exception
+ {
+ String name = "test.jar";
+ MemoryMapArchive tmp = new MemoryMapArchiveImpl(name);
+ Assert.assertEquals("Should return the same name as construtor arg", name, tmp.getName());
+ }
+
+ /**
+ * Test to ensure the MemoryMapArchive requires a name
+ * @throws Exception
+ */
+ @Test
+ public void testConstructorRequiresName() throws Exception
+ {
+ try
+ {
+ new MemoryMapArchiveImpl(null);
+ Assert.fail("Should throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding an asset to the path results in successful storage.
+ * @throws Exception
+ */
+ @Test
+ public void testAddAssetToPath() throws Exception
+ {
+ Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ Path location = new BasicPath("/", "test.properties");
+
+ archive.add(location, asset);
+
+ Assert.assertTrue("Asset should be placed on " + location.get(), archive.contains(location));
+ }
+
+ /**
+ * Ensure adding an asset to the path requires path.
+ * @throws Exception
+ */
+ @Test
+ public void testAddRequiresPath() throws Exception
+ {
+ Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+
+ try
+ {
+ archive.add((Path) null, asset);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding an asset to the path requires an asset.
+ * @throws Exception
+ */
+ @Test
+ public void testAddRequiresAssets() throws Exception
+ {
+ try
+ {
+ archive.add(new BasicPath("/", "Test.properties"), (Asset) null);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding an asset with a name results in successful storage
+ * @throws Exception
+ */
+ @Test
+ public void testAddAssetWithName() throws Exception
+ {
+ final String name = "test.properties";
+ final Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ Path location = new BasicPath("/");
+
+ archive.add(location, name, asset);
+
+ Path expectedPath = new BasicPath("/", "test.properties");
+
+ Assert.assertTrue("Asset should be placed on " + expectedPath.get(), archive.contains(expectedPath));
+ }
+
+ /**
+ * Ensure adding an asset with name requires the path attribute
+ * @throws Exception
+ */
+ @Test
+ public void testAddAssetWithNameRequiresPath() throws Exception
+ {
+ final String name = "test.properties";
+ final Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ try
+ {
+ archive.add(null, name, asset);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding an asset with name requires the name attribute
+ * @throws Exception
+ */
+ @Test
+ public void testAddAssetWithNameRequiresName() throws Exception
+ {
+ final Path path = new BasicPath("/", "Test.properties");
+ final String resource = "org/jboss/declarchive/impl/base/asset/Test.properties";
+ try
+ {
+ archive.add(path, null, new ClassLoaderAsset(resource));
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding an asset with name requires the asset attribute
+ * @throws Exception
+ */
+ @Test
+ public void testAddAssetWithNameRequiresAsset() throws Exception
+ {
+ final String name = "test.properties";
+ final Path path = new BasicPath("/", "Test.properties");
+ try
+ {
+ archive.add(path, name, null);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure deleting an asset successfully removes asset from storage
+ * @throws Exception
+ */
+ @Test
+ public void testDeleteAsset() throws Exception
+ {
+ String resource = "org/jboss/declarchive/impl/base/asset/Test.properties";
+ Path location = new BasicPath("/", "test.properties");
+ archive.add(location, new ClassLoaderAsset(resource));
+ Assert.assertTrue(archive.contains(location)); // Sanity check
+
+ Assert.assertTrue("Successfully deleting an Asset should ruturn true", archive.delete(location));
+
+ Assert.assertFalse("There should no longer be an asset at: " + location.get() + " after deleted", archive
+ .contains(location));
+ }
+
+ /**
+ * Ensure deleting a missing asset returns correct status
+ * @throws Exception
+ */
+ @Test
+ public void testDeleteMissingAsset() throws Exception
+ {
+ Path location = new BasicPath("/", "test.properties");
+
+ Assert.assertFalse("Deleting a non-existent Asset should ruturn false", archive.delete(location));
+ }
+
+ /**
+ * Ensure deleting an asset requires a path
+ * @throws Exception
+ */
+ @Test
+ public void testDeleteAssetRequiresPath() throws Exception
+ {
+ try
+ {
+ archive.delete(null);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure an asset can be retrieved by its path
+ * @throws Exception
+ */
+ @Test
+ public void testGetAsset() throws Exception
+ {
+ Path location = new BasicPath("/", "test.properties");
+ Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ archive.add(location, asset);
+
+ Assert.assertEquals("Asset should be returned from path: " + location.get(), asset, archive.get(location));
+ }
+
+ /**
+ * Ensure get asset requires a path
+ * @throws Exception
+ */
+ @Test
+ public void testGetAssetRequiresPath() throws Exception
+ {
+ try
+ {
+ archive.get((Path) null);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure get content returns the correct map of content
+ * @throws Exception
+ */
+ @Test
+ public void testToGetContent() throws Exception
+ {
+ Path location = new BasicPath("/", "test.properties");
+ Path locationTwo = new BasicPath("/", "test2.properties");
+
+ Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ Asset assetTwo = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test2.properties");
+ archive.add(location, asset).add(locationTwo, assetTwo);
+
+ Map<Path, Asset> content = archive.getContent();
+ Assert.assertEquals("Asset should existing in content with key: " + location.get(), content.get(location), asset);
+ Assert.assertEquals("Asset should existing in content with key: " + locationTwo.get(), content.get(locationTwo),
+ assetTwo);
+ }
+
+ /**
+ * Ensure adding content from another archive successfully stores all assets
+ * @throws Exception
+ */
+ @Test
+ public void testAddContents() throws Exception
+ {
+ MemoryMapArchive sourceArchive = new MemoryMapArchiveImpl();
+ Path location = new BasicPath("/", "test.properties");
+ Path locationTwo = new BasicPath("/", "test2.properties");
+
+ Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ Asset assetTwo = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test2.properties");
+ sourceArchive.add(location, asset).add(locationTwo, assetTwo);
+
+ archive.addContents(sourceArchive);
+ Assert.assertEquals("Asset should have been added to path: " + location.get(), archive.get(location), asset);
+ Assert.assertEquals("Asset should have been added to path: " + location.get(), archive.get(locationTwo), assetTwo);
+ }
+
+ /**
+ * Ensure adding content requires a source archive
+ * @throws Exception
+ */
+ @Test
+ public void testAddContentsRequiresSource() throws Exception
+ {
+ try
+ {
+ archive.addContents(null);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding content from another archive to a path successfully stores all assets to specific path
+ * @throws Exception
+ */
+ @Test
+ public void testAddContentsToPath() throws Exception
+ {
+ MemoryMapArchive sourceArchive = new MemoryMapArchiveImpl();
+ Path location = new BasicPath("/", "test.properties");
+ Path locationTwo = new BasicPath("/", "test2.properties");
+
+ Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ Asset assetTwo = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test2.properties");
+ sourceArchive.add(location, asset).add(locationTwo, assetTwo);
+
+ Path baseLocation = new BasicPath("somewhere");
+
+ archive.addContents(baseLocation, sourceArchive);
+
+ Path expectedPath = new BasicPath(baseLocation, location);
+ Path expectedPathTwo = new BasicPath(baseLocation, locationTwo);
+
+ Assert.assertEquals("Asset should have been added to path: " + expectedPath.get(), archive.get(expectedPath), asset);
+ Assert.assertEquals("Asset should have been added to path: " + expectedPathTwo.getClass(), archive
+ .get(expectedPathTwo), assetTwo);
+ }
+
+ /**
+ * Ensure adding content from another archive requires a path
+ * @throws Exception
+ */
+ @Test
+ public void testAddContentsToPathRequiresPath() throws Exception
+ {
+ try
+ {
+ archive.addContents(null, new MemoryMapArchiveImpl());
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding an archive to a path successfully stores all assets to specific path including the archive name
+ * @throws Exception
+ */
+ @Test
+ public void testAddArchiveToPath() throws Exception
+ {
+ MemoryMapArchive sourceArchive = new MemoryMapArchiveImpl("test.jar");
+ Path location = new BasicPath("/", "test.properties");
+ Path locationTwo = new BasicPath("/", "test2.properties");
+ Asset asset = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test.properties");
+ Asset assetTwo = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test2.properties");
+ sourceArchive.add(location, asset).add(locationTwo, assetTwo);
+
+ Path baseLocation = new BasicPath("somewhere");
+
+ archive.add(baseLocation, sourceArchive);
+
+ Path expectedPath = new BasicPath(new BasicPath(baseLocation, "test.jar"), location);
+ Path expectedPathTwo = new BasicPath(new BasicPath(baseLocation, "test.jar"), locationTwo);
+
+ Assert.assertEquals("Asset should have been added to path: " + expectedPath.get(), archive.get(expectedPath), asset);
+ Assert.assertEquals("Asset should have been added to path: " + expectedPathTwo.get(), archive.get(expectedPathTwo),
+ assetTwo);
+ }
+
+ /**
+ * Ensure adding an archive to a path requires a path
+ * @throws Exception
+ */
+ @Test
+ public void testAddArchiveToPathRequirePath() throws Exception
+ {
+ try
+ {
+ archive.add(null, new MemoryMapArchiveImpl());
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+ /**
+ * Ensure adding an archive to a path requires an archive
+ * @throws Exception
+ */
+ @Test
+ public void testAddArchiveToPathRequireArchive() throws Exception
+ {
+ try
+ {
+ archive.add(new BasicPath("/"), (Archive<?>) null);
+ Assert.fail("Should have throw an IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expectedException)
+ {
+ }
+ }
+
+}
Added: declarchive/trunk/impl-base/src/test/resources/org/jboss/declarchive/impl/base/asset/Test2.properties
===================================================================
--- declarchive/trunk/impl-base/src/test/resources/org/jboss/declarchive/impl/base/asset/Test2.properties (rev 0)
+++ declarchive/trunk/impl-base/src/test/resources/org/jboss/declarchive/impl/base/asset/Test2.properties 2009-08-21 18:55:49 UTC (rev 3474)
@@ -0,0 +1 @@
+declarch=true
\ No newline at end of file
Added: declarchive/trunk/spi/src/main/java/org/jboss/declarchive/spi/MemoryMapArchive.java
===================================================================
--- declarchive/trunk/spi/src/main/java/org/jboss/declarchive/spi/MemoryMapArchive.java (rev 0)
+++ declarchive/trunk/spi/src/main/java/org/jboss/declarchive/spi/MemoryMapArchive.java 2009-08-21 18:55:49 UTC (rev 3474)
@@ -0,0 +1,32 @@
+/*
+ * 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.declarchive.spi;
+
+import org.jboss.declarchive.api.Archive;
+
+/**
+ * MemoryMapArchive
+ *
+ * A pure in memory Archive backed by a Map.
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * @version $Revision: $
+ */
+public interface MemoryMapArchive extends Archive<MemoryMapArchive>
+{
+
+}
More information about the jboss-svn-commits
mailing list