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 2004/01/11 16:26:07 UTC

cvs commit: maven-components/maven-core/src/test/java/org/apache/maven/jelly/tags/maven DependencyResolverTest.java DependencyResolverTestData.java

bwalding    2004/01/11 07:26:07

  Added:       maven-core/src/test/java/org/apache/maven/jelly/tags/maven
                        DependencyResolverTest.java
                        DependencyResolverTestData.java
  Log:
  Ported from the old maven and upgraded to fit in more properly.
  
  Revision  Changes    Path
  1.1                  maven-components/maven-core/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.util.Iterator;
  import java.util.List;
  
  import junit.framework.TestCase;
  
  import org.apache.maven.project.Project;
  
  /**
   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
   * @version $Id: DependencyResolverTest.java,v 1.1 2004/01/11 15:26:07 bwalding Exp $
   */
  public class DependencyResolverTest extends TestCase {
      
  
  
      
      public void testSimple1External(boolean useInternalSplitter) throws Exception {
          testSimple1(false);
      }
          
      public void testSimple1Internal() throws Exception {
          testSimple1(true);
      }
              
      public void testSimple1(boolean useInternalSplitter) throws Exception {
          List projects = DependencyResolverTestData.getTestSimple1(useInternalSplitter);
          testSimple1Source(new WerkzDependencyResolver(), projects);
          testSimple1Binary(new WerkzDependencyResolver(), projects);
      }
  
      
      public void testSimple2External() throws Exception {
          testSimple2(false);
      }
      public void testSimple2Internal() throws Exception {
          testSimple2(true);
      }
      
      public void testSimple2(boolean useInternalSplitter) throws Exception {
          List projects = DependencyResolverTestData.getTestSimple2(useInternalSplitter);
          testSimple2(new WerkzDependencyResolver(), projects);
      }
  
      public void testSimple3External() throws Exception {
          testSimple3(false);
      }
      public void testSimple3Internal() throws Exception {
          testSimple3(true);
      }
      
      public void testSimple3(boolean useInternalSplitter) throws Exception {
          List projects = DependencyResolverTestData.getTestSimple3(useInternalSplitter);
          testSimple3(new WerkzDependencyResolver(), projects);
      }
  
      public void testComplexExternal() throws Exception {
          testComplex(false);
      }
      public void testComplexInternal() throws Exception {
          testComplex(true);
      }
      
      public void testComplex(boolean useInternalSplitter) throws Exception {
          List projects = DependencyResolverTestData.getTestComplex(useInternalSplitter);
          testComplex(new WerkzDependencyResolver(), projects);
      }
      
      public void testCycleExternal() throws Exception {
          testCycle(false);
      }
      public void testCycleInternal() throws Exception {
          testCycle(true);
      }
  
      public void testCycle(boolean useInternalSplitter) throws Exception {
          List projects = DependencyResolverTestData.getTestCycle(useInternalSplitter);
          //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)));
      }
  
      /**
       * @todo THIS IS A DUPLICATE of testSimple1Source
       * @param dri dependency resolver
       * @param projects list of projects to resolve
       * @throws Exception when anything happens
       */
      public void testSimple1Binary(DependencyResolverInterface dri, List projects) throws Exception {
          testSimple1Source(dri, projects);
      }
  
      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.getArtifactId() + ":" + project.getGroupId());
              if (iter.hasNext())
                  buf.append(",");
          }
          return buf.toString();
      }
  
  }
  
  
  
  1.1                  maven-components/maven-core/src/test/java/org/apache/maven/jelly/tags/maven/DependencyResolverTestData.java
  
  Index: DependencyResolverTestData.java
  ===================================================================
  package org.apache.maven.jelly.tags.maven;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.StringTokenizer;
  
  import org.apache.maven.model.Dependency;
  import org.apache.maven.model.Model;
  import org.apache.maven.project.Project;
  
  /**
   * @author Ben Walding
   * @version $Id: DependencyResolverTestData.java,v 1.1 2003/06/18 00:01:11
   *          bwalding Exp $
   */
  public class DependencyResolverTestData
  {
  
  	public static List getTestSimple1( boolean useInternalSplitter )
  	{
          //One build order for this is - benj
  		List results = new ArrayList();
  		results.add( createProject( "j:j", "n:n", useInternalSplitter));
  		results.add( createProject( "n:n", "e:e", useInternalSplitter));
  		results.add( createProject( "e:e", "b:b", useInternalSplitter));
  		results.add( createProject( "b:b", "", useInternalSplitter));
  		return results;
  	}
  
  	public static List getTestSimple2( boolean useInternalSplitter )
  	{
  		List results = new ArrayList();
  		results.add( createProject( "ox:ox", "nf:nf", useInternalSplitter));
  		results.add( createProject( "nf:nf", "ow:ow", useInternalSplitter));
  		results.add( createProject( "ow:ow", "br:br", useInternalSplitter));
  		results.add( createProject( "br:br", "ck:ck", useInternalSplitter));
  		results.add( createProject( "ck:ck", "ui:ui", useInternalSplitter));
  		results.add( createProject( "ui:ui", "eq:eq", useInternalSplitter));
  		results.add( createProject( "eq:eq", "th:th", useInternalSplitter));
  		results.add( createProject( "th:th", "", useInternalSplitter));
  		results.add( createProject( "ox:ox", "nf:nf", useInternalSplitter));
  		results.add( createProject( "ox:ox", "nf:nf", useInternalSplitter));
  		return results;
  	}
  
  	public static List getTestSimple3( boolean useInternalSplitter )
  	{
  		List results = new ArrayList();
  		results.add( createProject( "ox:ox", "nf:nf", useInternalSplitter));
  		results.add( createProject( "ui:ui", "eq:eq", useInternalSplitter));
  		results.add( createProject( "ow:ow", "br:br", useInternalSplitter));
  		results.add( createProject( "ck:ck", "ui:ui", useInternalSplitter));
  		results.add( createProject( "nf:nf", "ow:ow", useInternalSplitter));
  		results.add( createProject( "eq:eq", "th:th", useInternalSplitter));
  		results.add( createProject( "br:br", "ck:ck", useInternalSplitter));
  		results.add( createProject( "th:th", "", useInternalSplitter));
  		return results;
  	}
  
  	public static List getTestCycle( boolean useInternalSplitter )
  	{
  		List results = new ArrayList();
  		results.add( createProject( "a:a", "b:b", useInternalSplitter));
  		results.add( createProject( "b:b", "c:c", useInternalSplitter));
  		results.add( createProject( "c:c", "a:a", useInternalSplitter));
  		return results;
  	}
  
  	public static List getTestComplex( boolean useInternalSplitter )
  	{
  		List results = new ArrayList();
  		results.add( createProject( "tyre:tyre", "rubber:rubber",
  				useInternalSplitter));
  		results.add( createProject( "car:car",
  				"petrol:petrol,driver:driver,key:key,tyre:tyre",
  				useInternalSplitter));
  		results.add( createProject( "car:car", "", useInternalSplitter));
  		results.add( createProject( "driver:driver",
  				"wallet:wallet,license:license,key:key", useInternalSplitter));
  		results.add( createProject( "driver:driver", "beer:beer",
  				useInternalSplitter));
  		results.add( createProject( "beer:beer",
  				"water:water,hops:hops,malt:malt", useInternalSplitter));
  		results.add( createProject( "hops:hops", "", useInternalSplitter));
  		return results;
  	}
  
  	
  
  	private static String[] split( String id )
  	{
  		int colonPos = id.indexOf( ':');
  		String split[] = {id.substring( 0, colonPos ),
  				id.substring( colonPos + 1)};
  		return split;
  	}
  
  	private static Dependency createDependency( String artifactId,
  			String groupId )
  	{
  		Dependency d = new Dependency();
  		d.setArtifactId( artifactId);
  		d.setGroupId( groupId);
  		return d;
  	}
  
      private static Project createProject( String id, boolean useInternalSplitter )
      {
          String[] splitId = split( id);
          Project p = new Project();
          p.setGroupId( splitId[0]);
          p.setArtifactId( splitId[1]);
          return p;
      }
      
      private static Project createProject( String id, String dependencies,
  			boolean useInternalSplitter )
  	{
  		StringTokenizer depToks = new StringTokenizer( dependencies, ",", false );
  		Project p = createProject( id, useInternalSplitter);
          while ( depToks.hasMoreTokens() )
  		{
  			String token = depToks.nextToken();
  			String[] splitId = split( token );
  			Dependency d = createDependency( splitId[0], splitId[1] );
              p.addDependency(d);
  		}
  		return p;
  	}
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org