1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
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
45 private static String DIR_TMPL = "/jmeter/";
46
47
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
79 csvProperties.put(propertiesArray[i++], tokenizer.nextToken().replace("\"", ""));
80 }
81
82
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 }