1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
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
45 private static String DIR_TMPL = "/jmeter/";
46
47
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
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 }