/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.modules;

import org.jboss.modules.util.Util;
import org.junit.BeforeClass;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import static org.junit.Assert.assertNotNull;

/**
 * Abstract Test Case used as a base for all module tests.
 *
 * @author John Bailey
 */
public class AbstractModuleTestCase {
    protected static final ModuleIdentifier MODULE_ID = new ModuleIdentifier("test", "test", "1.0");

    @BeforeClass
    public static void initUrlHandler() {
        // Hack just to kick off Module's static init
        assertNotNull(Module.SYSTEM);
    }

    protected File getResource(final String path) throws Exception {
        return Util.getResourceFile(getClass(), path);
    }

    protected void copyResource(final String inputResource, final String outputBase, final String outputPath) throws Exception {
        final File resource = getResource(inputResource);
        final File outputDirectory = new File(getResource(outputBase), outputPath);

        if(!resource.exists())
            throw new IllegalArgumentException("Resource does not exist");
        if (outputDirectory.exists() && outputDirectory.isFile())
            throw new IllegalArgumentException("OutputDirectory must be a directory");
        if (!outputDirectory.exists()) {
            if (!outputDirectory.mkdirs())
                throw new RuntimeException("Failed to create output directory");
        }
        final File outputFile = new File(outputDirectory, resource.getName());
        FileReader in = null;
        FileWriter out = null;
        try {
            in = new FileReader(resource);
            out = new FileWriter(outputFile);
            int c;
            while ((c = in.read()) != -1)
                out.write(c);
        } finally {
            if(in != null) in.close();
            if(out != null) out.close();
        }
    }
}

@@ -1050,24 +1050,200 @@
 s.modules;%0A%0A
+import java.io.Closeable;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0A
 import org.j
@@ -1321,62 +1321,8 @@
 ile;
-%0Aimport java.io.FileReader;%0Aimport java.io.FileWriter;
 %0A%0Aim
@@ -2685,59 +2685,60 @@
     
-FileReader in = null;%0A        FileWriter out = null
+final InputStream in = new FileInputStream(resource)
 ;%0A  
@@ -2765,85 +2765,138 @@
     
+f
 in
- = new FileReader(resource);%0A            out = new FileWriter(outputFile);%0A
+al OutputStream out = new FileOutputStream(outputFile);%0A            try %7B%0A                final byte%5B%5D b = new byte%5B8192%5D;%0A    
     
@@ -2922,16 +2922,20 @@
         
+  
+  
 while ((
@@ -2946,16 +2946,17 @@
 in.read(
+b
 )) != -1
@@ -2956,17 +2956,22 @@
 ) != -1)
-%0A
+ %7B%0A   
         
@@ -2978,16 +2978,17 @@
         
+ 
 out.writ
@@ -2993,12 +2993,97 @@
 ite(
-c);%0A
+b, 0, c);%0A                %7D%0A                out.close();%0A                in.close();%0A    
     
@@ -3114,32 +3114,86 @@
     
+   
-if(in != null) in.c
+ safeClose(out);%0A            %7D%0A        %7D finally %7B%0A            safeC
 lose(
+in
 );%0A 
@@ -3203,40 +3203,200 @@
     
+%7D%0A
     
-if(out != null) out.close();
+%7D%0A%0A    private static void safeClose(final Closeable closeable) %7B%0A        if (closeable != null) try %7B%0A            closeable.close();%0A        %7D catch (IOException e) %7B%0A            // meh
 %0A   

Clean up a bunch of test failures