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;
037:
038: import java.text.SimpleDateFormat;
039: import java.util.Date;
040: import java.util.List;
041:
042: import junit.framework.TestCase;
043:
044: public class ModificationTest extends TestCase {
045:
046: public void testToXml() {
047: Date modifiedTime = new Date();
048: SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
049: Modification mod = new Modification();
050:
051: Modification.ModifiedFile modfile = mod.createModifiedFile(
052: "File\"Name&", "Folder'Name");
053: modfile.action = "checkin";
054:
055: mod.modifiedTime = modifiedTime;
056: mod.userName = "User<>Name";
057: mod.comment = "Comment";
058:
059: String base = "<modification type=\"unknown\">"
060: + "<file action=\"checkin\"><filename>File\"Name&</filename>"
061: + "<project>Folder'Name</project></file>" + "<date>"
062: + formatter.format(modifiedTime) + "</date>"
063: + "<user>User<>Name</user>"
064: + "<comment><![CDATA[Comment]]></comment>";
065: String closingTag = "</modification>";
066: String expected = base + closingTag;
067:
068: assertEquals(expected, mod.toXml(formatter));
069:
070: String expectedWithEmail = base
071: + "<email>foo.bar@quuuux.quuux.quux.qux</email>"
072: + closingTag;
073: mod.emailAddress = "foo.bar@quuuux.quuux.quux.qux";
074:
075: assertEquals(expectedWithEmail, mod.toXml(formatter));
076: }
077:
078: public void testBadComment() {
079: Date modifiedTime = new Date();
080: SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
081: Modification mod = new Modification();
082:
083: Modification.ModifiedFile modfile = mod.createModifiedFile(
084: "File\"Name&", "Folder'Name");
085: modfile.action = "checkin";
086:
087: mod.modifiedTime = modifiedTime;
088: mod.userName = "User<>Name";
089: mod.comment = "Attempting to heal the wounded build.\0x18";
090:
091: String base = "<modification type=\"unknown\">"
092: + "<file action=\"checkin\"><filename>File\"Name&</filename>"
093: + "<project>Folder'Name</project></file>"
094: + "<date>"
095: + formatter.format(modifiedTime)
096: + "</date>"
097: + "<user>User<>Name</user>"
098: + "<comment><![CDATA[Unable to parse comment. It contains illegal data.]]></comment>";
099: String closingTag = "</modification>";
100: String expected = base + closingTag;
101:
102: assertEquals(expected, mod.toXml(formatter));
103: }
104:
105: public void testToElementAndBack() throws Exception {
106: Date modifiedTime = new Date();
107: SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
108: Modification mod = new Modification();
109:
110: Modification.ModifiedFile modfile = mod.createModifiedFile(
111: "File\"Name&", "Folder'Name");
112: modfile.action = "checkin";
113:
114: mod.modifiedTime = modifiedTime;
115: mod.userName = "User<>Name";
116: mod.comment = "Attempting to heal the wounded build.\0x18";
117:
118: Modification modification = new Modification();
119: modification.fromElement(mod.toElement(formatter), formatter);
120: mod.equals(modification);
121:
122: // Test getFullPath() of Modificaiton object.
123: assertEquals("Folder'Name/File\"Name&", modification
124: .getFullPath());
125: }
126:
127: public void testGettingModifiedFiles() {
128: Modification mod = new Modification();
129:
130: // Test getting when no files are present
131: assertNull(mod.getFileName());
132: assertNull(mod.getFolderName());
133: assertNotNull(mod.getModifiedFiles());
134: assertEquals(0, mod.getModifiedFiles().size());
135:
136: // Add first file
137: final String filename1 = "filename-1";
138: final String folder1 = "folder-1";
139: Modification.ModifiedFile file1 = mod.createModifiedFile(
140: filename1, folder1);
141:
142: assertEquals(filename1, file1.fileName);
143: assertEquals(folder1, file1.folderName);
144:
145: assertEquals(filename1, mod.getFileName());
146: assertEquals(folder1, mod.getFolderName());
147:
148: List modList = mod.getModifiedFiles();
149: assertEquals(1, modList.size());
150: assertSame(file1, modList.get(0));
151:
152: // Add second file
153: final String filename2 = "filename-2";
154: final String folder2 = "folder-2";
155: Modification.ModifiedFile file2 = mod.createModifiedFile(
156: filename2, folder2);
157:
158: assertEquals(filename2, file2.fileName);
159: assertEquals(folder2, file2.folderName);
160:
161: assertEquals(filename1, mod.getFileName());
162: assertEquals(folder1, mod.getFolderName());
163:
164: modList = mod.getModifiedFiles();
165: assertEquals(2, modList.size());
166: assertSame(file1, modList.get(0));
167: assertSame(file2, modList.get(1));
168:
169: // Add third file
170: final String filename3 = "filename-3";
171: final String folder3 = "folder-3";
172: Modification.ModifiedFile file3 = mod.createModifiedFile(
173: filename3, folder3);
174:
175: assertEquals(filename3, file3.fileName);
176: assertEquals(folder3, file3.folderName);
177:
178: assertEquals(filename1, mod.getFileName());
179: assertEquals(folder1, mod.getFolderName());
180:
181: modList = mod.getModifiedFiles();
182: assertEquals(3, modList.size());
183: assertSame(file1, modList.get(0));
184: assertSame(file2, modList.get(1));
185: assertSame(file3, modList.get(2));
186: }
187: }
|