| 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.LinkedList; |
| 15 | import java.util.List; |
| 16 | import org.jtiger.framework.ReadOnlyArray; |
| 17 | import org.jtiger.framework.TestResultType; |
| 18 | import static org.jtiger.report.html.OverallResultFactory.newOverallResult; |
| 19 | import static org.jtiger.report.html.ResultMappingFactory.newResultMapping; |
| 20 | |
| 21 | final class OverallResultConverterFactory |
| 22 | { |
| 23 | private static final String NA = "-"; |
| 24 | |
| 25 | private OverallResultConverterFactory() |
| 26 | { |
| 27 | |
| 28 | } |
| 29 | |
| 30 | static OverallResultConverter newOverallResultConverter() |
| 31 | { |
| 32 | return new OverallResultConverterImpl(); |
| 33 | } |
| 34 | |
| 35 | private static final class OverallResultConverterImpl implements OverallResultConverter |
| 36 | { |
| 37 | OverallResultConverterImpl() |
| 38 | { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | public ReadOnlyArray<OverallResult> convertOverallResult(final ReadOnlyArray<FixtureResults> results) |
| 43 | { |
| 44 | final List<OverallResult> overallResults = new LinkedList<OverallResult>(); |
| 45 | |
| 46 | for(FixtureResults result : results) |
| 47 | { |
| 48 | final String name = result.getName(); |
| 49 | final String description = result.getDescription(); |
| 50 | final String elapsedTime = getElapsedTime(result); |
| 51 | final String overallResult = getResult(result); |
| 52 | |
| 53 | final OverallResult or = newOverallResult( |
| 54 | name, |
| 55 | description == null || description.length() == 0 ? NA : description, |
| 56 | elapsedTime == null ? NA : elapsedTime, |
| 57 | overallResult); |
| 58 | overallResults.add(or); |
| 59 | } |
| 60 | |
| 61 | return newSequence(overallResults.toArray(new OverallResult[overallResults.size()])); |
| 62 | } |
| 63 | |
| 64 | private String getElapsedTime(final FixtureResults result) |
| 65 | { |
| 66 | boolean added = false; |
| 67 | long total = 0; |
| 68 | |
| 69 | for(TestResult tr : result.getTestResults()) |
| 70 | { |
| 71 | if(isAllDigits(tr.getElapsedTime())) |
| 72 | { |
| 73 | total += Long.parseLong(tr.getElapsedTime()); |
| 74 | added = true; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return added ? String.valueOf(total) : null; |
| 79 | } |
| 80 | |
| 81 | private String getResult(final FixtureResults result) |
| 82 | { |
| 83 | TestResultType worstCase = TestResultType.values()[0]; |
| 84 | |
| 85 | final ResultMapping mapping = newResultMapping(); |
| 86 | |
| 87 | for(TestResult tr : result.getTestResults()) |
| 88 | { |
| 89 | final TestResultType type = mapping.getResultMapping(tr.getResult()); |
| 90 | |
| 91 | if(type != null && type.compareTo(worstCase) > 0) |
| 92 | { |
| 93 | worstCase = type; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return mapping.getResultMapping(worstCase); |
| 98 | } |
| 99 | |
| 100 | private boolean isAllDigits(final String s) |
| 101 | { |
| 102 | if(s == null || s.length() == 0) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | for(int i = 0; i < s.length(); i++) |
| 108 | { |
| 109 | if(!Character.isDigit(s.charAt(i))) |
| 110 | { |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | } |