View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.analyze;
20  
21  import java.io.File;
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  
25  import org.apache.maven.plugin.logging.Log;
26  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
27  
28  /**
29   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
30   * @version $Id$
31   */
32  public class TestAnalyzeDuplicateMojo extends AbstractDependencyMojoTestCase {
33      public void testDuplicate() throws Exception {
34          File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config.xml");
35          AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom);
36          assertNotNull(mojo);
37          DuplicateLog log = new DuplicateLog();
38          mojo.setLog(log);
39          mojo.execute();
40  
41          assertTrue(log.getContent()
42                  .contains("List of duplicate dependencies defined in <dependencies/> in " + "your pom.xml"));
43          assertTrue(log.getContent().contains("junit:junit:jar"));
44      }
45  
46      public void testDuplicate2() throws Exception {
47          File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config2.xml");
48          AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom);
49          assertNotNull(mojo);
50          DuplicateLog log = new DuplicateLog();
51          mojo.setLog(log);
52          mojo.execute();
53  
54          assertTrue(log.getContent()
55                  .contains("List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml"));
56          assertTrue(log.getContent().contains("junit:junit:jar"));
57      }
58  
59      class DuplicateLog implements Log {
60          StringBuilder sb = new StringBuilder();
61  
62          /** {@inheritDoc} */
63          public void debug(CharSequence content) {
64              print("debug", content);
65          }
66  
67          /** {@inheritDoc} */
68          public void debug(CharSequence content, Throwable error) {
69              print("debug", content, error);
70          }
71  
72          /** {@inheritDoc} */
73          public void debug(Throwable error) {
74              print("debug", error);
75          }
76  
77          /** {@inheritDoc} */
78          public void info(CharSequence content) {
79              print("info", content);
80          }
81  
82          /** {@inheritDoc} */
83          public void info(CharSequence content, Throwable error) {
84              print("info", content, error);
85          }
86  
87          /** {@inheritDoc} */
88          public void info(Throwable error) {
89              print("info", error);
90          }
91  
92          /** {@inheritDoc} */
93          public void warn(CharSequence content) {
94              print("warn", content);
95          }
96  
97          /** {@inheritDoc} */
98          public void warn(CharSequence content, Throwable error) {
99              print("warn", content, error);
100         }
101 
102         /** {@inheritDoc} */
103         public void warn(Throwable error) {
104             print("warn", error);
105         }
106 
107         /** {@inheritDoc} */
108         public void error(CharSequence content) {
109             System.err.println("[error] " + content.toString());
110         }
111 
112         /** {@inheritDoc} */
113         public void error(CharSequence content, Throwable error) {
114             StringWriter sWriter = new StringWriter();
115             PrintWriter pWriter = new PrintWriter(sWriter);
116 
117             error.printStackTrace(pWriter);
118 
119             System.err.println("[error] " + content.toString() + System.lineSeparator() + System.lineSeparator()
120                     + sWriter.toString());
121         }
122 
123         /**
124          * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable)
125          */
126         public void error(Throwable error) {
127             StringWriter sWriter = new StringWriter();
128             PrintWriter pWriter = new PrintWriter(sWriter);
129 
130             error.printStackTrace(pWriter);
131 
132             System.err.println("[error] " + sWriter.toString());
133         }
134 
135         /**
136          * @see org.apache.maven.plugin.logging.Log#isDebugEnabled()
137          */
138         public boolean isDebugEnabled() {
139             // TODO: Not sure how best to set these for this implementation...
140             return false;
141         }
142 
143         /**
144          * @see org.apache.maven.plugin.logging.Log#isInfoEnabled()
145          */
146         public boolean isInfoEnabled() {
147             return true;
148         }
149 
150         /**
151          * @see org.apache.maven.plugin.logging.Log#isWarnEnabled()
152          */
153         public boolean isWarnEnabled() {
154             return true;
155         }
156 
157         /**
158          * @see org.apache.maven.plugin.logging.Log#isErrorEnabled()
159          */
160         public boolean isErrorEnabled() {
161             return true;
162         }
163 
164         private void print(String prefix, CharSequence content) {
165             sb.append("[")
166                     .append(prefix)
167                     .append("] ")
168                     .append(content.toString())
169                     .append(System.lineSeparator());
170         }
171 
172         private void print(String prefix, Throwable error) {
173             StringWriter sWriter = new StringWriter();
174             PrintWriter pWriter = new PrintWriter(sWriter);
175 
176             error.printStackTrace(pWriter);
177 
178             sb.append("[")
179                     .append(prefix)
180                     .append("] ")
181                     .append(sWriter.toString())
182                     .append(System.lineSeparator());
183         }
184 
185         private void print(String prefix, CharSequence content, Throwable error) {
186             StringWriter sWriter = new StringWriter();
187             PrintWriter pWriter = new PrintWriter(sWriter);
188 
189             error.printStackTrace(pWriter);
190 
191             sb.append("[")
192                     .append(prefix)
193                     .append("] ")
194                     .append(content.toString())
195                     .append(System.lineSeparator())
196                     .append(System.lineSeparator());
197             sb.append(sWriter.toString()).append(System.lineSeparator());
198         }
199 
200         protected String getContent() {
201             return sb.toString();
202         }
203     }
204 }