@Before public void init() { enumMockery = new EnumMockery(); }
@Test public void When_Call_Legal_Then_Get_Legal() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("object", String.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); for (int i = 0; i < 30; i++) { String legal = (String) enumMockery.legal(metadata); enumMockery.validate(metadata, legal); } }
/** * Validate {@code candidate} checking if its value is present in the array supplied. */ @Override public void validate(Metadata<Enum> metadata, Object candidate) throws AssertionError { throwExceptionOnEmptyArray(metadata); boolean valid = false; String[] values = metadata.getAnnotation().value(); for (String value : values) { if (candidate == null) break; if (value.equals(candidate.toString())) { valid = true; break; } } String input = String.valueOf(candidate); if (input.isEmpty()) input = "empty"; String errorMessage = input + " not matches with any of specified values: " + Arrays.toString(values); if (!valid) throw new AssertionError(errorMessage); }
@Test public void When_Empty_Array_Then_Throw_Exception() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("emptyArray", String.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); String errorMessage = Messages .emptyEnumArray(Providers.class, method); try { enumMockery.legal(metadata); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is(errorMessage)); } try { enumMockery.illegal(metadata); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is(errorMessage)); } try { enumMockery.validate(metadata, "no matter"); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is(errorMessage)); } }
@Test public void When_Not_Pass_Validation_Then_Throw_Assertion_Error() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("object", String.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); try { String illegal = (String) enumMockery.illegal(metadata); enumMockery.validate(metadata, illegal); fail(); } catch (AssertionError e) { assertThat(e.getMessage(), is("empty not matches with any of specified values: " + "[asc, desc, sort, random]")); } }
@Test public void When_Illegal_With_Default_Value_Then_Get_Default() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("checkDefaults", String.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); String result = String .valueOf(enumMockery.illegal(metadata)); assertThat(result, is("illegal")); }
@Test public void When_Legal_With_Default_Value_Then_Get_Default() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("checkDefaults", String.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); String result = String .valueOf(enumMockery.legal(metadata)); assertThat(result, is("desc")); }
@Test public void When_Support_Type_Retrieved_Then_Is_Numeric_And_Text() { Type[] supportedTypes = enumMockery.supportedTypes(null); assertThat(supportedTypes.length, is(10)); Type[] expectedTypes = SupportedTypes.concat(SupportedTypes.NUMERIC, SupportedTypes.TEXT); for (Type expected : expectedTypes) { assertTrue(Arrays.asList(supportedTypes).contains(expected)); } }
@Test public void When_Call_Illegal_Then_Defer_To_Optional_Mockery() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("object", String.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); String legal = (String) enumMockery.illegal(metadata); assertTrue(legal.isEmpty()); }
@Test public void When_Pass_Validation_Then_Do_Not_Throw_Assertion_Error() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("object", String.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); String legal = (String) enumMockery.legal(metadata); enumMockery.validate(metadata, legal); }
@Test public void When_Call_Illegal_Primitive_Then_Defer_To_Optional_Mockery() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("primitive", int.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); int legal = (int) enumMockery.illegal(metadata); assertThat(legal, is(0)); }
/** * 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_Call_Legal_Primitive_Then_Get_Legal() throws NoSuchMethodException { Method method = Providers.class .getDeclaredMethod("primitive", int.class); Enum annotation = (Enum) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Enum> metadata = new Metadata<>(Providers.class, method, null, annotation, type); for (int i = 0; i < 30; i++) { int legal = (int) enumMockery.legal(metadata); enumMockery.validate(metadata, legal); } }
/** * 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); } }