001    /**
002     * 
003     */
004    package edu.samplu.common;
005    
006    import java.io.File;
007    import java.io.InputStream;
008    import java.util.Properties;
009    import org.apache.commons.io.FileUtils;
010    import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
011    import freemarker.cache.ClassTemplateLoader;
012    import freemarker.cache.TemplateLoader;
013    import freemarker.template.Configuration;
014    import sun.applet.Main;
015    
016    /**
017     * @author hardik
018     * 
019     */
020    public class FreemarkerSmoketestGenerator {
021        private static Configuration cfg = new Configuration();
022    
023        // Templates for File Generation
024        private static String DIR_TMPL = "/Gen/";
025    
026        //Configuration
027        private static TemplateLoader templateLoader = new ClassTemplateLoader(Main.class, DIR_TMPL);
028    
029        public static void main(String[] args) throws Exception {
030            cfg.setTemplateLoader(templateLoader);
031            String DEFAULT_PROPS_LOCATION = "/GenFiles/WorkFlowDocType.properties";
032            String STJUNITBASE_TMPL = "STJUnitBase.ftl";
033            String STJUNITBKMRKGEN_TMPL = "STJUnitBkMrkGen.ftl";
034            String STJUNITNAVGEN_TMPL = "STJUnitNavGen.ftl";
035            String STNGBASE_TMPL = "STNGBase.ftl";
036            String STNGBKMRKGEN_TMPL = "STNGBkMrkGen.ftl";
037            String STNGNAVGEN_TMPL = "STNGNavGen.ftl";
038            
039            //Here we can prepare a list of template & properties file and can iterate to generate files dynamically on single run.
040            createFile(DEFAULT_PROPS_LOCATION,STJUNITBASE_TMPL);
041            createFile(DEFAULT_PROPS_LOCATION,STJUNITBKMRKGEN_TMPL);
042            createFile(DEFAULT_PROPS_LOCATION,STJUNITNAVGEN_TMPL);
043            createFile(DEFAULT_PROPS_LOCATION,STNGBASE_TMPL);
044            createFile(DEFAULT_PROPS_LOCATION,STNGBKMRKGEN_TMPL);
045            createFile(DEFAULT_PROPS_LOCATION,STNGNAVGEN_TMPL);
046        }
047    
048        private static void createFile(String DEFAULT_PROPS_LOCATION, String TMPL) throws Exception
049        {
050            try {
051                //Loading Properties file
052                Properties props = new Properties();
053                InputStream in = null;
054                in = Main.class.getResourceAsStream(DEFAULT_PROPS_LOCATION);
055    
056                if (in != null) {
057                    props.load(in);
058                    in.close();
059                }
060                else
061                {
062                    throw new Exception("Problem with input stream.");
063                }
064    
065                // build file STJUnitBase and add to array              
066                File f1 = new File("src" + File.separatorChar + "it" + File.separatorChar + "resources"
067                        + File.separatorChar + "GenFiles" + File.separatorChar + props.getProperty("className")
068                        + TMPL.substring(0, TMPL.length() - 4) + ".java");
069    
070                String output1 = FreeMarkerTemplateUtils.processTemplateIntoString(cfg.getTemplate(TMPL),
071                        props);
072                FileUtils.writeStringToFile(f1, output1);
073    
074            } catch (Exception e) {
075                e.printStackTrace();
076                throw new Exception("Unable to generate files", e);
077            }
078    
079        }
080    
081    }