You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by bw...@apache.org on 2003/02/13 11:37:44 UTC

cvs commit: jakarta-turbine-maven/src/test/java/org/apache/maven/jelly/tags/maven DependencyResolverTest.java

bwalding    2003/02/13 02:37:44

  Added:       src/test/java/org/apache/maven/jelly/tags/maven
                        DependencyResolverTest.java
  Log:
  o MAVEN-267: Made new dependency resolver use Werkz
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-maven/src/test/java/org/apache/maven/jelly/tags/maven/DependencyResolverTest.java
  
  Index: DependencyResolverTest.java
  ===================================================================
  package org.apache.maven.jelly.tags.maven;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.commons.digester.Digester;
  import org.apache.maven.project.Dependency;
  import org.apache.maven.project.Project;
  import org.xml.sax.SAXException;
  
  import junit.framework.TestCase;
  
  /**
   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
   * @version $Id: DependencyResolverTest.java,v 1.1 2003/02/13 10:37:44 bwalding Exp $
   */
  public class DependencyResolverTest extends TestCase
  {
    protected InputStream getResource(String resource) throws IOException
    {
      String baseDir = System.getProperty("basedir");
      File f = new File(baseDir + "/src/test/dependency-resolver/" + resource);
      return new FileInputStream(f);
    }
  
    public void testSimple1() throws Exception
    {
      InputStream is = getResource("test-simple1.xml");
      List projects = getProjects(is);
      testSimple1Source(new GraphDependencyResolver(), projects);
      testSimple1Binary(new GraphDependencyResolver(), projects);
      testSimple1Source(new WerkzDependencyResolver(), projects);
      testSimple1Binary(new WerkzDependencyResolver(), projects);
    }
  
    public void testSimple2() throws Exception
    {
      InputStream is = getResource("test-simple2.xml");
      List projects = getProjects(is);
      testSimple2(new GraphDependencyResolver(), projects);
      testSimple2(new WerkzDependencyResolver(), projects);
    }
  
    public void testSimple3() throws Exception
    {
      InputStream is = getResource("test-simple3.xml");
      List projects = getProjects(is);
      testSimple3(new GraphDependencyResolver(), projects);
      testSimple3(new WerkzDependencyResolver(), projects);
    }
  
    public void testComplex() throws Exception
    {
      InputStream is = getResource("test-complex.xml");
      List projects = getProjects(is);
      testComplex(new GraphDependencyResolver(), projects);
      testComplex(new WerkzDependencyResolver(), projects);
    }
  
    public void testCycle() throws Exception
    {
      InputStream is = getResource("test-cycle.xml");
      List projects = getProjects(is);
      testCycle(new GraphDependencyResolver(), projects);
      testCycle(new WerkzDependencyResolver(), projects);
    }
  
    public void testCycle(DependencyResolverInterface dri, List projects) throws Exception
    {
      DependencyResolver dr = new DependencyResolver(dri);
      dr.setProjects(projects);
      try
      {
        dr.getSortedDependencies(true);
        fail("Shouldn't be able to resolve cycles");
      }
      catch (Exception e)
      {
        //Success
      }
  
      try
      {
        dr.getSortedDependencies(false);
        fail("Shouldn't be able to resolve cycles");
      }
      catch (Exception e)
      {
        //Success
      }
    }
  
    public void testSimple1Source(DependencyResolverInterface dri, List projects) throws Exception
    {
      DependencyResolver dr = new DependencyResolver(dri);
      dr.setProjects(projects);
  
      assertEquals("test1.sorted", "b:b,e:e,n:n,j:j", listify(dri.getSortedDependencies(true)));
  
      projects = dr.getSortedDependencies(true);
      assertBefore(projects, "b:b", "e:e");
      assertBefore(projects, "e:e", "n:n");
      assertBefore(projects, "n:n", "j:j");
  
      Project e = DependencyResolver.getProject(projects, "e:e");
      assertEquals("test1.sorted", "b:b,e:e", listify(dri.getSortedDependencies(e, true)));
      Project n = DependencyResolver.getProject(projects, "n:n");
      assertEquals("test1.sorted", "b:b,e:e,n:n", listify(dri.getSortedDependencies(n, true)));
      Project j = DependencyResolver.getProject(projects, "j:j");
      assertEquals("test1.sorted", "b:b,e:e,n:n,j:j", listify(dri.getSortedDependencies(j, true)));
    }
  
    public void testSimple1Binary(DependencyResolverInterface dri, List projects) throws Exception
    {
      DependencyResolver dr = new DependencyResolver(dri);
      dr.setProjects(projects);
  
      assertEquals("test1.sorted", "b:b,e:e,n:n,j:j", listify(dri.getSortedDependencies(true)));
  
      projects = dr.getSortedDependencies(true);
      assertBefore(projects, "b:b", "e:e");
      assertBefore(projects, "e:e", "n:n");
      assertBefore(projects, "n:n", "j:j");
  
      Project e = DependencyResolver.getProject(projects, "e:e");
      assertEquals("test1.sorted", "b:b,e:e", listify(dri.getSortedDependencies(e, true)));
      Project n = DependencyResolver.getProject(projects, "n:n");
      assertEquals("test1.sorted", "b:b,e:e,n:n", listify(dri.getSortedDependencies(n, true)));
      Project j = DependencyResolver.getProject(projects, "j:j");
      assertEquals("test1.sorted", "b:b,e:e,n:n,j:j", listify(dri.getSortedDependencies(j, true)));
    }
  
    public void testSimple2(DependencyResolverInterface dri, List projects) throws Exception
    {
      dri.setProjects(projects);
      projects = dri.getSortedDependencies(true);
  
      assertEquals(
        "test3.sorted",
        "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow,nf:nf,ox:ox",
        listify(dri.getSortedDependencies(true)));
      Project eq = DependencyResolver.getProject(projects, "eq:eq");
      assertEquals("test3.sorted", "th:th,eq:eq", listify(dri.getSortedDependencies(eq, true)));
      Project ck = DependencyResolver.getProject(projects, "ck:ck");
      assertEquals("test3.sorted", "th:th,eq:eq,ui:ui,ck:ck", listify(dri.getSortedDependencies(ck, true)));
      Project ow = DependencyResolver.getProject(projects, "ow:ow");
      assertEquals("test3.sorted", "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow", listify(dri.getSortedDependencies(ow, true)));
    }
  
    public void testSimple3(DependencyResolverInterface dri, List projects) throws Exception
    {
      dri.setProjects(projects);
      projects = dri.getSortedDependencies(true);
  
      assertEquals(
        "test2.sorted",
        "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow,nf:nf,ox:ox",
        listify(dri.getSortedDependencies(true)));
      Project eq = DependencyResolver.getProject(projects, "eq:eq");
      assertEquals("test3.sorted", "th:th,eq:eq", listify(dri.getSortedDependencies(eq, true)));
      Project ck = DependencyResolver.getProject(projects, "ck:ck");
      assertEquals("test3.sorted", "th:th,eq:eq,ui:ui,ck:ck", listify(dri.getSortedDependencies(ck, true)));
      Project ow = DependencyResolver.getProject(projects, "ow:ow");
      assertEquals("test3.sorted", "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow", listify(dri.getSortedDependencies(ow, true)));
    }
  
    public void testComplex(DependencyResolverInterface dri, List projects) throws Exception
    {
      dri.setProjects(projects);
      //System.out.println("Source DepResolver:" + dri.getClass().getName());
      projects = dri.getSortedDependencies(true);
      //dumpList(projects);
      assertEquals("SourceDep.size()", projects.size(), 12);
      assertBefore(projects, "tyre:tyre", "car:car");
      assertBefore(projects, "rubber:rubber", "tyre:tyre");
      assertBefore(projects, "beer:beer", "driver:driver");
  
      //System.out.println("Binary DepResolver:" + dri.getClass().getName());
      projects = dri.getSortedDependencies(false);
      //dumpList(projects);
      assertEquals("SourceDep.size()", projects.size(), 5);
      assertBefore(projects, "hops:hops", "beer:beer");
      assertBefore(projects, "tyre:tyre", "car:car");
      assertBefore(projects, "beer:beer", "driver:driver");
  
    }
  
    public void dumpList(List projects)
    {
      Iterator iter = projects.iterator();
      while (iter.hasNext())
      {
        Project project = (Project) iter.next();
        System.out.println(project.getId());
      }
    }
  
    public void assertBefore(List projects, String first, String second)
    {
      assertTrue(first + " earlier than " + second, getIndex(projects, first) < getIndex(projects, second));
    }
  
    public int getIndex(List projects, String id)
    {
      for (int i = 0; i < projects.size(); i++)
      {
        Project p = (Project) projects.get(i);
        if (p.getId().equals(id))
          return i;
      }
      throw new IllegalArgumentException("No such project: " + id);
    }
  
    public String listify(List projects)
    {
      StringBuffer buf = new StringBuffer();
      Iterator iter = projects.iterator();
      while (iter.hasNext())
      {
        Project project = (Project) iter.next();
        buf.append(project.getId());
        if (iter.hasNext())
          buf.append(",");
      }
      return buf.toString();
    }
  
    public static List getProjects(InputStream is) throws IOException, SAXException
    {
      Digester d = new Digester();
      List projects = new ArrayList();
      d.push(projects);
  
      d.addObjectCreate("*/project", Project.class);
      d.addCallMethod("*/id", "setId", 0);
      d.addSetNext("*/project", "add");
  
      d.addObjectCreate("*/dependency", Dependency.class);
      d.addCallMethod("*/id", "setId", 0);
      d.addSetNext("*/dependency", "addDependency");
  
      d.parse(is);
      return projects;
    }
  
  }