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