Search in sources :

Example 1 with ReaderResource

use of org.thymeleaf.resource.ReaderResource in project thymeleaf-tests by thymeleaf.

the class HtmlBulkTester method main.

public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Syntax: java " + HtmlBulkTester.class.getName() + " [test_folder]");
        System.exit(1);
    }
    final String testFolderName = args[0];
    final File testFolder = new File(testFolderName);
    if (!testFolder.exists() || !testFolder.isDirectory()) {
        System.err.println("Folder " + testFolderName + " does not exist or is not a folder");
        System.exit(1);
    }
    System.out.println("Using temporary folder for output: " + System.getProperty("java.io.tmpdir"));
    final File[] filesInTestFolder = testFolder.listFiles();
    for (final File fileInTestFolder : filesInTestFolder) {
        if (!fileInTestFolder.getName().endsWith(".html")) {
            continue;
        }
        final String fileInTestFolderName = fileInTestFolder.getName();
        final FileInputStream fileInTestFolderStream = new FileInputStream(fileInTestFolder);
        final InputStreamReader fileInTestFolderReader = new InputStreamReader(fileInTestFolderStream, "UTF-8");
        final File testOutput = File.createTempFile("thymeleaf-testing-" + fileInTestFolderName + "-", ".html");
        testOutput.deleteOnExit();
        final FileOutputStream testOutputStream = new FileOutputStream(testOutput);
        final OutputStreamWriter testOutputWriter = new OutputStreamWriter(testOutputStream, "UTF-8");
        final ITemplateHandler handler = new OutputTemplateHandler(testOutputWriter);
        handler.setProcessingContext(new ProcessingContext(TEMPLATE_ENGINE_CONFIGURATION, fileInTestFolderName, TemplateMode.HTML, Locale.US, null));
        System.out.print(fileInTestFolderName);
        System.out.print("[PARSING]");
        PARSER.parse(TEMPLATE_ENGINE_CONFIGURATION, TemplateMode.HTML, new ReaderResource(fileInTestFolderName, fileInTestFolderReader), handler);
        // Input stream will be closed by parser
        testOutputWriter.close();
        testOutputStream.close();
        System.out.print("[PARSED]");
        final FileInputStream testOutputCheckStream = new FileInputStream(testOutput);
        final List<String> outputLines = IOUtils.readLines(testOutputCheckStream, "UTF-8");
        final FileInputStream testInputCheckStream = new FileInputStream(fileInTestFolder);
        final List<String> inputLines = IOUtils.readLines(testInputCheckStream, "UTF-8");
        System.out.print("[CHECKING]");
        if (outputLines.equals(inputLines)) {
            System.out.print("[OK]");
        } else {
            System.out.print("[KO]");
        }
        System.out.println();
    }
}
Also used : ProcessingContext(org.thymeleaf.context.ProcessingContext) InputStreamReader(java.io.InputStreamReader) ReaderResource(org.thymeleaf.resource.ReaderResource) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ProcessingContext (org.thymeleaf.context.ProcessingContext)1 ReaderResource (org.thymeleaf.resource.ReaderResource)1