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.plugin.logging.Log;
28  
29  /**
30   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
31   * @version $Id$
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          /** {@inheritDoc} */
72          public void debug( CharSequence content )
73          {
74              print( "debug", content );
75          }
76  
77          /** {@inheritDoc} */
78          public void debug( CharSequence content, Throwable error )
79          {
80              print( "debug", content, error );
81          }
82  
83          /** {@inheritDoc} */
84          public void debug( Throwable error )
85          {
86              print( "debug", error );
87          }
88  
89          /** {@inheritDoc} */
90          public void info( CharSequence content )
91          {
92              print( "info", content );
93          }
94  
95          /** {@inheritDoc} */
96          public void info( CharSequence content, Throwable error )
97          {
98              print( "info", content, error );
99          }
100 
101         /** {@inheritDoc} */
102         public void info( Throwable error )
103         {
104             print( "info", error );
105         }
106 
107         /** {@inheritDoc} */
108         public void warn( CharSequence content )
109         {
110             print( "warn", content );
111         }
112 
113         /** {@inheritDoc} */
114         public void warn( CharSequence content, Throwable error )
115         {
116             print( "warn", content, error );
117         }
118 
119         /** {@inheritDoc} */
120         public void warn( Throwable error )
121         {
122             print( "warn", error );
123         }
124 
125         /** {@inheritDoc} */
126         public void error( CharSequence content )
127         {
128             System.err.println( "[error] " + content.toString() );
129         }
130 
131         /** {@inheritDoc} */
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          * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable)
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          * @see org.apache.maven.plugin.logging.Log#isDebugEnabled()
157          */
158         public boolean isDebugEnabled()
159         {
160             // TODO: Not sure how best to set these for this implementation...
161             return false;
162         }
163 
164         /**
165          * @see org.apache.maven.plugin.logging.Log#isInfoEnabled()
166          */
167         public boolean isInfoEnabled()
168         {
169             return true;
170         }
171 
172         /**
173          * @see org.apache.maven.plugin.logging.Log#isWarnEnabled()
174          */
175         public boolean isWarnEnabled()
176         {
177             return true;
178         }
179 
180         /**
181          * @see org.apache.maven.plugin.logging.Log#isErrorEnabled()
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 }