Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lucaminudel#19 from saleem/master
@saleem: unit tests for the text converter solution
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...iddiqui/src/main/java/tddmicroexercises/textconverter/UnicodeFileToHtmlTextConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tddmicroexercises.textconverter; | ||
|
||
import org.apache.commons.lang3.StringEscapeUtils; | ||
|
||
import java.io.BufferedReader; | ||
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{ | ||
|
||
BufferedReader reader = new BufferedReader(new FileReader(fullFilenameWithPath)); | ||
|
||
String line = reader.readLine(); | ||
String html = ""; | ||
while (line != null) | ||
{ | ||
html += StringEscapeUtils.escapeHtml4(line); | ||
html += "<br />"; | ||
line = reader.readLine(); | ||
} | ||
return html; | ||
|
||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...qui/src/test/java/tddmicroexercises/textconverter/UnicodeFileToHtmlTextConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package tddmicroexercises.textconverter; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.BufferedWriter; | ||
import org.junit.Before; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.apache.commons.lang3.StringEscapeUtils; | ||
import static org.junit.Assert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.powermock.api.mockito.PowerMockito.*; | ||
import static org.mockito.Mockito.anyString; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({UnicodeFileToHtmlTextConverter.class, StringEscapeUtils.class}) | ||
public class UnicodeFileToHtmlTextConverterTest { | ||
private static final String FILE_NAME = "temp"; | ||
private static final String FILE_EXT = "test"; | ||
private File tempInputFile; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
tempInputFile = File.createTempFile(FILE_NAME, FILE_EXT); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
tempInputFile.delete(); | ||
} | ||
|
||
private void writeToTempInputFile(String contents) throws Exception { | ||
BufferedWriter out = new BufferedWriter(new FileWriter(tempInputFile)); | ||
out.write(contents); | ||
out.close(); | ||
} | ||
|
||
@Test | ||
public void convertAppendsLineBreakTagToEndOfSimpleOneLine() throws Exception { | ||
writeToTempInputFile("Hello World"); | ||
UnicodeFileToHtmlTextConverter converter = new UnicodeFileToHtmlTextConverter(tempInputFile.getAbsolutePath()); | ||
assertThat(converter.convertToHtml(), is("Hello World<br />")); | ||
} | ||
|
||
@Test | ||
public void converterReturnsEmptyStringForEmptyFile() throws Exception { | ||
UnicodeFileToHtmlTextConverter converter = new UnicodeFileToHtmlTextConverter(tempInputFile.getAbsolutePath()); | ||
assertThat(converter.convertToHtml(), is("")); | ||
} | ||
|
||
@Test | ||
public void converterShouldCallStaticMethodInLibraryForEachLineInInputFile() throws Exception { | ||
// Arrange | ||
mockStatic(StringEscapeUtils.class); | ||
when(StringEscapeUtils.escapeHtml4(anyString())).thenReturn("return value does not matter"); | ||
writeToTempInputFile("Hello World"); | ||
UnicodeFileToHtmlTextConverter converter = new UnicodeFileToHtmlTextConverter(tempInputFile.getAbsolutePath()); | ||
// Act | ||
converter.convertToHtml(); | ||
// Assert | ||
verifyStatic(); | ||
StringEscapeUtils.escapeHtml4("Hello World"); | ||
} | ||
} |