diff --git a/pom.xml b/pom.xml index cf2d2bd..0f1822b 100644 --- a/pom.xml +++ b/pom.xml @@ -17,8 +17,8 @@ scm:git:git@github.com:codehaus-plexus/plexus-resources.git scm:git:git@github.com:codehaus-plexus/plexus-resources.git - http://github.com/codehaus-plexus/plexus-resources plexus-resources-1.2.0 + http://github.com/codehaus-plexus/plexus-resources jira @@ -98,15 +98,17 @@ org.apache.maven.plugins maven-scm-publish-plugin - ${project.reporting.outputDirectory} + ${project.reporting.outputDirectory} + scm-publish - site-deploy + publish-scm + site-deploy diff --git a/src/main/java/org/codehaus/plexus/resource/DefaultResourceManager.java b/src/main/java/org/codehaus/plexus/resource/DefaultResourceManager.java index 8ad3d1c..db14dfe 100644 --- a/src/main/java/org/codehaus/plexus/resource/DefaultResourceManager.java +++ b/src/main/java/org/codehaus/plexus/resource/DefaultResourceManager.java @@ -24,6 +24,15 @@ * SOFTWARE. */ +import javax.inject.Inject; +import javax.inject.Named; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; import org.codehaus.plexus.resource.loader.FileResourceCreationException; import org.codehaus.plexus.resource.loader.ResourceIOException; @@ -34,32 +43,21 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Map; -import javax.inject.Inject; -import javax.inject.Named; - /** * @author Trygve Laugstøl * @author Jason van Zyl * @version $Id$ */ @Named -public class DefaultResourceManager implements ResourceManager -{ - private static final Logger LOGGER = LoggerFactory.getLogger( DefaultResourceManager.class ); +public class DefaultResourceManager implements ResourceManager { + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultResourceManager.class); private final Map resourceLoaders; private File outputDirectory; @Inject - public DefaultResourceManager( Map resourceLoaders ) - { + public DefaultResourceManager(Map resourceLoaders) { this.resourceLoaders = resourceLoaders; } @@ -68,173 +66,131 @@ public DefaultResourceManager( Map resourceLoaders ) // ---------------------------------------------------------------------- @Override - public InputStream getResourceAsInputStream( String name ) - throws ResourceNotFoundException - { - PlexusResource resource = getResource( name ); - try - { + public InputStream getResourceAsInputStream(String name) throws ResourceNotFoundException { + PlexusResource resource = getResource(name); + try { return resource.getInputStream(); - } - catch ( IOException e ) - { - throw new ResourceIOException( "Failed to open resource " + resource.getName() + ": " + e.getMessage(), e ); + } catch (IOException e) { + throw new ResourceIOException("Failed to open resource " + resource.getName() + ": " + e.getMessage(), e); } } @Override - public File getResourceAsFile( String name ) - throws ResourceNotFoundException, FileResourceCreationException - { - return getResourceAsFile( getResource( name ) ); + public File getResourceAsFile(String name) throws ResourceNotFoundException, FileResourceCreationException { + return getResourceAsFile(getResource(name)); } @Override - public File getResourceAsFile( String name, String outputPath ) - throws ResourceNotFoundException, FileResourceCreationException - { - if ( outputPath == null ) - { - return getResourceAsFile( name ); + public File getResourceAsFile(String name, String outputPath) + throws ResourceNotFoundException, FileResourceCreationException { + if (outputPath == null) { + return getResourceAsFile(name); } - PlexusResource resource = getResource( name ); + PlexusResource resource = getResource(name); File outputFile; - if ( outputDirectory != null ) - { - outputFile = new File( outputDirectory, outputPath ); - } - else - { - outputFile = new File( outputPath ); + if (outputDirectory != null) { + outputFile = new File(outputDirectory, outputPath); + } else { + outputFile = new File(outputPath); } - createResourceAsFile( resource, outputFile ); + createResourceAsFile(resource, outputFile); return outputFile; } @Override - public File resolveLocation( String name, String outputPath ) - { + public File resolveLocation(String name, String outputPath) { // Honour what the original locator does and return null ... - try - { - return getResourceAsFile( name, outputPath ); - } - catch ( Exception e ) - { + try { + return getResourceAsFile(name, outputPath); + } catch (Exception e) { return null; } } @Override - public File resolveLocation( String name ) - { + public File resolveLocation(String name) { // Honour what the original locator does and return null ... - try - { - return getResourceAsFile( name ); - } - catch ( Exception e ) - { + try { + return getResourceAsFile(name); + } catch (Exception e) { return null; } } @Override - public void setOutputDirectory( File outputDirectory ) - { + public void setOutputDirectory(File outputDirectory) { this.outputDirectory = outputDirectory; } @Override - public void addSearchPath( String id, String path ) - { - ResourceLoader loader = resourceLoaders.get( id ); + public void addSearchPath(String id, String path) { + ResourceLoader loader = resourceLoaders.get(id); - if ( loader == null ) - { - throw new IllegalArgumentException( "unknown resource loader: " + id ); + if (loader == null) { + throw new IllegalArgumentException("unknown resource loader: " + id); } - loader.addSearchPath( path ); + loader.addSearchPath(path); } @Override - public PlexusResource getResource( String name ) - throws ResourceNotFoundException - { - for ( ResourceLoader resourceLoader : resourceLoaders.values() ) - { - try - { - PlexusResource resource = resourceLoader.getResource( name ); + public PlexusResource getResource(String name) throws ResourceNotFoundException { + for (ResourceLoader resourceLoader : resourceLoaders.values()) { + try { + PlexusResource resource = resourceLoader.getResource(name); - LOGGER.debug( "The resource '{}' was found as '{}'", name, resource.getName() ); + LOGGER.debug("The resource '{}' was found as '{}'", name, resource.getName()); return resource; - } - catch ( ResourceNotFoundException e ) - { - LOGGER.debug( "The resource '{}' was not found with resourceLoader '{}'", - name, resourceLoader.getClass().getName() ); + } catch (ResourceNotFoundException e) { + LOGGER.debug( + "The resource '{}' was not found with resourceLoader '{}'", + name, + resourceLoader.getClass().getName()); } } - throw new ResourceNotFoundException( name ); + throw new ResourceNotFoundException(name); } @Override - public File getResourceAsFile( PlexusResource resource ) - throws FileResourceCreationException - { - try - { + public File getResourceAsFile(PlexusResource resource) throws FileResourceCreationException { + try { File f = resource.getFile(); - if ( f != null ) - { + if (f != null) { return f; } - } - catch ( IOException e ) - { + } catch (IOException e) { // Ignore this, try to make use of resource.getInputStream(). } - final File outputFile = FileUtils.createTempFile( "plexus-resources", "tmp", outputDirectory ); + final File outputFile = FileUtils.createTempFile("plexus-resources", "tmp", outputDirectory); outputFile.deleteOnExit(); - createResourceAsFile( resource, outputFile ); + createResourceAsFile(resource, outputFile); return outputFile; } @Override - public void createResourceAsFile( PlexusResource resource, File outputFile ) - throws FileResourceCreationException - { + public void createResourceAsFile(PlexusResource resource, File outputFile) throws FileResourceCreationException { InputStream is = null; OutputStream os = null; - try - { + try { is = resource.getInputStream(); File dir = outputFile.getParentFile(); - if ( !dir.isDirectory() && !dir.mkdirs() ) - { - throw new FileResourceCreationException( "Failed to create directory " + dir.getPath() ); + if (!dir.isDirectory() && !dir.mkdirs()) { + throw new FileResourceCreationException("Failed to create directory " + dir.getPath()); } - os = new FileOutputStream( outputFile ); - IOUtil.copy( is, os ); + os = new FileOutputStream(outputFile); + IOUtil.copy(is, os); is.close(); is = null; os.close(); os = null; - } - catch ( IOException e ) - { - throw new FileResourceCreationException( "Cannot create file-based resource:" + e.getMessage(), e ); - } - finally - { - IOUtil.close( is ); - IOUtil.close( os ); + } catch (IOException e) { + throw new FileResourceCreationException("Cannot create file-based resource:" + e.getMessage(), e); + } finally { + IOUtil.close(is); + IOUtil.close(os); } } - } diff --git a/src/main/java/org/codehaus/plexus/resource/PlexusResource.java b/src/main/java/org/codehaus/plexus/resource/PlexusResource.java index c41d3bf..158d737 100644 --- a/src/main/java/org/codehaus/plexus/resource/PlexusResource.java +++ b/src/main/java/org/codehaus/plexus/resource/PlexusResource.java @@ -33,11 +33,10 @@ /** * A resource is a byte stream, possibly (but not necessarily) with additional attributes like {@link File}, {@link URL} * , or {@link URI}. - * + * * @since 1.0-alpha-5 */ -public interface PlexusResource -{ +public interface PlexusResource { /** *

