Skip to content

Commit

Permalink
FORGE-2717: Introduce FileResource.resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 31, 2017
1 parent f15064b commit 0653e23
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
Expand Up @@ -6,7 +6,6 @@
*/
package org.jboss.forge.addon.projects.ui;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -158,11 +157,7 @@ private void configureTargetLocationInput(InputComponentFactory factory, final U
defaultValue = resourceFactory.create(DirectoryResource.class, OperatingSystemUtils.getUserHomeDir());
}
}
targetLocation.setDefaultValue(defaultValue)
.setValueConverter(name -> {
Path newPath = defaultValue.getUnderlyingResourceObject().toPath().resolve(name);
return resourceFactory.create(DirectoryResource.class, newPath.toFile());
});
targetLocation.setDefaultValue(defaultValue).setValueConverter(defaultValue::resolveAsDirectory);
}

private void configureUseTargetLocationRootInput(InputComponentFactory factory, final UIContext context)
Expand Down
Expand Up @@ -12,6 +12,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;

import org.jboss.forge.addon.resource.monitor.ResourceMonitor;
import org.jboss.forge.furnace.util.Assert;
Expand Down Expand Up @@ -398,4 +400,32 @@ public OutputStream getResourceOutputStream(boolean append)
throw new ResourceException("Error while creating OutputStream for Resource " + this, ioe);
}
}

@Override
public Resource<File> resolve(String path)
{
try
{
Path newPath = getUnderlyingResourceObject().toPath().resolve(path);
return getResourceFactory().create(newPath.toFile());
}
catch (InvalidPathException e)
{
return null;
}
}

@Override
public <TYPE extends Resource<File>> TYPE resolve(final Class<TYPE> type, final String path)
{
try
{
Path newPath = getUnderlyingResourceObject().toPath().resolve(path);
return getResourceFactory().create(type, newPath.toFile());
}
catch (InvalidPathException e)
{
return null;
}
}
}
Expand Up @@ -7,6 +7,7 @@
package org.jboss.forge.addon.resource;

import java.io.File;
import java.nio.file.Path;

import org.jboss.forge.addon.resource.monitor.ResourceMonitor;

Expand Down Expand Up @@ -165,4 +166,49 @@ default void setExecutable(boolean executable)
*/
void setExecutable(boolean executable, boolean owner);

/**
* Resolve a given resource based on its path
*
* @param type
* @param path
* @return <code>null</code> if no resource could be resolved for the given object.
*/
Resource<File> resolve(final String path);

/**
* Resolve a given resource based on its path
*
* @param type
* @param path
* @return <code>null</code> if no resource could be resolved for the given object.
*/
<TYPE extends Resource<File>> TYPE resolve(final Class<TYPE> type, final String path);

/**
* Resolve a {@link FileResource} from a given name.
*
* Implementations usually call {@link Path#resolve(Path)}
*
* @param class
* @param name
*/
@SuppressWarnings("unchecked")
default FileResource<?> resolveAsFile(String name) throws ResourceException
{
return resolve(FileResource.class, name);
}

/**
* Resolve a {@link DirectoryResource} from a given name.
*
* Implementations usually call {@link Path#resolve(Path)}
*
* @param class
* @param name
*/
default DirectoryResource resolveAsDirectory(String name) throws ResourceException
{
return resolve(DirectoryResource.class, name);
}

}
Expand Up @@ -6,11 +6,15 @@
*/
package org.jboss.forge.addon.resource;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.jboss.arquillian.junit.Arquillian;
Expand Down Expand Up @@ -189,7 +193,7 @@ public void testMoveDirectoryResourceToFile() throws IOException
folder.mkdir();

DirectoryResource folderResource = resourceFactory.create(DirectoryResource.class, folder);
FileResource fileResource = resourceFactory.create(FileResource.class, file);
FileResource<?> fileResource = resourceFactory.create(FileResource.class, file);
folderResource.moveTo(fileResource);
}

Expand Down Expand Up @@ -273,4 +277,33 @@ public void testFileResourceAttributes() throws Exception
resource.setReadable(false);
Assert.assertFalse(resource.isReadable());
}

@Test
public void testResolve() throws Exception
{
Path testPath = Files.createTempDirectory("test");
File test = testPath.toFile();
File foo = new File(test, "foo");
Assert.assertTrue(foo.mkdir());
DirectoryResource testDir = resourceFactory.create(test).reify(DirectoryResource.class);
Resource<File> resolvedFoo = testDir.resolve("foo");
Assert.assertThat(resolvedFoo.getFullyQualifiedName(), equalTo(foo.getAbsolutePath()));
Files.delete(foo.toPath());
Files.deleteIfExists(testPath);
}

@Test
public void testResolveAsDirectory() throws Exception
{
Path testPath = Files.createTempDirectory("test");
File test = testPath.toFile();
File foo = new File(test, "foo");
Assert.assertFalse("Foo directory should not exist", foo.exists());
DirectoryResource testDir = resourceFactory.create(test).reify(DirectoryResource.class);
DirectoryResource resolvedFoo = testDir.resolveAsDirectory("foo");
Assert.assertThat(resolvedFoo.getFullyQualifiedName(), equalTo(foo.getAbsolutePath()));
Assert.assertThat(resolvedFoo.exists(), is(false));
Files.deleteIfExists(testPath);
}

}

0 comments on commit 0653e23

Please sign in to comment.