Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added my personal Java solution for TextConverter

  • Loading branch information...
commit 8fbd96a3bdef9e32182785980bbd1fcf96829a42 1 parent 5c89534
Roberto Simoni rsimoni authored
6 TDDMicroExercises.YoursSolutions/Roberto-Simoni/Java/.gitignore
View
@@ -0,0 +1,6 @@
+target/
+bin/
+.settings/
+.project
+.classpath
+.DS_Store
20 TDDMicroExercises.YoursSolutions/Roberto-Simoni/Java/TextConverter/pom.xml
View
@@ -0,0 +1,20 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>TextConverter</groupId>
+ <artifactId>TextConverter </artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.9</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.2</version>
+ </dependency>
+ </dependencies>
+</project>
38 ...YoursSolutions/Roberto-Simoni/Java/TextConverter/src/main/java/tddmicroexercises/textconvertor/Text.java
View
@@ -0,0 +1,38 @@
+package tddmicroexercises.textconvertor;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.commons.lang.StringEscapeUtils;
+
+public class Text {
+
+ private final Reader reader;
+
+ public Text(String text) {
+ this(new StringReader(text));
+ }
+
+ public Text(Reader reader) {
+ this.reader = reader;
+ }
+
+ public String toHtml() {
+ try {
+ StringBuilder builder = new StringBuilder();
+ BufferedReader br = new BufferedReader(reader);
+ String line = null;
+ while ((line = br.readLine()) != null) {
+ if (builder.length() > 0)
+ builder.append("<br/>");
+ builder.append(StringEscapeUtils.escapeHtml(line));
+ }
+ return builder.toString();
+ } catch (IOException e) {
+ throw new RuntimeException("Unable to convert text to HTML due to a " + e.getClass().getName() + " with message '" + e.getMessage() + "'", e);
+ }
+ }
+
+}
19 ...oni/Java/TextConverter/src/main/java/tddmicroexercises/textconvertor/UnicodeFileToHtmlTextConverter.java
View
@@ -0,0 +1,19 @@
+package tddmicroexercises.textconvertor;
+
+import java.io.FileReader;
+import java.io.IOException;
+
+public class UnicodeFileToHtmlTextConverter {
+
+ private String fullFilenameWithPath;
+
+ public UnicodeFileToHtmlTextConverter(String fullFilenameWithPath) {
+ this.fullFilenameWithPath = fullFilenameWithPath;
+ }
+
+ public String convertToHtml() throws IOException {
+ Text text = new Text(new FileReader(fullFilenameWithPath));
+ return text.toHtml();
+ }
+
+}
18 ...sSolutions/Roberto-Simoni/Java/TextConverter/src/test/java/tddmicroexercises/textconvertor/TextTest.java
View
@@ -0,0 +1,18 @@
+package tddmicroexercises.textconvertor;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TextTest {
+
+ @Test public void toHtml_returnsEscapedEntities() {
+ assertEquals("mum&amp;dad", new Text("mum&dad").toHtml());
+ assertEquals("less&lt;than", new Text("less<than").toHtml());
+ }
+
+ @Test public void toHtml_useHtmlBreakInPlaceOfNewLines() {
+ assertEquals("hello<br/>mum&amp;dad<br/>less&lt;than", new Text("hello\nmum&dad\nless<than").toHtml());
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.