| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.report.html; |
| 12 | |
| 13 | import static org.jtiger.framework.SequenceFactory.newSequence; |
| 14 | import java.util.Collections; |
| 15 | import java.util.Comparator; |
| 16 | import java.util.LinkedList; |
| 17 | import java.util.List; |
| 18 | import org.jtiger.framework.FixtureResult; |
| 19 | import org.jtiger.framework.ReadOnlyArray; |
| 20 | import org.jtiger.framework.TestResultType; |
| 21 | import static org.jtiger.report.html.FixtureResultsFactory.newFixtureResults; |
| 22 | import static org.jtiger.report.html.TestResultFactory.newTestResult; |
| 23 | import static org.jtiger.report.html.ResultMappingFactory.newResultMapping; |
| 24 | |
| 25 | final class FixtureResultConverterFactory |
| 26 | { |
| 27 | private static final String NA = "-"; |
| 28 | |
| 29 | private FixtureResultConverterFactory() |
| 30 | { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | static FixtureResultConverter newFixtureResultConverter() |
| 35 | { |
| 36 | return new FixtureResultConverterImpl(); |
| 37 | } |
| 38 | |
| 39 | private static final class FixtureResultConverterImpl implements FixtureResultConverter |
| 40 | { |
| 41 | FixtureResultConverterImpl() |
| 42 | { |
| 43 | |
| 44 | } |
| 45 | |
| 46 | public ReadOnlyArray<FixtureResults> convertFixtureResults(final org.jtiger.framework.FixtureResults results) |
| 47 | { |
| 48 | final List<FixtureResults> frs = new LinkedList<FixtureResults>(); |
| 49 | |
| 50 | for(FixtureResult result : results) |
| 51 | { |
| 52 | final ResultMapping mapping = newResultMapping(); |
| 53 | |
| 54 | final List<TestResult> trs = new LinkedList<TestResult>(); |
| 55 | |
| 56 | String fixtureName = null; |
| 57 | String fixtureDescription = null; |
| 58 | String fixtureClassName = null; |
| 59 | boolean set = false; |
| 60 | |
| 61 | for(org.jtiger.framework.TestResult tr : result) |
| 62 | { |
| 63 | final String testName = tr.getTestName(); |
| 64 | final Throwable exception = tr.getException(); |
| 65 | final Long elapsedTime = tr.getElapsedTime(); |
| 66 | final String message = tr.getMessage(); |
| 67 | final String testMethodName = tr.getTestMethodName(); |
| 68 | final String testDescription = tr.getTestDescription(); |
| 69 | final TestResultType testResultType = tr.getTestResultType(); |
| 70 | |
| 71 | final TestResult r = newTestResult( |
| 72 | testName, |
| 73 | exception == null ? NA : exception.getClass().getName(), |
| 74 | elapsedTime == null ? NA : String.valueOf(elapsedTime), |
| 75 | message == null || message.length() == 0 ? NA : message, |
| 76 | testMethodName, |
| 77 | testDescription.length() == 0 ? NA : testDescription, |
| 78 | mapping.getResultMapping(testResultType)); |
| 79 | |
| 80 | fixtureName = tr.getFixtureName(); |
| 81 | fixtureDescription = tr.getFixtureDescription() == null || tr.getFixtureDescription().length() == 0 ? NA : tr.getFixtureDescription(); |
| 82 | fixtureClassName = tr.getFixtureClass().getName(); |
| 83 | set = true; |
| 84 | |
| 85 | trs.add(r); |
| 86 | } |
| 87 | |
| 88 | if(set) |
| 89 | { |
| 90 | Collections.sort(trs, new Comparator<TestResult>() |
| 91 | { |
| 92 | public int compare(final TestResult tr1, final TestResult tr2) |
| 93 | { |
| 94 | return tr1.getName().compareTo(tr2.getName()); |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | final FixtureResults fr = newFixtureResults( |
| 99 | fixtureName, |
| 100 | fixtureDescription, |
| 101 | fixtureClassName, |
| 102 | newSequence(trs.toArray(new TestResult[trs.size()]))); |
| 103 | |
| 104 | frs.add(fr); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | Collections.sort(frs, new Comparator<FixtureResults>() |
| 109 | { |
| 110 | public int compare(final FixtureResults fr1, final FixtureResults fr2) |
| 111 | { |
| 112 | return fr1.getName().compareTo(fr2.getName()); |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | return newSequence(frs.toArray(new FixtureResults[frs.size()])); |
| 117 | } |
| 118 | } |
| 119 | } |