|
18 | 18 | */ |
19 | 19 | package org.apache.maven.plugins.dependency.analyze; |
20 | 20 |
|
| 21 | +import javax.inject.Inject; |
| 22 | + |
21 | 23 | import java.io.File; |
22 | | -import java.io.PrintWriter; |
23 | | -import java.io.StringWriter; |
24 | 24 |
|
25 | | -import org.apache.maven.execution.MavenSession; |
| 25 | +import org.apache.maven.api.di.Provides; |
| 26 | +import org.apache.maven.api.plugin.testing.Basedir; |
| 27 | +import org.apache.maven.api.plugin.testing.InjectMojo; |
| 28 | +import org.apache.maven.api.plugin.testing.MojoExtension; |
| 29 | +import org.apache.maven.api.plugin.testing.MojoTest; |
26 | 30 | import org.apache.maven.plugin.logging.Log; |
27 | | -import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase; |
28 | | -import org.apache.maven.plugins.dependency.testUtils.stubs.DuplicateDependencies2ProjectStub; |
29 | | -import org.apache.maven.plugins.dependency.testUtils.stubs.DuplicateDependenciesProjectStub; |
30 | 31 | import org.apache.maven.project.MavenProject; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 34 | +import org.mockito.ArgumentCaptor; |
| 35 | +import org.mockito.Mock; |
| 36 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 37 | + |
| 38 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | +import static org.mockito.Mockito.when; |
31 | 41 |
|
32 | 42 | /** |
33 | 43 | * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> |
34 | 44 | * @version $Id$ |
35 | 45 | */ |
36 | | -public class TestAnalyzeDuplicateMojo extends AbstractDependencyMojoTestCase { |
37 | | - public void testDuplicate() throws Exception { |
38 | | - MavenProject project = new DuplicateDependenciesProjectStub(); |
39 | | - getContainer().addComponent(project, MavenProject.class.getName()); |
40 | | - |
41 | | - MavenSession session = newMavenSession(project); |
42 | | - getContainer().addComponent(session, MavenSession.class.getName()); |
43 | | - |
44 | | - File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config.xml"); |
45 | | - AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom); |
46 | | - assertNotNull(mojo); |
47 | | - DuplicateLog log = new DuplicateLog(); |
48 | | - mojo.setLog(log); |
49 | | - mojo.execute(); |
| 46 | +@ExtendWith(MockitoExtension.class) |
| 47 | +@MojoTest |
| 48 | +public class TestAnalyzeDuplicateMojo { |
50 | 49 |
|
51 | | - assertTrue(log.getContent() |
52 | | - .contains("List of duplicate dependencies defined in <dependencies/> in " + "your pom.xml")); |
53 | | - assertTrue(log.getContent().contains("junit:junit:jar")); |
54 | | - } |
| 50 | + @Inject |
| 51 | + private MavenProject project; |
| 52 | + |
| 53 | + @Mock |
| 54 | + private Log log; |
55 | 55 |
|
56 | | - public void testDuplicate2() throws Exception { |
57 | | - MavenProject project = new DuplicateDependencies2ProjectStub(); |
58 | | - getContainer().addComponent(project, MavenProject.class.getName()); |
| 56 | + @Provides |
| 57 | + private Log logProvides() { |
| 58 | + return log; |
| 59 | + } |
59 | 60 |
|
60 | | - MavenSession session = newMavenSession(project); |
61 | | - getContainer().addComponent(session, MavenSession.class.getName()); |
| 61 | + @Test |
| 62 | + @Basedir("/unit/duplicate-dependencies") |
| 63 | + @InjectMojo(goal = "analyze-duplicate") |
| 64 | + public void testDuplicate(AnalyzeDuplicateMojo mojo) throws Exception { |
| 65 | + when(project.getFile()).thenReturn(new File(MojoExtension.getBasedir(), "plugin-config.xml")); |
| 66 | + when(log.isInfoEnabled()).thenReturn(true); |
62 | 67 |
|
63 | | - File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config2.xml"); |
64 | | - AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom); |
65 | | - assertNotNull(mojo); |
66 | | - DuplicateLog log = new DuplicateLog(); |
67 | | - mojo.setLog(log); |
68 | 68 | mojo.execute(); |
69 | 69 |
|
70 | | - assertTrue(log.getContent() |
71 | | - .contains("List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml")); |
72 | | - assertTrue(log.getContent().contains("junit:junit:jar")); |
| 70 | + ArgumentCaptor<String> logCapture = ArgumentCaptor.forClass(String.class); |
| 71 | + verify(log).info(logCapture.capture()); |
| 72 | + |
| 73 | + assertTrue(logCapture |
| 74 | + .getValue() |
| 75 | + .contains("List of duplicate dependencies defined in <dependencies/> in your pom.xml")); |
| 76 | + assertTrue(logCapture.getValue().contains("junit:junit:jar")); |
73 | 77 | } |
74 | 78 |
|
75 | | - static class DuplicateLog implements Log { |
76 | | - StringBuilder sb = new StringBuilder(); |
77 | | - |
78 | | - /** {@inheritDoc} */ |
79 | | - @Override |
80 | | - public void debug(CharSequence content) { |
81 | | - print("debug", content); |
82 | | - } |
83 | | - |
84 | | - /** {@inheritDoc} */ |
85 | | - @Override |
86 | | - public void debug(CharSequence content, Throwable error) { |
87 | | - print("debug", content, error); |
88 | | - } |
89 | | - |
90 | | - /** {@inheritDoc} */ |
91 | | - @Override |
92 | | - public void debug(Throwable error) { |
93 | | - print("debug", error); |
94 | | - } |
95 | | - |
96 | | - /** {@inheritDoc} */ |
97 | | - @Override |
98 | | - public void info(CharSequence content) { |
99 | | - print("info", content); |
100 | | - } |
101 | | - |
102 | | - /** {@inheritDoc} */ |
103 | | - @Override |
104 | | - public void info(CharSequence content, Throwable error) { |
105 | | - print("info", content, error); |
106 | | - } |
107 | | - |
108 | | - /** {@inheritDoc} */ |
109 | | - @Override |
110 | | - public void info(Throwable error) { |
111 | | - print("info", error); |
112 | | - } |
113 | | - |
114 | | - /** {@inheritDoc} */ |
115 | | - @Override |
116 | | - public void warn(CharSequence content) { |
117 | | - print("warn", content); |
118 | | - } |
119 | | - |
120 | | - /** {@inheritDoc} */ |
121 | | - @Override |
122 | | - public void warn(CharSequence content, Throwable error) { |
123 | | - print("warn", content, error); |
124 | | - } |
125 | | - |
126 | | - /** {@inheritDoc} */ |
127 | | - @Override |
128 | | - public void warn(Throwable error) { |
129 | | - print("warn", error); |
130 | | - } |
131 | | - |
132 | | - /** {@inheritDoc} */ |
133 | | - @Override |
134 | | - public void error(CharSequence content) { |
135 | | - System.err.println("[error] " + content.toString()); |
136 | | - } |
137 | | - |
138 | | - /** {@inheritDoc} */ |
139 | | - @Override |
140 | | - public void error(CharSequence content, Throwable error) { |
141 | | - StringWriter sWriter = new StringWriter(); |
142 | | - PrintWriter pWriter = new PrintWriter(sWriter); |
143 | | - |
144 | | - error.printStackTrace(pWriter); |
145 | | - |
146 | | - System.err.println( |
147 | | - "[error] " + content.toString() + System.lineSeparator() + System.lineSeparator() + sWriter); |
148 | | - } |
149 | | - |
150 | | - /** |
151 | | - * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable) |
152 | | - */ |
153 | | - @Override |
154 | | - public void error(Throwable error) { |
155 | | - StringWriter sWriter = new StringWriter(); |
156 | | - PrintWriter pWriter = new PrintWriter(sWriter); |
157 | | - |
158 | | - error.printStackTrace(pWriter); |
159 | | - |
160 | | - System.err.println("[error] " + sWriter); |
161 | | - } |
162 | | - |
163 | | - /** |
164 | | - * @see org.apache.maven.plugin.logging.Log#isDebugEnabled() |
165 | | - */ |
166 | | - @Override |
167 | | - public boolean isDebugEnabled() { |
168 | | - return false; |
169 | | - } |
170 | | - |
171 | | - /** |
172 | | - * @see org.apache.maven.plugin.logging.Log#isInfoEnabled() |
173 | | - */ |
174 | | - @Override |
175 | | - public boolean isInfoEnabled() { |
176 | | - return true; |
177 | | - } |
178 | | - |
179 | | - /** |
180 | | - * @see org.apache.maven.plugin.logging.Log#isWarnEnabled() |
181 | | - */ |
182 | | - @Override |
183 | | - public boolean isWarnEnabled() { |
184 | | - return true; |
185 | | - } |
186 | | - |
187 | | - /** |
188 | | - * @see org.apache.maven.plugin.logging.Log#isErrorEnabled() |
189 | | - */ |
190 | | - @Override |
191 | | - public boolean isErrorEnabled() { |
192 | | - return true; |
193 | | - } |
194 | | - |
195 | | - private void print(String prefix, CharSequence content) { |
196 | | - sb.append("[") |
197 | | - .append(prefix) |
198 | | - .append("] ") |
199 | | - .append(content.toString()) |
200 | | - .append(System.lineSeparator()); |
201 | | - } |
202 | | - |
203 | | - private void print(String prefix, Throwable error) { |
204 | | - StringWriter sWriter = new StringWriter(); |
205 | | - PrintWriter pWriter = new PrintWriter(sWriter); |
206 | | - |
207 | | - error.printStackTrace(pWriter); |
208 | | - |
209 | | - sb.append("[").append(prefix).append("] ").append(sWriter).append(System.lineSeparator()); |
210 | | - } |
211 | | - |
212 | | - private void print(String prefix, CharSequence content, Throwable error) { |
213 | | - StringWriter sWriter = new StringWriter(); |
214 | | - PrintWriter pWriter = new PrintWriter(sWriter); |
215 | | - |
216 | | - error.printStackTrace(pWriter); |
217 | | - |
218 | | - sb.append("[") |
219 | | - .append(prefix) |
220 | | - .append("] ") |
221 | | - .append(content.toString()) |
222 | | - .append(System.lineSeparator()) |
223 | | - .append(System.lineSeparator()); |
224 | | - sb.append(sWriter).append(System.lineSeparator()); |
225 | | - } |
226 | | - |
227 | | - protected String getContent() { |
228 | | - return sb.toString(); |
229 | | - } |
| 79 | + @Test |
| 80 | + @Basedir("/unit/duplicate-dependencies") |
| 81 | + @InjectMojo(goal = "analyze-duplicate") |
| 82 | + public void testDuplicate2(AnalyzeDuplicateMojo mojo) throws Exception { |
| 83 | + when(project.getFile()).thenReturn(new File(MojoExtension.getBasedir(), "plugin-config2.xml")); |
| 84 | + when(log.isInfoEnabled()).thenReturn(true); |
| 85 | + |
| 86 | + mojo.execute(); |
| 87 | + |
| 88 | + ArgumentCaptor<String> logCapture = ArgumentCaptor.forClass(String.class); |
| 89 | + verify(log).info(logCapture.capture()); |
| 90 | + |
| 91 | + assertTrue(logCapture |
| 92 | + .getValue() |
| 93 | + .contains("List of duplicate dependencies defined in <dependencyManagement/> in your pom.xml")); |
| 94 | + assertTrue(logCapture.getValue().contains("junit:junit:jar")); |
230 | 95 | } |
231 | 96 | } |
0 commit comments