forked from alexeisnyk/juliet-test-suite-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_Main_java_ServletMain_java_and_web_xml.py
189 lines (140 loc) · 6.95 KB
/
update_Main_java_ServletMain_java_and_web_xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#! /usr/bin/env/python 3.0
#
# Running this script will update files that are used when all of the test
# cases are compiled into a single .war file.
#
import sys,os,re
# add parent directory to search path so we can use py_common
sys.path.append("..")
import py_common
def build_list_of_java_testcase_files(directory):
files_to_check = []
for root, dirs, files in os.walk(directory):
for name in files:
result = re.search(py_common.get_primary_testcase_filename_regex(), name, re.IGNORECASE)
if result != None:
files_to_check.append(os.path.realpath(os.path.join(root,name)))
# don't enumerate files in support directories
if 'testcasesupport' in dirs:
dirs.remove('testcasesupport')
return files_to_check
def update_file(file_path, file, tag_start, tag_end, lines):
full_file_path = os.path.join(file_path, file)
file_contents = py_common.open_file_and_get_contents(full_file_path)
# get contents from start of file up to tag, get contents from end tag
# to EOF
up_to_tag_start = file_contents.split(tag_start)[0]
tag_end_to_eof = file_contents.split(tag_end)[1]
auto_gen_content = "\n".join(lines)
# re-build the file with the modified content between the tags
modified_file_contents = up_to_tag_start + \
tag_start + "\n" + \
auto_gen_content + "\n" + \
"\t\t\t" + tag_end + \
tag_end_to_eof
# write out the new file
outfile = os.path.join(file_path, file)
py_common.write_file(outfile, modified_file_contents)
def generate_web_xml_entry(namespace, classname):
entry = []
entry.append("\t<servlet>")
entry.append("\t\t<description/>")
entry.append("\t\t<display-name>" + classname + "</display-name>")
entry.append("\t\t<servlet-name>" + classname + "</servlet-name>")
entry.append("\t\t<servlet-class>" + namespace + classname + "</servlet-class>")
entry.append("\t</servlet>")
entry.append("\t<servlet-mapping>")
entry.append("\t\t<servlet-name>" + classname + "</servlet-name>")
entry.append("\t\t<url-pattern>" + classname + "</url-pattern>")
entry.append("\t</servlet-mapping>")
return entry
def update_package_name(file, package):
lines = py_common.open_file_and_get_lines(file)
lines.pop(0)
lines.insert(0, "package " + package + ";\n")
py_common.write_file(file, "".join(lines))
# Main.java has 9 separate locations where we need to replace text due to
# us running into the Java classfile limit on the length of a method (64K of
# bytecode)
def update_Main_java(file_path, non_servlet_testcase_lines, package):
# tags for Main.java that indicate where to replace text
main_dot_java = "Main.java";
auto_gen_tag_start = "/* BEGIN-AUTOGENERATED-JAVA-TESTS-X */"
auto_gen_tag_end = "/* END-AUTOGENERATED-JAVA-TESTS-X */"
for i in range(1,10):
# replace the X in the tags with the current number
current_auto_gen_tag_start = auto_gen_tag_start.replace("X", str(i))
current_auto_gen_tag_end = auto_gen_tag_end.replace("X", str(i))
# filter non_servlet_testcase_lines to just ones for CWE1, CWE2, etc
test = re.compile("CWE"+str(i))
#current_non_servlet_testcase_lines = filter(test.search,non_servlet_testcase_lines)
current_non_servlet_testcase_lines = [f for f in non_servlet_testcase_lines if test.search(f)]
# call update-file
update_file(file_path, main_dot_java, current_auto_gen_tag_start, current_auto_gen_tag_end, current_non_servlet_testcase_lines)
# replace package name with the correct package
update_package_name(os.path.join(file_path, main_dot_java), package)
# ServletMain.java has 9 separate locations where we need to replace text due to
# us running into the Java classfile limit on the length of a method (64K of
# bytecode)
def update_Servlet_Main_java(file_path, servlet_testcase_lines, package):
# tags for Main.java that indicate where to replace text
servlet_main_dot_java = "ServletMain.java";
auto_gen_tag_start = "/* BEGIN-AUTOGENERATED-SERVLET-TESTS-X */"
auto_gen_tag_end = "/* END-AUTOGENERATED-SERVLET-TESTS-X */"
for i in range(1,10):
# replace the X in the tags with the current number
current_auto_gen_tag_start = auto_gen_tag_start.replace("X", str(i))
current_auto_gen_tag_end = auto_gen_tag_end.replace("X", str(i))
# filter non_servlet_testcase_lines to just ones for CWE1, CWE2, etc
test = re.compile("CWE"+str(i))
#current_non_servlet_testcase_lines = filter(test.search,non_servlet_testcase_lines)
current_servlet_testcase_lines = [f for f in servlet_testcase_lines if test.search(f)]
# call update-file
update_file(file_path, servlet_main_dot_java, current_auto_gen_tag_start, current_auto_gen_tag_end, current_servlet_testcase_lines)
# replace package name with the correct package
update_package_name(os.path.join(file_path, servlet_main_dot_java), package)
def update_Main_java_ServletMain_java_and_web_xml(testcase_location=".", main_path="src/testcasesupport/", web_path="WebContent/WEB-INF/"):
# get list of testcase files
testcase_files = build_list_of_java_testcase_files(testcase_location)
# build up the class instantiation lines
non_servlet_testcase_lines = []
servlet_testcase_lines = []
web_lines = []
namespace = ""
for fullfilepath in testcase_files:
filename = os.path.basename(fullfilepath)
# figure out the namespace based on the file path
tc_index = fullfilepath.index("src/testcases")
f_index = fullfilepath.index(filename)
namespace = "testcases" + fullfilepath[tc_index + len("src/testcases"):f_index].replace("/", ".")
# remove .java from the end of the filename to get the classname
classname = filename[:-5]
# there are 2 types of testcases; if the class extends AbstractTestCaseServlet,
# then we want to put that in the ServletMain file;
# otherwise, it goes in the normal Main file.
#if "extends AbstractTestCaseServlet" in open_file_and_get_contents(fullfilepath):
reServlet = re.compile("Servlet", re.IGNORECASE)
if reServlet.search(classname) != None:
line = "\t\t\t(new " + namespace + \
classname + \
"()).runTest(request, response);"
servlet_testcase_lines.append(line)
lines = generate_web_xml_entry(namespace, classname)
web_lines.extend(lines)
else:
line = "\t\t\t(new " + namespace + \
classname + \
"()).runTest(\"" + classname + "\");"
non_servlet_testcase_lines.append(line)
servlet_main_dot_java = "ServletMain.java";
web_xml = "web.xml"
# tags for web.xml that indicate where to replace text
auto_gen_tag_start_web = "<!-- BEGIN-AUTOGENERATED-SERVLETS -->"
auto_gen_tag_end_web = "<!-- END-AUTOGENERATED-SERVLETS -->"
package = namespace[:-1] # Remove the trailing .
#update Main.java
update_Main_java(main_path, non_servlet_testcase_lines, package)
#updated ServletMain.java
update_Servlet_Main_java(main_path, servlet_testcase_lines, package)
#update web.xml
update_file(web_path, web_xml, auto_gen_tag_start_web, auto_gen_tag_end_web, web_lines)