| 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 java.beans.XMLEncoder; |
| 14 | import java.io.ByteArrayInputStream; |
| 15 | import java.io.ByteArrayOutputStream; |
| 16 | import java.io.File; |
| 17 | import java.io.FileNotFoundException; |
| 18 | import java.io.FileOutputStream; |
| 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | import java.io.OutputStream; |
| 22 | import javax.xml.transform.Transformer; |
| 23 | import javax.xml.transform.TransformerConfigurationException; |
| 24 | import javax.xml.transform.TransformerException; |
| 25 | import javax.xml.transform.TransformerFactory; |
| 26 | import javax.xml.transform.stream.StreamResult; |
| 27 | import javax.xml.transform.stream.StreamSource; |
| 28 | import org.jtiger.framework.FixtureResultsHandlerException; |
| 29 | |
| 30 | final class FixtureHtmlFileWriterFactory |
| 31 | { |
| 32 | private static final String XSL_RESOURCE = "/org/jtiger/report/xsl/fixture.xsl"; |
| 33 | private static final String PARAM_BUILD_TIME = "buildTime"; |
| 34 | private static final String PARAM_FIXTURE_NAME = "fixtureName"; |
| 35 | private static final String PARAM_FIXTURE_DESCRIPTION = "fixtureDescription"; |
| 36 | private static final String PARAM_FIXTURE_CLASS = "fixtureClass"; |
| 37 | |
| 38 | private FixtureHtmlFileWriterFactory() |
| 39 | { |
| 40 | |
| 41 | } |
| 42 | |
| 43 | static FixtureHtmlFileWriter newFixtureHtmlFileWriter() |
| 44 | { |
| 45 | return new FixtureHtmlFileWriterImpl(); |
| 46 | } |
| 47 | |
| 48 | private static final class FixtureHtmlFileWriterImpl implements FixtureHtmlFileWriter |
| 49 | { |
| 50 | FixtureHtmlFileWriterImpl() |
| 51 | { |
| 52 | |
| 53 | } |
| 54 | |
| 55 | public void writeFixtureHtml( |
| 56 | final FixtureResults result, |
| 57 | final File destination, |
| 58 | final FilenamePolicy policy, |
| 59 | final String buildTime) |
| 60 | throws FixtureResultsHandlerException |
| 61 | { |
| 62 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 63 | final XMLEncoder encoder = new XMLEncoder(out); |
| 64 | |
| 65 | for(TestResult tr : result.getTestResults()) |
| 66 | { |
| 67 | final Object bean = new TestResultBeanImpl(tr.getName(), tr.getException(), tr.getElapsedTime(), |
| 68 | tr.getMessage(), tr.getMethodName(), tr.getDescription(), tr.getResult()); |
| 69 | |
| 70 | encoder.writeObject(bean); |
| 71 | } |
| 72 | |
| 73 | encoder.close(); |
| 74 | |
| 75 | |
| 76 | InputStream in = null; |
| 77 | |
| 78 | try |
| 79 | { |
| 80 | in = getClass().getResourceAsStream(XSL_RESOURCE); |
| 81 | final Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(in)); |
| 82 | t.setParameter(PARAM_BUILD_TIME, buildTime); |
| 83 | t.setParameter(PARAM_FIXTURE_NAME, result.getName()); |
| 84 | t.setParameter(PARAM_FIXTURE_DESCRIPTION, result.getDescription()); |
| 85 | t.setParameter(PARAM_FIXTURE_CLASS, result.getClassName()); |
| 86 | |
| 87 | OutputStream o = null; |
| 88 | |
| 89 | try |
| 90 | { |
| 91 | final File f = new File(destination, policy.nextFilename()); |
| 92 | f.getParentFile().mkdirs(); |
| 93 | o = new FileOutputStream(f); |
| 94 | final StreamResult r = new StreamResult(o); |
| 95 | t.transform(new StreamSource(new ByteArrayInputStream(out.toByteArray())), r); |
| 96 | } |
| 97 | catch(FileNotFoundException e) |
| 98 | { |
| 99 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 100 | } |
| 101 | finally |
| 102 | { |
| 103 | if(o != null) |
| 104 | { |
| 105 | try |
| 106 | { |
| 107 | o.close(); |
| 108 | } |
| 109 | catch(IOException e) |
| 110 | { |
| 111 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | catch(TransformerConfigurationException e) |
| 117 | { |
| 118 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 119 | } |
| 120 | catch(TransformerException e) |
| 121 | { |
| 122 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 123 | } |
| 124 | finally |
| 125 | { |
| 126 | if(in != null) |
| 127 | { |
| 128 | try |
| 129 | { |
| 130 | in.close(); |
| 131 | } |
| 132 | catch(IOException e) |
| 133 | { |
| 134 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |