1 package org.apache.maven.plugin.dependency;
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.logging.Log;
27
28
29
30
31
32 public class TestAnalyzeDuplicateMojo
33 extends AbstractDependencyMojoTestCase
34 {
35 public void testDuplicate()
36 throws Exception
37 {
38 File testPom =
39 new File( getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config.xml" );
40 AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo( "analyze-duplicate", testPom );
41 assertNotNull( mojo );
42 DuplicateLog log = new DuplicateLog();
43 mojo.setLog( log );
44 mojo.execute();
45
46 assertTrue( log.getContent().indexOf(
47 "List of duplicate dependencies defined in <dependencies/> in "
48 + "your pom.xml" ) != -1 );
49 assertTrue( log.getContent().indexOf( "junit:junit:jar" ) != -1 );
50 }
51
52 public void testDuplicate2()
53 throws Exception
54 {
55 File testPom =
56 new File( getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config2.xml" );
57 AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo( "analyze-duplicate", testPom );
58 assertNotNull( mojo );
59 DuplicateLog log = new DuplicateLog();
60 mojo.setLog( log );
61 mojo.execute();
62
63 assertTrue( log.getContent().indexOf(
64 "List of duplicate dependencies defined in <dependencyManagement/> in "
65 + "your pom.xml" ) != -1 );
66 assertTrue( log.getContent().indexOf( "junit:junit:jar" ) != -1 );
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( "[" + prefix + "] " ).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( "[" + prefix + "] " ).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( "[" + prefix + "] " ).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 }