Skip to content

Commit

Permalink
Made table formatting more robust by providing support for unicode br…
Browse files Browse the repository at this point in the history
…ackets and new line chars.
  • Loading branch information
wakaleo committed Feb 13, 2015
1 parent 4b66966 commit 56f672a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 21 deletions.
12 changes: 9 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ subprojects {
exclude '**/*Sample*'
minHeapSize = "128m"
maxHeapSize = "512m"
jvmArgs '-XX:MaxPermSize=256m'
if (!JavaVersion.current().isJava8Compatible()) {
jvmArgs '-XX:MaxPermSize=256m'
}
maxParallelForks = 2
}

task integrationTests(type: Test) {
Expand All @@ -123,8 +126,11 @@ subprojects {
reports.junitXml.destination = "${buildDir}/reports/integration-tests"
reports.html.destination = "${buildDir}/reports/integration-tests"
minHeapSize = "128m"
maxHeapSize = "2048m"
jvmArgs '-XX:MaxPermSize=256m'
maxHeapSize = "4096m"
if (!JavaVersion.current().isJava8Compatible()) {
jvmArgs '-XX:MaxPermSize=256m'
}
maxParallelForks = 2
}

test {
Expand Down
3 changes: 0 additions & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dependencies {
compile project(':serenity-report-resources')

compile 'com.google.code.gson:gson:2.3.1'
compile 'com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.1'

compile ("cglib:cglib:2.2.2") {
exclude group: 'asm', module: 'asm'
Expand Down Expand Up @@ -66,7 +65,6 @@ dependencies {
compile "com.thoughtworks.xstream:xstream:1.4.1"
compile "org.apache.commons:commons-lang3:3.3.2"
compile "commons-collections:commons-collections:3.2.1"
compile "net.sf.flexjson:flexjson:2.1"
compile "org.freemarker:freemarker:2.3.19"
compile ("net.sourceforge.jexcelapi:jxl:2.6.12") {
exclude group: 'log4j', module: 'log4j'
Expand All @@ -77,7 +75,6 @@ dependencies {
compile 'org.hamcrest:hamcrest-library:1.3'
compile "com.thoughtworks.xstream:xstream:1.4.1"
compile "commons-collections:commons-collections:3.2.1"
compile "net.sf.flexjson:flexjson:2.1"
compile ("net.sourceforge.htmlunit:htmlunit:2.15") {
exclude group: 'org.apache.commons', module: 'commons-lang3'
exclude group: 'commons-logging', module: 'commons-logging'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.regex.Pattern;

import static org.apache.commons.collections.IteratorUtils.toList;

public class ExampleTable {

private final static String NEWLINE_CHAR = "\u2424";
private final static String NEWLINE = "\u0085";
private final static String LINE_SEPARATOR = "\u2028";
private final static String PARAGRAPH_SEPARATOR = "\u2089";
private final static String LEFT_BRACKET = "\u0FF3B";
private final static String RIGHT_BRACKET = "\u0FF3D";

List<String> headers;
List<List<String>> rows = Lists.newArrayList();
final static Pattern NEW_LINE = Pattern.compile("(\\r\\n)|(\\n)|(\\r)|(␤)|(\\r␤)");
private static final String SQUARE_BRACKETS_OR_WHITE_SPACE = "[][] \t";
final static Pattern NEW_LINE
= Pattern.compile("(\\r\\n)|(\\n)|(\\r)|(" + NEWLINE_CHAR + ")|(" + LINE_SEPARATOR + ")|(" + PARAGRAPH_SEPARATOR + ")|(\\r" + NEWLINE_CHAR + ")");
private static final String SQUARE_BRACKETS_OR_WHITE_SPACE = "[]" + LEFT_BRACKET + RIGHT_BRACKET + "\t";

public ExampleTable(String tableContents) {
tableContents = stripBracketsFromOuterPipes(tableContents);
List<String> lines = toList(Splitter.on(NEW_LINE)
.omitEmptyStrings()
.trimResults(CharMatcher.anyOf(SQUARE_BRACKETS_OR_WHITE_SPACE))
Expand All @@ -26,6 +37,13 @@ public ExampleTable(String tableContents) {
}
}

public static String stripBracketsFromOuterPipes(String text) {
text = StringUtils.replace(text, "[|", "|");
text = StringUtils.replace(text,"|]","|");
text = StringUtils.replace(text,LEFT_BRACKET + "|","|");
text = StringUtils.replace(text,"|" + RIGHT_BRACKET,"|");
return text;
}
private void addRowFrom(String row) {
rows.add(cellsFrom(row));
}
Expand Down
20 changes: 15 additions & 5 deletions core/src/main/java/net/thucydides/core/reports/html/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public class Formatter {
private static final String ASCIIDOC = "asciidoc";
private static final String NEW_LINE = System.getProperty("line.separator");

private final static String NEWLINE_CHAR = "\u2424";
private final static String NEWLINE = "\u0085";
private final static String LINE_SEPARATOR = "\u2028";
private final static String PARAGRAPH_SEPARATOR = "\u2089";
private final static String LEFT_BRACKET = "\u0FF3B";
private final static String RIGHT_BRACKET = "\u0FF3D";

private final IssueTracking issueTracking;
private final EnvironmentVariables environmentVariables;
private final MarkupRenderer asciidocRenderer;
Expand Down Expand Up @@ -159,8 +166,9 @@ public String addLineBreaks(final String text) {
}

public String convertAnyTables(String text) {
text = convertNonStandardNLChars(text);
if (shouldFormatEmbeddedTables() && containsEmbeddedTable(text)) {
text = convertNonStandardNLChars(text);
text = ExampleTable.stripBracketsFromOuterPipes(text);
text = withTablesReplaced(text);

}
Expand All @@ -180,9 +188,11 @@ private String withTablesReplaced(String text) {
}

private String convertNonStandardNLChars(String text) {
StringUtils.replace(text, "\r␤", NEW_LINE)
.replace("␤", NEW_LINE);
return StringUtils.replace(text, "␤", NEW_LINE);
text = StringUtils.replace(text, NEWLINE_CHAR, NEW_LINE);
text = StringUtils.replace(text, NEWLINE, NEW_LINE);
text = StringUtils.replace(text, LINE_SEPARATOR, NEW_LINE);
text = StringUtils.replace(text, PARAGRAPH_SEPARATOR, NEW_LINE);
return text;
}

private boolean shouldFormatEmbeddedTables() {
Expand Down Expand Up @@ -210,7 +220,7 @@ private List<String> getEmbeddedTablesIn(String text) {
try {
String line;
while ((line = reader.readLine()) != null) {
if (!inTable && containsTableStart(line)){ // start of a table
if (!inTable && line.contains("|")){ // start of a table
inTable = true;
} else if (inTable && !line.contains("|") && !(isBlank(line))){ // end of a table
embeddedTables.add(tableText.toString().trim());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.thucydides.core.reports.json.gson;

import com.fatboyindustrial.gsonjavatime.Converters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.Inject;
Expand All @@ -27,7 +26,7 @@ protected Gson getGson() {
@Inject
public GsonJSONConverter(EnvironmentVariables environmentVariables) {
this.environmentVariables = environmentVariables;
GsonBuilder gsonBuilder = Converters.registerAll(new GsonBuilder())
GsonBuilder gsonBuilder = new GsonBuilder()
.registerTypeAdapterFactory(OptionalTypeAdapter.FACTORY)
.registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter())
.registerTypeAdapter(File.class, new FileSerializer())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package net.thucydides.core.requirements;

import com.fatboyindustrial.gsonjavatime.Converters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.thucydides.core.reports.json.gson.ClassTypeAdapter;
import net.thucydides.core.reports.json.gson.CollectionAdapter;
import net.thucydides.core.reports.json.gson.OptionalTypeAdapter;
import net.thucydides.core.requirements.model.Requirement;
import org.apache.commons.lang3.reflect.TypeUtils;

import java.io.*;
import java.lang.reflect.Type;
Expand All @@ -24,7 +21,7 @@ public class RequirementPersister {
public RequirementPersister(File outputDirectory, String rootDirectory) {
this.outputDirectory = outputDirectory;
this.rootDirectory = rootDirectory;
this.gson = Converters.registerAll(new GsonBuilder())
this.gson = new GsonBuilder()
.registerTypeAdapterFactory(OptionalTypeAdapter.FACTORY)
.registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter()).create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ class WhenFormattingDataForTheHTMLReports extends Specification {
embeddedTable == "A table like this:<br><table class='embedded'><thead><th>owner</th><th>points</th></thead><tbody><tr><td>Jane</td><td>80000</td></tr><tr><td>Joe</td><td>50000</td></tr></tbody></table>"
}

def UNICODE_NL = "\u2424"

def "should identify a table within a step using NL new lines"() {
given:
def singleCellTable = "A table like this:[| owner | points || Jane | 80000 || Joe | 50000 |]"
def singleCellTable = "A table like this:${UNICODE_NL}[| owner | points |${UNICODE_NL}| Jane | 80000 |${UNICODE_NL}| Joe | 50000 |]"

def formatter = new Formatter(issueTracking);
when:
Expand Down Expand Up @@ -249,10 +251,13 @@ class WhenFormattingDataForTheHTMLReports extends Specification {
embeddedTable == "A table like this:<br><table class='embedded'><thead><th>owner</th><th>points</th></thead><tbody><tr><td>Jane</td><td>80000</td></tr><tr><td>Joe</td><td>50000</td></tr></tbody></table>"
}

def LEFT_BRACKET = '\u0FF3B'
def RIGHT_BRACKET = '\u0FF3D'


def "should identify a table within a step using Windows bracket chars"() {
given:
def singleCellTable = "A table like this:\r\n| owner | points |\r\n| Jane | 80000 |\r\n| Joe | 50000 |"
def singleCellTable = "A table like this:\r\n${LEFT_BRACKET}| owner | points |\r\n| Jane | 80000 |\r\n| Joe | 50000 |${RIGHT_BRACKET}"

def formatter = new Formatter(issueTracking);
when:
Expand Down

0 comments on commit 56f672a

Please sign in to comment.