001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2003, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.sourcecontrols;
037:
038: import java.io.File;
039: import java.io.UnsupportedEncodingException;
040: import java.net.URL;
041: import java.net.URLDecoder;
042:
043: import junit.framework.TestCase;
044: import net.sourceforge.cruisecontrol.CruiseControlException;
045:
046: /**
047: * Unit Tests of maven2 snapshot dependency sourcecontrol.
048: *
049: * Date: Feb 8, 2006
050: * Time: 9:15:47 PM
051: *
052: * @author Dan Rollo
053: */
054: public class Maven2SnapshotDependencyTest extends TestCase {
055:
056: private static final String BAD_REPOSITORY = "folder";
057: private static final String PROJECT_XML_RELATIVE_PATH = "net/sourceforge/cruisecontrol/sourcecontrols/maven2-pom.xml";
058:
059: private static final String TEST_PROJECT_XML;
060: private static final String TEST_REPOSITORY;
061:
062: static {
063: URL projectUrl = Maven2SnapshotDependencyTest.class
064: .getClassLoader()
065: .getResource(PROJECT_XML_RELATIVE_PATH);
066: try {
067: TEST_PROJECT_XML = URLDecoder.decode(projectUrl.getPath(),
068: "utf-8");
069: } catch (UnsupportedEncodingException e) {
070: throw new RuntimeException(e);
071: }
072: // Use the parent folder of the project xml as repository folder
073: TEST_REPOSITORY = new File(TEST_PROJECT_XML).getParentFile()
074: .getAbsolutePath()
075: + "/maven2repo";
076: }
077:
078: private Maven2SnapshotDependency dep;
079:
080: public Maven2SnapshotDependencyTest(String name) {
081: super (name);
082: }
083:
084: protected void setUp() {
085: dep = new Maven2SnapshotDependency();
086: }
087:
088: public void testValidateNoProject() {
089:
090: try {
091: dep.validate();
092: fail("Maven2SnapshotDependency should throw exceptions when required attributes are not set.");
093: } catch (CruiseControlException e) {
094: assertEquals(
095: "'pomFile' is required for Maven2SnapshotDependency",
096: e.getMessage());
097: }
098: }
099:
100: public void testValidateProjectDoesNotExist() {
101:
102: String fileName = BAD_REPOSITORY;
103: dep.setPomFile(fileName);
104: File f = new File(fileName);
105: try {
106: dep.validate();
107: fail("Maven2SnapshotDependency should throw exceptions when required attributes have bad values.");
108: } catch (CruiseControlException e) {
109: assertEquals("Pom file '" + f.getAbsolutePath()
110: + "' does not exist.", e.getMessage());
111: }
112: }
113:
114: public void testValidateProjectIsDirectory() {
115:
116: String fileName = TEST_REPOSITORY;
117: dep.setPomFile(fileName);
118: File f = new File(fileName);
119: try {
120: dep.validate();
121: fail("Maven2SnapshotDependency should throw exceptions when required attributes have bad values.");
122: } catch (CruiseControlException e) {
123: assertEquals(
124: "The directory '"
125: + f.getAbsolutePath()
126: + "' cannot be used as the pomFile for Maven2SnapshotDependency.",
127: e.getMessage());
128: }
129: }
130:
131: public void testValidateRepositoryNotSet() throws Exception {
132: dep.setPomFile(TEST_PROJECT_XML);
133: dep.validate();
134: }
135:
136: public void testValidateRepositoryDoesNotExist() throws Exception {
137:
138: String fileName = BAD_REPOSITORY;
139: dep.setPomFile(TEST_PROJECT_XML);
140: dep.setLocalRepository(fileName);
141: File f = new File(fileName);
142: try {
143: dep.validate();
144: fail("Maven2SnapshotDependency should throw exceptions when repository has bad value.");
145: } catch (CruiseControlException e) {
146: assertEquals("Local Maven repository '"
147: + f.getAbsolutePath() + "' does not exist.", e
148: .getMessage());
149: }
150: }
151:
152: public void testValidateRepositoryIsNotDirectory() throws Exception {
153:
154: String fileName = TEST_PROJECT_XML;
155: dep.setPomFile(fileName);
156: dep.setLocalRepository(fileName);
157: File f = new File(fileName);
158: try {
159: dep.validate();
160: fail("Maven2SnapshotDependency should throw exceptions when repository has bad value.");
161: } catch (CruiseControlException e) {
162: assertEquals("Local Maven repository '"
163: + f.getAbsolutePath() + "' must be a directory.", e
164: .getMessage());
165: }
166: }
167:
168: public void testValidateOkWithRepo() throws Exception {
169: dep.setPomFile(TEST_PROJECT_XML);
170: dep.setLocalRepository(TEST_REPOSITORY);
171: dep.validate();
172: }
173:
174: public void testValidateOk() throws Exception {
175: dep.setPomFile(TEST_PROJECT_XML);
176: dep.validate();
177: }
178:
179: /* @todo NOTE: Add a single slash to the front of this line to try running these tests.
180:
181:
182: //this test is not jre 1.3 compatible because it invokes the MavenEmbedder.
183: //mocking that connection would allow the test to run under 1.3 and also
184: //make the test much faster. (current ~7 seconds)
185:
186: public void testGetPomXml() throws Exception {
187:
188: dep.setPomFile(TEST_PROJECT_XML);
189: //@todo Fix when maven embedder honors alignWithUserInstallation
190: //dep.setLocalRepository(TEST_REPOSITORY);
191:
192:
193: Maven2SnapshotDependency.ArtifactInfo[] artifactInfos = dep.getSnapshotInfos();
194: assertEquals("Filename list is not the correct size", 2, artifactInfos.length);
195:
196: //@todo Fix when maven embedder honors alignWithUserInstallation
197: // assertEquals("Unexpected filename",
198: // new File(TEST_REPOSITORY + "/ccdeptest/cc-maven-test/1.0-SNAPSHOT/cc-maven-test-1.0-SNAPSHOT.jar"),
199: // artifactInfos[0].getLocalRepoFile());
200:
201: assertArtifactInfosContains(artifactInfos,
202: File.separatorChar + "ccdeptest"
203: + File.separatorChar + "cc-maven-test"
204: + File.separatorChar + "1.0-SNAPSHOT"
205: + File.separatorChar + "cc-maven-test-1.0-SNAPSHOT.jar",
206: Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY);
207:
208: //@todo Fix when maven embedder honors alignWithUserInstallation
209: //assertEquals("Unexpected filename",
210: // new File(TEST_REPOSITORY + "/ccdeptest/maven/1.0-SNAPSHOT/maven-1.0-SNAPSHOT-source.jar"),
211: // artifactInfos[1].getLocalRepoFile());
212:
213: assertArtifactInfosContains(artifactInfos,
214: File.separatorChar + "ccdeptest"
215: + File.separatorChar + "maven"
216: + File.separatorChar + "1.0-SNAPSHOT"
217: + File.separatorChar + "maven-1.0-SNAPSHOT-source.jar",
218: Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY);
219: }
220:
221: // Fails if the given modset does not contain a mod with the given comment.
222: // @param artifactInfos artifactInfo objects to search through
223: // @param expectedFilenameSuffix the filename we expected to find in the given artifactInfos.
224: // @param expectedArtifactType the artifact type expected for tge matched filenamesuffix
225: private void assertArtifactInfosContains(final Maven2SnapshotDependency.ArtifactInfo[] artifactInfos,
226: final String expectedFilenameSuffix, final String expectedArtifactType) {
227: for (int i = 0; i < artifactInfos.length; i++) {
228: if (artifactInfos[i].getLocalRepoFile().getAbsolutePath().endsWith(expectedFilenameSuffix)
229: && artifactInfos[0].getArtifactType().equals(expectedArtifactType)) {
230:
231: // found a matching filename AND artifactType
232: return;
233: }
234: }
235: fail("Missing expected artifactInfo with filename suffix: " + expectedFilenameSuffix
236: + "; AND artifact type: " + expectedArtifactType
237: + " \n in artifactInfos: " + java.util.Arrays.asList(artifactInfos).toString());
238: }
239:
240:
241: //This test is too slow (~55 second). Need tests that don't require going over the network.
242:
243: public void testGetModifications() throws Exception {
244:
245: dep.setPomFile(TEST_PROJECT_XML);
246:
247: //@todo Fix when maven embedder honors alignWithUserInstallation
248: // dep.setLocalRepository(TEST_REPOSITORY);
249:
250: java.util.Date epoch = new java.util.Date(0);
251: java.util.Date now = new java.util.Date();
252: java.util.List modifications = dep.getModifications(epoch, now);
253: assertEquals("Modification list is not the correct size. " + modifications.toString(),
254: 2, modifications.size());
255:
256: assertModsetContainsModWithComment(modifications, Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY
257: + Maven2SnapshotDependency.COMMENT_MISSING_IN_LOCALREPO + "cc-maven-test");
258:
259: assertModsetContainsModWithComment(modifications, Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY
260: + Maven2SnapshotDependency.COMMENT_MISSING_IN_LOCALREPO + "maven");
261: }
262:
263:
264: // Fails if the given modset does not contain a mod with the given comment.
265: // @param modset a list of modifications to search through
266: // @param expectedModComment the comment we expected to find in a modification in the given modificationset.
267: private void assertModsetContainsModWithComment(final java.util.List modset, final String expectedModComment) {
268: for (int i = 0; i < modset.size(); i++) {
269: if (expectedModComment.equals(((net.sourceforge.cruisecontrol.Modification) modset.get(i)).comment)) {
270: // found a matching comment
271: return;
272: }
273: }
274: fail("Missing expected modification comment: " + expectedModComment
275: + " in modificationset: " + modset.toString());
276: }
277: //*/
278:
279: // @todo Add support/test of transitive dependencies with snapshots
280: }
|