KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > builders > Maven2ScriptTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.builders;
38
39 import java.util.Hashtable JavaDoc;
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 JavaDoc {
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     /**
97      * String[] getCommandLineArgs(Map, boolean, boolean, boolean, String)
98      * @throws CruiseControlException
99      */

100     public void testGetCommandLineArgs() throws CruiseControlException {
101         Maven2Script script = getScript();
102
103         TestUtil.assertArray(
104             "NoDebug:",
105             new String JavaDoc[] {
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 JavaDoc[] {
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 JavaDoc[] {
129                 CMD_MVN,
130                 "-B",
131                 "-f",
132                 CMD_POM,
133                 "clean",
134                 "jar",
135                 "-Dlabel=" + CMD_LABEL },
136             // notice the spaces in goalSet
137
script.buildCommandline().getCommandline());
138     }
139
140     public void testPropsWithSpace() throws CruiseControlException {
141         Maven2Script script = getScript();
142
143         Hashtable JavaDoc propWithSpace = new Hashtable JavaDoc();
144         propWithSpace.put("propertyWithSpace", "I have a space");
145         script.setBuildProperties(propWithSpace);
146         TestUtil.assertArray(
147             "NoDebug:",
148             new String JavaDoc[] {
149             CMD_MVN,
150             "-B",
151             "-f",
152             CMD_POM //,
153
// @todo Fix Maven2Scipt to handle props w/ spaces
154
//"-DpropertyWithSpace=I have a space"
155
},
156             script.buildCommandline().getCommandline());
157     }
158
159     private static final String JavaDoc CMD_MVN = "testmaven.sh";
160     private static final String JavaDoc CMD_POM = "testproject.xml";
161     private static final String JavaDoc CMD_LABEL = "200.1.23";
162
163     private Maven2Script getScript() {
164       Maven2Script script = new Maven2Script(null, null, null, null, null, null, null);
165       // none should exist for this test
166
script.setMvnScript(CMD_MVN);
167       script.setPomFile(CMD_POM);
168
169       Hashtable JavaDoc properties = new Hashtable JavaDoc();
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 JavaDoc[] {
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