public ValidMockery() { safetyCast = new SafetyCast(); }
/** * Return empty if type is String, 0 if it is a number, and otherwise return a null reference. */ private Object legalOrIllegal(Metadata<Optional> metadata) { Type type = metadata.getType(); if (type.equals(String.class)) { return safetyCast.with("", type); } for (Type numericType : SupportedTypes.NUMERIC) { if (type.equals(numericType)) { return safetyCast.with(0, type); } } return null; }
/** * Return empty if type is string, otherwise return 0. */ @Override public Object illegal(Metadata<Valid> metadata) { String defaultOptional = metadata.getAnnotation().illegal(); Type type = metadata.getType(); if (!defaultOptional.isEmpty()) { return safetyCast.with(defaultOptional, metadata.getType()); } else if (type.equals(String.class)) { return safetyCast.with("", type); } else { return safetyCast.with(0, type); } }
public OptionalMockery() { safetyCast = new SafetyCast(); }
/** * Return empty if type is string, otherwise return 0. */ @Override public Object illegal(Metadata<Enum> metadata) { throwExceptionOnEmptyArray(metadata); String defaultOptional = metadata.getAnnotation().illegal(); Type type = metadata.getType(); if (!defaultOptional.isEmpty()) { return safetyCast.with(defaultOptional, metadata.getType()); } else if (type.equals(String.class)) { return safetyCast.with("", type); } else { return safetyCast.with(0, type); } }
public EnumMockery() { safetyCast = new SafetyCast(); }
/** * Return as legal value the generated value from the regex pattern supplied. */ @Override public Object legal(Metadata<Valid> metadata) { Valid valid = metadata.getAnnotation(); String defaultLegal = valid.legal(); if (!defaultLegal.isEmpty()) { return safetyCast.with(defaultLegal, metadata.getType()); } String regex = valid.value(); String result = new Generex(regex).random(); result = result.replace("^", "") .replace("$", "") .replace("?", ""); if (regex.equals(Valid.Template.ID)) { if (result.length() > 1 && result.charAt(0) == '0') { result = result.replaceFirst("0", ""); } } if (regex.equals(Valid.Template.INT) || regex.equals(Valid.Template.ID) || regex.equals(Valid.Template.NUMBER)) { if (result.length() > 8) { result = result.substring(0, 7); } } return safetyCast.with(result, metadata.getType()); }
@Before public void init() { safetyCast = new SafetyCast(); }
/** * Return as legal value a random one from the array supplied, or if legal value was set then return that. */ @Override public Object legal(Metadata<Enum> metadata) { throwExceptionOnEmptyArray(metadata); Enum enumAnnotation = metadata.getAnnotation(); String defaultLegal = enumAnnotation.legal(); if (!defaultLegal.isEmpty()) { return safetyCast.with(defaultLegal, metadata.getType()); } int length = enumAnnotation.value().length; int randomIndex = new Random().nextInt(length); String result = enumAnnotation.value()[randomIndex]; return safetyCast.with(result, metadata.getType()); }
@Test public void When_double_Then_Get_double() { Object object = "2"; double result = (double) safetyCast.with(object, double.class); assertThat(result, is(2d)); }
@Test public void When_String_Then_Get_String() { Object object = "345"; String result = (String) safetyCast.with(object, String.class); assertThat(result, is("345")); }
@Test public void When_long_Then_Get_long() { Object object = "98"; long result = (long) safetyCast.with(object, long.class); assertThat(result, is(98l)); }
@Test public void When_int_Then_Get_int() { Object object = "4"; int result = (int) safetyCast.with(object, int.class); assertThat(result, is(4)); }
@Test public void When_float_Then_Get_float() { Object object = "66"; float result = (float) safetyCast.with(object, float.class); assertThat(result, is(66f)); }
@Test public void When_Double_Then_Get_Double() { Object object = "23"; Double result = (Double) safetyCast.with(object, Double.class); assertThat(result, is(23d)); }
@Test public void When_Integer_Then_Get_Integer() { Object object = "12"; Integer result = (Integer) safetyCast.with(object, Integer.class); assertThat(result, is(12)); }
@Test public void When_Long_Then_Get_Long() { Object object = "34"; Long result = (Long) safetyCast.with(object, Long.class); assertThat(result, is(34l)); }
@Test public void When_Float_Then_Get_Float() { Object object = "43"; Float result = (Float) safetyCast.with(object, Float.class); assertThat(result, is(43f)); }