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          @Override
80          public void debug(CharSequence content) {
81              print("debug", content);
82          }
83  
84          /** {@inheritDoc} */
85          @Override
86          public void debug(CharSequence content, Throwable error) {
87              print("debug", content, error);
88          }
89  
90          /** {@inheritDoc} */
91          @Override
92          public void debug(Throwable error) {
93              print("debug", error);
94          }
95  
96          /** {@inheritDoc} */
97          @Override
98          public void info(CharSequence content) {
99              print("info", content);
100         }
101 
102         /** {@inheritDoc} */
103         @Override
104         public void info(CharSequence content, Throwable error) {
105             print("info", content, error);
106         }
107 
108         /** {@inheritDoc} */
109         @Override
110         public void info(Throwable error) {
111             print("info", error);
112         }
113 
114         /** {@inheritDoc} */
115         @Override
116         public void warn(CharSequence content) {
117             print("warn", content);
118         }
119 
120         /** {@inheritDoc} */
121         @Override
122         public void warn(CharSequence content, Throwable error) {
123             print("warn", content, error);
124         }
125 
126         /** {@inheritDoc} */
127         @Override
128         public void warn(Throwable error) {
129             print("warn", error);
130         }
131 
132         /** {@inheritDoc} */
133         @Override
134         public void error(CharSequence content) {
135             System.err.println("[error] " + content.toString());
136         }
137 
138         /** {@inheritDoc} */
139         @Override
140         public void error(CharSequence content, Throwable error) {
141             StringWriter sWriter = new StringWriter();
142             PrintWriter pWriter = new PrintWriter(sWriter);
143 
144             error.printStackTrace(pWriter);
145 
146             System.err.println(
147                     "[error] " + content.toString() + System.lineSeparator() + System.lineSeparator() + sWriter);
148         }
149 
150         /**
151          * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable)
152          */
153         @Override
154         public void error(Throwable error) {
155             StringWriter sWriter = new StringWriter();
156             PrintWriter pWriter = new PrintWriter(sWriter);
157 
158             error.printStackTrace(pWriter);
159 
160             System.err.println("[error] " + sWriter);
161         }
162 
163         /**
164          * @see org.apache.maven.plugin.logging.Log#isDebugEnabled()
165          */
166         @Override
167         public boolean isDebugEnabled() {
168             return false;
169         }
170 
171         /**
172          * @see org.apache.maven.plugin.logging.Log#isInfoEnabled()
173          */
174         @Override
175         public boolean isInfoEnabled() {
176             return true;
177         }
178 
179         /**
180          * @see org.apache.maven.plugin.logging.Log#isWarnEnabled()
181          */
182         @Override
183         public boolean isWarnEnabled() {
184             return true;
185         }
186 
187         /**
188          * @see org.apache.maven.plugin.logging.Log#isErrorEnabled()
189          */
190         @Override
191         public boolean isErrorEnabled() {
192             return true;
193         }
194 
195         private void print(String prefix, CharSequence content) {
196             sb.append("[")
197                     .append(prefix)
198                     .append("] ")
199                     .append(content.toString())
200                     .append(System.lineSeparator());
201         }
202 
203         private void print(String prefix, Throwable error) {
204             StringWriter sWriter = new StringWriter();
205             PrintWriter pWriter = new PrintWriter(sWriter);
206 
207             error.printStackTrace(pWriter);
208 
209             sb.append("[").append(prefix).append("] ").append(sWriter).append(System.lineSeparator());
210         }
211 
212         private void print(String prefix, CharSequence content, Throwable error) {
213             StringWriter sWriter = new StringWriter();
214             PrintWriter pWriter = new PrintWriter(sWriter);
215 
216             error.printStackTrace(pWriter);
217 
218             sb.append("[")
219                     .append(prefix)
220                     .append("] ")
221                     .append(content.toString())
222                     .append(System.lineSeparator())
223                     .append(System.lineSeparator());
224             sb.append(sWriter).append(System.lineSeparator());
225         }
226 
227         protected String getContent() {
228             return sb.toString();
229         }
230     }
231 }