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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import hudson.model.FreeStyleProject;
27 import hudson.tasks.BuildStepMonitor;
28 import hudson.tasks.Builder;
29 import hudson.tasks.BuildStepDescriptor;
30 import hudson.tasks.Publisher;
31 import hudson.util.FormFieldValidatorTest.BrokenFormValidatorBuilder.DescriptorImpl;
32 import org.jvnet.hudson.test.Bug;
33 import org.jvnet.hudson.test.HudsonTestCase;
34 import org.jvnet.hudson.test.recipes.WithPlugin;
35
36 /**
37 * @author Kohsuke Kawaguchi
38 */
39 public class FormFieldValidatorTest extends HudsonTestCase {
40 @Bug(2771)
41 @WithPlugin("tasks.jpi")
42 public void test2771() throws Exception {
43 FreeStyleProject p = createFreeStyleProject();
44 new WebClient().getPage(p,"configure");
45 }
46
47 public static class BrokenFormValidatorBuilder extends Publisher {
48 public static final class DescriptorImpl extends BuildStepDescriptor {
49 public boolean isApplicable(Class jobType) {
50 return true;
51 }
52
53 public void doCheckXyz() {
54 throw new Error("doCheckXyz is broken");
55 }
56
57 public String getDisplayName() {
58 return "I have broken form field validation";
59 }
60 }
61
62 public BuildStepMonitor getRequiredMonitorService() {
63 return BuildStepMonitor.BUILD;
64 }
65 }
66
67 /**
68 * Make sure that the validation methods are really called by testing a negative case.
69 */
70 @Bug(3382)
71 public void testNegative() throws Exception {
72 DescriptorImpl d = new DescriptorImpl();
73 Publisher.all().add(d);
74 try {
75 FreeStyleProject p = createFreeStyleProject();
76 p.getPublishersList().add(new BrokenFormValidatorBuilder());
77 new WebClient().getPage(p,"configure");
78 fail("should have failed");
79 } catch(AssertionError e) {
80 if(e.getMessage().contains("doCheckXyz is broken"))
81 ; // expected
82 else
83 throw e;
84 } finally {
85 Publisher.all().remove(d);
86 }
87 }
88
89 }
90