KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > Maven2SnapshotDependencyTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import java.io.File JavaDoc;
40 import java.net.URL JavaDoc;
41 import java.net.URLDecoder JavaDoc;
42
43 import junit.framework.TestCase;
44 import net.sourceforge.cruisecontrol.CruiseControlException;
45
46 /**
47  * Unit Tests of maven2 snapshot dependency sourcecontrol.
48  *
49  * Date: Feb 8, 2006
50  * Time: 9:15:47 PM
51  *
52  * @author Dan Rollo
53  */

54 public class Maven2SnapshotDependencyTest extends TestCase {
55
56     private static final String JavaDoc BAD_REPOSITORY = "folder";
57
58     private static final String JavaDoc PROJECT_XML_RELATIVE_PATH =
59         "net/sourceforge/cruisecontrol/sourcecontrols/maven2-pom.xml";
60
61     private static final String JavaDoc TEST_PROJECT_XML;
62     private static final String JavaDoc TEST_REPOSITORY;
63
64     static {
65         URL JavaDoc projectUrl = ClassLoader.getSystemResource(PROJECT_XML_RELATIVE_PATH);
66         TEST_PROJECT_XML = URLDecoder.decode(projectUrl.getPath());
67         // Use the parent folder of the project xml as repository folder
68
TEST_REPOSITORY = new File JavaDoc(TEST_PROJECT_XML).getParentFile().getAbsolutePath() + "/maven2repo";
69     }
70
71     private Maven2SnapshotDependency dep;
72
73     public Maven2SnapshotDependencyTest(String JavaDoc name) {
74         super(name);
75     }
76
77     protected void setUp() {
78         dep = new Maven2SnapshotDependency();
79     }
80
81
82     public void testValidateNoProject() {
83
84         try {
85             dep.validate();
86             fail("Maven2SnapshotDependency should throw exceptions when required attributes are not set.");
87         } catch (CruiseControlException e) {
88             assertEquals("'pomFile' is required for Maven2SnapshotDependency", e.getMessage());
89         }
90     }
91
92     public void testValidateProjectDoesNotExist() {
93
94         String JavaDoc fileName = BAD_REPOSITORY;
95         dep.setPomFile(fileName);
96         File JavaDoc f = new File JavaDoc(fileName);
97         try {
98             dep.validate();
99             fail("Maven2SnapshotDependency should throw exceptions when required attributes have bad values.");
100         } catch (CruiseControlException e) {
101             assertEquals("Pom file '" + f.getAbsolutePath()
102                 + "' does not exist.", e.getMessage());
103         }
104     }
105
106     public void testValidateProjectIsDirectory() {
107
108         String JavaDoc fileName = TEST_REPOSITORY;
109         dep.setPomFile(fileName);
110         File JavaDoc f = new File JavaDoc(fileName);
111         try {
112             dep.validate();
113             fail("Maven2SnapshotDependency should throw exceptions when required attributes have bad values.");
114         } catch (CruiseControlException e) {
115             assertEquals(
116                 "The directory '"
117                     + f.getAbsolutePath()
118                     + "' cannot be used as the pomFile for Maven2SnapshotDependency.",
119                 e.getMessage());
120         }
121     }
122
123     public void testValidateRepositoryNotSet() throws Exception JavaDoc {
124         dep.setPomFile(TEST_PROJECT_XML);
125         dep.validate();
126     }
127
128     public void testValidateRepositoryDoesNotExist() throws Exception JavaDoc {
129
130         String JavaDoc fileName = BAD_REPOSITORY;
131         dep.setPomFile(TEST_PROJECT_XML);
132         dep.setLocalRepository(fileName);
133         File JavaDoc f = new File JavaDoc(fileName);
134         try {
135             dep.validate();
136             fail("Maven2SnapshotDependency should throw exceptions when repository has bad value.");
137         } catch (CruiseControlException e) {
138             assertEquals("Local Maven repository '" + f.getAbsolutePath()
139                 + "' does not exist.", e.getMessage());
140         }
141     }
142
143     public void testValidateRepositoryIsNotDirectory() throws Exception JavaDoc {
144
145         String JavaDoc fileName = TEST_PROJECT_XML;
146         dep.setPomFile(fileName);
147         dep.setLocalRepository(fileName);
148         File JavaDoc f = new File JavaDoc(fileName);
149         try {
150             dep.validate();
151             fail("Maven2SnapshotDependency should throw exceptions when repository has bad value.");
152         } catch (CruiseControlException e) {
153             assertEquals("Local Maven repository '" + f.getAbsolutePath()
154                 + "' must be a directory.", e.getMessage());
155         }
156     }
157
158     public void testValidateOkWithRepo() throws Exception JavaDoc {
159         dep.setPomFile(TEST_PROJECT_XML);
160         dep.setLocalRepository(TEST_REPOSITORY);
161         dep.validate();
162     }
163
164     public void testValidateOk() throws Exception JavaDoc {
165         dep.setPomFile(TEST_PROJECT_XML);
166         dep.validate();
167     }
168
169 /*
170     this test is not jre 1.3 compatible because it invokes the MavenEmbedder.
171     mocking that connection would allow the test to run under 1.3 and also
172     make the test much faster. (current ~7 seconds)
173
174     public void testGetPomXml() throws Exception {
175
176         dep.setPomFile(TEST_PROJECT_XML);
177          //@todo Fix when maven embedder honors alignWithUserInstallation
178         //dep.setLocalRepository(TEST_REPOSITORY);
179         
180
181         Maven2SnapshotDependency.ArtifactInfo[] artifactInfos = dep.getSnapshotInfos();
182         assertEquals("Filename list is not the correct size", 2, artifactInfos.length);
183
184          //@todo Fix when maven embedder honors alignWithUserInstallation
185         // assertEquals("Unexpected filename",
186         // new File(TEST_REPOSITORY + "/ccdeptest/cc-maven-test/1.0-SNAPSHOT/cc-maven-test-1.0-SNAPSHOT.jar"),
187         // artifactInfos[0].getLocalRepoFile());
188         
189         assertTrue("Unexpected filename [" + artifactInfos[0].getLocalRepoFile().getAbsolutePath() + "]",
190                 artifactInfos[0].getLocalRepoFile().getAbsolutePath().endsWith(
191                         File.separatorChar + "ccdeptest"
192                         + File.separatorChar + "cc-maven-test"
193                         + File.separatorChar + "1.0-SNAPSHOT"
194                         + File.separatorChar + "cc-maven-test-1.0-SNAPSHOT.jar"
195                 ));
196
197         assertEquals("Unexpected artifact type", Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY,
198                 artifactInfos[0].getArtifactType());
199
200
201          //@todo Fix when maven embedder honors alignWithUserInstallation
202         //assertEquals("Unexpected filename",
203         // new File(TEST_REPOSITORY + "/ccdeptest/maven/1.0-SNAPSHOT/maven-1.0-SNAPSHOT-source.jar"),
204         // artifactInfos[1].getLocalRepoFile());
205         
206         assertTrue("Unexpected filename [" + artifactInfos[1].getLocalRepoFile().getAbsolutePath() + "]",
207                 artifactInfos[1].getLocalRepoFile().getAbsolutePath().endsWith(
208                         File.separatorChar + "ccdeptest"
209                         + File.separatorChar + "maven"
210                         + File.separatorChar + "1.0-SNAPSHOT"
211                         + File.separatorChar + "maven-1.0-SNAPSHOT-source.jar"
212                 ));
213
214         assertEquals("Unexpected artifact type", Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY,
215                 artifactInfos[1].getArtifactType());
216     }
217 */

218
219 /*
220
221     This test is too slow (~55 second). Need tests that don't require going over the network.
222  
223     public void testGetModifications() throws Exception {
224
225         dep.setPomFile(TEST_PROJECT_XML);
226         
227         //@todo Fix when maven embedder honors alignWithUserInstallation
228         // dep.setLocalRepository(TEST_REPOSITORY);
229         
230         Date epoch = new Date(0);
231         Date now = new Date();
232         List modifications = dep.getModifications(epoch, now);
233         assertEquals("Modification list is not the correct size. " + modifications.toString(),
234                 2, modifications.size());
235
236         Modification mod = (Modification) modifications.get(0);
237         assertEquals("Wrong Modification: " + mod, Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY
238                         + Maven2SnapshotDependency.COMMENT_MISSING_IN_LOCALREPO + "cc-maven-test", mod.comment);
239
240         mod = (Modification) modifications.get(1);
241         assertEquals("Wrong Modification: " + mod, Maven2SnapshotDependency.ArtifactInfo.ART_TYPE_DEPENDENCY
242                         + Maven2SnapshotDependency.COMMENT_MISSING_IN_LOCALREPO + "maven", mod.comment);
243     }
244 */

245     
246     // @todo Add support/test of transitive dependencies with snapshots
247
}
248
Popular Tags