Skip to content

Commit

Permalink
Merged #16: [JENKINS-31967] Specifying decimal numbers for JUnit heal…
Browse files Browse the repository at this point in the history
…th factor in Pipeline snippets results in invalid code
  • Loading branch information
jglick committed May 25, 2017
2 parents 8d0f4ad + 1485eca commit 5eaa477
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
Expand Up @@ -10,7 +10,6 @@
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.util.ReflectionUtils;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ObjectUtils;
Expand Down Expand Up @@ -318,7 +317,7 @@ private Object[] buildArguments(Map<String,?> bag, Type[] types, String[] names,
if (a != null) {
args[i] = coerce(this.type.getName() + "." + name, type, a);
} else if (type instanceof Class && ((Class) type).isPrimitive()) {
args[i] = ReflectionUtils.getVmDefaultValueForPrimitiveType((Class)type);
args[i] = getVmDefaultValueForPrimitiveType((Class)type);
if (args[i]==null && callEvenIfNoArgs)
throw new UnsupportedOperationException("not yet handling @DataBoundConstructor default value of " + type + "; pass an explicit value for " + name);
} else {
Expand Down Expand Up @@ -710,4 +709,23 @@ private Object readResolve() {
private static final Logger LOGGER = Logger.getLogger(DescribableModel.class.getName());

private static final long serialVersionUID = 1L;

// TODO: switch to use {@link hudson.util.ReflectionUtils}
/**
* Given the primitive type, returns the VM default value for that type in a boxed form.
*/
private static Object getVmDefaultValueForPrimitiveType(Class<?> type) {
return defaultPrimitiveValue.get(type);
}

private static final Map<Class,Object> defaultPrimitiveValue = new HashMap<Class, Object>();
static {
defaultPrimitiveValue.put(boolean.class, false);
defaultPrimitiveValue.put(byte.class, (byte) 0);
defaultPrimitiveValue.put(short.class, (short) 0);
defaultPrimitiveValue.put(int.class, 0);
defaultPrimitiveValue.put(long.class, 0L);
defaultPrimitiveValue.put(float.class, 0.0f);
defaultPrimitiveValue.put(double.class, 0.0d);
}
}
Expand Up @@ -789,4 +789,88 @@ private void roundTrip(Class<?> c, Map<String,Object> m, String toString) throws
private static void schema(Class<?> c, String schema) throws Exception {
assertEquals(schema, new DescribableModel(c).toString());
}

public static final class AllJavaStandardTypesClass {
// final values (set in constructor)
private final boolean booleanValue1;
private final byte byteValue1;
private final short shortValue1;
private final int intValue1;
private final long longValue1;
private final float floatValue1;
private final double doubleValue1;


@DataBoundConstructor
public AllJavaStandardTypesClass(boolean booleanValue1, byte byteValue1, short shortValue1,
int intValue1, long longValue1, float floatValue1, double doubleValue1) {
this.booleanValue1 = booleanValue1;
this.byteValue1 = byteValue1;
this.shortValue1 = shortValue1;
this.intValue1 = intValue1;
this.longValue1 = longValue1;
this.floatValue1 = floatValue1;
this.doubleValue1 = doubleValue1;
}

public boolean isBooleanValue1() {
return booleanValue1;
}
public byte getByteValue1() {
return byteValue1;
}
public short getShortValue1() {
return shortValue1;
}
public int getIntValue1() {
return intValue1;
}
public long getLongValue1() {
return longValue1;
}
public float getFloatValue1() {
return floatValue1;
}
public double getDoubleValue1() {
return doubleValue1;
}

@Override
public String toString() {
return "AllJavaStandardTypesClass{" +
"" + booleanValue1 +
"," + byteValue1 +
"," + shortValue1 +
"," + intValue1 +
"," + longValue1 +
"," + floatValue1 +
"," + doubleValue1 +
'}';
}
}

@Issue("JENKINS-31967")
@Test
public void testJavaStandardTypes() throws Exception {
// check instantiate with not default values
roundTrip(AllJavaStandardTypesClass.class, map(
"booleanValue1", Boolean.TRUE,
"byteValue1", Byte.MAX_VALUE,
"shortValue1", Short.MAX_VALUE,
"intValue1", Integer.MAX_VALUE,
"longValue1", Long.MAX_VALUE,
"floatValue1", Float.MAX_VALUE,
"doubleValue1", Double.MAX_VALUE
));
// check with default values
roundTrip(AllJavaStandardTypesClass.class, map(
"booleanValue1", false,
"byteValue1", (byte) 0,
"shortValue1", (short) 0,
"intValue1", 0,
"longValue1", (long) 0,
"floatValue1", (float) 0.0,
"doubleValue1", 0.0
));
}
}

0 comments on commit 5eaa477

Please sign in to comment.