Skip to content

Commit

Permalink
Further fixes for FORGE-940, committing to verify on *nix
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Jun 19, 2013
1 parent cbca116 commit 924321b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testCreateAddonProject()
{
Project project = projectFactory.createTempProject();
project.getProjectRoot().deleteOnExit();

MetadataFacet metadataFacet = project.getFacet(MetadataFacet.class);
metadataFacet.setProjectName("testproject");
metadataFacet.setProjectVersion("1.0.0-SNAPSHOT");
Expand Down Expand Up @@ -236,14 +236,17 @@ public void testCreateAddonProject()
ForgeContainerAPIFacet.FORGE_API_DEPENDENCY));
Assert.assertTrue(testsProject.getFacet(DependencyFacet.class).hasEffectiveManagedDependency(
ForgeContainerAPIFacet.FORGE_API_DEPENDENCY));

project.getProjectRoot().delete(true);
project.getProjectRoot().deleteOnExit();
}

@Test
public void testSimpleAddonProject()
{
Project project = projectFactory.createTempProject();
project.getProjectRoot().deleteOnExit();

MetadataFacet metadataFacet = project.getFacet(MetadataFacet.class);
metadataFacet.setProjectName("testproject");
metadataFacet.setProjectVersion("1.0.0-SNAPSHOT");
Expand All @@ -262,6 +265,9 @@ public void testSimpleAddonProject()
ForgeContainerAPIFacet.FORGE_API_DEPENDENCY));
Assert.assertTrue(project.getFacet(DependencyFacet.class).hasEffectiveManagedDependency(
ForgeContainerAPIFacet.FORGE_API_DEPENDENCY));

project.getProjectRoot().delete(true);
project.getProjectRoot().deleteOnExit();
}

