1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.io.File ; 40 import java.net.URL ; 41 import java.net.URLDecoder ; 42 import java.util.Date ; 43 import java.util.List ; 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 BAD_REPOSITORY = "folder"; 51 52 private static final String PROJECT_XML_RELATIVE_PATH = 53 "net/sourceforge/cruisecontrol/sourcecontrols/maven-project.xml"; 54 55 private static final String TEST_PROJECT_XML; 56 private static final String TEST_REPOSITORY; 57 58 static { 59 URL projectUrl = ClassLoader.getSystemResource(PROJECT_XML_RELATIVE_PATH); 60 TEST_PROJECT_XML = URLDecoder.decode(projectUrl.getPath()); 61 TEST_REPOSITORY = new File (TEST_PROJECT_XML).getParentFile().getAbsolutePath(); 63 } 64 65 public MavenSnapshotDependencyTest(String 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 fileName = BAD_REPOSITORY; 84 dep.setProjectFile(fileName); 85 File f = new File (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 fileName = TEST_REPOSITORY; 99 dep.setProjectFile(fileName); 100 File f = new File (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 fileName = BAD_REPOSITORY; 117 dep.setProjectFile(TEST_PROJECT_XML); 118 dep.setLocalRepository(fileName); 119 File f = new File (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 fileName = TEST_PROJECT_XML; 133 dep.setProjectFile(fileName); 134 dep.setLocalRepository(fileName); 135 File f = new File (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 { 160 MavenSnapshotDependency dep = new MavenSnapshotDependency(); 161 162 dep.setProjectFile(TEST_PROJECT_XML); 163 dep.setLocalRepository(TEST_REPOSITORY); 164 File testProjectFile = new File (TEST_PROJECT_XML); 165 List filenames = dep.getSnapshotFilenames(testProjectFile); 166 assertEquals("Filename list is not the correct size", 2, filenames.size()); 167 String filename = (String ) filenames.get(0); 168 String expectedFilename = TEST_REPOSITORY + "/maven/jars/cc-maven-test-1.0-SNAPSHOT.jar"; 169 File expectedFile = new File (expectedFilename); 170 assertEquals("Unexpected filename", expectedFile.getAbsolutePath(), filename); 171 filename = (String ) filenames.get(1); 172 expectedFilename = TEST_REPOSITORY + "/maven/jars/maven-1.0-SNAPSHOT.jar"; 173 expectedFile = new File (expectedFilename); 174 assertEquals("Unexpected filename", expectedFile.getAbsolutePath(), filename); 175 } 176 177 public void testGettingModifications() throws Exception { 178 MavenSnapshotDependency dep = new MavenSnapshotDependency(); 179 180 dep.setProjectFile(TEST_PROJECT_XML); 181 dep.setLocalRepository(TEST_REPOSITORY); 182 Date epoch = new Date (0); 183 Date now = new Date (); 184 List modifications = dep.getModifications(epoch, now); 185 assertEquals("Modification list is not the correct size", 2, modifications.size()); 186 } 187 } 188 | Popular Tags |