package org.opennms.test;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;

import junit.framework.Assert;

public class ConfigurationTestUtils extends Assert {
    public static Reader getReaderForResource(Object obj, String resource) {
        return new InputStreamReader(getInputStreamForResource(obj, resource));
    }

    public static InputStream getInputStreamForResource(Object obj,
            String resource) {
        InputStream is = obj.getClass().getResourceAsStream(resource);
        assertNotNull("could not get file resource '" + resource + "'", is);
        return is;
    }
    
    public static Reader getReaderForResourceWithReplacements(Object obj,
            String resource, String[][] replacements) throws IOException {
        String newConfig = getConfigForResourceWithReplacements(obj, resource,
                                                                replacements);
        return new StringReader(newConfig);
    }
    
    
    public static InputStream getInputStreamForResourceWithReplacements(Object obj,
            String resource, String[][] replacements) throws IOException {
        String newConfig = getConfigForResourceWithReplacements(obj, resource,
                                                                replacements);
        return new ByteArrayInputStream(newConfig.getBytes());
    }
    
    
    public static String getConfigForResourceWithReplacements(Object obj,
            String resource, String[][] replacements) throws IOException {

        Reader inputReader = getReaderForResource(obj, resource);
        BufferedReader bufferedReader = new BufferedReader(inputReader);
        
        StringBuffer buffer = new StringBuffer();

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            buffer.append(line);
            buffer.append("\n");
        }
    
        String newConfig = buffer.toString();
        for (String[] replacement : replacements) {
            newConfig = newConfig.replaceAll(replacement[0], replacement[1]);
        }
    
        return newConfig;
    }

}

@@ -80,32 +80,123 @@
 rayInputStream;%0A
+import java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0A
 import java.io.I
@@ -2347,10 +2347,580 @@
 %0A    %7D%0A%0A
+    public static Reader getReaderForConfigFile(String configFile) %7B%0A        return new InputStreamReader(getInputStreamForResource(configFile, configFile));%0A    %7D%0A%0A    public static InputStream getInputStreamForConfigFile(String configFile) throws FileNotFoundException %7B%0A        String path = %22../opennms-daemon/src/main/filtered/etc/%22 + configFile;%0A        File file = new File(path);%0A        assertTrue(%22configuration file '%22 + configFile + %22' does not exist at %22 + path, file.exists());%0A        InputStream is = new FileInputStream(file);%0A        return is;%0A    %7D%0A%0A
 %7D%0A

Add methods to get an InputStream or a Reader for a configuration file from the source tree.