1 37 package net.sourceforge.cruisecontrol.util; 38 39 import java.text.SimpleDateFormat ; 40 import java.util.Date ; 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.Map ; 44 import java.util.Set ; 45 46 import junit.framework.TestCase; 47 import net.sourceforge.cruisecontrol.CruiseControlException; 48 import net.sourceforge.cruisecontrol.Modification; 49 import net.sourceforge.cruisecontrol.testutil.TestUtil; 50 51 import org.jdom.Element; 52 53 56 public class XMLLogHelperTest extends TestCase { 57 58 private Element successfulLogElement; 59 private Element failedLogElement; 60 61 private static final Date SOME_DATE = new Date (2333); 62 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("yyyyMMddHHmmss"); 63 64 public XMLLogHelperTest(String name) { 65 super(name); 66 } 67 68 private Modification[] createModifications(String username1, String username2) { 69 Modification[] mods = new Modification[3]; 70 mods[0] = createModification(username1, false); 71 mods[1] = createModification(username2, false); 72 mods[2] = createModification("user3", true); 73 return mods; 74 } 75 76 private Modification createModification(String name, boolean addemail) { 77 Modification mod = new Modification(); 78 mod.userName = name; 79 mod.comment = "This is the checkin for " + name; 80 if (addemail) { 81 mod.emailAddress = name + "@host.com"; 82 } 83 mod.modifiedTime = SOME_DATE; 84 85 Modification.ModifiedFile modfile = mod.createModifiedFile("file.txt", "myfolder"); 86 modfile.action = "checkin"; 87 return mod; 88 } 89 90 private Element createModificationsElement(String username1, String username2) { 91 Element modificationsElement = new Element("modifications"); 92 Modification[] mods = createModifications(username1, username2); 93 modificationsElement.addContent(mods[0].toElement(DATE_FORMAT)); 94 modificationsElement.addContent(mods[1].toElement(DATE_FORMAT)); 95 modificationsElement.addContent(mods[2].toElement(DATE_FORMAT)); 96 return modificationsElement; 97 } 98 99 private Element createBuildElement(boolean successful) { 100 Element buildElement = new Element("build"); 101 if (!successful) { 102 buildElement.setAttribute("error", "No Build Necessary"); 103 } 104 return buildElement; 105 } 106 107 public void setUp() { 108 successfulLogElement = new Element("cruisecontrol"); 109 successfulLogElement.addContent(TestUtil.createInfoElement("1.0", false)); 110 successfulLogElement.addContent(createBuildElement(true)); 111 successfulLogElement.addContent(createModificationsElement("username1", "username2")); 112 113 failedLogElement = new Element("cruisecontrol"); 114 failedLogElement.addContent(TestUtil.createInfoElement("1.1", true)); 115 failedLogElement.addContent(createBuildElement(false)); 116 failedLogElement.addContent(createModificationsElement("username3", "username4")); 117 } 118 119 public void testGetLabel() { 120 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 121 XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement); 122 try { 123 assertEquals("1.0", successHelper.getLabel()); 124 assertEquals("1.1", failureHelper.getLabel()); 125 } catch (CruiseControlException e) { 126 assertTrue(false); 127 } 128 } 129 130 public void testGetLogFileName() { 131 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 132 try { 133 assertEquals("log20020313120000.xml", successHelper.getLogFileName()); 134 } catch (CruiseControlException e) { 135 assertTrue(false); 136 } 137 } 138 139 public void testWasPreviousBuildSuccessful() { 140 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 141 XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement); 142 try { 143 assertEquals(false, successHelper.wasPreviousBuildSuccessful()); 144 assertEquals(true, failureHelper.wasPreviousBuildSuccessful()); 145 } catch (CruiseControlException e) { 146 assertTrue(false); 147 } 148 } 149 150 public void testGetCruiseControlInfoProperty() { 151 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 152 try { 153 assertEquals("1.0", successHelper.getCruiseControlInfoProperty("label")); 154 } catch (CruiseControlException e) { 155 assertTrue(false); 156 } 157 try { 158 successHelper.getCruiseControlInfoProperty("notaproperty"); 159 } catch (CruiseControlException e) { 160 assertEquals("Property: notaproperty not found.", e.getMessage()); 161 } 162 } 163 164 public void testIsBuildNecessary() { 165 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 166 XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement); 167 assertEquals(true, successHelper.isBuildNecessary()); 168 assertEquals(false, failureHelper.isBuildNecessary()); 169 } 170 171 public void testGetProjectName() { 172 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 173 174 try { 175 assertEquals("someproject", successHelper.getProjectName()); 176 } catch (CruiseControlException e) { 177 assertTrue(false); 178 } 179 } 180 181 public void testIsBuildSuccessful() { 182 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 183 XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement); 184 assertEquals(true, successHelper.isBuildSuccessful()); 185 assertEquals(false, failureHelper.isBuildSuccessful()); 186 } 187 188 public void testGetBuildParticipants() { 189 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement); 190 Set successHelperParticipants = successHelper.getBuildParticipants(); 191 assertEquals(true, successHelperParticipants.contains("username1")); 192 assertEquals(true, successHelperParticipants.contains("username2")); 193 assertEquals(false, successHelperParticipants.contains("notaperson")); 194 assertEquals(true, successHelperParticipants.contains("user3@host.com")); 195 196 Element ccElement = new Element("cruisecontrol"); 198 Element modsElement = new Element("modifications"); 199 Element cl1Element = new Element("changelist"); 200 cl1Element.setAttribute("user", "user1"); 201 Element cl2Element = new Element("changelist"); 202 cl2Element.setAttribute("user", "user2"); 203 204 modsElement.addContent(cl1Element); 205 modsElement.addContent(cl2Element); 206 ccElement.addContent(modsElement); 207 208 XMLLogHelper helper = new XMLLogHelper(ccElement); 209 Set p4Users = helper.getBuildParticipants(); 210 211 assertEquals(true, p4Users.contains("user1")); 212 assertEquals(true, p4Users.contains("user2")); 213 assertEquals(false, p4Users.contains("notaperson")); 214 } 215 216 public void testGetModifications() { 217 XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement, DATE_FORMAT); 222 Set modifications = successHelper.getModifications(); 223 Modification[] mods = createModifications("username1", "username2"); 224 Map map = createMapByUserName(mods); 225 for (Iterator iterator = modifications.iterator(); iterator.hasNext();) { 226 Modification actual = (Modification) iterator.next(); 227 Modification expected = (Modification) map.get(actual.userName); 228 assertNotNull(actual.userName, expected); 229 assertModificationsEquals(expected, actual); 230 } 231 } 232 233 private Map createMapByUserName(Modification[] modifications) { 234 Map map = new HashMap (); 235 for (int i = 0; i < modifications.length; i++) { 236 map.put(modifications[i].userName, modifications[i]); 237 } 238 239 return map; 240 } 241 242 private void assertModificationsEquals(Modification expected, Modification actual) { 243 assertDateEquals(expected.modifiedTime, actual.modifiedTime); 244 actual.modifiedTime = SOME_DATE; 245 assertEquals(expected, actual); 246 } 247 248 private void assertDateEquals(Date expected, Date actual) { 249 assertEquals(DATE_FORMAT.format(expected), DATE_FORMAT.format(actual)); 250 } 251 252 } | Popular Tags |