Skip to content
Snippets Groups Projects
Commit 1b2fc645 authored by felmer's avatar felmer
Browse files

- FileUtilitiesTest: add testLoadText()

- FileUtilities: add loadStringResource()

SVN: 234
parent 7fb30e7b
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,8 @@ import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
......@@ -60,14 +62,7 @@ public final class FileUtilities
try
{
fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder builder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
{
builder.append(line).append('\n');
}
return new String(builder);
return readString(new BufferedReader(fileReader));
} catch (IOException ex)
{
throw new EnvironmentFailureException("Error when loading file " + file, ex);
......@@ -86,6 +81,51 @@ public final class FileUtilities
}
}
/**
* Loads a resource as a string.
*
* @param clazz Class for which <code>getResourceAsStream()</code> will be invoked.
* @param resource Absolute path of the resource (will be the argument of <code>getResourceAsStream()</code>).
* @return <code>null</code> if the specified resource does not exist.
* @throws EnvironmentFailureException if an <code>IOException</code> occurs during reading the resource.
*/
public static String loadStringResource(Class clazz, String resource)
{
InputStream stream = clazz.getResourceAsStream(resource);
if (stream == null)
{
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
try
{
return readString(reader);
} catch (IOException ex)
{
throw new EnvironmentFailureException("Couldn't read resource " + resource, ex);
} finally
{
try
{
reader.close();
} catch (IOException ex)
{
throw new EnvironmentFailureException("Couldn't close reader for resource " + resource, ex);
}
}
}
private static String readString(BufferedReader reader) throws IOException
{
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line).append('\n');
}
return new String(builder);
}
/**
* Checks whether a <var>directory</var> of some <var>kind</var> is fully accessible to the program.
*
......
......@@ -16,7 +16,10 @@
package ch.systemsx.cisd.common.utilities;
import static org.testng.AssertJUnit.assertEquals;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.testng.annotations.BeforeSuite;
......@@ -32,7 +35,6 @@ import ch.systemsx.cisd.common.logging.LogInitializer;
*/
public class FileUtilitiesTest
{
private static final File workingDirectory = new File("targets" + File.separator + "unit-test-wd");
@BeforeSuite
......@@ -88,4 +90,22 @@ public class FileUtilitiesTest
FileUtilities.deleteRecursively(root);
}
@Test
public void testLoadText() throws Exception
{
File file = new File(workingDirectory, "test.txt");
FileWriter writer = new FileWriter(file);
try
{
writer.write("Hello\nWorld!");
} finally
{
writer.close();
}
String text = FileUtilities.loadText(file);
assert file.delete();
assertEquals("Hello\nWorld!\n", text);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment