001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.samplu.common;
017    
018    import java.io.BufferedReader;
019    import java.io.File;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.InputStreamReader;
023    import java.util.Properties;
024    import java.util.StringTokenizer;
025    
026    import freemarker.cache.ClassTemplateLoader;
027    import freemarker.cache.TemplateLoader;
028    import freemarker.template.Configuration;
029    import org.apache.commons.io.FileUtils;
030    import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
031    
032    /**
033     * The JMeter.ftl must use "@{" for JMeter variables these are converted to "${" after the templating.  Required to avoid
034     * Freemarker errors caused by JMeter variables being interpreted as Freemarker variables.
035     *
036     * @author Kuali Rice Team (rice.collab@kuali.org)
037     */
038    public class FreemarkerJMeterGenerator {
039    
040        private static Configuration cfg = new Configuration();
041    
042        private static final String[] propertiesArray = {"testplanname", "testpath", "viewid", "pageid", "pageundertesttimeoutms"};
043    
044        // Templates for File Generation
045        private static String DIR_TMPL = "/jmeter/";
046    
047        //Configuration
048        private static TemplateLoader templateLoader = new ClassTemplateLoader(FreemarkerJMeterGenerator.class, DIR_TMPL);
049    
050        public static void main(String[] args) throws Exception {
051            cfg.setTemplateLoader(templateLoader);
052    
053            String template = "KRAD-JMeter.ftl";
054            String csvLocation = "/jmeter/KRAD.csv";
055    
056            csvFreemarkerTemplate(template, csvLocation);
057        }
058    
059        private static void csvFreemarkerTemplate(String template, String csvLocation) throws IOException {
060            InputStream in = FreemarkerJMeterGenerator.class.getResourceAsStream(csvLocation);
061            BufferedReader csvReader = new BufferedReader(new InputStreamReader(in));
062    
063            try {
064                String csvLine = null;
065                int i = 0;
066                Properties csvProperties = null;
067                StringTokenizer tokenizer = null;
068                File jmeterTest = null;
069                String pathPart = null;
070    
071                while ((csvLine = csvReader.readLine()) != null && (!csvLine.isEmpty())) {
072                    i = 0;
073                    csvProperties = new Properties();
074                    tokenizer = new StringTokenizer(csvLine, ",");
075    
076                    while (tokenizer.hasMoreTokens()) {
077                        csvProperties.put(propertiesArray[i++], tokenizer.nextToken());
078                    }
079    
080                    // TODO would be nice to allow overriding via System params like the other Freemarker Generators do
081                    pathPart = csvProperties.getProperty(propertiesArray[1]);
082                    pathPart = pathPart.substring(pathPart.lastIndexOf(File.separatorChar) + 1, pathPart.length());
083                    jmeterTest = new File(pathPart + "_" + csvProperties.getProperty(propertiesArray[2]) + "_" + csvProperties.getProperty(propertiesArray[3]) + ".jmx");
084                    String output = FreeMarkerTemplateUtils.processTemplateIntoString(cfg.getTemplate(template), csvProperties);
085                    output = output.replace("@{", "${");
086                    FileUtils.writeStringToFile(jmeterTest, output);
087                }
088            } catch (Exception e) {
089                e.printStackTrace();
090            } finally {
091                if (csvReader != null) {
092                    csvReader.close();
093                }
094            }
095        }
096    }