* Returns the resource as an {@link InputStream}. In general, you should not assume, that this method may me called @@ -49,7 +48,7 @@ public interface PlexusResource * If you need a reliable way of reloading the resource more than once, then you should use * {@link ResourceManager#getResourceAsFile(PlexusResource)}. *

- * + * * @return An {@link InputStream} with the resources contents, never null. */ InputStream getInputStream() throws IOException; @@ -59,7 +58,7 @@ public interface PlexusResource * Returns the resource as a file, if possible. A resource doesn't need to be available as a file: If you require a * file, use {@link ResourceManager#getResourceAsFile(PlexusResource)}. *

- * + * * @return A {@link File} containing the resources contents, if available, or null. */ File getFile() throws IOException; @@ -68,7 +67,7 @@ public interface PlexusResource *

* Returns the resources URL, if possible. A resource doesn't need to have an URL. *

- * + * * @return The resources URL, if available, or null. */ URL getURL() throws IOException; @@ -77,7 +76,7 @@ public interface PlexusResource *

* Returns the resources URI, if possible. A resource doesn't need to have an URI. *

- * + * * @return The resources URI, if available, or null. */ URI getURI() throws IOException; diff --git a/src/main/java/org/codehaus/plexus/resource/ResourceManager.java b/src/main/java/org/codehaus/plexus/resource/ResourceManager.java index 4fc4d95..a2ff6e5 100644 --- a/src/main/java/org/codehaus/plexus/resource/ResourceManager.java +++ b/src/main/java/org/codehaus/plexus/resource/ResourceManager.java @@ -24,72 +24,64 @@ * SOFTWARE. */ -import java.io.InputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; -import org.codehaus.plexus.resource.loader.ResourceNotFoundException; import org.codehaus.plexus.resource.loader.FileResourceCreationException; +import org.codehaus.plexus.resource.loader.ResourceNotFoundException; /** * @author Trygve Laugstøl * @author Jason van Zyl * @version $Id$ */ -public interface ResourceManager -{ - InputStream getResourceAsInputStream( String name ) - throws ResourceNotFoundException; +public interface ResourceManager { + InputStream getResourceAsInputStream(String name) throws ResourceNotFoundException; - File getResourceAsFile( String name ) - throws ResourceNotFoundException, FileResourceCreationException; + File getResourceAsFile(String name) throws ResourceNotFoundException, FileResourceCreationException; - File getResourceAsFile( String name, String outputFile ) - throws ResourceNotFoundException, FileResourceCreationException; + File getResourceAsFile(String name, String outputFile) + throws ResourceNotFoundException, FileResourceCreationException; - void setOutputDirectory( File outputDirectory ); + void setOutputDirectory(File outputDirectory); - void addSearchPath( String resourceLoaderId, String searchPath ); + void addSearchPath(String resourceLoaderId, String searchPath); /** * Provides compatibility with the Locator utility used by several Maven Plugins. - * + * * @deprecated */ - File resolveLocation( String location, String localfile ) - throws IOException; + File resolveLocation(String location, String localfile) throws IOException; /** * Provides compatibility with the Locator utility used by several Maven Plugins. - * + * * @deprecated */ - File resolveLocation( String location ) - throws IOException; + File resolveLocation(String location) throws IOException; /** * Searches for a resource with the given name. - * + * * @since 1.0-alpha-5 */ - PlexusResource getResource( String name ) - throws ResourceNotFoundException; + PlexusResource getResource(String name) throws ResourceNotFoundException; /** * Returns a file with the given resources contents. If the resource is already available as a file, returns that * file. Otherwise, a file in the resource managers output directory is created and the resource is downloaded to * that file. - * + * * @since 1.0-alpha-5 */ - File getResourceAsFile( PlexusResource resource ) - throws FileResourceCreationException; + File getResourceAsFile(PlexusResource resource) throws FileResourceCreationException; /** * Downloads the resource to the given output file. - * + * * @since 1.0-alpha-5 */ - void createResourceAsFile( PlexusResource resource, File outputFile ) - throws FileResourceCreationException; + void createResourceAsFile(PlexusResource resource, File outputFile) throws FileResourceCreationException; } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/AbstractResourceLoader.java b/src/main/java/org/codehaus/plexus/resource/loader/AbstractResourceLoader.java index 112efae..320e34e 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/AbstractResourceLoader.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/AbstractResourceLoader.java @@ -24,43 +24,35 @@ * SOFTWARE. */ -import org.codehaus.plexus.resource.PlexusResource; - import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import org.codehaus.plexus.resource.PlexusResource; + /** * @author Trygve Laugstøl * @version $Id$ */ -public abstract class AbstractResourceLoader implements ResourceLoader -{ +public abstract class AbstractResourceLoader implements ResourceLoader { protected final List paths = new ArrayList<>(); @Override - public void addSearchPath( String path ) - { - if ( !paths.contains( path ) ) - { - paths.add( path ); + public void addSearchPath(String path) { + if (!paths.contains(path)) { + paths.add(path); } } @Override @Deprecated - public InputStream getResourceAsInputStream( String name ) - throws ResourceNotFoundException - { - PlexusResource resource = getResource( name ); - try - { + public InputStream getResourceAsInputStream(String name) throws ResourceNotFoundException { + PlexusResource resource = getResource(name); + try { return resource.getInputStream(); - } - catch ( IOException e ) - { - throw new ResourceIOException( "Failed to open resource " + resource.getName() + ": " + e.getMessage(), e ); + } catch (IOException e) { + throw new ResourceIOException("Failed to open resource " + resource.getName() + ": " + e.getMessage(), e); } } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/FilePlexusResource.java b/src/main/java/org/codehaus/plexus/resource/loader/FilePlexusResource.java index 9f6e959..4274376 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/FilePlexusResource.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/FilePlexusResource.java @@ -36,45 +36,35 @@ /** * Implementation of {@link PlexusResource} for files. */ -public class FilePlexusResource - implements PlexusResource -{ +public class FilePlexusResource implements PlexusResource { private final File file; - public FilePlexusResource( File file ) - { + public FilePlexusResource(File file) { this.file = file; } @Override - public File getFile() - { + public File getFile() { return file; } @Override - public InputStream getInputStream() - throws IOException - { - return new FileInputStream( file ); + public InputStream getInputStream() throws IOException { + return new FileInputStream(file); } @Override - public String getName() - { + public String getName() { return file.getPath(); } @Override - public URI getURI() - { + public URI getURI() { return file.toURI(); } @Override - public URL getURL() - throws IOException - { + public URL getURL() throws IOException { return file.toURI().toURL(); } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/FileResourceCreationException.java b/src/main/java/org/codehaus/plexus/resource/loader/FileResourceCreationException.java index 349cc0d..c5d409e 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/FileResourceCreationException.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/FileResourceCreationException.java @@ -25,23 +25,18 @@ */ /** @author Jason van Zyl */ -public class FileResourceCreationException - extends Exception -{ +public class FileResourceCreationException extends Exception { private static final long serialVersionUID = 3203687211821479687L; - public FileResourceCreationException( String string ) - { - super( string ); + public FileResourceCreationException(String string) { + super(string); } - public FileResourceCreationException( String string, Throwable throwable ) - { - super( string, throwable ); + public FileResourceCreationException(String string, Throwable throwable) { + super(string, throwable); } - public FileResourceCreationException( Throwable throwable ) - { - super( throwable ); + public FileResourceCreationException(Throwable throwable) { + super(throwable); } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/FileResourceLoader.java b/src/main/java/org/codehaus/plexus/resource/loader/FileResourceLoader.java index ec6c24f..8a86587 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/FileResourceLoader.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/FileResourceLoader.java @@ -24,22 +24,21 @@ * SOFTWARE. */ -import org.codehaus.plexus.resource.PlexusResource; -import org.codehaus.plexus.util.FileUtils; +import javax.inject.Named; import java.io.File; import java.io.IOException; -import javax.inject.Named; + +import org.codehaus.plexus.resource.PlexusResource; +import org.codehaus.plexus.util.FileUtils; /** * @author Trygve Laugstøl * @author Jason van Zyl * @version $Id$ */ -@Named( FileResourceLoader.ID ) -public class FileResourceLoader - extends AbstractResourceLoader -{ +@Named(FileResourceLoader.ID) +public class FileResourceLoader extends AbstractResourceLoader { public static final String ID = "file"; // ---------------------------------------------------------------------- @@ -47,69 +46,52 @@ public class FileResourceLoader // ---------------------------------------------------------------------- @Override - public PlexusResource getResource( String name ) - throws ResourceNotFoundException - { - for ( String path : paths ) - { - final File file = new File( path, name ); - - if ( file.canRead() ) - { - return new FilePlexusResource( file ); + public PlexusResource getResource(String name) throws ResourceNotFoundException { + for (String path : paths) { + final File file = new File(path, name); + + if (file.canRead()) { + return new FilePlexusResource(file); } } - File file = new File( name ); - if ( file.isAbsolute() && file.canRead() ) - { - return new FilePlexusResource( file ); + File file = new File(name); + if (file.isAbsolute() && file.canRead()) { + return new FilePlexusResource(file); } - throw new ResourceNotFoundException( name ); + throw new ResourceNotFoundException(name); } /** * @deprecated Use {@link org.codehaus.plexus.resource.ResourceManager#getResourceAsFile(PlexusResource)}. */ @Deprecated - public static File getResourceAsFile( String name, String outputPath, File outputDirectory ) - throws FileResourceCreationException + public static File getResourceAsFile(String name, String outputPath, File outputDirectory) + throws FileResourceCreationException { - { - File f = new File( name ); + File f = new File(name); - if ( f.exists() ) - { - if ( outputPath == null ) - { + if (f.exists()) { + if (outputPath == null) { return f; - } - else - { - try - { + } else { + try { File outputFile; - if ( outputDirectory != null ) - { - outputFile = new File( outputDirectory, outputPath ); - } - else - { - outputFile = new File( outputPath ); + if (outputDirectory != null) { + outputFile = new File(outputDirectory, outputPath); + } else { + outputFile = new File(outputPath); } - if ( !outputFile.getParentFile().exists() ) - { + if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } - FileUtils.copyFile( f, outputFile ); + FileUtils.copyFile(f, outputFile); return outputFile; - } - catch ( IOException e ) - { - throw new FileResourceCreationException( "Cannot create file-based resource.", e ); + } catch (IOException e) { + throw new FileResourceCreationException("Cannot create file-based resource.", e); } } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/JarHolder.java b/src/main/java/org/codehaus/plexus/resource/loader/JarHolder.java index e86c0ef..48f36e1 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/JarHolder.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/JarHolder.java @@ -39,52 +39,43 @@ /** * A small wrapper around a Jar - * + * * @author Dave Bryson * @author Jason van Zyl * @version $Id$ */ -public class JarHolder -{ +public class JarHolder { private final String urlpath; private JarFile theJar = null; private JarURLConnection conn = null; - public JarHolder( String urlpath ) - { + public JarHolder(String urlpath) { this.urlpath = urlpath; - try - { - URL url = new URL( urlpath ); + try { + URL url = new URL(urlpath); conn = (JarURLConnection) url.openConnection(); - conn.setAllowUserInteraction( false ); + conn.setAllowUserInteraction(false); - conn.setDoInput( true ); + conn.setDoInput(true); - conn.setDoOutput( false ); + conn.setDoOutput(false); conn.connect(); theJar = conn.getJarFile(); - } - catch ( IOException ioe ) - { + } catch (IOException ioe) { } } - public void close() - { - try - { + public void close() { + try { theJar.close(); - } - catch ( Exception e ) - { + } catch (Exception e) { } theJar = null; @@ -92,94 +83,73 @@ public void close() conn = null; } - public InputStream getResource( String theentry ) - throws ResourceNotFoundException - { + public InputStream getResource(String theentry) throws ResourceNotFoundException { InputStream data = null; - try - { - JarEntry entry = theJar.getJarEntry( theentry ); + try { + JarEntry entry = theJar.getJarEntry(theentry); - if ( entry != null ) - { - data = theJar.getInputStream( entry ); + if (entry != null) { + data = theJar.getInputStream(entry); } - } - catch ( Exception fnfe ) - { - throw new ResourceNotFoundException( fnfe.getMessage() ); + } catch (Exception fnfe) { + throw new ResourceNotFoundException(fnfe.getMessage()); } return data; } - public Hashtable getEntries() - { - Hashtable allEntries = new Hashtable<>( 559 ); + public Hashtable getEntries() { + Hashtable allEntries = new Hashtable<>(559); - if ( theJar != null ) - { + if (theJar != null) { Enumeration all = theJar.entries(); - while ( all.hasMoreElements() ) - { + while (all.hasMoreElements()) { JarEntry je = all.nextElement(); // We don't map plain directory entries - if ( !je.isDirectory() ) - { - allEntries.put( je.getName(), this.urlpath ); + if (!je.isDirectory()) { + allEntries.put(je.getName(), this.urlpath); } } } return allEntries; } - public String getUrlPath() - { + public String getUrlPath() { return urlpath; } - public PlexusResource getPlexusResource( final String name ) - { - final JarEntry entry = theJar.getJarEntry( name ); - if ( entry == null ) - { + public PlexusResource getPlexusResource(final String name) { + final JarEntry entry = theJar.getJarEntry(name); + if (entry == null) { return null; } - return new PlexusResource() - { + return new PlexusResource() { @Override - public File getFile() - { + public File getFile() { return null; } @Override - public InputStream getInputStream() - throws IOException - { - return theJar.getInputStream( entry ); + public InputStream getInputStream() throws IOException { + return theJar.getInputStream(entry); } @Override - public String getName() - { + public String getName() { return conn.getURL() + name; } @Override - public URI getURI() - { + public URI getURI() { return null; } @Override - public URL getURL() - throws IOException - { - return new URL( conn.getJarFileURL(), name ); + public URL getURL() throws IOException { + return new URL(conn.getJarFileURL(), name); } }; } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/JarResourceLoader.java b/src/main/java/org/codehaus/plexus/resource/loader/JarResourceLoader.java index 9f72353..95c6043 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/JarResourceLoader.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/JarResourceLoader.java @@ -1,6 +1,5 @@ package org.codehaus.plexus.resource.loader; - /* * The MIT License * @@ -25,78 +24,70 @@ * SOFTWARE. */ -import org.codehaus.plexus.resource.PlexusResource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import javax.inject.Named; import java.util.LinkedHashMap; import java.util.Map; -import javax.inject.Named; +import org.codehaus.plexus.resource.PlexusResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Jason van Zyl */ -@Named( JarResourceLoader.ID ) -public class JarResourceLoader - extends AbstractResourceLoader -{ - private static final Logger LOGGER = LoggerFactory.getLogger( JarResourceLoader.class ); +@Named(JarResourceLoader.ID) +public class JarResourceLoader extends AbstractResourceLoader { + private static final Logger LOGGER = LoggerFactory.getLogger(JarResourceLoader.class); public static final String ID = "jar"; /** * Maps entries to the parent JAR File (key = the entry *excluding* plain directories, value = the JAR URL). */ - private final Map entryDirectory = new LinkedHashMap<>( 559 ); + private final Map entryDirectory = new LinkedHashMap<>(559); /** * Maps JAR URLs to the actual JAR (key = the JAR URL, value = the JAR). */ - private final Map jarFiles = new LinkedHashMap<>( 89 ); + private final Map jarFiles = new LinkedHashMap<>(89); - private void loadJar( String path ) - { - LOGGER.debug( "JarResourceLoader : trying to load '{}'", path ); + private void loadJar(String path) { + LOGGER.debug("JarResourceLoader : trying to load '{}'", path); // Check path information - if ( path == null ) - { - LOGGER.error( "JarResourceLoader : can not load JAR - JAR path is null" ); + if (path == null) { + LOGGER.error("JarResourceLoader : can not load JAR - JAR path is null"); return; } - if ( !path.startsWith( "jar:" ) ) - { - LOGGER.error( "JarResourceLoader : JAR path must start with jar: -> " - + "see java.net.JarURLConnection for information" ); + if (!path.startsWith("jar:")) { + LOGGER.error("JarResourceLoader : JAR path must start with jar: -> " + + "see java.net.JarURLConnection for information"); return; } - if ( !path.endsWith( "!/" ) ) - { + if (!path.endsWith("!/")) { path += "!/"; } // Close the jar if it's already open this is useful for a reload - closeJar( path ); + closeJar(path); // Create a new JarHolder - JarHolder temp = new JarHolder( path ); + JarHolder temp = new JarHolder(path); // Add it's entries to the entryCollection - addEntries( temp.getEntries() ); + addEntries(temp.getEntries()); // Add it to the Jar table - jarFiles.put( temp.getUrlPath(), temp ); + jarFiles.put(temp.getUrlPath(), temp); } /** * Closes a Jar file and set its URLConnection to null. */ - private void closeJar( String path ) - { - if ( jarFiles.containsKey( path ) ) - { - JarHolder theJar = jarFiles.get( path ); + private void closeJar(String path) { + if (jarFiles.containsKey(path)) { + JarHolder theJar = jarFiles.get(path); theJar.close(); } @@ -105,9 +96,8 @@ private void closeJar( String path ) /** * Copy all the entries into the entryDirectory. It will overwrite any duplicate keys. */ - private void addEntries( Map entries ) - { - entryDirectory.putAll( entries ); + private void addEntries(Map entries) { + entryDirectory.putAll(entries); } /** @@ -118,43 +108,35 @@ private void addEntries( Map entries ) * @throws ResourceNotFoundException if resource not found. */ @Override - public PlexusResource getResource( String source ) - throws ResourceNotFoundException - { - if ( source == null || source.length() == 0 ) - { - throw new ResourceNotFoundException( "Need to have a resource!" ); + public PlexusResource getResource(String source) throws ResourceNotFoundException { + if (source == null || source.length() == 0) { + throw new ResourceNotFoundException("Need to have a resource!"); } /* * if a / leads off, then just nip that :) */ - if ( source.startsWith( "/" ) ) - { - source = source.substring( 1 ); + if (source.startsWith("/")) { + source = source.substring(1); } - if ( entryDirectory.containsKey( source ) ) - { - String jarurl = entryDirectory.get( source ); + if (entryDirectory.containsKey(source)) { + String jarurl = entryDirectory.get(source); - final JarHolder holder = jarFiles.get( jarurl ); - if ( holder != null ) - { - return holder.getPlexusResource( source ); + final JarHolder holder = jarFiles.get(jarurl); + if (holder != null) { + return holder.getPlexusResource(source); } } - throw new ResourceNotFoundException( "JarResourceLoader Error: cannot find resource " + source ); + throw new ResourceNotFoundException("JarResourceLoader Error: cannot find resource " + source); } @Override - public void addSearchPath( String path ) - { - if ( !paths.contains( path ) ) - { - loadJar( path ); - paths.add( path ); + public void addSearchPath(String path) { + if (!paths.contains(path)) { + loadJar(path); + paths.add(path); } } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/ResourceIOException.java b/src/main/java/org/codehaus/plexus/resource/loader/ResourceIOException.java index a880f9f..629d19b 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/ResourceIOException.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/ResourceIOException.java @@ -29,18 +29,14 @@ * {@link ResourceLoader} encounters an {@link java.io.IOException}, which indicates that the resource exists, but * wasn't accessible. */ -public class ResourceIOException - extends ResourceNotFoundException -{ +public class ResourceIOException extends ResourceNotFoundException { private static final long serialVersionUID = 1342518075415496931L; - public ResourceIOException( String name, Throwable cause ) - { - super( name, cause ); + public ResourceIOException(String name, Throwable cause) { + super(name, cause); } - public ResourceIOException( String name ) - { - super( name ); + public ResourceIOException(String name) { + super(name); } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/ResourceLoader.java b/src/main/java/org/codehaus/plexus/resource/loader/ResourceLoader.java index 9b1d943..c1fb165 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/ResourceLoader.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/ResourceLoader.java @@ -32,24 +32,21 @@ * @author Trygve Laugstøl * @version $Id$ */ -public interface ResourceLoader -{ +public interface ResourceLoader { /** * @deprecated Use {@link #getResource(String)}. */ @Deprecated - InputStream getResourceAsInputStream( String name ) - throws ResourceNotFoundException; + InputStream getResourceAsInputStream(String name) throws ResourceNotFoundException; - void addSearchPath( String path ); + void addSearchPath(String path); /** * Returns the resource with the given name. - * + * * @param name The resources name. * @return The resource with the given name. * @throws ResourceNotFoundException The resource wasn't found, or wasn't available. */ - PlexusResource getResource( String name ) - throws ResourceNotFoundException; + PlexusResource getResource(String name) throws ResourceNotFoundException; } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/ResourceNotFoundException.java b/src/main/java/org/codehaus/plexus/resource/loader/ResourceNotFoundException.java index efbb37d..8490678 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/ResourceNotFoundException.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/ResourceNotFoundException.java @@ -28,18 +28,14 @@ * @author Trygve Laugstøl * @version $Id$ */ -public class ResourceNotFoundException - extends Exception -{ +public class ResourceNotFoundException extends Exception { private static final long serialVersionUID = -566548849252110330L; - public ResourceNotFoundException( String name ) - { - super( "Could not find resource '" + name + "'." ); + public ResourceNotFoundException(String name) { + super("Could not find resource '" + name + "'."); } - public ResourceNotFoundException( String name, Throwable cause ) - { - super( "Could not find resource '" + name + "'.", cause ); + public ResourceNotFoundException(String name, Throwable cause) { + super("Could not find resource '" + name + "'.", cause); } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoader.java b/src/main/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoader.java index 3b625a7..3736aa9 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoader.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoader.java @@ -24,21 +24,20 @@ * SOFTWARE. */ -import org.codehaus.plexus.resource.PlexusResource; - -import java.net.URL; import javax.inject.Named; import javax.inject.Singleton; +import java.net.URL; + +import org.codehaus.plexus.resource.PlexusResource; + /** * @author Trygve Laugstøl * @version $Id$ */ -@Named( ThreadContextClasspathResourceLoader.ID ) +@Named(ThreadContextClasspathResourceLoader.ID) @Singleton -public class ThreadContextClasspathResourceLoader - extends AbstractResourceLoader -{ +public class ThreadContextClasspathResourceLoader extends AbstractResourceLoader { public static final String ID = "classloader"; // ---------------------------------------------------------------------- @@ -46,27 +45,22 @@ public class ThreadContextClasspathResourceLoader // ---------------------------------------------------------------------- @Override - public PlexusResource getResource( String name ) - throws ResourceNotFoundException - { + public PlexusResource getResource(String name) throws ResourceNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if ( classLoader == null ) - { - throw new ResourceNotFoundException( name ); + if (classLoader == null) { + throw new ResourceNotFoundException(name); } - if ( name != null && name.startsWith( "/" ) ) - { - name = name.substring( 1 ); + if (name != null && name.startsWith("/")) { + name = name.substring(1); } - final URL url = classLoader.getResource( name ); - if ( url == null ) - { - throw new ResourceNotFoundException( name ); + final URL url = classLoader.getResource(name); + if (url == null) { + throw new ResourceNotFoundException(name); } - return new URLPlexusResource( url ); + return new URLPlexusResource(url); } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/URLPlexusResource.java b/src/main/java/org/codehaus/plexus/resource/loader/URLPlexusResource.java index 54fe171..c969e6f 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/URLPlexusResource.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/URLPlexusResource.java @@ -35,44 +35,35 @@ /** * Implementation of {@link PlexusResource} for URL's. */ -public class URLPlexusResource - implements PlexusResource -{ +public class URLPlexusResource implements PlexusResource { private final URL url; - public URLPlexusResource( URL url ) - { + public URLPlexusResource(URL url) { this.url = url; } @Override - public File getFile() - { + public File getFile() { return null; } @Override - public InputStream getInputStream() - throws IOException - { + public InputStream getInputStream() throws IOException { return url.openStream(); } @Override - public String getName() - { + public String getName() { return url.toExternalForm(); } @Override - public URI getURI() - { + public URI getURI() { return null; } @Override - public URL getURL() - { + public URL getURL() { return url; } } diff --git a/src/main/java/org/codehaus/plexus/resource/loader/URLResourceLoader.java b/src/main/java/org/codehaus/plexus/resource/loader/URLResourceLoader.java index 65d6085..9554706 100644 --- a/src/main/java/org/codehaus/plexus/resource/loader/URLResourceLoader.java +++ b/src/main/java/org/codehaus/plexus/resource/loader/URLResourceLoader.java @@ -24,9 +24,7 @@ * SOFTWARE. */ -import org.codehaus.plexus.resource.PlexusResource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import javax.inject.Named; import java.io.IOException; import java.io.InputStream; @@ -34,16 +32,17 @@ import java.net.URL; import java.util.HashMap; import java.util.Map; -import javax.inject.Named; + +import org.codehaus.plexus.resource.PlexusResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Jason van Zyl */ -@Named( URLResourceLoader.ID ) -public class URLResourceLoader - extends AbstractResourceLoader -{ - private static final Logger LOGGER = LoggerFactory.getLogger( URLResourceLoader.class ); +@Named(URLResourceLoader.ID) +public class URLResourceLoader extends AbstractResourceLoader { + private static final Logger LOGGER = LoggerFactory.getLogger(URLResourceLoader.class); public static final String ID = "url"; @@ -57,38 +56,28 @@ public class URLResourceLoader * @throws ResourceNotFoundException if resource not found. */ @Override - public PlexusResource getResource( String name ) - throws ResourceNotFoundException - { - if ( name == null || name.length() == 0 ) - { - throw new ResourceNotFoundException( "URLResourceLoader : No template name provided" ); + public PlexusResource getResource(String name) throws ResourceNotFoundException { + if (name == null || name.length() == 0) { + throw new ResourceNotFoundException("URLResourceLoader : No template name provided"); } - for ( String path : paths ) - { - try - { - URL u = new URL( path + name ); + for (String path : paths) { + try { + URL u = new URL(path + name); final InputStream inputStream = u.openStream(); - if ( inputStream != null ) - { - LOGGER.debug( "URLResourceLoader: Found '{}' at '{}'", name, path ); + if (inputStream != null) { + LOGGER.debug("URLResourceLoader: Found '{}' at '{}'", name, path); // save this root for later re-use - templateRoots.put( name, path ); + templateRoots.put(name, path); - return new URLPlexusResource( u ) - { + return new URLPlexusResource(u) { private boolean useSuper; - public synchronized InputStream getInputStream() - throws IOException - { - if ( !useSuper ) - { + public synchronized InputStream getInputStream() throws IOException { + if (!useSuper) { useSuper = true; return inputStream; } @@ -96,35 +85,25 @@ public synchronized InputStream getInputStream() } }; } - } - catch ( MalformedURLException mue ) - { - LOGGER.debug( "URLResourceLoader: No valid URL '{}{}'", path, name ); - } - catch ( IOException ioe ) - { - LOGGER.debug( "URLResourceLoader: Exception when looking for '{}' at '{}'", name, path, ioe ); + } catch (MalformedURLException mue) { + LOGGER.debug("URLResourceLoader: No valid URL '{}{}'", path, name); + } catch (IOException ioe) { + LOGGER.debug("URLResourceLoader: Exception when looking for '{}' at '{}'", name, path, ioe); } } // here we try to download without any path just the name which can be an url - try - { - URL u = new URL( name ); + try { + URL u = new URL(name); final InputStream inputStream = u.openStream(); - if ( inputStream != null ) - { - return new URLPlexusResource( u ) - { + if (inputStream != null) { + return new URLPlexusResource(u) { private boolean useSuper; - public synchronized InputStream getInputStream() - throws IOException - { - if ( !useSuper ) - { + public synchronized InputStream getInputStream() throws IOException { + if (!useSuper) { useSuper = true; return inputStream; } @@ -132,17 +111,13 @@ public synchronized InputStream getInputStream() } }; } - } - catch ( MalformedURLException mue ) - { - LOGGER.debug( "URLResourceLoader: No valid URL '{}'", name ); - } - catch ( IOException ioe ) - { - LOGGER.debug( "URLResourceLoader: Exception when looking for '{}'", name, ioe ); + } catch (MalformedURLException mue) { + LOGGER.debug("URLResourceLoader: No valid URL '{}'", name); + } catch (IOException ioe) { + LOGGER.debug("URLResourceLoader: Exception when looking for '{}'", name, ioe); } // convert to a general Velocity ResourceNotFoundException - throw new ResourceNotFoundException( name ); + throw new ResourceNotFoundException(name); } } diff --git a/src/test/java/org/codehaus/plexus/resource/ResourceManagerTest.java b/src/test/java/org/codehaus/plexus/resource/ResourceManagerTest.java index bda24bf..c9da412 100644 --- a/src/test/java/org/codehaus/plexus/resource/ResourceManagerTest.java +++ b/src/test/java/org/codehaus/plexus/resource/ResourceManagerTest.java @@ -24,101 +24,94 @@ * SOFTWARE. */ -import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import javax.inject.Inject; + +import java.io.File; +import java.io.InputStream; import org.codehaus.plexus.testing.PlexusTest; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; import org.junit.jupiter.api.Test; -import java.io.File; -import java.io.InputStream; -import javax.inject.Inject; +import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Trygve Laugstøl * @version $Id$ */ @PlexusTest -class ResourceManagerTest -{ +class ResourceManagerTest { @Inject private ResourceManager resourceManager; @Test - void testResourceManagerRetrievingInputStreams() - throws Exception - { + void testResourceManagerRetrievingInputStreams() throws Exception { InputStream in; - File absoluteFile = new File( getBasedir(), "src/test/file-resources/dir/file.txt" ).getAbsoluteFile(); - assertTrue( absoluteFile.isFile() ); - assertTrue( absoluteFile.isAbsolute() ); - in = resourceManager.getResourceAsInputStream( absoluteFile.getAbsolutePath() ); - assertEquals( "file.txt", IOUtil.toString( in, "UTF-8" ) ); + File absoluteFile = new File(getBasedir(), "src/test/file-resources/dir/file.txt").getAbsoluteFile(); + assertTrue(absoluteFile.isFile()); + assertTrue(absoluteFile.isAbsolute()); + in = resourceManager.getResourceAsInputStream(absoluteFile.getAbsolutePath()); + assertEquals("file.txt", IOUtil.toString(in, "UTF-8")); - in = resourceManager.getResourceAsInputStream( "/dir/file.txt" ); - assertEquals( "file.txt", IOUtil.toString( in, "UTF-8" ) ); + in = resourceManager.getResourceAsInputStream("/dir/file.txt"); + assertEquals("file.txt", IOUtil.toString(in, "UTF-8")); - in = resourceManager.getResourceAsInputStream( "dir/file.txt" ); - assertEquals( "file.txt", IOUtil.toString( in, "UTF-8" ) ); + in = resourceManager.getResourceAsInputStream("dir/file.txt"); + assertEquals("file.txt", IOUtil.toString(in, "UTF-8")); - in = resourceManager.getResourceAsInputStream( "/dir/classpath.txt" ); - assertEquals( "classpath.txt", IOUtil.toString( in, "UTF-8" ) ); + in = resourceManager.getResourceAsInputStream("/dir/classpath.txt"); + assertEquals("classpath.txt", IOUtil.toString(in, "UTF-8")); - in = resourceManager.getResourceAsInputStream( "dir/classpath.txt" ); - assertEquals( "classpath.txt", IOUtil.toString( in, "UTF-8" ) ); + in = resourceManager.getResourceAsInputStream("dir/classpath.txt"); + assertEquals("classpath.txt", IOUtil.toString(in, "UTF-8")); } @Test - void testResourceManagerRetrievingFiles() - throws Exception - { + void testResourceManagerRetrievingFiles() throws Exception { File f; - File absoluteFile = new File( getBasedir(), "src/test/file-resources/dir/file.txt" ).getAbsoluteFile(); - assertTrue( absoluteFile.isFile() ); - assertTrue( absoluteFile.isAbsolute() ); - f = resourceManager.getResourceAsFile( absoluteFile.getAbsolutePath() ); - assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) ); + File absoluteFile = new File(getBasedir(), "src/test/file-resources/dir/file.txt").getAbsoluteFile(); + assertTrue(absoluteFile.isFile()); + assertTrue(absoluteFile.isAbsolute()); + f = resourceManager.getResourceAsFile(absoluteFile.getAbsolutePath()); + assertEquals("file.txt", FileUtils.fileRead(f, "UTF-8")); - f = resourceManager.getResourceAsFile( "/dir/file.txt" ); - assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) ); + f = resourceManager.getResourceAsFile("/dir/file.txt"); + assertEquals("file.txt", FileUtils.fileRead(f, "UTF-8")); - f = resourceManager.getResourceAsFile( "dir/file.txt" ); - assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) ); + f = resourceManager.getResourceAsFile("dir/file.txt"); + assertEquals("file.txt", FileUtils.fileRead(f, "UTF-8")); - f = resourceManager.getResourceAsFile( "/dir/classpath.txt" ); - assertEquals( "classpath.txt", FileUtils.fileRead( f, "UTF-8" ) ); + f = resourceManager.getResourceAsFile("/dir/classpath.txt"); + assertEquals("classpath.txt", FileUtils.fileRead(f, "UTF-8")); - f = resourceManager.getResourceAsFile( "dir/classpath.txt" ); - assertEquals( "classpath.txt", FileUtils.fileRead( f, "UTF-8" ) ); + f = resourceManager.getResourceAsFile("dir/classpath.txt"); + assertEquals("classpath.txt", FileUtils.fileRead(f, "UTF-8")); } @Test - void testResourceManagerRetrievingFilesToSpecificLocation() - throws Exception - { - File outDir = new File( getBasedir(), "target/test/unit/output-directory" ); - - resourceManager.setOutputDirectory( outDir ); - - File ef = new File( outDir, "test/f.txt" ); - FileUtils.forceDelete( ef ); - assertFalse( ef.exists() ); - File f = resourceManager.getResourceAsFile( "dir/file.txt", "test/f.txt" ); - assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) ); - assertEquals( ef, f ); - - File ec = new File( outDir, "test/c.txt" ); - FileUtils.forceDelete( ec ); - assertFalse( ec.exists() ); - File c = resourceManager.getResourceAsFile( "dir/classpath.txt", "test/c.txt" ); - assertEquals( "classpath.txt", FileUtils.fileRead( c, "UTF-8" ) ); - assertEquals( ec, c ); + void testResourceManagerRetrievingFilesToSpecificLocation() throws Exception { + File outDir = new File(getBasedir(), "target/test/unit/output-directory"); + + resourceManager.setOutputDirectory(outDir); + + File ef = new File(outDir, "test/f.txt"); + FileUtils.forceDelete(ef); + assertFalse(ef.exists()); + File f = resourceManager.getResourceAsFile("dir/file.txt", "test/f.txt"); + assertEquals("file.txt", FileUtils.fileRead(f, "UTF-8")); + assertEquals(ef, f); + + File ec = new File(outDir, "test/c.txt"); + FileUtils.forceDelete(ec); + assertFalse(ec.exists()); + File c = resourceManager.getResourceAsFile("dir/classpath.txt", "test/c.txt"); + assertEquals("classpath.txt", FileUtils.fileRead(c, "UTF-8")); + assertEquals(ec, c); } - } diff --git a/src/test/java/org/codehaus/plexus/resource/loader/AbstractResourceLoaderTest.java b/src/test/java/org/codehaus/plexus/resource/loader/AbstractResourceLoaderTest.java index 375912f..3418797 100644 --- a/src/test/java/org/codehaus/plexus/resource/loader/AbstractResourceLoaderTest.java +++ b/src/test/java/org/codehaus/plexus/resource/loader/AbstractResourceLoaderTest.java @@ -24,51 +24,44 @@ * SOFTWARE. */ -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import javax.inject.Inject; + +import java.io.InputStream; import org.codehaus.plexus.util.IOUtil; -import java.io.InputStream; -import javax.inject.Inject; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Trygve Laugstøl * @version $Id$ */ -public abstract class AbstractResourceLoaderTest -{ +public abstract class AbstractResourceLoaderTest { @Inject protected ResourceLoader resourceLoader; - protected void assertResource( String name, String expectedContent ) - throws Exception - { + protected void assertResource(String name, String expectedContent) throws Exception { - InputStream is = resourceLoader.getResource( name ).getInputStream(); + InputStream is = resourceLoader.getResource(name).getInputStream(); - assertNotNull( is, "The returned input stream is null, name: '" + name + "'." ); + assertNotNull(is, "The returned input stream is null, name: '" + name + "'."); - String actualContent = IOUtil.toString( is, "UTF-8" ); + String actualContent = IOUtil.toString(is, "UTF-8"); - assertEquals( expectedContent, actualContent ); + assertEquals(expectedContent, actualContent); } - protected void assertMissingResource( String name ) - throws Exception - { - try - { - InputStream is = resourceLoader.getResource( name ).getInputStream(); + protected void assertMissingResource(String name) throws Exception { + try { + InputStream is = resourceLoader.getResource(name).getInputStream(); - String content = IOUtil.toString( is, "UTF-8" ); + String content = IOUtil.toString(is, "UTF-8"); - fail( "Expected ResourceNotFoundException while looking for a resource named '" + name + "'. Content:\n" - + content ); - } - catch ( ResourceNotFoundException e ) - { + fail("Expected ResourceNotFoundException while looking for a resource named '" + name + "'. Content:\n" + + content); + } catch (ResourceNotFoundException e) { // expected } } diff --git a/src/test/java/org/codehaus/plexus/resource/loader/FileResourceLoaderTest.java b/src/test/java/org/codehaus/plexus/resource/loader/FileResourceLoaderTest.java index f8daa81..d2bd160 100644 --- a/src/test/java/org/codehaus/plexus/resource/loader/FileResourceLoaderTest.java +++ b/src/test/java/org/codehaus/plexus/resource/loader/FileResourceLoaderTest.java @@ -24,55 +24,44 @@ * SOFTWARE. */ - -import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.File; import org.codehaus.plexus.resource.PlexusResource; import org.codehaus.plexus.testing.PlexusTest; import org.junit.jupiter.api.Test; -import java.io.File; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Trygve Laugstøl * @version $Id$ */ @PlexusTest -class FileResourceLoaderTest - extends AbstractResourceLoaderTest -{ +class FileResourceLoaderTest extends AbstractResourceLoaderTest { @Test - void testLookupWithAAbsolutePathName() - throws Exception - { - assertResource( "/dir/file.txt", "file.txt" ); + void testLookupWithAAbsolutePathName() throws Exception { + assertResource("/dir/file.txt", "file.txt"); } @Test - void testLookupWithARelativePath() - throws Exception - { - assertResource( "dir/file.txt", "file.txt" ); + void testLookupWithARelativePath() throws Exception { + assertResource("dir/file.txt", "file.txt"); } @Test - void testLookupWhenTheResourceIsMissing() - throws Exception - { - assertMissingResource( "/foo.txt" ); + void testLookupWhenTheResourceIsMissing() throws Exception { + assertMissingResource("/foo.txt"); - assertMissingResource( "foo.txt" ); + assertMissingResource("foo.txt"); } @Test - void testPlexusResource() - throws Exception - { - PlexusResource resource = resourceLoader.getResource( "/dir/file.txt" ); - final File f = new File( "src/test/file-resources", "/dir/file.txt" ); - assertEquals( f.getAbsolutePath(), resource.getFile().getPath() ); - assertEquals( f.toURI(), resource.getURI() ); - assertEquals( f.toURI().toURL(), resource.getURL() ); - assertEquals( f.getAbsolutePath(), resource.getName() ); + void testPlexusResource() throws Exception { + PlexusResource resource = resourceLoader.getResource("/dir/file.txt"); + final File f = new File("src/test/file-resources", "/dir/file.txt"); + assertEquals(f.getAbsolutePath(), resource.getFile().getPath()); + assertEquals(f.toURI(), resource.getURI()); + assertEquals(f.toURI().toURL(), resource.getURL()); + assertEquals(f.getAbsolutePath(), resource.getName()); } } diff --git a/src/test/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoaderTest.java b/src/test/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoaderTest.java index e3958d0..49745dc 100644 --- a/src/test/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoaderTest.java +++ b/src/test/java/org/codehaus/plexus/resource/loader/ThreadContextClasspathResourceLoaderTest.java @@ -24,72 +24,60 @@ * SOFTWARE. */ -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; +import java.net.URL; import org.codehaus.plexus.resource.PlexusResource; import org.codehaus.plexus.testing.PlexusTest; import org.junit.jupiter.api.Test; -import java.net.URL; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Trygve Laugstøl * @version $Id$ */ @PlexusTest -class ThreadContextClasspathResourceLoaderTest - extends AbstractResourceLoaderTest -{ +class ThreadContextClasspathResourceLoaderTest extends AbstractResourceLoaderTest { @Test - void testLookupWithAAbsolutePathName() - throws Exception - { - assertResource( "/dir/classpath.txt", "classpath.txt" ); + void testLookupWithAAbsolutePathName() throws Exception { + assertResource("/dir/classpath.txt", "classpath.txt"); } @Test - void testLookupWithARelativePath() - throws Exception - { - assertResource( "dir/classpath.txt", "classpath.txt" ); + void testLookupWithARelativePath() throws Exception { + assertResource("dir/classpath.txt", "classpath.txt"); } @Test - void testLookupWhenTheResourceIsMissing() - throws Exception - { - assertMissingResource( "/foo.txt" ); + void testLookupWhenTheResourceIsMissing() throws Exception { + assertMissingResource("/foo.txt"); - assertMissingResource( "foo.txt" ); + assertMissingResource("foo.txt"); } @Test - void testLookupWithANullThreadContextClassLoader() - throws Exception - { + void testLookupWithANullThreadContextClassLoader() throws Exception { ClassLoader loader = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader( null ); + Thread.currentThread().setContextClassLoader(null); - assertMissingResource( "/dir/classpath.txt" ); + assertMissingResource("/dir/classpath.txt"); - assertMissingResource( "dir/classpath.txt" ); + assertMissingResource("dir/classpath.txt"); - Thread.currentThread().setContextClassLoader( loader ); + Thread.currentThread().setContextClassLoader(loader); } @Test - void testPlexusResource() - throws Exception - { - PlexusResource resource = resourceLoader.getResource( "/dir/classpath.txt" ); - assertNull( resource.getFile() ); - assertNull( resource.getURI() ); - URL url = Thread.currentThread().getContextClassLoader().getResource( "dir/classpath.txt" ); - assertNotNull( url ); - assertEquals( url, resource.getURL() ); - assertEquals( url.toExternalForm(), resource.getName() ); + void testPlexusResource() throws Exception { + PlexusResource resource = resourceLoader.getResource("/dir/classpath.txt"); + assertNull(resource.getFile()); + assertNull(resource.getURI()); + URL url = Thread.currentThread().getContextClassLoader().getResource("dir/classpath.txt"); + assertNotNull(url); + assertEquals(url, resource.getURL()); + assertEquals(url.toExternalForm(), resource.getName()); } } diff --git a/src/test/java/org/codehaus/plexus/resource/loader/URLResourceLoaderTest.java b/src/test/java/org/codehaus/plexus/resource/loader/URLResourceLoaderTest.java index 906dd2b..fd81c4e 100644 --- a/src/test/java/org/codehaus/plexus/resource/loader/URLResourceLoaderTest.java +++ b/src/test/java/org/codehaus/plexus/resource/loader/URLResourceLoaderTest.java @@ -1,10 +1,5 @@ package org.codehaus.plexus.resource.loader; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -12,10 +7,13 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; -@ExtendWith( MockitoExtension.class ) -class URLResourceLoaderTest -{ +@ExtendWith(MockitoExtension.class) +class URLResourceLoaderTest { @Mock private Logger logger; @@ -23,19 +21,14 @@ class URLResourceLoaderTest private ResourceLoader resourceLoader = new URLResourceLoader(); @Test - void testMalformedURL() - { - try - { - resourceLoader.getResource( "LICENSE.txt" ); + void testMalformedURL() { + try { + resourceLoader.getResource("LICENSE.txt"); fail(); + } catch (ResourceNotFoundException e) { + verify(logger).debug("URLResourceLoader: No valid URL '{}'", "LICENSE.txt"); + verifyNoMoreInteractions(logger); + assertEquals("Could not find resource 'LICENSE.txt'.", e.getMessage()); } - catch ( ResourceNotFoundException e ) - { - verify( logger ).debug( "URLResourceLoader: No valid URL '{}'", "LICENSE.txt" ); - verifyNoMoreInteractions( logger ); - assertEquals( "Could not find resource 'LICENSE.txt'.", e.getMessage() ); - } - } } diff --git a/src/test/java/org/codehaus/plexus/resource/loader/manager/ResourceManagerTest.java b/src/test/java/org/codehaus/plexus/resource/loader/manager/ResourceManagerTest.java index 422ba26..fd73384 100644 --- a/src/test/java/org/codehaus/plexus/resource/loader/manager/ResourceManagerTest.java +++ b/src/test/java/org/codehaus/plexus/resource/loader/manager/ResourceManagerTest.java @@ -24,27 +24,25 @@ * SOFTWARE. */ -import static org.junit.jupiter.api.Assertions.assertNotNull; +import javax.inject.Inject; import org.codehaus.plexus.resource.ResourceManager; import org.codehaus.plexus.testing.PlexusTest; import org.junit.jupiter.api.Test; -import javax.inject.Inject; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * @author Trygve Laugstøl * @version $Id$ */ @PlexusTest -class ResourceManagerTest -{ +class ResourceManagerTest { @Inject private ResourceManager resourceManager; @Test - void testFoo() - { - assertNotNull( resourceManager ); + void testFoo() { + assertNotNull(resourceManager); } }