File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\tools\JDKInstallerTest.java.html

1 package hudson.tools;
2
3 import com.gargoylesoftware.htmlunit.html.HtmlForm;
4 import com.gargoylesoftware.htmlunit.html.HtmlPage;
5 import hudson.model.DownloadService;
6 import hudson.tools.JDKInstaller.DescriptorImpl;
7 import org.jvnet.hudson.test.HudsonTestCase;
8 import hudson.model.JDK;
9 import hudson.model.FreeStyleProject;
10 import hudson.model.FreeStyleBuild;
11 import hudson.model.TaskListener;
12 import hudson.tasks.Shell;
13 import hudson.util.StreamTaskListener;
14 import hudson.tools.JDKInstaller.Platform;
15 import hudson.tools.JDKInstaller.CPU;
16 import hudson.FilePath;
17 import hudson.Functions;
18 import hudson.Launcher.LocalLauncher;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.Properties;
25 import java.util.logging.Logger;
26
27 import org.jvnet.hudson.test.Bug;
28 import org.xml.sax.SAXException;
29
30 /**
31 * @author Kohsuke Kawaguchi
32 */
33 public class JDKInstallerTest extends HudsonTestCase {
34 boolean old;
35
36 @Override
37 protected void setUp() throws Exception {
38 super.setUp();
39 old = DownloadService.neverUpdate;
40 DownloadService.neverUpdate = false;
41
42 File f = new File(new File(System.getProperty("user.home")),".jenkins-ci.org");
43 if (!f.exists()) {
44 LOGGER.warning(f+" doesn't exist. Skipping JDK installation tests");
45 } else {
46 Properties prop = new Properties();
47 FileInputStream in = new FileInputStream(f);
48 try {
49 prop.load(in);
50 String u = prop.getProperty("oracle.userName");
51 String p = prop.getProperty("oracle.password");
52 if (u==null || p==null) {
53 LOGGER.warning(f+" doesn't contain oracle.userName and oracle.password. Skipping JDK installation tests.");
54 } else {
55 DescriptorImpl d = jenkins.getDescriptorByType(DescriptorImpl.class);
56 d.doPostCredential(u,p);
57 }
58 } finally {
59 in.close();
60 }
61 }
62 }
63
64 @Override
65 protected void tearDown() throws Exception {
66 DownloadService.neverUpdate = old;
67 super.tearDown();
68 }
69
70 public void testEnterCredential() throws Exception {
71 HtmlPage p = createWebClient().goTo("/descriptorByName/hudson.tools.JDKInstaller/enterCredential");
72 HtmlForm form = p.getFormByName("postCredential");
73 form.getInputByName("username").setValueAttribute("foo");
74 form.getInputByName("password").setValueAttribute("bar");
75 form.submit(null);
76
77 DescriptorImpl d = jenkins.getDescriptorByType(DescriptorImpl.class);
78 assertEquals("foo",d.getUsername());
79 assertEquals("bar",d.getPassword().getPlainText());
80 }
81
82 /**
83 * Tests the configuration round trip.
84 */
85 public void testConfigRoundtrip() throws Exception {
86 File tmp = env.temporaryDirectoryAllocator.allocate();
87 JDKInstaller installer = new JDKInstaller("jdk-6u13-oth-JPR@CDS-CDS_Developer", true);
88
89 jenkins.getJDKs().add(new JDK("test",tmp.getAbsolutePath(), Arrays.asList(
90 new InstallSourceProperty(Arrays.<ToolInstaller>asList(installer)))));
91
92 submit(new WebClient().goTo("configure").getFormByName("config"));
93
94 JDK jdk = jenkins.getJDK("test");
95 InstallSourceProperty isp = jdk.getProperties().get(InstallSourceProperty.class);
96 assertEquals(1,isp.installers.size());
97 assertEqualBeans(installer,isp.installers.get(JDKInstaller.class),"id,acceptLicense");
98 }
99
100 /**
101 * Can we locate the bundles?
102 */
103 public void testLocate() throws Exception {
104 // this is a really time consuming test, so only run it when we really want.
105 if(!Boolean.getBoolean("jenkins.testJDKInstaller"))
106 return;
107
108 retrieveUpdateCenterData();
109
110 JDKInstaller i = new JDKInstaller("jdk-7u3-oth-JPR", true);
111 StreamTaskListener listener = StreamTaskListener.fromStdout();
112 i.locate(listener, Platform.LINUX, CPU.i386);
113 i.locate(listener, Platform.WINDOWS, CPU.amd64);
114 i.locate(listener, Platform.SOLARIS, CPU.Sparc);
115 }
116
117 private void retrieveUpdateCenterData() throws IOException, SAXException {
118 new WebClient().goTo("/"); // make sure data is loaded
119 }
120
121 /**
122 * Tests the auto installation.
123 */
124 public void testAutoInstallation6u13() throws Exception {
125 doTestAutoInstallation("jdk-6u13-oth-JPR@CDS-CDS_Developer", "1.6.0_13-b03");
126 }
127
128 /**
129 * JDK7 is distributed as a gzip file
130 */
131 public void testAutoInstallation7() throws Exception {
132 doTestAutoInstallation("jdk-7-oth-JPR", "1.7.0-b147");
133 }
134
135 @Bug(3989)
136 public void testAutoInstallation142_17() throws Exception {
137 doTestAutoInstallation("j2sdk-1.4.2_17-oth-JPR@CDS-CDS_Developer", "1.4.2_17-b06");
138 }
139
140 /**
141 * End-to-end installation test.
142 */
143 private void doTestAutoInstallation(String id, String fullversion) throws Exception {
144 // this is a really time consuming test, so only run it when we really want
145 if(!Boolean.getBoolean("jenkins.testJDKInstaller"))
146 return;
147
148 retrieveUpdateCenterData();
149
150 File tmp = env.temporaryDirectoryAllocator.allocate();
151 JDKInstaller installer = new JDKInstaller(id, true);
152
153 JDK jdk = new JDK("test", tmp.getAbsolutePath(), Arrays.asList(
154 new InstallSourceProperty(Arrays.<ToolInstaller>asList(installer))));
155
156 jenkins.getJDKs().add(jdk);
157
158 FreeStyleProject p = createFreeStyleProject();
159 p.setJDK(jdk);
160 p.getBuildersList().add(new Shell("java -fullversion\necho $JAVA_HOME"));
161 FreeStyleBuild b = buildAndAssertSuccess(p);
162 @SuppressWarnings("deprecation") String log = b.getLog();
163 System.out.println(log);
164 // make sure it runs with the JDK that just got installed
165 assertTrue(log.contains(fullversion));
166 assertTrue(log.contains(tmp.getAbsolutePath()));
167 }
168
169 /**
170 * Fake installation on Unix.
171 */
172 public void testFakeUnixInstall() throws Exception {
173 // If we're on Windows, don't bother doing this.
174 if (Functions.isWindows())
175 return;
176
177 File bundle = File.createTempFile("fake-jdk-by-hudson","sh");
178 try {
179 new FilePath(bundle).write(
180 "#!/bin/bash -ex\n" +
181 "mkdir -p jdk1.6.0_dummy/bin\n" +
182 "touch jdk1.6.0_dummy/bin/java","ASCII");
183 TaskListener l = StreamTaskListener.fromStdout();
184
185 File d = env.temporaryDirectoryAllocator.allocate();
186
187 new JDKInstaller("",true).install(new LocalLauncher(l),Platform.LINUX,
188 new JDKInstaller.FilePathFileSystem(jenkins),l,d.getPath(),bundle.getPath());
189
190 assertTrue(new File(d,"bin/java").exists());
191 } finally {
192 bundle.delete();
193 }
194 }
195
196 private static final Logger LOGGER = Logger.getLogger(JDKInstallerTest.class.getName());
197 }
198