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: import java.util.Date;
043: import java.util.List;
044: import java.util.Properties;
045:
046: import junit.framework.TestCase;
047: import net.sourceforge.cruisecontrol.CruiseControlException;
048:
049: public class MavenSnapshotDependencyTest extends TestCase {
050:
051: private static final String BAD_REPOSITORY = "folder";
052:
053: private static final String PROJECT_XML_RELATIVE_PATH = "net/sourceforge/cruisecontrol/sourcecontrols/maven-project.xml";
054:
055: private static final String TEST_PROJECT_XML;
056: private static final String TEST_REPOSITORY;
057:
058: static {
059: URL projectUrl = MavenSnapshotDependencyTest.class
060: .getClassLoader()
061: .getResource(PROJECT_XML_RELATIVE_PATH);
062: try {
063: TEST_PROJECT_XML = URLDecoder.decode(projectUrl.getPath(),
064: "utf-8");
065: } catch (UnsupportedEncodingException e) {
066: throw new RuntimeException(e);
067: }
068: // Use the parent folder of the project xml as repository folder
069: TEST_REPOSITORY = new File(TEST_PROJECT_XML).getParentFile()
070: .getAbsolutePath();
071: }
072:
073: public MavenSnapshotDependencyTest(String name) {
074: super (name);
075: }
076:
077: public void testValidateNoProject() {
078: MavenSnapshotDependency dep = new MavenSnapshotDependency();
079:
080: try {
081: dep.validate();
082: fail("MavenSnapshotDependency should throw exceptions when required attributes are not set.");
083: } catch (CruiseControlException e) {
084: assertEquals(
085: "'projectFile' is required for MavenSnapshotDependency",
086: e.getMessage());
087: }
088: }
089:
090: public void testValidateProjectDoesNotExist() {
091: MavenSnapshotDependency dep = new MavenSnapshotDependency();
092:
093: String fileName = BAD_REPOSITORY;
094: dep.setProjectFile(fileName);
095: File f = new File(fileName);
096: try {
097: dep.validate();
098: fail("MavenSnapshotDependency should throw exceptions when required attributes have bad values.");
099: } catch (CruiseControlException e) {
100: assertEquals("Project file '" + f.getAbsolutePath()
101: + "' does not exist.", e.getMessage());
102: }
103: }
104:
105: public void testValidateProjectIsDirectory() {
106: MavenSnapshotDependency dep = new MavenSnapshotDependency();
107:
108: String fileName = TEST_REPOSITORY;
109: dep.setProjectFile(fileName);
110: File f = new File(fileName);
111: try {
112: dep.validate();
113: fail("MavenSnapshotDependency 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 projectFile for MavenSnapshotDependency.",
119: e.getMessage());
120: }
121: }
122:
123: public void testValidateRepositoryDoesNotExist() {
124: MavenSnapshotDependency dep = new MavenSnapshotDependency();
125:
126: String fileName = BAD_REPOSITORY;
127: dep.setProjectFile(TEST_PROJECT_XML);
128: dep.setLocalRepository(fileName);
129: File f = new File(fileName);
130: try {
131: dep.validate();
132: fail("MavenSnapshotDependency should throw exceptions when repository has bad value.");
133: } catch (CruiseControlException e) {
134: assertEquals("Local Maven repository '"
135: + f.getAbsolutePath() + "' does not exist.", e
136: .getMessage());
137: }
138: }
139:
140: public void testValidateRepositoryIsNotDirectory() {
141: MavenSnapshotDependency dep = new MavenSnapshotDependency();
142:
143: String fileName = TEST_PROJECT_XML;
144: dep.setProjectFile(fileName);
145: dep.setLocalRepository(fileName);
146: File f = new File(fileName);
147: try {
148: dep.validate();
149: fail("MavenSnapshotDependency should throw exceptions when repository has bad value.");
150: } catch (CruiseControlException e) {
151: assertEquals("Local Maven repository '"
152: + f.getAbsolutePath() + "' must be a directory.", e
153: .getMessage());
154: }
155: }
156:
157: public void testValidateOk() {
158: MavenSnapshotDependency dep = new MavenSnapshotDependency();
159:
160: dep.setProjectFile(TEST_PROJECT_XML);
161: dep.setLocalRepository(TEST_REPOSITORY);
162: try {
163: dep.validate();
164: assertTrue(true);
165: } catch (CruiseControlException e) {
166: fail("MavenSnapshotDependency should not throw exceptions when attributes have valid values: "
167: + e.getMessage());
168: }
169: }
170:
171: public void testGetProjectXml() throws Exception {
172: MavenSnapshotDependency dep = new MavenSnapshotDependency();
173:
174: dep.setProjectFile(TEST_PROJECT_XML);
175: dep.setLocalRepository(TEST_REPOSITORY);
176: File testProjectFile = new File(TEST_PROJECT_XML);
177: List filenames = dep.getSnapshotFilenames(testProjectFile);
178: assertEquals("Filename list is not the correct size", 2,
179: filenames.size());
180: String filename = (String) filenames.get(0);
181: String expectedFilename = TEST_REPOSITORY
182: + "/maven/jars/cc-maven-test-1.0-SNAPSHOT.jar";
183: File expectedFile = new File(expectedFilename);
184: assertEquals("Unexpected filename", expectedFile
185: .getAbsolutePath(), filename);
186: filename = (String) filenames.get(1);
187: expectedFilename = TEST_REPOSITORY
188: + "/maven/jars/maven-1.0-SNAPSHOT.jar";
189: expectedFile = new File(expectedFilename);
190: assertEquals("Unexpected filename", expectedFile
191: .getAbsolutePath(), filename);
192: }
193:
194: public void testGettingModifications() throws Exception {
195: MavenSnapshotDependency dep = new MavenSnapshotDependency();
196:
197: dep.setProjectFile(TEST_PROJECT_XML);
198: dep.setLocalRepository(TEST_REPOSITORY);
199: Date epoch = new Date(0);
200: Date now = new Date();
201: List modifications = dep.getModifications(epoch, now);
202: assertEquals("Modification list is not the correct size", 2,
203: modifications.size());
204: }
205:
206: public void testGettingModificationsFromPOMWithoutNamespace()
207: throws Exception {
208: MavenSnapshotDependency dep = new MavenSnapshotDependency();
209:
210: dep.setProjectFile(getPOMPath("without-namespace-project.xml"));
211: dep.setLocalRepository(TEST_REPOSITORY);
212: Date epoch = new Date(0);
213: Date now = new Date();
214: List modifications = dep.getModifications(epoch, now);
215: assertEquals("Modification list is not the correct size", 3,
216: modifications.size());
217: }
218:
219: public void testGettingModificationsFromNamespacedPOM()
220: throws Exception {
221: MavenSnapshotDependency dep = new MavenSnapshotDependency();
222:
223: dep.setProjectFile(getPOMPath("with-namespace-project.xml"));
224: dep.setLocalRepository(TEST_REPOSITORY);
225: Date epoch = new Date(0);
226: Date now = new Date();
227: List modifications = dep.getModifications(epoch, now);
228: assertEquals("Modification list is not the correct size", 3,
229: modifications.size());
230: }
231:
232: private String getPOMPath(String file)
233: throws UnsupportedEncodingException {
234: String resource = "net/sourceforge/cruisecontrol/sourcecontrols/"
235: + file;
236: URL url = MavenSnapshotDependencyTest.class.getClassLoader()
237: .getResource(resource);
238: return URLDecoder.decode(url.getPath(), "UTF-8");
239: }
240:
241: public void testReplaceVariables() throws Exception {
242: MavenSnapshotDependency dep = new MavenSnapshotDependency();
243:
244: Properties p = new Properties();
245: p.put("env", "dev");
246: p.put("a", "alphabetagamma");
247: p.put("b", "b");
248: p.put("c", "cecilia");
249: p.put("foobarsnafu", "x");
250: p.put("cvs.user", "donmike");
251: p.put("cvsroot", ":ext:${cvs.user}@host:/cvs");
252: p.put("foo", "${bar}");
253: p.put("bar", "${foo}");
254: assertEquals("01. Variable replacement failed",
255: "foo-dev-1.0.jar", dep.replaceVariables(p,
256: "foo-${env}-1.0.jar"));
257: assertEquals(
258: "02. Variable replacement failed for variable-only-string",
259: "dev", dep.replaceVariables(p, "${env}"));
260: assertEquals(
261: "03. Variable replacement failed for an empty string",
262: "", dep.replaceVariables(p, ""));
263: assertEquals(
264: "04. Variable replacement failed for partial variable",
265: "${ffffffff", dep.replaceVariables(p, "${ffffffff"));
266: assertEquals("05. Multivariable replacement failed",
267: "alphabetagammabxcecilia", dep.replaceVariables(p,
268: "${a}${b}${foobarsnafu}${c}"));
269: assertEquals("06. Multireplacement failed",
270: "CVSROOT=:ext:donmike@host:/cvs!alphabetagamma", dep
271: .replaceVariables(p, "CVSROOT=${cvsroot}!${a}"));
272: assertEquals("07. Infinite loop", "${bar}${foo}", dep
273: .replaceVariables(p, "${foo}${bar}"));
274: assertTrue(
275: "08. System environment variable replacement failed",
276: dep.replaceVariables(p, "${user.home}").equals(
277: System.getProperty("user.home")));
278: assertEquals("09. Null replacement failed", null, dep
279: .replaceVariables(p, null));
280: }
281:
282: }
|