/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.tasks;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.PasswordParameterDefinition;
import hudson.model.StringParameterDefinition;
import hudson.tasks.Ant.AntInstallation;
import hudson.tasks.Ant.AntInstallation.DescriptorImpl;
import hudson.tasks.Ant.AntInstaller;
import hudson.tools.InstallSourceProperty;
import hudson.tools.ToolProperty;
import hudson.tools.ToolPropertyDescriptor;
import hudson.util.DescribableList;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.SingleFileSCM;
/**
* @author Kohsuke Kawaguchi
*/
public class AntTest extends HudsonTestCase {
/**
* Tests the round-tripping of the configuration.
*/
public void testConfigRoundtrip() throws Exception {
FreeStyleProject p = createFreeStyleProject();
p.getBuildersList().add(new Ant("a",null,"-b","c.xml","d=e"));
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage(p,"configure");
HtmlForm form = page.getFormByName("config");
submit(form);
Ant a = p.getBuildersList().get(Ant.class);
assertNotNull(a);
assertEquals("a",a.getTargets());
assertNull(a.getAnt());
assertEquals("-b",a.getAntOpts());
assertEquals("c.xml",a.getBuildFile());
assertEquals("d=e",a.getProperties());
}
/**
* Simulates the addition of the new Ant via UI and makes sure it works.
*/
public void testGlobalConfigAjax() throws Exception {
HtmlPage p = new WebClient().goTo("configure");
HtmlForm f = p.getFormByName("config");
HtmlButton b = getButtonByCaption(f, "Add Ant");
b.click();
findPreviousInputElement(b,"name").setValueAttribute("myAnt");
findPreviousInputElement(b,"home").setValueAttribute("/tmp/foo");
submit(f);
verify();
// another submission and verfify it survives a roundtrip
p = new WebClient().goTo("configure");
f = p.getFormByName("config");
submit(f);
verify();
}
private void verify() throws Exception {
AntInstallation[] l = get(DescriptorImpl.class).getInstallations();
assertEquals(1,l.length);
assertEqualBeans(l[0],new AntInstallation("myAnt","/tmp/foo",NO_PROPERTIES),"name,home");
// by default we should get the auto installer
DescribableList<ToolProperty<?>,ToolPropertyDescriptor> props = l[0].getProperties();
assertEquals(1,props.size());
InstallSourceProperty isp = props.get(InstallSourceProperty.class);
assertEquals(1,isp.installers.size());
assertNotNull(isp.installers.get(AntInstaller.class));
}
public void testSensitiveParameters() throws Exception {
FreeStyleProject project = createFreeStyleProject();
ParametersDefinitionProperty pdb = new ParametersDefinitionProperty(
new StringParameterDefinition("string", "defaultValue", "string description"),
new PasswordParameterDefinition("password", "12345", "password description"),
new StringParameterDefinition("string2", "Value2", "string description")
);
project.addProperty(pdb);
project.setScm(new SingleFileSCM("build.xml", hudson.tasks._ant.AntTargetAnnotationTest.class.getResource("simple-build.xml")));
project.getBuildersList().add(new Ant("foo",null,null,null,null));
FreeStyleBuild build = project.scheduleBuild2(0).get();
String buildLog = build.getLog();
assertNotNull(buildLog);
System.out.println(buildLog);
assertFalse(buildLog.contains("-Dpassword=12345"));
}
}