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