View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.jdepend;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  
24  import jdepend.framework.JDepend;
25  import jdepend.framework.JavaPackage;
26  import junit.framework.TestCase;
27  
28  /**
29   * @author Rob Oxspring
30   */
31  public class JDependTest extends TestCase {
32  
33      private JDepend dependancies = null;
34  
35      public void setUp() throws IOException {
36          dependancies = new JDepend();
37          dependancies.addDirectory("target/classes");
38          dependancies.analyze();
39      }
40  
41      public void testJUnitNotPresent() {
42          // if junit dependancy is found then jdepend has been poluted
43          // with test classes and all tests are meaningless
44          assertNull(
45              "JUnit dependancy found",
46              dependancies.getPackage("junit.framework"));
47  
48          // the same applies to jdepend
49          assertNull(
50              "JDepend dependancy found",
51              dependancies.getPackage("jdepend.framework"));
52      }
53  
54      public void testAcceptableDistance() {
55          Collection packages = dependancies.getPackages();
56          // only interested in cli2
57          packages = cli2Packages(packages);
58          // resources is well off the line
59          packages =
60              namedPackages(packages, "org.apache.commons.cli2.resource", false);
61  
62          for (final Iterator i = packages.iterator(); i.hasNext();) {
63              final JavaPackage pkg = (JavaPackage)i.next();
64              final float distance = pkg.distance();
65              final String message = pkg.getName() + " too far from line: " + distance;
66              assertTrue(
67                  message,
68                  distance < 0.21d);
69          }
70      }
71  
72      public void testNoCyclesPresent() {
73          assertEquals("Cycles exist", false, dependancies.containsCycles());
74      }
75  
76      public void testApiIndependance() {
77          dependancies.analyze();
78  
79          final JavaPackage apiPackage =
80              dependancies.getPackage("org.apache.commons.cli2");
81          final Collection dependsUpon = cli2Packages(apiPackage.getEfferents());
82  
83          assertEquals("Api should depend on one package", 1, dependsUpon.size());
84  
85          JavaPackage pkg = (JavaPackage) dependsUpon.iterator().next();
86          assertEquals(
87                  "Wrong package name",
88                  "org.apache.commons.cli2.resource",
89                  pkg.getName());
90      }
91  
92      private Collection cli2Packages(final Collection incoming) {
93          return namedPackages(incoming, "org.apache.commons.cli2", true);
94      }
95  
96      private Collection namedPackages(
97          final Collection incoming,
98          final String name,
99          final boolean include) {
100         final Collection outgoing = new ArrayList();
101         for (final Iterator i = incoming.iterator(); i.hasNext();) {
102             final JavaPackage pkg = (JavaPackage)i.next();
103             if (include ^ !pkg.getName().startsWith(name)) {
104                 outgoing.add(pkg);
105             }
106         }
107         return outgoing;
108     }
109 }