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.plugin.logging.Log;
26 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
27
28
29
30
31
32 public class TestAnalyzeDuplicateMojo extends AbstractDependencyMojoTestCase {
33 public void testDuplicate() throws Exception {
34 File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config.xml");
35 AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom);
36 assertNotNull(mojo);
37 DuplicateLog log = new DuplicateLog();
38 mojo.setLog(log);
39 mojo.execute();
40
41 assertTrue(log.getContent()
42 .contains("List of duplicate dependencies defined in <dependencies/> in " + "your pom.xml"));
43 assertTrue(log.getContent().contains("junit:junit:jar"));
44 }
45
46 public void testDuplicate2() throws Exception {
47 File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config2.xml");
48 AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom);
49 assertNotNull(mojo);
50 DuplicateLog log = new DuplicateLog();
51 mojo.setLog(log);
52 mojo.execute();
53
54 assertTrue(log.getContent()
55 .contains("List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml"));
56 assertTrue(log.getContent().contains("junit:junit:jar"));
57 }
58
59 class DuplicateLog implements Log {
60 StringBuilder sb = new StringBuilder();
61
62
63 public void debug(CharSequence content) {
64 print("debug", content);
65 }
66
67
68 public void debug(CharSequence content, Throwable error) {
69 print("debug", content, error);
70 }
71
72
73 public void debug(Throwable error) {
74 print("debug", error);
75 }
76
77
78 public void info(CharSequence content) {
79 print("info", content);
80 }
81
82
83 public void info(CharSequence content, Throwable error) {
84 print("info", content, error);
85 }
86
87
88 public void info(Throwable error) {
89 print("info", error);
90 }
91
92
93 public void warn(CharSequence content) {
94 print("warn", content);
95 }
96
97
98 public void warn(CharSequence content, Throwable error) {
99 print("warn", content, error);
100 }
101
102
103 public void warn(Throwable error) {
104 print("warn", error);
105 }
106
107
108 public void error(CharSequence content) {
109 System.err.println("[error] " + content.toString());
110 }
111
112
113 public void error(CharSequence content, Throwable error) {
114 StringWriter sWriter = new StringWriter();
115 PrintWriter pWriter = new PrintWriter(sWriter);
116
117 error.printStackTrace(pWriter);
118
119 System.err.println("[error] " + content.toString() + System.lineSeparator() + System.lineSeparator()
120 + sWriter.toString());
121 }
122
123
124
125
126 public void error(Throwable error) {
127 StringWriter sWriter = new StringWriter();
128 PrintWriter pWriter = new PrintWriter(sWriter);
129
130 error.printStackTrace(pWriter);
131
132 System.err.println("[error] " + sWriter.toString());
133 }
134
135
136
137
138 public boolean isDebugEnabled() {
139
140 return false;
141 }
142
143
144
145
146 public boolean isInfoEnabled() {
147 return true;
148 }
149
150
151
152
153 public boolean isWarnEnabled() {
154 return true;
155 }
156
157
158
159
160 public boolean isErrorEnabled() {
161 return true;
162 }
163
164 private void print(String prefix, CharSequence content) {
165 sb.append("[")
166 .append(prefix)
167 .append("] ")
168 .append(content.toString())
169 .append(System.lineSeparator());
170 }
171
172 private void print(String prefix, Throwable error) {
173 StringWriter sWriter = new StringWriter();
174 PrintWriter pWriter = new PrintWriter(sWriter);
175
176 error.printStackTrace(pWriter);
177
178 sb.append("[")
179 .append(prefix)
180 .append("] ")
181 .append(sWriter.toString())
182 .append(System.lineSeparator());
183 }
184
185 private void print(String prefix, CharSequence content, Throwable error) {
186 StringWriter sWriter = new StringWriter();
187 PrintWriter pWriter = new PrintWriter(sWriter);
188
189 error.printStackTrace(pWriter);
190
191 sb.append("[")
192 .append(prefix)
193 .append("] ")
194 .append(content.toString())
195 .append(System.lineSeparator())
196 .append(System.lineSeparator());
197 sb.append(sWriter.toString()).append(System.lineSeparator());
198 }
199
200 protected String getContent() {
201 return sb.toString();
202 }
203 }
204 }