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