1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.util.Hashtable ; 40 41 import junit.framework.TestCase; 42 import net.sourceforge.cruisecontrol.CruiseControlException; 43 import net.sourceforge.cruisecontrol.testutil.TestUtil; 44 45 import org.apache.log4j.Level; 46 import org.apache.log4j.Logger; 47 import org.apache.log4j.spi.LoggerRepository; 48 import org.jdom.Element; 49 50 public class Maven2ScriptTest extends TestCase { 51 52 53 public void testConsumeLine() throws Exception { 54 final Element buildLogElement = new Element("testBuild"); 55 final Maven2Script script = new Maven2Script(buildLogElement, null, null, null, null, null, null); 56 57 58 int contentIdx = 0; 59 Element currElement; 60 61 script.consumeLine("[ERROR] BUILD ERROR"); 62 currElement = ((Element) buildLogElement.getContent().get(contentIdx++)); 63 assertEquals("BUILD ERROR detected", buildLogElement.getAttribute("error").getValue()); 64 assertNull("BUILD ERROR detected", buildLogElement.getAttribute("success")); 65 assertEquals("message", currElement.getName()); 66 assertEquals("error", currElement.getAttribute("priority").getValue()); 67 68 script.consumeLine("BUILD SUCCESSFUL asdfasdf"); 69 currElement = ((Element) buildLogElement.getContent().get(contentIdx++)); 70 assertEquals("message", currElement.getName()); 71 assertEquals("info", currElement.getAttribute("priority").getValue()); 72 assertEquals("BUILD SUCCESSFUL detected", buildLogElement.getAttribute("success").getValue()); 73 74 script.consumeLine("[surefire] Tests run: 17, Failures: 1, Errors: 0"); 75 currElement = ((Element) buildLogElement.getContent().get(contentIdx++)); 76 assertEquals("message", currElement.getName()); 77 assertEquals("info", currElement.getAttribute("priority").getValue()); 78 79 script.consumeLine("[test info like //loading]"); 80 currElement = ((Element) buildLogElement.getContent().get(contentIdx++)); 81 assertEquals("message", currElement.getName()); 82 assertEquals("info", currElement.getAttribute("priority").getValue()); 83 84 script.consumeLine("[testmavengoal:pattern]"); 85 script.flushCurrentElement(); 86 currElement = ((Element) buildLogElement.getContent().get(contentIdx++)); 87 assertEquals("mavengoal", currElement.getName()); 88 assertEquals("testmavengoal:pattern", currElement.getAttribute("name").getValue()); 89 90 script.consumeLine("[INFO] Copying artifact[jar:saxon:saxon:6.5.3] to[saxon-6.5.3.jar]"); 91 currElement = ((Element) buildLogElement.getContent().get(contentIdx++)); 92 assertEquals("message", currElement.getName()); 93 assertEquals("info", currElement.getAttribute("priority").getValue()); 94 } 95 96 100 public void testGetCommandLineArgs() throws CruiseControlException { 101 Maven2Script script = getScript(); 102 103 TestUtil.assertArray( 104 "NoDebug:", 105 new String [] { 106 CMD_MVN, 107 "-B", 108 "-f", 109 CMD_POM, 110 "-Dlabel=" + CMD_LABEL }, 111 script.buildCommandline().getCommandline()); 112 113 script.setMvnScript("myscript.bat"); 114 TestUtil.assertArray( 115 "Windows:", 116 new String [] { 117 "myscript.bat", 118 "-B", 119 "-f", 120 CMD_POM, 121 "-Dlabel=" + CMD_LABEL }, 122 script.buildCommandline().getCommandline()); 123 124 script.setMvnScript(CMD_MVN); 125 script.setGoalset(" clean jar"); 126 TestUtil.assertArray( 127 "WithTarget:", 128 new String [] { 129 CMD_MVN, 130 "-B", 131 "-f", 132 CMD_POM, 133 "clean", 134 "jar", 135 "-Dlabel=" + CMD_LABEL }, 136 script.buildCommandline().getCommandline()); 138 } 139 140 public void testPropsWithSpace() throws CruiseControlException { 141 Maven2Script script = getScript(); 142 143 Hashtable propWithSpace = new Hashtable (); 144 propWithSpace.put("propertyWithSpace", "I have a space"); 145 script.setBuildProperties(propWithSpace); 146 TestUtil.assertArray( 147 "NoDebug:", 148 new String [] { 149 CMD_MVN, 150 "-B", 151 "-f", 152 CMD_POM }, 156 script.buildCommandline().getCommandline()); 157 } 158 159 private static final String CMD_MVN = "testmaven.sh"; 160 private static final String CMD_POM = "testproject.xml"; 161 private static final String CMD_LABEL = "200.1.23"; 162 163 private Maven2Script getScript() { 164 Maven2Script script = new Maven2Script(null, null, null, null, null, null, null); 165 script.setMvnScript(CMD_MVN); 167 script.setPomFile(CMD_POM); 168 169 Hashtable properties = new Hashtable (); 170 properties.put("label", CMD_LABEL); 171 script.setBuildProperties(properties); 172 return script; 173 } 174 175 public void testGetCommandLineArgsWithDebug() throws CruiseControlException { 176 Logger logger = Logger.getLogger(Maven2Script.class); 177 LoggerRepository loggerRepository = logger.getLoggerRepository(); 178 Level threshold = loggerRepository.getThreshold(); 179 Level level = logger.getLevel(); 180 181 loggerRepository.setThreshold(Level.ALL); 182 logger.setLevel(Level.DEBUG); 183 Maven2Script script = getScript(); 184 TestUtil.assertArray( 185 "WithDebug:", 186 new String [] { 187 CMD_MVN, 188 "-B", 189 "-X", 190 "-f", 191 CMD_POM, 192 "-Dlabel=" + CMD_LABEL }, 193 script.buildCommandline().getCommandline()); 194 195 loggerRepository.setThreshold(threshold); 196 logger.setLevel(level); 197 } 198 } 199 | Popular Tags |