1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.analyze;
20
21 import java.io.File;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24
25 import org.apache.maven.execution.MavenSession;
26 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 import org.apache.maven.project.MavenProject;
31
32
33
34
35
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();
50
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 }
55
56 public void testDuplicate2() throws Exception {
57 MavenProject project = new DuplicateDependencies2ProjectStub();
58 getContainer().addComponent(project, MavenProject.class.getName());
59
60 MavenSession session = newMavenSession(project);
61 getContainer().addComponent(session, MavenSession.class.getName());
62
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 mojo.execute();
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"));
73 }
74
75 static class DuplicateLog implements Log {
76 StringBuilder sb = new StringBuilder();
77
78
79 public void debug(CharSequence content) {
80 print("debug", content);
81 }
82
83
84 public void debug(CharSequence content, Throwable error) {
85 print("debug", content, error);
86 }
87
88
89 public void debug(Throwable error) {
90 print("debug", error);
91 }
92
93
94 public void info(CharSequence content) {
95 print("info", content);
96 }
97
98
99 public void info(CharSequence content, Throwable error) {
100 print("info", content, error);
101 }
102
103
104 public void info(Throwable error) {
105 print("info", error);
106 }
107
108
109 public void warn(CharSequence content) {
110 print("warn", content);
111 }
112
113
114 public void warn(CharSequence content, Throwable error) {
115 print("warn", content, error);
116 }
117
118
119 public void warn(Throwable error) {
120 print("warn", error);
121 }
122
123
124 public void error(CharSequence content) {
125 System.err.println("[error] " + content.toString());
126 }
127
128
129 public void error(CharSequence content, Throwable error) {
130 StringWriter sWriter = new StringWriter();
131 PrintWriter pWriter = new PrintWriter(sWriter);
132
133 error.printStackTrace(pWriter);
134
135 System.err.println(
136 "[error] " + content.toString() + System.lineSeparator() + System.lineSeparator() + sWriter);
137 }
138
139
140
141
142 public void error(Throwable error) {
143 StringWriter sWriter = new StringWriter();
144 PrintWriter pWriter = new PrintWriter(sWriter);
145
146 error.printStackTrace(pWriter);
147
148 System.err.println("[error] " + sWriter);
149 }
150
151
152
153
154 public boolean isDebugEnabled() {
155
156 return false;
157 }
158
159
160
161
162 public boolean isInfoEnabled() {
163 return true;
164 }
165
166
167
168
169 public boolean isWarnEnabled() {
170 return true;
171 }
172
173
174
175
176 public boolean isErrorEnabled() {
177 return true;
178 }
179
180 private void print(String prefix, CharSequence content) {
181 sb.append("[")
182 .append(prefix)
183 .append("] ")
184 .append(content.toString())
185 .append(System.lineSeparator());
186 }
187
188 private void print(String prefix, Throwable error) {
189 StringWriter sWriter = new StringWriter();
190 PrintWriter pWriter = new PrintWriter(sWriter);
191
192 error.printStackTrace(pWriter);
193
194 sb.append("[").append(prefix).append("] ").append(sWriter).append(System.lineSeparator());
195 }
196
197 private void print(String prefix, CharSequence content, Throwable error) {
198 StringWriter sWriter = new StringWriter();
199 PrintWriter pWriter = new PrintWriter(sWriter);
200
201 error.printStackTrace(pWriter);
202
203 sb.append("[")
204 .append(prefix)
205 .append("] ")
206 .append(content.toString())
207 .append(System.lineSeparator())
208 .append(System.lineSeparator());
209 sb.append(sWriter).append(System.lineSeparator());
210 }
211
212 protected String getContent() {
213 return sb.toString();
214 }
215 }
216 }