TemplateUtils { public static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ) throws IOException { ApplicationContextBean appCtx = ContextUtils.toContext( app ); Context wrappingCtx = Context .newBuilder( appCtx ) .resolver( MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, MethodValueResolver.INSTANCE, new ComponentPathResolver() ).build(); try { for( TemplateEntry template : templates ) { logger.fine( "Processing template " + template.getTemplateFile() + " to application " + app + "." ); File target; String targetFilePath = template.getTargetFilePath(); if( ! Utils.isEmptyOrWhitespaces( targetFilePath )) { target = new File( targetFilePath.replace( "${app}", app.getName())); } else { String filename = template.getTemplateFile().getName().replaceFirst( "\\.tpl$", "" ); target = new File( outputDirectory, app.getName() + "/" + filename ); } Utils.createDirectory( target.getParentFile()); String output = template.getTemplate().apply( wrappingCtx ); Utils.writeStringInto( output, target ); logger.fine( "Template " + template.getTemplateFile() + " was processed with application " + app + ". Output is in " + target ); } } finally { wrappingCtx.destroy(); } } private  TemplateUtils(); static String findApplicationName( final File templateDir, final File templateFile ); static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ); static void deleteGeneratedFiles( Application app, File outputDirectory ); static Collection<TemplateEntry> findTemplatesForApplication( String appName, Collection<TemplateEntry> templates );  }
@Test public void testGenerate_defaultDirectory() throws Exception { File dir = this.folder.newFolder(); File tplFile = new File( dir, "basic.txt.tpl" ); InputStream in = getClass().getResourceAsStream( "/templates/basic.txt.tpl" ); try { Utils.copyStream( in, tplFile ); } finally { Utils.closeQuietly( in ); } TemplateEntry te = new TemplateWatcher( null, dir, 100 ).compileTemplate( tplFile ); Assert.assertNotNull( te ); Assert.assertNull( te.getAppName()); Assert.assertEquals( tplFile, te.getTemplateFile()); Assert.assertNotNull( te.getTemplate()); Logger logger = Logger.getLogger( getClass().getName()); File outputDir = this.folder.newFolder(); Application app = GenerationTest.testApplicationForTemplates(); Assert.assertEquals( 0, outputDir.listFiles().length ); TemplateUtils.generate( app, outputDir, Collections.singleton( te ), logger ); Assert.assertEquals( 1, outputDir.listFiles().length ); File targetFile = new File( outputDir, app.getName() + "/basic.txt" ); Assert.assertTrue( targetFile.exists()); String currentContent = Utils.readFileContent( targetFile ); File expectedFile = TestUtils.findTestFile( "/output/basic.txt" ); String expectedContent = Utils.readFileContent( expectedFile ); Assert.assertEquals( expectedContent, currentContent ); }
@Test public void testGenerate_customDirectory() throws Exception { File targetFile = this.folder.newFile(); Utils.deleteFilesRecursively( targetFile ); String templateContent; InputStream in = getClass().getResourceAsStream( "/templates/basic-with-custom-output.txt.tpl" ); try { ByteArrayOutputStream os = new ByteArrayOutputStream(); Utils.copyStreamUnsafelyUseWithCaution( in, os ); templateContent = os.toString( "UTF-8" ).replace( "%DIR%", targetFile.getAbsolutePath()); } finally { Utils.closeQuietly( in ); } File dir = this.folder.newFolder(); File tplFile = new File( dir, "basic-with-custom-output.txt.tpl" ); Utils.writeStringInto( templateContent, tplFile ); TemplateEntry te = new TemplateWatcher( null, dir, 100 ).compileTemplate( tplFile ); Assert.assertNotNull( te ); Assert.assertNull( te.getAppName()); Assert.assertEquals( tplFile, te.getTemplateFile()); Assert.assertEquals( targetFile.getAbsolutePath(), te.getTargetFilePath()); Assert.assertNotNull( te.getTemplate()); Assert.assertFalse( targetFile.exists()); Logger logger = Logger.getLogger( getClass().getName()); File outputDir = this.folder.newFolder(); Assert.assertEquals( 0, outputDir.listFiles().length ); TemplateUtils.generate( GenerationTest.testApplicationForTemplates(), outputDir, Collections.singleton( te ), logger ); Assert.assertEquals( 0, outputDir.listFiles().length ); Assert.assertTrue( targetFile.exists()); String currentContent = Utils.readFileContent( targetFile ); File expectedFile = TestUtils.findTestFile( "/output/basic-with-custom-output.txt" ); String expectedContent = Utils.readFileContent( expectedFile ); Assert.assertEquals( expectedContent, currentContent ); }
@Test public void testGenerate_customDirectory_byApp() throws Exception { File targetFilePattern = new File( this.folder.newFolder(), "${app}/${app}.paf" ); String templateContent; InputStream in = getClass().getResourceAsStream( "/templates/basic-with-custom-output.txt.tpl" ); try { ByteArrayOutputStream os = new ByteArrayOutputStream(); Utils.copyStreamUnsafelyUseWithCaution( in, os ); templateContent = os.toString( "UTF-8" ).replace( "%DIR%", targetFilePattern.getAbsolutePath()); } finally { Utils.closeQuietly( in ); } File dir = this.folder.newFolder(); File tplFile = new File( dir, "basic-with-custom-output.txt.tpl" ); Utils.writeStringInto( templateContent, tplFile ); TemplateEntry te = new TemplateWatcher( null, dir, 100 ).compileTemplate( tplFile ); Assert.assertNotNull( te ); Assert.assertNull( te.getAppName()); Assert.assertEquals( tplFile, te.getTemplateFile()); Assert.assertEquals( targetFilePattern.getAbsolutePath(), te.getTargetFilePath()); Assert.assertNotNull( te.getTemplate()); Application app = GenerationTest.testApplicationForTemplates(); File targetFile = new File( targetFilePattern.getParentFile().getParentFile(), app.getName() + "/" + app.getName() + ".paf" ); Assert.assertFalse( targetFilePattern.exists()); Assert.assertFalse( targetFile.exists()); Logger logger = Logger.getLogger( getClass().getName()); File outputDir = this.folder.newFolder(); Assert.assertEquals( 0, outputDir.listFiles().length ); TemplateUtils.generate( app, outputDir, Collections.singleton( te ), logger ); Assert.assertEquals( 0, outputDir.listFiles().length ); Assert.assertFalse( targetFilePattern.exists()); Assert.assertTrue( targetFile.exists()); String currentContent = Utils.readFileContent( targetFile ); File expectedFile = TestUtils.findTestFile( "/output/basic-with-custom-output.txt" ); String expectedContent = Utils.readFileContent( expectedFile ); Assert.assertEquals( expectedContent, currentContent ); }
@Test( expected = IOException.class ) public void testGenerate_io_exception() throws Exception { File inexistingTemplate = new File( "inexisting.tpl" ); TemplateEntry te = new TemplateEntry( inexistingTemplate, null, null, null ); Application app = new Application( "test", null ); Logger logger = Logger.getLogger( getClass().getName()); File outputDir = this.folder.newFile(); TemplateUtils.generate( app, outputDir, Collections.singleton( te ), logger ); }