View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.samplu.krad;
17  
18  import freemarker.cache.ClassTemplateLoader;
19  import freemarker.cache.TemplateLoader;
20  import freemarker.template.Configuration;
21  import org.apache.commons.io.FileUtils;
22  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
23  
24  import java.io.BufferedReader;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.InputStreamReader;
29  import java.util.Properties;
30  import java.util.StringTokenizer;
31  
32  /**
33   * The JMeter.ftl must use "@{" for JMeter variables these are converted to "${" after the templating.  Required to avoid
34   * Freemarker errors caused by JMeter variables being interpreted as Freemarker variables.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class FreemarkerJMeterGenerator {
39  
40      private static Configuration cfg = new Configuration();
41  
42      private static final String[] propertiesArray = {"testplanname", "testpath", "viewid", "pageid", "pageundertesttimeoutms"};
43  
44      // Templates for File Generation
45      private static String DIR_TMPL = "/jmeter/";
46  
47      //Configuration
48      private static TemplateLoader templateLoader = new ClassTemplateLoader(FreemarkerJMeterGenerator.class, DIR_TMPL);
49  
50      public static void main(String[] args) throws Exception {
51          cfg.setTemplateLoader(templateLoader);
52  
53          String template = "KRAD-JMeter.ftl";
54          String csvLocation = "/jmeter/KRAD.csv";
55  
56          csvFreemarkerTemplate(template, csvLocation);
57      }
58  
59      private static void csvFreemarkerTemplate(String template, String csvLocation) throws IOException {
60          InputStream in = FreemarkerJMeterGenerator.class.getResourceAsStream(csvLocation);
61          BufferedReader csvReader = new BufferedReader(new InputStreamReader(in));
62  
63          try {
64              String csvLine = null;
65              int i = 0;
66              Properties csvProperties = null;
67              StringTokenizer tokenizer = null;
68              File jmeterTest = null;
69              String pathPart = null;
70  
71              while ((csvLine = csvReader.readLine()) != null && (!csvLine.isEmpty())) {
72                  i = 0;
73                  csvProperties = new Properties();
74                  tokenizer = new StringTokenizer(csvLine, ",");
75  
76                  while (tokenizer.hasMoreTokens()) {
77                      csvProperties.put(propertiesArray[i++], tokenizer.nextToken());
78                  }
79  
80                  // TODO would be nice to allow overriding via System params like the other Freemarker Generators do
81                  pathPart = csvProperties.getProperty(propertiesArray[1]);
82                  pathPart = pathPart.substring(pathPart.lastIndexOf(File.separatorChar) + 1, pathPart.length());
83                  jmeterTest = new File(pathPart + "_" + csvProperties.getProperty(propertiesArray[2]) + "_" + csvProperties.getProperty(propertiesArray[3]) + ".jmx");
84                  String output = FreeMarkerTemplateUtils.processTemplateIntoString(cfg.getTemplate(template), csvProperties);
85                  output = output.replace("@{", "${");
86                  FileUtils.writeStringToFile(jmeterTest, output);
87              }
88          } catch (Exception e) {
89              e.printStackTrace();
90          } finally {
91              if (csvReader != null) {
92                  csvReader.close();
93              }
94          }
95      }
96  }