View Javadoc
1   package org.apache.maven.plugin.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.plugin.dependency.AbstractDependencyMojoTestCase;
27  import org.apache.maven.plugin.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: TestAnalyzeDuplicateMojo.html 937155 2015-01-21 21:53:50Z khmarbaise $
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().contains("List of duplicate dependencies defined in <dependencies/> in "
49                  + "your pom.xml"));
50          assertTrue(log.getContent().contains("junit:junit:jar"));
51      }
52  
53      public void testDuplicate2()
54          throws Exception
55      {
56          File testPom =
57              new File( getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config2.xml" );
58          AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo( "analyze-duplicate", testPom );
59          assertNotNull( mojo );
60          DuplicateLog log = new DuplicateLog();
61          mojo.setLog( log );
62          mojo.execute();
63  
64          assertTrue(log.getContent().contains("List of duplicate dependencies defined in <dependencyManagement/> in "
65                  + "your pom.xml"));
66          assertTrue(log.getContent().contains("junit:junit:jar"));
67      }
68  
69      class DuplicateLog
70          implements Log
71      {
72          StringBuilder sb = new StringBuilder();
73  
74          /** {@inheritDoc} */
75          public void debug( CharSequence content )
76          {
77              print( "debug", content );
78          }
79  
80          /** {@inheritDoc} */
81          public void debug( CharSequence content, Throwable error )
82          {
83              print( "debug", content, error );
84          }
85  
86          /** {@inheritDoc} */
87          public void debug( Throwable error )
88          {
89              print( "debug", error );
90          }
91  
92          /** {@inheritDoc} */
93          public void info( CharSequence content )
94          {
95              print( "info", content );
96          }
97  
98          /** {@inheritDoc} */
99          public void info( CharSequence content, Throwable error )
100         {
101             print( "info", content, error );
102         }
103 
104         /** {@inheritDoc} */
105         public void info( Throwable error )
106         {
107             print( "info", error );
108         }
109 
110         /** {@inheritDoc} */
111         public void warn( CharSequence content )
112         {
113             print( "warn", content );
114         }
115 
116         /** {@inheritDoc} */
117         public void warn( CharSequence content, Throwable error )
118         {
119             print( "warn", content, error );
120         }
121 
122         /** {@inheritDoc} */
123         public void warn( Throwable error )
124         {
125             print( "warn", error );
126         }
127 
128         /** {@inheritDoc} */
129         public void error( CharSequence content )
130         {
131             System.err.println( "[error] " + content.toString() );
132         }
133 
134         /** {@inheritDoc} */
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          * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable)
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          * @see org.apache.maven.plugin.logging.Log#isDebugEnabled()
160          */
161         public boolean isDebugEnabled()
162         {
163             // TODO: Not sure how best to set these for this implementation...
164             return false;
165         }
166 
167         /**
168          * @see org.apache.maven.plugin.logging.Log#isInfoEnabled()
169          */
170         public boolean isInfoEnabled()
171         {
172             return true;
173         }
174 
175         /**
176          * @see org.apache.maven.plugin.logging.Log#isWarnEnabled()
177          */
178         public boolean isWarnEnabled()
179         {
180             return true;
181         }
182 
183         /**
184          * @see org.apache.maven.plugin.logging.Log#isErrorEnabled()
185          */
186         public boolean isErrorEnabled()
187         {
188             return true;
189         }
190 
191         private void print( String prefix, CharSequence content )
192         {
193             sb.append("[").append(prefix).append("] ").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("[").append(prefix).append("] ").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("[").append(prefix).append("] ").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 }