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