001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, 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.IOException;
040: import java.net.MalformedURLException;
041: import java.net.URL;
042: import java.net.UnknownHostException;
043: import java.util.Date;
044: import java.util.List;
045:
046: import junit.framework.TestCase;
047: import net.sourceforge.cruisecontrol.CruiseControlException;
048: import net.sourceforge.cruisecontrol.Modification;
049:
050: public class HttpFileTest extends TestCase {
051:
052: public void testValidate() {
053: HttpFile httpFile = new HttpFile();
054:
055: try {
056: httpFile.validate();
057: fail("HttpFile should throw when url not set.");
058: } catch (CruiseControlException e) {
059: assertEquals("'url' is required for HttpFile", e
060: .getMessage());
061: }
062:
063: httpFile.setURL("Invalid URL");
064: try {
065: httpFile.validate();
066: fail("HttpFile should throw when an invalid URL is provided");
067: } catch (CruiseControlException e) {
068: assertEquals("'url' is not a valid connection string", e
069: .getMessage());
070: }
071: }
072:
073: public void testGetModifications() throws Exception {
074: final long timestamp = 100;
075: HttpFile httpFile = new HttpFile() {
076: protected long getURLLastModified(URL url) {
077: return timestamp;
078: }
079: };
080: httpFile.setURL("http://dummy.domain.net/my/path?que=ry");
081: List modifications = httpFile.getModifications(new Date(1),
082: new Date());
083: assertEquals(1, modifications.size());
084: Modification modif = (Modification) modifications.get(0);
085: assertEquals("my/path?que=ry", modif.getFileName());
086: assertEquals("dummy.domain.net", modif.getFolderName());
087: assertEquals("dummy.domain.net/my/path?que=ry", modif
088: .getFullPath());
089: assertEquals("", modif.comment);
090: assertEquals(timestamp, modif.modifiedTime.getTime());
091: assertEquals("User", modif.userName);
092: }
093:
094: public void testShouldGetEmptyModificationListWhenURLNotAvailable()
095: throws MalformedURLException {
096: HttpFile httpFile = new HttpFile() {
097: protected long getURLLastModified(URL url)
098: throws IOException {
099: throw new UnknownHostException(url.getHost());
100: }
101: };
102: String urlString = "http://NOTAREALURL"
103: + System.currentTimeMillis() + ".com";
104: new URL(urlString);
105: httpFile.setURL(urlString);
106: assertEquals(0, httpFile.getModifications(new Date(),
107: new Date()).size());
108: }
109:
110: public void testShouldGetEmptyModificationnListWhenURLMalformed() {
111: HttpFile httpFile = new HttpFile();
112: httpFile.setURL("THISISAMALFORMEDURL");
113: assertEquals(0, httpFile.getModifications(new Date(),
114: new Date()).size());
115: }
116:
117: public void testGetModificationsInvalidURL() throws Exception {
118: HttpFile httpFile = new HttpFile();
119: File tempFile = File.createTempFile("HttpFileTest", null);
120: tempFile.deleteOnExit();
121: httpFile.setURL(tempFile.toURI().toURL().toExternalForm());
122: tempFile.delete();
123: // should not throw with a URL that cannot be connected to
124: httpFile.getModifications(new Date(), new Date());
125: }
126:
127: public void testEmptyPath() throws Exception {
128: final long timestamp = 100;
129: HttpFile httpFile = new HttpFile() {
130: protected long getURLLastModified(URL url) {
131: return timestamp;
132: }
133: };
134: httpFile.setURL("http://dummy.domain.net");
135: List modifications = httpFile.getModifications(new Date(1),
136: new Date());
137: assertEquals(1, modifications.size());
138: Modification modif = (Modification) modifications.get(0);
139: assertEquals("", modif.getFileName());
140: assertEquals("dummy.domain.net", modif.getFolderName());
141: assertEquals("dummy.domain.net/", modif.getFullPath());
142: assertEquals("", modif.comment);
143: assertEquals(timestamp, modif.modifiedTime.getTime());
144: assertEquals("User", modif.userName);
145: }
146: }
|