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