View Javadoc

1   package org.apache.maven.plugin.dependency;
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.logging.Log;
27  
28  /**
29   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
30   * @version $Id: TestAnalyzeDuplicateMojo.java 1032021 2010-11-06 12:21:39Z bentmann $
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          StringBuffer sb = new StringBuffer();
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( "[" + 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 }