File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\Result.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, 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.model;
25
26 import com.thoughtworks.xstream.converters.SingleValueConverter;
27 import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
28 import hudson.cli.declarative.OptionHandlerExtension;
29 import hudson.util.EditDistance;
30 import hudson.util.EnumConverter;
31 import org.apache.commons.beanutils.Converter;
32 import org.kohsuke.args4j.CmdLineException;
33 import org.kohsuke.args4j.CmdLineParser;
34 import org.kohsuke.args4j.OptionDef;
35 import org.kohsuke.args4j.spi.*;
36 import org.kohsuke.stapler.Stapler;
37 import org.kohsuke.stapler.export.CustomExportedBean;
38
39 import java.io.Serializable;
40 import java.util.ArrayList;
41 import java.util.List;
42
43 /**
44 * The build outcome.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public final class Result implements Serializable, CustomExportedBean {
49 /**
50 * The build had no errors.
51 */
52 public static final Result SUCCESS = new Result("SUCCESS",BallColor.BLUE,0);
53 /**
54 * The build had some errors but they were not fatal.
55 * For example, some tests failed.
56 */
57 public static final Result UNSTABLE = new Result("UNSTABLE",BallColor.YELLOW,1);
58 /**
59 * The build had a fatal error.
60 */
61 public static final Result FAILURE = new Result("FAILURE",BallColor.RED,2);
62 /**
63 * The module was not built.
64 * <p>
65 * This status code is used in a multi-stage build (like maven2)
66 * where a problem in earlier stage prevented later stages from building.
67 */
68 public static final Result NOT_BUILT = new Result("NOT_BUILT",BallColor.NOTBUILT,3);
69 /**
70 * The build was manually aborted.
71 *
72 * If you are catching {@link InterruptedException} and interpreting it as {@link #ABORTED},
73 * you should check {@link Executor#abortResult()} instead (starting 1.417.)
74 */
75 public static final Result ABORTED = new Result("ABORTED",BallColor.ABORTED,4);
76
77 private final String name;
78
79 /**
80 * Bigger numbers are worse.
81 */
82 public final int ordinal;
83
84 /**
85 * Default ball color for this status.
86 */
87 public final BallColor color;
88
89 private Result(String name, BallColor color, int ordinal) {
90 this.name = name;
91 this.color = color;
92 this.ordinal = ordinal;
93 }
94
95 /**
96 * Combines two {@link Result}s and returns the worse one.
97 */
98 public Result combine(Result that) {
99 if(this.ordinal < that.ordinal)
100 return that;
101 else
102 return this;
103 }
104
105 public boolean isWorseThan(Result that) {
106 return this.ordinal > that.ordinal;
107 }
108
109 public boolean isWorseOrEqualTo(Result that) {
110 return this.ordinal >= that.ordinal;
111 }
112
113 public boolean isBetterThan(Result that) {
114 return this.ordinal < that.ordinal;
115 }
116
117 public boolean isBetterOrEqualTo(Result that) {
118 return this.ordinal <= that.ordinal;
119 }
120
121
122 @Override
123 public String toString() {
124 return name;
125 }
126
127 public String toExportedObject() {
128 return name;
129 }
130
131 public static Result fromString(String s) {
132 for (Result r : all)
133 if (s.equalsIgnoreCase(r.name))
134 return r;
135 return FAILURE;
136 }
137
138 private static List<String> getNames() {
139 List<String> l = new ArrayList<String>();
140 for (Result r : all)
141 l.add(r.name);
142 return l;
143 }
144
145 // Maintain each Result as a singleton deserialized (like build result from a slave node)
146 private Object readResolve() {
147 for (Result r : all)
148 if (ordinal==r.ordinal)
149 return r;
150 return FAILURE;
151 }
152
153 private static final long serialVersionUID = 1L;
154
155 private static final Result[] all = new Result[] {SUCCESS,UNSTABLE,FAILURE,NOT_BUILT,ABORTED};
156
157 public static final SingleValueConverter conv = new AbstractSingleValueConverter () {
158 public boolean canConvert(Class clazz) {
159 return clazz==Result.class;
160 }
161
162 public Object fromString(String s) {
163 return Result.fromString(s);
164 }
165 };
166
167 @OptionHandlerExtension
168 public static final class OptionHandlerImpl extends OptionHandler<Result> {
169 public OptionHandlerImpl(CmdLineParser parser, OptionDef option, Setter<? super Result> setter) {
170 super(parser, option, setter);
171 }
172
173 @Override
174 public int parseArguments(Parameters params) throws CmdLineException {
175 String param = params.getParameter(0);
176 Result v = fromString(param.replace('-', '_'));
177 if (v==null)
178 throw new CmdLineException(owner,"No such status '"+param+"'. Did you mean "+
179 EditDistance.findNearest(param.replace('-', '_').toUpperCase(), getNames()));
180 setter.addValue(v);
181 return 1;
182 }
183
184 @Override
185 public String getDefaultMetaVariable() {
186 return "STATUS";
187 }
188 }
189
190 static {
191 Stapler.CONVERT_UTILS.register(new Converter() {
192 public Object convert(Class type, Object value) {
193 return Result.fromString(value.toString());
194 }
195 }, Result.class);
196 }
197 }
198