package at.gv.egiz.param_tests.serialization; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.param_tests.testinfo.TestInfo; /** * Base for all test information serializer. It uses template method pattern, * fixing an algorithm for serialization, but delegating the actual steps to the * subclasses. A two-step hierarchy shall be formed, with this class at the * upper-most level, technology-specific (like HTML) serializer at the next and * technology- and test-type-specific serializer at the lowest level. * * @author mtappler * * @param * the type of TestInfo which instance of this class, respectively * subclasses of it can serialize */ public abstract class TestInfoSerializer { /** * logger for this class */ private static final Logger logger = LoggerFactory .getLogger(TestInfoSerializer.class); /** * basic test information */ protected TestInfo baseTestInfo; /** * Method for creating an of a subclass of TestInfo. The test * information created using this method shall be retrieved, when * getBaseTestInfo() is called. * * @return instance of a TestInfo-subclass */ public abstract T createTestInfo(); /** * Clone method for serializer, it shall create a shallow copy of * this and return it. It does not use * Object.clone() for type safety, because this method returns * an Object-instance. * * @return a clone of this */ public abstract TestInfoSerializer cloneSerializer(); /** * getter * * @return basic test information */ public TestInfo getBaseTestInfo() { return baseTestInfo; } /** * Main serialization method. This method creates a test result file in the * test directory and then calls the methods to perform the serialization * steps. The following parts shall be written: *
    *
  1. header
  2. *
  3. test specific parameter
  4. *
  5. test result
  6. *
  7. test specific data
  8. *
  9. footer
  10. *
*/ public void serialize() { File outputFile = new File(baseTestInfo.getBaseTestData() .getTestDirectory() + "/test_result." + fileEnding()); FileOutputStream os = null; try { os = new FileOutputStream(outputFile); PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF8")); writeHeader(pw); writeTestSpecificParameters(pw); writeTestResult(pw); writeTestData(pw); writeFooter(pw); pw.flush(); } catch (FileNotFoundException e) { logger.warn("File not found for test info serialization.", e); } catch (UnsupportedEncodingException e) { logger.warn("Use unsupported encoding for serialization.", e); } finally { if (os != null) IOUtils.closeQuietly(os); } } /** * This writes the test result in some format like HTML to the provided * PrintWriter-object. * * @param pw * the PrintWriter-object whill shall be used for * serialization */ protected abstract void writeTestResult(PrintWriter pw); /** * This writes test specific parameters in some format like HTML to the * provided PrintWriter-object. * * @param pw * the PrintWriter-object whill shall be used for * serialization */ protected abstract void writeTestSpecificParameters(PrintWriter pw); /** * This writes the file header in some format like HTML to the provided * PrintWriter-object. * * @param pw * the PrintWriter-object whill shall be used for * serialization */ protected abstract void writeHeader(PrintWriter pw); /** * This writes test specific data in some format like HTML to the provided * PrintWriter-object. * * @param pw * the PrintWriter-object whill shall be used for * serialization */ protected abstract void writeTestData(PrintWriter pw); /** * This writes the file footer in some format like HTML to the provided * PrintWriter-object. * * @param pw * the PrintWriter-object whill shall be used for * serialization */ protected abstract void writeFooter(PrintWriter pw); /** * This method returns the file ending used for the test result file, like * "html" or "xml". * * @return the file ending string */ public abstract String fileEnding(); /** * This method returns a description of the concrete test type, which a * concrete implementation of this class serializes. * * @return a test type description */ public abstract String getTestType(); }