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.execution.MavenSession;
26  import org.apache.maven.plugin.logging.Log;
27  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
28  import org.apache.maven.plugins.dependency.testUtils.stubs.DuplicateDependencies2ProjectStub;
29  import org.apache.maven.plugins.dependency.testUtils.stubs.DuplicateDependenciesProjectStub;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   * @version $Id$
35   */
36  public class TestAnalyzeDuplicateMojo extends AbstractDependencyMojoTestCase {
37      public void testDuplicate() throws Exception {
38          MavenProject project = new DuplicateDependenciesProjectStub();
39          getContainer().addComponent(project, MavenProject.class.getName());
40  
41          MavenSession session = newMavenSession(project);
42          getContainer().addComponent(session, MavenSession.class.getName());
43  
44          File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config.xml");
45          AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom);
46          assertNotNull(mojo);
47          DuplicateLog log = new DuplicateLog();
48          mojo.setLog(log);
49          mojo.execute();
50  
51          assertTrue(log.getContent()
52                  .contains("List of duplicate dependencies defined in <dependencies/> in " + "your pom.xml"));
53          assertTrue(log.getContent().contains("junit:junit:jar"));
54      }
55  
56      public void testDuplicate2() throws Exception {
57          MavenProject project = new DuplicateDependencies2ProjectStub();
58          getContainer().addComponent(project, MavenProject.class.getName());
59  
60          MavenSession session = newMavenSession(project);
61          getContainer().addComponent(session, MavenSession.class.getName());
62  
63          File testPom = new File(getBasedir(), "target/test-classes/unit/duplicate-dependencies/plugin-config2.xml");
64          AnalyzeDuplicateMojo mojo = (AnalyzeDuplicateMojo) lookupMojo("analyze-duplicate", testPom);
65          assertNotNull(mojo);
66          DuplicateLog log = new DuplicateLog();
67          mojo.setLog(log);
68          mojo.execute();
69  
70          assertTrue(log.getContent()
71                  .contains("List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml"));
72          assertTrue(log.getContent().contains("junit:junit:jar"));
73      }
74  
75      static class DuplicateLog implements Log {
76          StringBuilder sb = new StringBuilder();
77  
78          /** {@inheritDoc} */
79          public void debug(CharSequence content) {
80              print("debug", content);
81          }
82  
83          /** {@inheritDoc} */
84          public void debug(CharSequence content, Throwable error) {
85              print("debug", content, error);
86          }
87  
88          /** {@inheritDoc} */
89          public void debug(Throwable error) {
90              print("debug", error);
91          }
92  
93          /** {@inheritDoc} */
94          public void info(CharSequence content) {
95              print("info", content);
96          }
97  
98          /** {@inheritDoc} */
99          public void info(CharSequence content, Throwable error) {
100             print("info", content, error);
101         }
102 
103         /** {@inheritDoc} */
104         public void info(Throwable error) {
105             print("info", error);
106         }
107 
108         /** {@inheritDoc} */
109         public void warn(CharSequence content) {
110             print("warn", content);
111         }
112 
113         /** {@inheritDoc} */
114         public void warn(CharSequence content, Throwable error) {
115             print("warn", content, error);
116         }
117 
118         /** {@inheritDoc} */
119         public void warn(Throwable error) {
120             print("warn", error);
121         }
122 
123         /** {@inheritDoc} */
124         public void error(CharSequence content) {
125             System.err.println("[error] " + content.toString());
126         }
127 
128         /** {@inheritDoc} */
129         public void error(CharSequence content, Throwable error) {
130             StringWriter sWriter = new StringWriter();
131             PrintWriter pWriter = new PrintWriter(sWriter);
132 
133             error.printStackTrace(pWriter);
134 
135             System.err.println(
136                     "[error] " + content.toString() + System.lineSeparator() + System.lineSeparator() + sWriter);
137         }
138 
139         /**
140          * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable)
141          */
142         public void error(Throwable error) {
143             StringWriter sWriter = new StringWriter();
144             PrintWriter pWriter = new PrintWriter(sWriter);
145 
146             error.printStackTrace(pWriter);
147 
148             System.err.println("[error] " + sWriter);
149         }
150 
151         /**
152          * @see org.apache.maven.plugin.logging.Log#isDebugEnabled()
153          */
154         public boolean isDebugEnabled() {
155             // TODO: Not sure how best to set these for this implementation...
156             return false;
157         }
158 
159         /**
160          * @see org.apache.maven.plugin.logging.Log#isInfoEnabled()
161          */
162         public boolean isInfoEnabled() {
163             return true;
164         }
165 
166         /**
167          * @see org.apache.maven.plugin.logging.Log#isWarnEnabled()
168          */
169         public boolean isWarnEnabled() {
170             return true;
171         }
172 
173         /**
174          * @see org.apache.maven.plugin.logging.Log#isErrorEnabled()
175          */
176         public boolean isErrorEnabled() {
177             return true;
178         }
179 
180         private void print(String prefix, CharSequence content) {
181             sb.append("[")
182                     .append(prefix)
183                     .append("] ")
184                     .append(content.toString())
185                     .append(System.lineSeparator());
186         }
187 
188         private void print(String prefix, Throwable error) {
189             StringWriter sWriter = new StringWriter();
190             PrintWriter pWriter = new PrintWriter(sWriter);
191 
192             error.printStackTrace(pWriter);
193 
194             sb.append("[").append(prefix).append("] ").append(sWriter).append(System.lineSeparator());
195         }
196 
197         private void print(String prefix, CharSequence content, Throwable error) {
198             StringWriter sWriter = new StringWriter();
199             PrintWriter pWriter = new PrintWriter(sWriter);
200 
201             error.printStackTrace(pWriter);
202 
203             sb.append("[")
204                     .append(prefix)
205                     .append("] ")
206                     .append(content.toString())
207                     .append(System.lineSeparator())
208                     .append(System.lineSeparator());
209             sb.append(sWriter).append(System.lineSeparator());
210         }
211 
212         protected String getContent() {
213             return sb.toString();
214         }
215     }
216 }