Skip to content

Commit

Permalink
Merge pull request lucaminudel#19 from saleem/master
Browse files Browse the repository at this point in the history
@saleem: unit tests for the text converter solution
  • Loading branch information
lucaminudel committed Oct 21, 2013
2 parents f130238 + 29c03ce commit 39e2941
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
@@ -1,13 +1,12 @@
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'

sourceCompatibility = 1.7
version = '1.0'

jar {
manifest {
attributes 'Implementation-Title': 'TDD Micro Exercises - Java', 'Implementation-Version': version
attributes 'Implementation-Title': 'TDD Micro Exercises - Java', 'Implementation-Version': version
}
}

Expand All @@ -16,8 +15,14 @@ repositories {
}

dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version : '3.1'

// hamcrest has to be before junit: http://stackoverflow.com/questions/7869711/getting-nosuchmethoderror-org-hamcrest-matcher-describemismatch-when-running
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.5.1'
testCompile('org.powermock:powermock-module-junit4:1.5.1') {
exclude module: 'junit'
}
}
@@ -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;

}
}
@@ -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");
}
}

0 comments on commit 39e2941

Please sign in to comment.