001: package org.apache.maven.project;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import junit.framework.TestCase;
023: import org.apache.maven.model.Build;
024: import org.apache.maven.model.Dependency;
025: import org.apache.maven.model.Extension;
026: import org.apache.maven.model.Model;
027: import org.codehaus.plexus.util.dag.CycleDetectedException;
028:
029: import java.util.ArrayList;
030: import java.util.Collections;
031: import java.util.List;
032:
033: /**
034: * Test sorting projects by dependencies.
035: *
036: * @author <a href="mailto:brett@apache.org">Brett Porter</a>
037: * @version $Id: ProjectSorterTest.java 513038 2007-02-28 22:54:19Z jvanzyl $
038: */
039: public class ProjectSorterTest extends TestCase {
040:
041: public void testShouldNotFailWhenProjectReferencesNonExistentProject()
042: throws CycleDetectedException, DuplicateProjectException {
043: MavenProject project = createProject("group", "artifact", "1.0");
044: Model model = project.getModel();
045:
046: Build build = model.getBuild();
047:
048: if (build == null) {
049: build = new Build();
050: model.setBuild(build);
051: }
052:
053: Extension extension = new Extension();
054:
055: extension.setArtifactId("other-artifact");
056: extension.setGroupId("other.group");
057: extension.setVersion("1.0");
058:
059: build.addExtension(extension);
060:
061: new ProjectSorter(Collections.singletonList(project));
062: }
063:
064: public void testMatchingArtifactIdsDifferentGroupIds()
065: throws CycleDetectedException, DuplicateProjectException {
066: List projects = new ArrayList();
067: MavenProject project1 = createProject("groupId1", "artifactId",
068: "1.0");
069: projects.add(project1);
070: MavenProject project2 = createProject("groupId2", "artifactId",
071: "1.0");
072: projects.add(project2);
073: project1.getDependencies().add(createDependency(project2));
074:
075: projects = new ProjectSorter(projects).getSortedProjects();
076:
077: assertEquals(project2, projects.get(0));
078: assertEquals(project1, projects.get(1));
079: }
080:
081: public void testMatchingGroupIdsDifferentArtifactIds()
082: throws CycleDetectedException, DuplicateProjectException {
083: List projects = new ArrayList();
084: MavenProject project1 = createProject("groupId", "artifactId1",
085: "1.0");
086: projects.add(project1);
087: MavenProject project2 = createProject("groupId", "artifactId2",
088: "1.0");
089: projects.add(project2);
090: project1.getDependencies().add(createDependency(project2));
091:
092: projects = new ProjectSorter(projects).getSortedProjects();
093:
094: assertEquals(project2, projects.get(0));
095: assertEquals(project1, projects.get(1));
096: }
097:
098: public void testMatchingIdsAndVersions()
099: throws CycleDetectedException {
100: List projects = new ArrayList();
101: MavenProject project1 = createProject("groupId", "artifactId",
102: "1.0");
103: projects.add(project1);
104: MavenProject project2 = createProject("groupId", "artifactId",
105: "1.0");
106: projects.add(project2);
107:
108: try {
109: projects = new ProjectSorter(projects).getSortedProjects();
110: fail("Duplicate projects should fail");
111: } catch (DuplicateProjectException e) {
112: // expected
113: assertTrue(true);
114: }
115: }
116:
117: public void testMatchingIdsAndDifferentVersions()
118: throws CycleDetectedException {
119: List projects = new ArrayList();
120: MavenProject project1 = createProject("groupId", "artifactId",
121: "1.0");
122: projects.add(project1);
123: MavenProject project2 = createProject("groupId", "artifactId",
124: "2.0");
125: projects.add(project2);
126:
127: try {
128: projects = new ProjectSorter(projects).getSortedProjects();
129: fail("Duplicate projects should fail");
130: } catch (DuplicateProjectException e) {
131: // expected
132: assertTrue(true);
133: }
134: }
135:
136: private Dependency createDependency(MavenProject project) {
137: Dependency depdendency = new Dependency();
138: depdendency.setArtifactId(project.getArtifactId());
139: depdendency.setGroupId(project.getGroupId());
140: depdendency.setVersion(project.getVersion());
141: return depdendency;
142: }
143:
144: private static MavenProject createProject(String groupId,
145: String artifactId, String version) {
146: Model model = new Model();
147: model.setGroupId(groupId);
148: model.setArtifactId(artifactId);
149: model.setVersion(version);
150: return new MavenProject(model);
151: }
152: }
|