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