1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.api.di.testing;
20
21 import java.io.*;
22
23 import org.apache.maven.di.Injector;
24 import org.apache.maven.di.Key;
25 import org.apache.maven.di.impl.DIException;
26 import org.junit.jupiter.api.extension.AfterEachCallback;
27 import org.junit.jupiter.api.extension.BeforeEachCallback;
28 import org.junit.jupiter.api.extension.ExtensionContext;
29
30
31
32
33
34
35
36
37
38
39
40
41 public class MavenDIExtension implements BeforeEachCallback, AfterEachCallback {
42 protected static ExtensionContext context;
43 protected Injector injector;
44 protected static String basedir;
45
46 @Override
47 public void beforeEach(ExtensionContext context) throws Exception {
48 basedir = getBasedir();
49
50 setContext(context);
51
52 getInjector().bindInstance((Class<Object>) context.getRequiredTestClass(), context.getRequiredTestInstance());
53 getInjector().injectInstance(context.getRequiredTestInstance());
54 }
55
56 protected void setContext(ExtensionContext context) {
57 this.context = context;
58 }
59
60 @SuppressWarnings("unchecked")
61 protected void setupContainer() {
62 try {
63 injector = Injector.create();
64 injector.bindInstance(ExtensionContext.class, this.context);
65 injector.discover(this.context.getRequiredTestClass().getClassLoader());
66 injector.bindInstance(Injector.class, injector);
67 injector.bindInstance((Class) this.context.getRequiredTestClass(), this.context.getRequiredTestInstance());
68 } catch (Exception e) {
69 throw new IllegalArgumentException("Failed to create DI injector.", e);
70 }
71 }
72
73 @Override
74 public void afterEach(ExtensionContext context) throws Exception {
75 if (injector != null) {
76
77
78 injector = null;
79 }
80 }
81
82 public Injector getInjector() {
83 if (injector == null) {
84 setupContainer();
85 }
86
87 return injector;
88 }
89
90
91
92
93
94 protected <T> T lookup(Class<T> componentClass) throws DIException {
95 return getInjector().getInstance(componentClass);
96 }
97
98 protected <T> T lookup(Class<T> componentClass, String roleHint) throws DIException {
99 return getInjector().getInstance(Key.ofType(componentClass, roleHint));
100 }
101
102 protected <T> T lookup(Class<T> componentClass, Object qualifier) throws DIException {
103 return getInjector().getInstance(Key.ofType(componentClass, qualifier));
104 }
105
106 protected void release(Object component) throws DIException {
107
108
109 }
110
111
112
113
114
115 public static File getTestFile(String path) {
116 return new File(getBasedir(), path);
117 }
118
119 public static File getTestFile(String basedir, String path) {
120 File basedirFile = new File(basedir);
121
122 if (!basedirFile.isAbsolute()) {
123 basedirFile = getTestFile(basedir);
124 }
125
126 return new File(basedirFile, path);
127 }
128
129 public static String getTestPath(String path) {
130 return getTestFile(path).getAbsolutePath();
131 }
132
133 public static String getTestPath(String basedir, String path) {
134 return getTestFile(basedir, path).getAbsolutePath();
135 }
136
137 public static String getBasedir() {
138 if (basedir != null) {
139 return basedir;
140 }
141
142 basedir = System.getProperty("basedir");
143
144 if (basedir == null) {
145 basedir = new File("").getAbsolutePath();
146 }
147
148 return basedir;
149 }
150 }