KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Date JavaDoc;
43 import java.util.List JavaDoc;
44
45 import junit.framework.TestCase;
46 import net.sourceforge.cruisecontrol.CruiseControlException;
47
48 public class MavenSnapshotDependencyTest extends TestCase {
49
50     private static final String JavaDoc BAD_REPOSITORY = "folder";
51
52     private static final String JavaDoc PROJECT_XML_RELATIVE_PATH =
53         "net/sourceforge/cruisecontrol/sourcecontrols/maven-project.xml";
54
55     private static final String JavaDoc TEST_PROJECT_XML;
56     private static final String JavaDoc TEST_REPOSITORY;
57
58     static {
59         URL JavaDoc projectUrl = ClassLoader.getSystemResource(PROJECT_XML_RELATIVE_PATH);
60         TEST_PROJECT_XML = URLDecoder.decode(projectUrl.getPath());
61         // Use the parent folder of the project xml as repository folder
62
TEST_REPOSITORY = new File JavaDoc(TEST_PROJECT_XML).getParentFile().getAbsolutePath();
63     }
64
65     public MavenSnapshotDependencyTest(String JavaDoc name) {
66         super(name);
67     }
68
69     public void testValidateNoProject() {
70         MavenSnapshotDependency dep = new MavenSnapshotDependency();
71
72         try {
73             dep.validate();
74             fail("MavenSnapshotDependency should throw exceptions when required attributes are not set.");
75         } catch (CruiseControlException e) {
76             assertEquals("'projectFile' is required for MavenSnapshotDependency", e.getMessage());
77         }
78     }
79
80     public void testValidateProjectDoesNotExist() {
81         MavenSnapshotDependency dep = new MavenSnapshotDependency();
82
83         String JavaDoc fileName = BAD_REPOSITORY;
84         dep.setProjectFile(fileName);
85         File JavaDoc f = new File JavaDoc(fileName);
86         try {
87             dep.validate();
88             fail("MavenSnapshotDependency should throw exceptions when required attributes have bad values.");
89         } catch (CruiseControlException e) {
90             assertEquals("Project file '" + f.getAbsolutePath()
91                 + "' does not exist.", e.getMessage());
92         }
93     }
94
95     public void testValidateProjectIsDirectory() {
96         MavenSnapshotDependency dep = new MavenSnapshotDependency();
97
98         String JavaDoc fileName = TEST_REPOSITORY;
99         dep.setProjectFile(fileName);
100         File JavaDoc f = new File JavaDoc(fileName);
101         try {
102             dep.validate();
103             fail("MavenSnapshotDependency should throw exceptions when required attributes have bad values.");
104         } catch (CruiseControlException e) {
105             assertEquals(
106                 "The directory '"
107                     + f.getAbsolutePath()
108                     + "' cannot be used as the projectFile for MavenSnapshotDependency.",
109                 e.getMessage());
110         }
111     }
112
113     public void testValidateRepositoryDoesNotExist() {
114         MavenSnapshotDependency dep = new MavenSnapshotDependency();
115
116         String JavaDoc fileName = BAD_REPOSITORY;
117         dep.setProjectFile(TEST_PROJECT_XML);
118         dep.setLocalRepository(fileName);
119         File JavaDoc f = new File JavaDoc(fileName);
120         try {
121             dep.validate();
122             fail("MavenSnapshotDependency should throw exceptions when repository has bad value.");
123         } catch (CruiseControlException e) {
124             assertEquals("Local Maven repository '" + f.getAbsolutePath()
125                 + "' does not exist.", e.getMessage());
126         }
127     }
128
129     public void testValidateRepositoryIsNotDirectory() {
130         MavenSnapshotDependency dep = new MavenSnapshotDependency();
131
132         String JavaDoc fileName = TEST_PROJECT_XML;
133         dep.setProjectFile(fileName);
134         dep.setLocalRepository(fileName);
135         File JavaDoc f = new File JavaDoc(fileName);
136         try {
137             dep.validate();
138             fail("MavenSnapshotDependency should throw exceptions when repository has bad value.");
139         } catch (CruiseControlException e) {
140             assertEquals("Local Maven repository '" + f.getAbsolutePath()
141                 + "' must be a directory.", e.getMessage());
142         }
143     }
144
145     public void testValidateOk() {
146         MavenSnapshotDependency dep = new MavenSnapshotDependency();
147
148         dep.setProjectFile(TEST_PROJECT_XML);
149         dep.setLocalRepository(TEST_REPOSITORY);
150         try {
151             dep.validate();
152             assertTrue(true);
153         } catch (CruiseControlException e) {
154             fail("MavenSnapshotDependency should not throw exceptions when attributes have valid values: "
155                 + e.getMessage());
156         }
157     }
158
159     public void testGetProjectXml() throws Exception JavaDoc {
160         MavenSnapshotDependency dep = new MavenSnapshotDependency();
161
162         dep.setProjectFile(TEST_PROJECT_XML);
163         dep.setLocalRepository(TEST_REPOSITORY);
164         File JavaDoc testProjectFile = new File JavaDoc(TEST_PROJECT_XML);
165         List JavaDoc filenames = dep.getSnapshotFilenames(testProjectFile);
166         assertEquals("Filename list is not the correct size", 2, filenames.size());
167         String JavaDoc filename = (String JavaDoc) filenames.get(0);
168         String JavaDoc expectedFilename = TEST_REPOSITORY + "/maven/jars/cc-maven-test-1.0-SNAPSHOT.jar";
169         File JavaDoc expectedFile = new File JavaDoc(expectedFilename);
170         assertEquals("Unexpected filename", expectedFile.getAbsolutePath(), filename);
171         filename = (String JavaDoc) filenames.get(1);
172         expectedFilename = TEST_REPOSITORY + "/maven/jars/maven-1.0-SNAPSHOT.jar";
173         expectedFile = new File JavaDoc(expectedFilename);
174         assertEquals("Unexpected filename", expectedFile.getAbsolutePath(), filename);
175     }
176
177     public void testGettingModifications() throws Exception JavaDoc {
178         MavenSnapshotDependency dep = new MavenSnapshotDependency();
179
180         dep.setProjectFile(TEST_PROJECT_XML);
181         dep.setLocalRepository(TEST_REPOSITORY);
182         Date JavaDoc epoch = new Date JavaDoc(0);
183         Date JavaDoc now = new Date JavaDoc();
184         List JavaDoc modifications = dep.getModifications(epoch, now);
185         assertEquals("Modification list is not the correct size", 2, modifications.size());
186     }
187 }
188
Popular Tags