View Javadoc
1   /**
2    * Copyright 2005-2015 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 org.kuali.rice.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 FreemarkerJmeterTestGenerator {
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(FreemarkerJmeterTestGenerator.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 = FreemarkerJmeterTestGenerator.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                  System.out.println("Processing " + csvLine);
75                  tokenizer = new StringTokenizer(csvLine, ",");
76  
77                  while (tokenizer.hasMoreTokens()) {
78                      // remove quotes (from the testplanname)
79                      csvProperties.put(propertiesArray[i++], tokenizer.nextToken().replace("\"", ""));
80                  }
81  
82                  // TODO would be nice to allow overriding via System params like the other Freemarker Generators do
83                  pathPart = csvProperties.getProperty(propertiesArray[1]);
84                  if (pathPart != null) {
85                      pathPart = pathPart.substring(pathPart.lastIndexOf(File.separatorChar) + 1, pathPart.length());
86                      jmeterTest = new File(pathPart + "_" + csvProperties.getProperty(propertiesArray[2]) + "_" + csvProperties.getProperty(propertiesArray[3]) + ".jmx");
87                      String output = FreeMarkerTemplateUtils.processTemplateIntoString(cfg.getTemplate(template), csvProperties);
88                      output = output.replace("@{", "${");
89                      FileUtils.writeStringToFile(jmeterTest, output);
90                  }
91              }
92          } catch (Exception e) {
93              e.printStackTrace();
94          } finally {
95              if (csvReader != null) {
96                  csvReader.close();
97              }
98          }
99      }
100 }