Merge branch 'master' into master

This commit is contained in:
iaforek 2017-05-11 11:16:33 +01:00 committed by GitHub
commit 37da5f8854
7 changed files with 81 additions and 52 deletions

View File

@ -8,7 +8,7 @@ public class RemoveLastChar {
return (s.substring(0, s.length() - 1));
}
}
public static String chop(String s) {
if (s == null) {
return s;

View File

@ -2,7 +2,7 @@ package com.baeldung.stackoverflowerror;
import org.junit.Test;
public class AccountHolderUnitTest {
public class AccountHolderManualTest {
@Test(expected = StackOverflowError.class)
public void whenInstanciatingAccountHolder_thenThrowsException() {
AccountHolder holder = new AccountHolder();

View File

@ -1,10 +1,9 @@
package com.baeldung.stackoverflowerror;
import static org.junit.Assert.fail;
import org.junit.Test;
public class CyclicDependancyUnitTest {
public class CyclicDependancyManualTest {
@Test
public void whenInstanciatingClassOne_thenThrowsException() {
try {

View File

@ -2,10 +2,9 @@ package com.baeldung.stackoverflowerror;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
public class InfiniteRecursionWithTerminationConditionUnitTest {
public class InfiniteRecursionWithTerminationConditionManualTest {
@Test
public void givenPositiveIntNoOne_whenCalcFact_thenThrowsException() {
int numToCalcFactorial = 1;

View File

@ -1,29 +1,33 @@
package com.baeldung.stackoverflowerror;
import org.junit.Test;
public class UnintendedInfiniteRecursionUnitTest {
public class UnintendedInfiniteRecursionManualTest {
@Test(expected = StackOverflowError.class)
public void givenPositiveIntNoOne_whenCalFact_thenThrowsException() {
int numToCalcFactorial = 1;
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
int numToCalcFactorial= 1;
UnintendedInfiniteRecursion uir
= new UnintendedInfiniteRecursion();
uir.calculateFactorial(numToCalcFactorial);
}
@Test(expected = StackOverflowError.class)
public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() {
int numToCalcFactorial = 2;
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
int numToCalcFactorial= 2;
UnintendedInfiniteRecursion uir
= new UnintendedInfiniteRecursion();
uir.calculateFactorial(numToCalcFactorial);
}
@Test(expected = StackOverflowError.class)
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
int numToCalcFactorial = -1;
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
int numToCalcFactorial= -1;
UnintendedInfiniteRecursion uir
= new UnintendedInfiniteRecursion();
uir.calculateFactorial(numToCalcFactorial);
}
}

View File

@ -2,7 +2,6 @@ package com.baeldung.string;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

View File

@ -10,56 +10,84 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class CustomisedReports implements IReporter {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class);
private static final String ROW_TEMPLATE = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
String reportTemplate = initReportTemplate();
String resultRow = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
StringBuilder rows = new StringBuilder();
suites.forEach(suite -> {
Map<String, ISuiteResult> suiteResults = suite.getResults();
suiteResults.forEach((testName, suiteResult) -> {
ITestContext testContext = suiteResult.getTestContext();
final String body = suites
.stream()
.flatMap(suiteToResults())
.collect(Collectors.joining());
Stream<ITestResult> failedTests = testContext.getFailedTests().getAllResults().stream();
Stream<ITestResult> passedTests = testContext.getPassedTests().getAllResults().stream();
Stream<ITestResult> skippedTests = testContext.getSkippedTests().getAllResults().stream();
String suiteName = suite.getName();
Stream<ITestResult> allTestResults = Stream.concat(Stream.concat(failedTests, passedTests), skippedTests);
generateReportRows(resultRow, rows, testName, suiteName, allTestResults);
});
});
reportTemplate = reportTemplate.replaceFirst("</tbody>", rows.toString() + "</tbody>");
saveReportTemplate(outputDirectory, reportTemplate);
saveReportTemplate(outputDirectory, reportTemplate.replaceFirst("</tbody>", String.format("%s</tbody>", body)));
}
private void generateReportRows(String resultRow, StringBuilder rows, String testName, String suiteName, Stream<ITestResult> allTestResults) {
allTestResults
.forEach(testResult -> {
String testReportRow = "";
if (testResult.getStatus() == ITestResult.FAILURE) {
testReportRow = String.format(resultRow, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
}
if (testResult.getStatus() == ITestResult.SUCCESS) {
testReportRow = String.format(resultRow, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
private Function<ISuite, Stream<? extends String>> suiteToResults() {
return suite -> suite.getResults().entrySet()
.stream()
.flatMap(resultsToRows(suite));
}
private Function<Map.Entry<String, ISuiteResult>, Stream<? extends String>> resultsToRows(ISuite suite) {
return e -> {
ITestContext testContext = e.getValue().getTestContext();
Set<ITestResult> failedTests = testContext
.getFailedTests()
.getAllResults();
Set<ITestResult> passedTests = testContext
.getPassedTests()
.getAllResults();
Set<ITestResult> skippedTests = testContext
.getSkippedTests()
.getAllResults();
String suiteName = suite.getName();
return Stream
.of(failedTests, passedTests, skippedTests)
.flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream());
};
}
private List<String> generateReportRows(String testName, String suiteName, Set<ITestResult> allTestResults) {
return allTestResults.stream()
.map(testResultToResultRow(testName, suiteName))
.collect(toList());
}
private Function<ITestResult, String> testResultToResultRow(String testName, String suiteName) {
return testResult -> {
switch (testResult.getStatus()) {
case ITestResult.FAILURE:
return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
case ITestResult.SUCCESS:
return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
case ITestResult.SKIP:
return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
default:
return "";
}
if (testResult.getStatus() == ITestResult.SKIP) {
testReportRow = String.format(resultRow, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
}
rows.append(testReportRow);
});
};
}
private String initReportTemplate() {
String template = null;
byte[] reportTemplate = null;
byte[] reportTemplate;
try {
reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html"));
template = new String(reportTemplate, "UTF-8");