001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.jdepend;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Iterator;
023
024import jdepend.framework.JDepend;
025import jdepend.framework.JavaPackage;
026import junit.framework.TestCase;
027
028/**
029 * @author Rob Oxspring
030 */
031public class JDependTest extends TestCase {
032
033    private JDepend dependancies = null;
034
035    public void setUp() throws IOException {
036        dependancies = new JDepend();
037        dependancies.addDirectory("target/classes");
038        dependancies.analyze();
039    }
040
041    public void testJUnitNotPresent() {
042        // if junit dependancy is found then jdepend has been poluted
043        // with test classes and all tests are meaningless
044        assertNull(
045            "JUnit dependancy found",
046            dependancies.getPackage("junit.framework"));
047
048        // the same applies to jdepend
049        assertNull(
050            "JDepend dependancy found",
051            dependancies.getPackage("jdepend.framework"));
052    }
053
054    public void testAcceptableDistance() {
055        Collection packages = dependancies.getPackages();
056        // only interested in cli2
057        packages = cli2Packages(packages);
058        // resources is well off the line
059        packages =
060            namedPackages(packages, "org.apache.commons.cli2.resource", false);
061
062        for (final Iterator i = packages.iterator(); i.hasNext();) {
063            final JavaPackage pkg = (JavaPackage)i.next();
064            final float distance = pkg.distance();
065            final String message = pkg.getName() + " too far from line: " + distance;
066            assertTrue(
067                message,
068                distance < 0.21d);
069        }
070    }
071
072    public void testNoCyclesPresent() {
073        assertEquals("Cycles exist", false, dependancies.containsCycles());
074    }
075
076    public void testApiIndependance() {
077        dependancies.analyze();
078
079        final JavaPackage apiPackage =
080            dependancies.getPackage("org.apache.commons.cli2");
081        final Collection dependsUpon = cli2Packages(apiPackage.getEfferents());
082
083        assertEquals("Api should depend on one package", 1, dependsUpon.size());
084
085        JavaPackage pkg = (JavaPackage) dependsUpon.iterator().next();
086        assertEquals(
087                "Wrong package name",
088                "org.apache.commons.cli2.resource",
089                pkg.getName());
090    }
091
092    private Collection cli2Packages(final Collection incoming) {
093        return namedPackages(incoming, "org.apache.commons.cli2", true);
094    }
095
096    private Collection namedPackages(
097        final Collection incoming,
098        final String name,
099        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}