Inject FileSystem 

git-svn-id: https://svn.apache.org/repos/asf/creadur/tentacles/trunk@1462789 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/creadur/tentacles/Deauthorize.java b/src/main/java/org/apache/creadur/tentacles/Deauthorize.java
index a02334b..224d3a2 100644
--- a/src/main/java/org/apache/creadur/tentacles/Deauthorize.java
+++ b/src/main/java/org/apache/creadur/tentacles/Deauthorize.java
@@ -29,65 +29,80 @@
 
 /**
  * Little utility that will yank the author comments from java files.
- *
+ * 
  * If the resulting comment block is effectively empty, it will be yanked too.
  */
 public class Deauthorize {
 
     /**
      * All input must be valid directories.
-     *
+     * 
      * Invalid input is logged to System.err and skipped
-     *
-     * @param args a list of directories to scan and fix
+     * 
+     * @param args
+     *            a list of directories to scan and fix
      * @throws Exception
      */
-    public static void main(String[] args) throws Exception {
+    public static void main(final String[] args) throws Exception {
 
-        if (args.length == 0) throw new IllegalArgumentException("At least one directory must be specified");
+        if (args.length == 0) {
+            throw new IllegalArgumentException(
+                    "At least one directory must be specified");
+        }
 
         final List<File> dirs = new ArrayList<File>();
 
         // Check the input args upfront
-        for (String arg : args) {
+        for (final String arg : args) {
             final File dir = new File(arg);
 
-            if (not(dir.exists(), "Does not exist: %s", arg)) continue;
-            if (not(dir.isDirectory(), "Not a directory: %s", arg)) continue;
+            if (not(dir.exists(), "Does not exist: %s", arg)) {
+                continue;
+            }
+            if (not(dir.isDirectory(), "Not a directory: %s", arg)) {
+                continue;
+            }
 
             dirs.add(dir);
         }
 
         // Exit if we got bad input
-        if (dirs.size() != args.length) System.exit(1);
+        if (dirs.size() != args.length) {
+            System.exit(1);
+        }
 
         // Go!
-        for (File dir : dirs) {
+        for (final File dir : dirs) {
             deauthorize(dir);
         }
     }
 
     /**
      * Iterate over all the java files in the given directory
-     *
-     * Read in the file so we can guess the line ending -- if we didn't need to do that we could just stream.
-     * Run the content through Swizzle Stream and filter out any author tags as well as any comment blocks
-     * that wind up (or already were) empty as a result.
-     *
+     * 
+     * Read in the file so we can guess the line ending -- if we didn't need to
+     * do that we could just stream. Run the content through Swizzle Stream and
+     * filter out any author tags as well as any comment blocks that wind up (or
+     * already were) empty as a result.
+     * 
      * If that had any effect on the contents of the file, write it back out.
-     *
+     * 
      * Should skip any files that are not readable or writable.
-     *
-     * Will log an error on System.err for any files that were updated and were not writable.  Files that are
-     * not writable and don't need updating are simply ignored.
-     *
+     * 
+     * Will log an error on System.err for any files that were updated and were
+     * not writable. Files that are not writable and don't need updating are
+     * simply ignored.
+     * 
      * @param dir
      * @throws IOException
      */
-    private static void deauthorize(File dir) throws IOException {
-        for (File file : FileSystem.collect(dir, ".*\\.java")) {
+    private static void deauthorize(final File dir) throws IOException {
+        for (final File file : new FileSystem().collect(dir, ".*\\.java")) {
 
-            if (not(file.canRead(), "File not readable: %s", file.getAbsolutePath())) continue;
+            if (not(file.canRead(), "File not readable: %s",
+                    file.getAbsolutePath())) {
+                continue;
+            }
 
             final String text = IO.slurp(file);
 
@@ -102,30 +117,42 @@
             in = new ExcludeFilterInputStream(in, " * @author", eol);
 
             // Clean "empty" comments
-            in = new DelimitedTokenReplacementInputStream(in, startComment, endComment, new StringTokenHandler() {
-                @Override
-                public String handleToken(String commentBlock) throws IOException {
+            in =
+                    new DelimitedTokenReplacementInputStream(in, startComment,
+                            endComment, new StringTokenHandler() {
+                                @Override
+                                public String handleToken(
+                                        final String commentBlock)
+                                        throws IOException {
 
-                    // Yank if empty
-                    if (commentBlock.replaceAll("[\\s*]", "").length() == 0) return eol;
+                                    // Yank if empty
+                                    if (commentBlock.replaceAll("[\\s*]", "")
+                                            .length() == 0) {
+                                        return eol;
+                                    }
 
-                    // Keep otherwise
-                    return startComment + commentBlock + endComment;
-                }
-            });
+                                    // Keep otherwise
+                                    return startComment + commentBlock
+                                            + endComment;
+                                }
+                            });
 
             final byte[] content = IO.read(in);
 
             if (content.length != file.length()) {
 
-                if (not(file.canWrite(), "File not writable: %s", file.getAbsolutePath())) continue;
+                if (not(file.canWrite(), "File not writable: %s",
+                        file.getAbsolutePath())) {
+                    continue;
+                }
 
                 IO.copy(content, file);
             }
         }
     }
 
-    private static boolean not(boolean b, String message, Object... details) {
+    private static boolean not(boolean b, final String message,
+            final Object... details) {
         b = !b;
         if (b) {
             System.err.printf(message, details);
diff --git a/src/main/java/org/apache/creadur/tentacles/FileSystem.java b/src/main/java/org/apache/creadur/tentacles/FileSystem.java
index f15ac37..5c9df55 100644
--- a/src/main/java/org/apache/creadur/tentacles/FileSystem.java
+++ b/src/main/java/org/apache/creadur/tentacles/FileSystem.java
@@ -27,11 +27,11 @@
  */
 public class FileSystem {
 
-    public static List<File> collect(final File dir, final String regex) {
+    public List<File> collect(final File dir, final String regex) {
         return collect(dir, Pattern.compile(regex));
     }
 
-    private static List<File> collect(final File dir, final Pattern pattern) {
+    private List<File> collect(final File dir, final Pattern pattern) {
         return collect(dir, new FileFilter() {
             @Override
             public boolean accept(final File file) {
@@ -40,7 +40,7 @@
         });
     }
 
-    public static List<File> collect(final File dir, final FileFilter filter) {
+    public List<File> collect(final File dir, final FileFilter filter) {
         final List<File> accepted = new ArrayList<File>();
         if (filter.accept(dir)) {
             accepted.add(dir);
@@ -56,11 +56,11 @@
         return accepted;
     }
 
-    public static void mkparent(final File file) {
+    public void mkparent(final File file) {
         mkdirs(file.getParentFile());
     }
 
-    public static void mkdirs(final File file) {
+    public void mkdirs(final File file) {
 
         if (!file.exists()) {
 
diff --git a/src/main/java/org/apache/creadur/tentacles/Main.java b/src/main/java/org/apache/creadur/tentacles/Main.java
index d9e8a90..72ae690 100644
--- a/src/main/java/org/apache/creadur/tentacles/Main.java
+++ b/src/main/java/org/apache/creadur/tentacles/Main.java
@@ -60,27 +60,36 @@
     private final File content;
     private final Reports reports;
     private final Map<String, String> licenses = new HashMap<String, String>();
-    private final NexusClient client = new NexusClient();
+    private final NexusClient client;
 
     private final Configuration configuration;
+    private final FileSystem fileSystem;
 
     public Main(final String... args) throws Exception {
-        this(new Configuration(args));
+        this(new Configuration(args), new FileSystem());
     }
 
-    public Main(final Configuration configuration) throws Exception {
+    public Main(final Configuration configuration, final FileSystem fileSystem)
+            throws Exception {
+        this(configuration, fileSystem, new NexusClient(fileSystem));
+    }
+
+    public Main(final Configuration configuration, final FileSystem fileSystem,
+            final NexusClient client) throws Exception {
+        this.client = client;
         this.configuration = configuration;
+        this.fileSystem = fileSystem;
 
         this.local =
                 new File(this.configuration.getRootDirectoryForLocalOutput());
 
-        FileSystem.mkdirs(this.local);
+        this.fileSystem.mkdirs(this.local);
 
         this.repository = new File(this.local, "repo");
         this.content = new File(this.local, "content");
 
-        FileSystem.mkdirs(this.repository);
-        FileSystem.mkdirs(this.content);
+        this.fileSystem.mkdirs(this.repository);
+        this.fileSystem.mkdirs(this.content);
 
         log.info("Remote repository: "
                 + this.configuration.getStagingRepositoryURI());
@@ -113,7 +122,7 @@
         prepare();
 
         final List<File> jars =
-                FileSystem.collect(this.repository, new FileFilter() {
+                this.fileSystem.collect(this.repository, new FileFilter() {
                     @Override
                     public boolean accept(final File pathname) {
                         return pathname.isFile();
@@ -151,7 +160,7 @@
 
         for (final Archive archive : archives) {
             final List<File> files =
-                    FileSystem.collect(contents(archive.getFile()),
+                    this.fileSystem.collect(contents(archive.getFile()),
                             new LicenseFilter());
             for (final File file : files) {
                 final License license = new License(IO.slurp(file));
@@ -201,7 +210,7 @@
 
         final File contents = contents(archive.getFile());
         final List<File> files =
-                FileSystem.collect(contents, new Filters(
+                this.fileSystem.collect(contents, new Filters(
                         new DeclaredFilter(contents), new LicenseFilter()));
 
         for (final File file : files) {
@@ -239,8 +248,8 @@
 
             final File contents = contents(archive.getFile());
             final List<File> files =
-                    FileSystem.collect(contents, new Filters(new DeclaredFilter(
-                            contents), new NoticeFilter()));
+                    this.fileSystem.collect(contents, new Filters(
+                            new DeclaredFilter(contents), new NoticeFilter()));
 
             for (final File file : files) {
 
@@ -276,7 +285,7 @@
 
         for (final Archive archive : archives) {
             final List<File> files =
-                    FileSystem.collect(contents(archive.getFile()),
+                    this.fileSystem.collect(contents(archive.getFile()),
                             new NoticeFilter());
             for (final File file : files) {
                 final Notice notice = new Notice(IO.slurp(file));
@@ -310,7 +319,8 @@
     }
 
     private List<URI> allNoticeFiles() {
-        final List<File> legal = FileSystem.collect(this.content, new LegalFilter());
+        final List<File> legal =
+                this.fileSystem.collect(this.content, new LegalFilter());
         for (final File file : legal) {
             log.info("Legal " + file);
         }
@@ -344,15 +354,16 @@
                 .startsWith("file:")) {
             final File file =
                     new File(this.configuration.getStagingRepositoryURI());
-            final List<File> collect = FileSystem.collect(file, new FileFilter() {
-                @Override
-                public boolean accept(final File pathname) {
-                    final String path = pathname.getAbsolutePath();
-                    return path.matches(Main.this.configuration
-                            .getFileRepositoryPathNameFilter())
-                            && isValidArchive(path);
-                }
-            });
+            final List<File> collect =
+                    this.fileSystem.collect(file, new FileFilter() {
+                        @Override
+                        public boolean accept(final File pathname) {
+                            final String path = pathname.getAbsolutePath();
+                            return path.matches(Main.this.configuration
+                                    .getFileRepositoryPathNameFilter())
+                                    && isValidArchive(path);
+                        }
+                    });
 
             for (final File f : collect) {
                 files.add(copy(f));
@@ -385,7 +396,7 @@
 
                     final File fileEntry = new File(contents, path);
 
-                    FileSystem.mkparent(fileEntry);
+                    this.fileSystem.mkparent(fileEntry);
 
                     // Open the output file
 
@@ -556,7 +567,7 @@
         }
 
         final File contents = new File(this.content, path + ".contents");
-        FileSystem.mkdirs(contents);
+        this.fileSystem.mkdirs(contents);
         return contents;
     }
 
@@ -574,7 +585,7 @@
 
         log.info("Copy " + uri);
 
-        FileSystem.mkparent(file);
+        this.fileSystem.mkparent(file);
 
         IO.copy(IO.read(src), file);
 
@@ -751,9 +762,9 @@
         private Map<URI, URI> mapOther() {
             final File jarContents = contents(this.file);
             final List<File> legal =
-                    FileSystem.collect(jarContents,
-                            new Filters(new N(new DeclaredFilter(jarContents)),
-                                    new LegalFilter()));
+                    Main.this.fileSystem.collect(jarContents, new Filters(
+                            new N(new DeclaredFilter(jarContents)),
+                            new LegalFilter()));
 
             final Map<URI, URI> map = new LinkedHashMap<URI, URI>();
             for (final File file : legal) {
@@ -769,8 +780,10 @@
         private Map<URI, URI> map() {
             final File jarContents = contents(this.file);
             final List<File> legal =
-                    FileSystem.collect(jarContents, new Filters(new DeclaredFilter(
-                            jarContents), new LegalFilter()));
+                    Main.this.fileSystem
+                            .collect(jarContents, new Filters(
+                                    new DeclaredFilter(jarContents),
+                                    new LegalFilter()));
 
             final Map<URI, URI> map = new LinkedHashMap<URI, URI>();
             for (final File file : legal) {
diff --git a/src/main/java/org/apache/creadur/tentacles/NexusClient.java b/src/main/java/org/apache/creadur/tentacles/NexusClient.java
index a839f79..59bbbd5 100644
--- a/src/main/java/org/apache/creadur/tentacles/NexusClient.java
+++ b/src/main/java/org/apache/creadur/tentacles/NexusClient.java
@@ -32,18 +32,21 @@
 
 public class NexusClient {
 
-    private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(NexusClient.class);
+    private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
+            .getLogger(NexusClient.class);
 
     private final DefaultHttpClient client;
+    private final FileSystem fileSystem;
 
-    public NexusClient() {
+    public NexusClient(final FileSystem fileSystem) {
         this.client = new DefaultHttpClient();
+        this.fileSystem = fileSystem;
     }
 
-    public File download(URI uri, File file) throws IOException {
+    public File download(final URI uri, final File file) throws IOException {
         if (file.exists()) {
 
-            long length = getConentLength(uri);
+            final long length = getConentLength(uri);
 
             if (file.length() == length) {
                 log.info("Exists " + uri);
@@ -59,37 +62,41 @@
 
         final InputStream content = response.getEntity().getContent();
 
-        FileSystem.mkparent(file);
+        this.fileSystem.mkparent(file);
 
         IO.copy(content, file);
 
         return file;
     }
 
-    private long getConentLength(URI uri) throws IOException {
-        HttpResponse head = head(uri);
-        Header[] headers = head.getHeaders("Content-Length");
+    private long getConentLength(final URI uri) throws IOException {
+        final HttpResponse head = head(uri);
+        final Header[] headers = head.getHeaders("Content-Length");
 
-        for (Header header : headers) {
+        for (final Header header : headers) {
             return new Long(header.getValue());
         }
 
         return -1;
     }
 
-    private HttpResponse get(URI uri) throws IOException {
+    private HttpResponse get(final URI uri) throws IOException {
         final HttpGet request = new HttpGet(uri);
-        request.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
-        return client.execute(request);
+        request.setHeader(
+                "User-Agent",
+                "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
+        return this.client.execute(request);
     }
 
-    private HttpResponse head(URI uri) throws IOException {
+    private HttpResponse head(final URI uri) throws IOException {
         final HttpHead request = new HttpHead(uri);
-        request.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
-        return client.execute(request);
+        request.setHeader(
+                "User-Agent",
+                "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
+        return this.client.execute(request);
     }
 
-    public Set<URI> crawl(URI index) throws IOException {
+    public Set<URI> crawl(final URI index) throws IOException {
         log.info("Crawl " + index);
         final Set<URI> resources = new LinkedHashSet<URI>();
 
@@ -100,7 +107,8 @@
 
         final Set<URI> crawl = new LinkedHashSet<URI>();
 
-        //<a href="https://repository.apache.org/content/repositories/orgapacheopenejb-094/archetype-catalog.xml">archetype-catalog.xml</a>
+        // <a
+        // href="https://repository.apache.org/content/repositories/orgapacheopenejb-094/archetype-catalog.xml">archetype-catalog.xml</a>
         while (lexer.readAndMark("<a ", "/a>")) {
 
             try {
@@ -109,8 +117,12 @@
 
                 final URI uri = index.resolve(link);
 
-                if (name.equals("../")) continue;
-                if (link.equals("../")) continue;
+                if (name.equals("../")) {
+                    continue;
+                }
+                if (link.equals("../")) {
+                    continue;
+                }
 
                 if (name.endsWith("/")) {
                     crawl.add(uri);
@@ -126,7 +138,7 @@
 
         content.close();
 
-        for (URI uri : crawl) {
+        for (final URI uri : crawl) {
             resources.addAll(crawl(uri));
         }