Permalink
Browse files

Text works on File(s) too

  • Loading branch information...
1 parent 8fbd96a commit c7017fbb6fc41c224de9d4fed874c9fd0c8c409e @rsimoni rsimoni committed Sep 7, 2012
@@ -11,6 +11,13 @@
<type>jar</type>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.4</version>
+ <scope>test</scope>
+ </dependency>
+
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
@@ -1,6 +1,9 @@
package tddmicroexercises.textconvertor;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
@@ -15,10 +18,14 @@ public Text(String text) {
this(new StringReader(text));
}
+ public Text(File file) throws FileNotFoundException {
+ this(new FileReader(file));
+ }
+
public Text(Reader reader) {
this.reader = reader;
}
-
+
public String toHtml() {
try {
StringBuilder builder = new StringBuilder();
@@ -2,6 +2,11 @@
import static org.junit.Assert.*;
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
public class TextTest {
@@ -15,4 +20,17 @@
assertEquals("hello<br/>mum&amp;dad<br/>less&lt;than", new Text("hello\nmum&dad\nless<than").toHtml());
}
+ @Test public void worksOnFilesToo() throws IOException {
+ File file = aNewFileContaining("hello\nmum&dad\nless<than");
+
+ assertEquals("hello<br/>mum&amp;dad<br/>less&lt;than", new Text(file).toHtml());
+ }
+
+ private File aNewFileContaining(String content) throws IOException {
+ File file = new File(SystemUtils.JAVA_IO_TMPDIR, "textconverter-data.txt");
+ if (file.exists()) file.delete();
+ FileUtils.writeStringToFile(file, content);
+ return file;
+ }
+
}

0 comments on commit c7017fb

Please sign in to comment.