View Javadoc
1   package org.apache.maven.plugins.dependency.analyze;
2   
3   /* 
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
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   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
32   * @version $Id$
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          /** {@inheritDoc} */
73          public void debug( CharSequence content )
74          {
75              print( "debug", content );
76          }
77  
78          /** {@inheritDoc} */
79          public void debug( CharSequence content, Throwable error )
80          {
81              print( "debug", content, error );
82          }
83  
84          /** {@inheritDoc} */
85          public void debug( Throwable error )
86          {
87              print( "debug", error );
88          }
89  
90          /** {@inheritDoc} */
91          public void info( CharSequence content )
92          {
93              print( "info", content );
94          }
95  
96          /** {@inheritDoc} */
97          public void info( CharSequence content, Throwable error )
98          {
99              print( "info", content, error );
100         }
101 
102         /** {@inheritDoc} */
103         public void info( Throwable error )
104         {
105             print( "info", error );
106         }
107 
108         /** {@inheritDoc} */
109         public void warn( CharSequence content )
110         {
111             print( "warn", content );
112         }
113 
114         /** {@inheritDoc} */
115         public void warn( CharSequence content, Throwable error )
116         {
117             print( "warn", content, error );
118         }
119 
120         /** {@inheritDoc} */
121         public void warn( Throwable error )
122         {
123             print( "warn", error );
124         }
125 
126         /** {@inheritDoc} */
127         public void error( CharSequence content )
128         {
129             System.err.println( "[error] " + content.toString() );
130         }
131 
132         /** {@inheritDoc} */
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          * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable)
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          * @see org.apache.maven.plugin.logging.Log#isDebugEnabled()
158          */
159         public boolean isDebugEnabled()
160         {
161             // TODO: Not sure how best to set these for this implementation...
162             return false;
163         }
164 
165         /**
166          * @see org.apache.maven.plugin.logging.Log#isInfoEnabled()
167          */
168         public boolean isInfoEnabled()
169         {
170             return true;
171         }
172 
173         /**
174          * @see org.apache.maven.plugin.logging.Log#isWarnEnabled()
175          */
176         public boolean isWarnEnabled()
177         {
178             return true;
179         }
180 
181         /**
182          * @see org.apache.maven.plugin.logging.Log#isErrorEnabled()
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 }