/*
 * Contributions to FindBugs
 * Copyright (C) 2009, Tom\u00e1s Pollak
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package edu.umd.cs.findbugs.architecture;

import jdepend.framework.JDepend;
import jdepend.framework.JavaPackage;
import junit.framework.TestCase;

/**
 * Verifies the package dependencies.
 * 
 * @author Tom\u00e1s Pollak
 * @author Andrei Loskutov
 */
public class PackageDependenciesTest extends TestCase {
    private JDepend engine;

    public void testGui2Dependencies() {
        String expectedNotEfferent = "edu.umd.cs.findbugs.gui2";

        assertPackageConstraint("edu.umd.cs.findbugs", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.asm", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.ba", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.bcel", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.classfile", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.cloud", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.cloud.db", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.detect", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.graph", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.io", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.log", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.model", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.plan", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.util", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.visitclass", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.xml", expectedNotEfferent);

    }

    public void testCloudDependencies() {
        String expectedNotEfferent = "edu.umd.cs.findbugs.cloud.db";
        assertPackageConstraint("edu.umd.cs.findbugs", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.asm", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.ba", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.bcel", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.classfile", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.cloud", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.detect", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.graph", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.io", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.log", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.model", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.plan", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.util", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.visitclass", expectedNotEfferent);
        assertPackageConstraint("edu.umd.cs.findbugs.xml", expectedNotEfferent);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Get the classes root directory
        String rootDirectory = getClass().getResource("/").getFile();

        // Setup the JDepend analysis
        engine = new JDepend();
        engine.addDirectory(rootDirectory);
        engine.analyze();
    }

    @Override
    protected void tearDown() throws Exception {
        engine = null;

        super.tearDown();
    }

    private void assertPackageConstraint(String afferent, String expectedNotEfferent) {
        JavaPackage afferentPackage = engine.getPackage(afferent);
        JavaPackage efferentPackage = engine.getPackage(expectedNotEfferent);
        assertFalse(afferentPackage.getName() + " shouldn't depend on " + efferentPackage.getName(), afferentPackage
                .getEfferents().contains(efferentPackage));
    }
}

@@ -1750,94 +1750,8 @@
 t);%0A
-        assertPackageConstraint(%22edu.umd.cs.findbugs.cloud.db%22, expectedNotEfferent);%0A
     
@@ -1822,32 +1822,32 @@
 edNotEfferent);%0A
+
         assertPa
@@ -2502,1360 +2502,8 @@
  %7D%0A%0A
-    public void testCloudDependencies() %7B%0A        String expectedNotEfferent = %22edu.umd.cs.findbugs.cloud.db%22;%0A        assertPackageConstraint(%22edu.umd.cs.findbugs%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.asm%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.ba%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.bcel%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.classfile%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.cloud%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.detect%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.graph%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.io%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.log%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.model%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.plan%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.util%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.visitclass%22, expectedNotEfferent);%0A        assertPackageConstraint(%22edu.umd.cs.findbugs.xml%22, expectedNotEfferent);%0A    %7D%0A%0A
     
@@ -3129,75 +3129,248 @@
     
-JavaPackage efferentPackage = engine.getPackage(expectedNotEfferent
+assertNotNull(%22Afferent package not found: %22 + afferent, afferentPackage);%0A        JavaPackage efferentPackage = engine.getPackage(expectedNotEfferent);%0A        assertNotNull(%22Efferent package not found: %22 + efferentPackage, efferentPackage
 );%0A 

Fix PackageDependenciesTest