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