TemplateUtils { public static String findApplicationName( final File templateDir, final File templateFile ) { final File parentDir = templateFile.getParentFile(); final String appName; if( templateDir.equals( parentDir )) appName = null; else if( templateDir.equals( parentDir.getParentFile())) appName = parentDir.getName(); else throw new IllegalArgumentException( "Not a template file: " + templateFile ); return appName; } 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 testFindApplicationName_valid() throws Exception { File templatesDir = new File( "whatever" ); File globalTemplate = new File( templatesDir, "root.tpl" ); File specificTemplate = new File( templatesDir, "specific/specific.tpl" ); Assert.assertEquals( "specific", TemplateUtils.findApplicationName( templatesDir, specificTemplate )); Assert.assertNull( TemplateUtils.findApplicationName( templatesDir, globalTemplate )); }
@Test( expected = IllegalArgumentException.class ) public void testFindApplicationName_invalid() throws Exception { File templatesDir = new File( "whatever" ); File specificTemplate = new File( templatesDir, "specific/sub/dir/specific.tpl" ); TemplateUtils.findApplicationName( templatesDir, specificTemplate ); }