@Test
Expand All @@ -270,7 +276,7 @@ public void testDependencyResolution()
{
Project project = projectFactory.createTempProject();
project.getProjectRoot().deleteOnExit();

MetadataFacet metadataFacet = project.getFacet(MetadataFacet.class);
metadataFacet.setProjectName("testproject");
metadataFacet.setProjectVersion("1.0.0-SNAPSHOT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static ForgeArchive getDeployment()

@Inject
private NewProjectWizard command;

@Inject
private NewProjectWizard command2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public T createTempResource()
public <R extends Resource<?>> R reify(final Class<R> type)
{
Resource<?> result = resourceFactory.create((Class) type, file);
if (type.isAssignableFrom(result.getClass()))
if (result != null && type.isAssignableFrom(result.getClass()))
{
return (R) result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
*/
package org.jboss.forge.addon.resource;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.jboss.forge.addon.facets.Faceted;
import org.jboss.forge.furnace.addons.Addon;

/**
* A Resource is an abstraction on top of usable items within a Furnace project. For instance, files, source code, etc.
* Like a simplified virtual file system, a Resource is represented hierarchically with a parent and children. This
* allows addons to say, direct access to project elements within a consistent API from files to class members. </br>
* However, Resource instances should be treated as representative query result objects. A modification to an instance
* variable in a resource will not be persisted. Rather than thinking of the Resource object as meta-data (which it is
* not), it is better conceptualized as a wrapper or "view" of an underlying resource such as a File. For this reason,
* custom Resource types should never implement any sort of static cache and should preferably lazily initialize their
* data.
* A {@link Resource} is an abstraction on top of usable items within a Furnace project. For instance, files, source
* code, etc. Like a simplified virtual file system, a Resource is represented hierarchically with a parent and
* children. This allows {@link Addon} instances to, for example, directly access to project elements within a
* consistent API from files to class members.
* <p>
* However, resource instances should be treated as representative query result objects. A modification to an instance
* variable in a resource will not be persisted. Rather than thinking of the resource object as meta-data (which it is
* not), it is better conceptualized as a wrapper or "view" of an underlying resource such as a {@link File}. For this
* reason, custom resource types should never implement any sort of static cache and should preferably lazily initialize
* their data.
*
* @author Mike Brock
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
Expand All @@ -29,14 +33,14 @@ public interface Resource<T> extends Faceted<ResourceFacet>
{

/**
* Delete this resource, return true if successful, false if not.
* Delete this {@link Resource}, return <code>true</code> if successful, <code>false</code> if not.
*
* @throws UnsupportedOperationException if deleting is not supported by the underlying implementation.
*/
boolean delete() throws UnsupportedOperationException;

/**
* Delete this resource, return true if successful, false if not.
* Delete this resource, return <code>true</code> if successful, <code>false</code> if not.
*
* @param recursive if false and this resource both supports recursive deletion and contains children, deletion will
* not occur; otherwise, if true, deletion will propagate to all child resources. Implementations may
Expand All @@ -46,37 +50,37 @@ public interface Resource<T> extends Faceted<ResourceFacet>
boolean delete(boolean recursive) throws UnsupportedOperationException;

/**
* Return the common name of the resource. If it's a file, for instance, just the file name.
* Return the common name of the {@link Resource}. If it's a file, for instance, just the file name.
*
* @return The name of the resource.
* @return The name of the {@link Resource}.
*/
String getName();

/**
* Return the fully qualified name of the resource (if applicable). In the case of a file, this would normally be the
* full path name.
* Return the fully qualified name of the resource (if applicable). In the case of a {@link File} resource, this
* would normally be the full path name.
*
* @return The fully qualified name.
*/
String getFullyQualifiedName();

/**
* Get the parent of the current resource. Returns null if the current resource is the project root.
* Get the parent of the current resource. Returns <code>null</code> if the current resource is the filesystem root.
*
* @return An instance of the resource parent.
* @return An instance of the {@link Resource} parent.
*/
Resource<?> getParent();

/**
* Create a new resource instance for the target resource reference of the type that this current resource is.
*
* @param file The target reference to create the resource instance from.
* @return A new resource.
* @return A new {@link Resource} instance.
*/
Resource<T> createFrom(T file);

/**
* Return a list of child resources of the current resource.
* Return a list of child resources of the current resource. (Never null.)
*/
List<Resource<?>> listResources();

Expand All @@ -86,7 +90,7 @@ public interface Resource<T> extends Faceted<ResourceFacet>
List<Resource<?>> listResources(ResourceFilter filter);

/**
* Get the underlying object represented by this {@link Resource}
* Get the underlying object represented by this {@link Resource}.
*/
T getUnderlyingResourceObject();

Expand All @@ -96,24 +100,24 @@ public interface Resource<T> extends Faceted<ResourceFacet>
InputStream getResourceInputStream();

/**
* Get a child of this resource. Returns null if no child by the given name can be found.
* Get a child of this resource. Returns <code>null</code> if no child by the given name can be found.
*/
Resource<?> getChild(String name);

/**
* Return true if this {@link Resource} exists, return false if not.
* Return <code>true</code> if this {@link Resource} exists, return <code>false</code> if not.
*/
boolean exists();

/**
* Ask this {@link Resource} if it is actually a resource of the given type; if it is, return a new reference to the
* resource as the given type, otherwise return null.
* resource as the given type, otherwise return <code>null</code>.
*/
<R extends Resource<?>> R reify(final Class<R> type);

/**
* Return the {@link ResourceFactory} with which this {@link Resource} was created. If no factory was used, return
* null.
* <code>null</code>.
*/
ResourceFactory getResourceFactory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ public interface ResourceFactory
{
/**
* Create a {@link Resource} of the given type, using the provided underlying resource instance.
*
* @return <code>null</code> if no resource could be created for the given object.
*/
public abstract <E, T extends Resource<E>> T create(Class<T> type, E underlyingResource);

/**
* Create a {@link Resource} to represent the provided underlying resource. The resource type will be detected
* automatically.
*
* @return <code>null</code> if no resource could be created for the given object.
*/
public abstract <E> Resource<E> create(E underlyingResource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ public boolean handles(Class<?> type, Object resource)
@SuppressWarnings("unchecked")
public <T extends Resource<File>> T getResource(ResourceFactory factory, Class<FileResource<?>> type, File resource)
{
if (DirectoryResource.class.isAssignableFrom(type) || (resource.exists() && resource.isDirectory()))
if ((DirectoryResource.class.isAssignableFrom(type) && (!resource.exists() || resource.isDirectory()))
|| (resource.exists() && resource.isDirectory()))
return (T) new DirectoryResourceImpl(factory, resource);
return (T) new FileResourceImpl(factory, resource);
}

@Override
public <T extends Resource<File>> Class<?> getResourceType(Class<FileResource<?>> type, File resource)
{
if (DirectoryResource.class.isAssignableFrom(type) || (resource.exists() && resource.isDirectory()))
if ((DirectoryResource.class.isAssignableFrom(type) && (!resource.exists() || resource.isDirectory()))
|| (resource.exists() && resource.isDirectory()))
return DirectoryResource.class;
return FileResource.class;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.addons.AddonId;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.forge.furnace.util.OperatingSystemUtils;
import org.jboss.forge.furnace.util.Streams;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
Expand Down Expand Up @@ -49,15 +50,17 @@ public static ForgeArchive getDeployment()
@Test
public void testCreateUnknownFileResource() throws Exception
{
FileResource<?> resource = factory.create(new File(UUID.randomUUID().toString())).reify(FileResource.class);
File tempFile = new File(UUID.randomUUID().toString());
FileResource<?> resource = factory.create(tempFile).reify(FileResource.class);
Assert.assertNotNull(resource);
Assert.assertEquals(FileResourceImpl.class, resource.getClass());
}

@Test
public void testCreateFileResource() throws Exception
{
File file = File.createTempFile("temp", "file");
File file = File.createTempFile("forge", "testCreateFileResource");
file.deleteOnExit();
FileResource<?> resource = factory.create(file).reify(FileResource.class);
Assert.assertNotNull(resource);
Assert.assertEquals(FileResourceImpl.class, resource.getClass());
Expand All @@ -66,9 +69,10 @@ public void testCreateFileResource() throws Exception
@Test
public void testCreateDirectoryResource() throws Exception
{
File dir = File.createTempFile("temp", "file");
File dir = File.createTempFile("forge", "testCreateDirectoryResource");
dir.delete();
dir.mkdir();
dir.deleteOnExit();

Resource<File> resource = factory.create(dir);
Assert.assertNotNull(resource);
Expand All @@ -79,12 +83,14 @@ public void testCreateDirectoryResource() throws Exception
@Test
public void testCreateDirectoryResourceViaRiefy() throws Exception
{
File dir = File.createTempFile("temp", "file");
File dir = File.createTempFile("forge", "testCreateDirectoryResourceViaRiefy");
dir.delete();
dir.mkdir();
dir.deleteOnExit();

File child = new File(dir, "child");
child.mkdir();
child.deleteOnExit();

DirectoryResource resource = factory.create(dir).reify(DirectoryResource.class);
Assert.assertNotNull(resource);
Expand All @@ -98,7 +104,8 @@ public void testCreateDirectoryResourceViaRiefy() throws Exception
@Test
public void testFileSize() throws Exception
{
File file = File.createTempFile("temp", "file");
File file = File.createTempFile("forge", "testFileSize");
file.deleteOnExit();

FileOutputStream fos = new FileOutputStream(file);
Streams.write(new ByteArrayInputStream("Test".getBytes()), fos);
Expand All @@ -113,45 +120,65 @@ public void testFileSize() throws Exception
@Test(expected = UnsupportedOperationException.class)
public void testDirectorySize() throws Exception
{
File dir = File.createTempFile("temp", "file");
File dir = File.createTempFile("forge", "testDirectorySize");
dir.delete();
dir.mkdir();
dir.deleteOnExit();
factory.create(dir).reify(DirectoryResource.class).getSize();
}

@Test
public void testFileFlags() throws Exception
{
File tempFile = File.createTempFile("temp", "file");
File tempFile = File.createTempFile("forge", "testFileFlags");
tempFile.deleteOnExit();
FileResource<?> resource = factory.create(tempFile).reify(FileResource.class);
Assert.assertFalse(resource.isExecutable());

if (OperatingSystemUtils.isWindows())
Assert.assertTrue(resource.isExecutable());
else
Assert.assertFalse(resource.isExecutable());

Assert.assertTrue(resource.isReadable());
Assert.assertTrue(resource.isWritable());
}

@Test
public void testDirectoryFlags() throws Exception
{
File dir = File.createTempFile("temp", "file");
dir.delete();
dir.mkdir();
dir.deleteOnExit();
DirectoryResource resource = factory.create(dir).reify(DirectoryResource.class);
File file = File.createTempFile("forge", "testDirectoryFlags");
file.delete();
DirectoryResource resource = factory.create(file).reify(DirectoryResource.class);
resource.deleteOnExit();
resource.mkdir();
Assert.assertFalse(resource.isExecutable());
Assert.assertFalse(resource.isReadable());
Assert.assertFalse(resource.isWritable());
}

@Test
public void testReifyDirectoryResourceFailsIfFileExists() throws Exception
{
File tempFile = File.createTempFile("forge", "testReifyDirectoryResourceFailsIfFileExists");
tempFile.deleteOnExit();
DirectoryResource reified = factory.create(tempFile).reify(DirectoryResource.class);
Assert.assertNull(reified);
}

@Test
public void testRenameResource() throws Exception
{
File file = File.createTempFile("temp", "file");
FileResource<?> resource = factory.create(file).reify(FileResource.class);
Assert.assertNotNull(resource);
FileResource<?> child = resource.getParent().getChild("testFile").reify(FileResource.class);
resource.renameTo(child);
Assert.assertEquals(child.getFullyQualifiedName(), resource.getFullyQualifiedName());
File tempFile = File.createTempFile("forge", "testRenameResource");
tempFile.delete();
DirectoryResource tempDir = factory.create(tempFile).reify(DirectoryResource.class);
tempDir.deleteOnExit();

Assert.assertNotNull(tempDir);
FileResource<?> child = tempDir.getChild("testFile").reify(FileResource.class);
FileResource<?> child2 = tempDir.getChild("testFile2").reify(FileResource.class);
Assert.assertTrue(child.createNewFile());
Assert.assertTrue(child.renameTo(child2));
Assert.assertEquals(child.getFullyQualifiedName(), child2.getFullyQualifiedName());
}

}

0 comments on commit 924321b

Please sign in to comment.