Skip to content

Commit 2aad806

Browse files
committed
injection extension update
1 parent 74bef24 commit 2aad806

File tree

3 files changed

+90
-32
lines changed

3 files changed

+90
-32
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import sys
3+
import re
4+
5+
6+
def migration_injection(content):
7+
# Add import statements
8+
new_imports = [
9+
"import org.junit.jupiter.api.Test;",
10+
"import com.addepar.infra.library.testing.extention.GuiceInjectionExtension;",
11+
]
12+
if "org.junit.jupiter.api.extension.ExtendWith" not in content:
13+
new_imports.append("import org.junit.jupiter.api.extension.ExtendWith;")
14+
content_new = re.sub(
15+
"import org.junit.jupiter.api.Test;",
16+
"\n".join(new_imports),
17+
content,
18+
)
19+
20+
# Remove @SuppressWarnings("EmptyCatchBlock")
21+
content_new = re.sub(
22+
r" @SuppressWarnings\(\"EmptyCatchBlock\"\)\s*\n", "", content_new
23+
)
24+
25+
content_new = re.sub(
26+
r"try {\n\s*injector\.injectMembers\(this\);\n\s*} catch \(RuntimeException e\) {\n\s*}",
27+
"injector.injectMembers(this);",
28+
content_new,
29+
)
30+
31+
# Add @ExtendWith(GuiceInjectionExtension.class)
32+
new_lines = []
33+
content_iter = iter(content_new.split("\n"))
34+
for line in content_iter:
35+
if "public class" in line or "public final class" in line:
36+
left_spaces = " " * (len(line) - len(line.lstrip()))
37+
new_lines.append(left_spaces + "@ExtendWith(GuiceInjectionExtension.class)")
38+
new_lines.append(line)
39+
content_new = "\n".join(new_lines)
40+
41+
return content_new
42+
43+
44+
def migrate_tests(test_dir):
45+
test_files = []
46+
for path, directory, files in os.walk(test_dir):
47+
# print("p", path)
48+
# print("f", files)
49+
for file in files:
50+
if file.endswith(".java"):
51+
test_files.append(os.path.join(path, file))
52+
53+
for file_name in test_files:
54+
# print("f ", file_name)
55+
with open(file_name, "r") as f:
56+
content = f.read()
57+
if (
58+
"@BeforeAll" in content
59+
and "injector.injectMembers" in content
60+
and not "GuiceInjectionExtension" in content
61+
):
62+
print("Converting ", file_name)
63+
content_new = migration_injection(content)
64+
with open(file_name, "w") as fn:
65+
fn.write(content_new)
66+
67+
68+
def main():
69+
if len(sys.argv) == 1:
70+
print("usage: {} <directory_to_migrate>".format(sys.argv[0]))
71+
sys.exit(1)
72+
73+
test_dir = sys.argv[1]
74+
migrate_tests(test_dir)
75+
76+
77+
if __name__ == "__main__":
78+
sys.exit(main())

scripts/testng-junit/src/testng2junit5.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,8 @@
5555

5656

5757
before_inject_template = ''' @BeforeAll
58-
@SuppressWarnings("EmptyCatchBlock")
5958
public void setup() {
60-
try {
61-
injector.injectMembers(this);
62-
} catch (RuntimeException e) {
63-
}
59+
injector.injectMembers(this);
6460
}
6561
'''
6662

@@ -118,6 +114,8 @@ def migrate_imports(content):
118114
# refer to migrate_guice_annotation
119115
if '@Guice' in content_new and '@BeforeAll' not in content_new:
120116
imports.append('import org.junit.jupiter.api.BeforeAll;')
117+
imports.append('import org.junit.jupiter.api.extension.ExtendWith;')
118+
imports.append('import com.addepar.infra.library.testing.extention.GuiceInjectionExtension;')
121119

122120
content_new = re.sub('org.junit.jupiter.api.Test;', '\n'.join(imports), content_new)
123121

@@ -387,12 +385,8 @@ def migrate_asserts(content):
387385
# private final Injector injector = Guice.createInjector(new SomeModule());
388386
#
389387
# @BeforeAll
390-
# @SuppressWarnings("EmptyCatchBlock")
391388
# public void someTest() {
392-
# try {
393-
# injector.injectMembers(this);
394-
# } catch (RuntimeException e) {
395-
# }
389+
# injector.injectMembers(this);
396390
# }
397391
# }
398392
#
@@ -423,6 +417,7 @@ def migrate_guice_annotation(content):
423417
or 'public abstract class' in line):
424418
left_spaces = ' ' * (len(line) - len(line.lstrip()))
425419
new_content.append(left_spaces + '@TestInstance(Lifecycle.PER_CLASS)')
420+
new_content.append(left_spaces + '@ExtendWith(GuiceInjectionExtension.class)')
426421
new_content.append(line)
427422

428423
if '{' in line:
@@ -439,18 +434,12 @@ def migrate_guice_annotation(content):
439434
# ....insert here....
440435
if '@BeforeAll' in line:
441436
new_content.append(line)
442-
new_content.append(' @SuppressWarnings("EmptyCatchBlock")')
443437
# this should be the line of the method and keep adding the line until we get {
444438
# insert injectMember as the first line below the below method.
445439
insert_lines_after_method(
446440
new_content,
447441
content_iter,
448-
[
449-
" try {",
450-
" injector.injectMembers(this);",
451-
" } catch (RuntimeException e) {",
452-
" }",
453-
],
442+
[" injector.injectMembers(this);"],
454443
)
455444
add_injected_member = True
456445
continue

scripts/testng-junit/src/tests/test_migrate_guice_annotation.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
expected_1 = """
1919
2020
@TestInstance(Lifecycle.PER_CLASS)
21+
@ExtendWith(GuiceInjectionExtension.class)
2122
public class SomeTest {
2223
2324
private final Injector injector = Guice.createInjector(new SomeModule());
2425
2526
@BeforeAll
26-
@SuppressWarnings("EmptyCatchBlock")
2727
public void someTest() {
28-
try {
29-
injector.injectMembers(this);
30-
} catch (RuntimeException e) {
31-
}
28+
injector.injectMembers(this);
3229
}
3330
}
3431
"""
@@ -49,17 +46,14 @@
4946
expected_2 = """
5047
5148
@TestInstance(Lifecycle.PER_CLASS)
49+
@ExtendWith(GuiceInjectionExtension.class)
5250
public class SomeTest {
5351
5452
private final Injector injector = Guice.createInjector();
5553
5654
@BeforeAll
57-
@SuppressWarnings("EmptyCatchBlock")
5855
public void someTest() {
59-
try {
60-
injector.injectMembers(this);
61-
} catch (RuntimeException e) {
62-
}
56+
injector.injectMembers(this);
6357
}
6458
}
6559
"""
@@ -81,17 +75,14 @@
8175
expected_3 = """
8276
8377
@TestInstance(Lifecycle.PER_CLASS)
78+
@ExtendWith(GuiceInjectionExtension.class)
8479
public class SomeTest {
8580
8681
private final Injector injector = Guice.createInjector(new TestModuleA(), new Test.ModuleB());
8782
8883
@BeforeAll
89-
@SuppressWarnings("EmptyCatchBlock")
9084
public void someTest() {
91-
try {
92-
injector.injectMembers(this);
93-
} catch (RuntimeException e) {
94-
}
85+
injector.injectMembers(this);
9586
}
9687
}
9788
"""

0 commit comments

Comments
 (0)