private void throwExceptionOnEmptyArray(Metadata<Enum> metadata) { if (metadata.getAnnotation().value().length == 0) { String errorMessage = Messages.emptyEnumArray(metadata.getMockingClass(), metadata.getMethod()); throw new RuntimeException(errorMessage); } }
public InterceptorMetadata with(Class<?> mockingClass, Method method, Object[] args) { Annotation[] annotations = mockingClass.getAnnotations(); List<InterceptorMetadata> interceptorsMetadata = new ArrayList<>(); for (Annotation annotation : annotations) { Interceptor interceptorAnnotation = annotation.annotationType() .getAnnotation(Interceptor.class); if (interceptorAnnotation == null) continue; Metadata metadata = new Metadata(mockingClass, method, args, annotation, method.getGenericReturnType()); Interceptor.Behaviour interceptor = instantiateInterface .from(interceptorAnnotation.value()); interceptorsMetadata.add(new InterceptorMetadata(metadata, interceptor)); } if (interceptorsMetadata.size() > 1) { String message = Messages.multipleInterceptorsFoundOnClass(mockingClass); throw new RuntimeException(message); } if (interceptorsMetadata.isEmpty()) { String message = Messages.noInterceptorsFoundOnClass(mockingClass); throw new RuntimeException(message); } return interceptorsMetadata.get(0); }
private MockeryMetadata mockeryMetadata(Class<?> mockingClass, Method method, Object[] args, Type type, Annotation[] annotations) { List<MockeryMetadata> mockeriesMetadata = new ArrayList<>(); for (Annotation annotation : annotations) { Mockery mockeryAnnotation = annotation.annotationType() .getAnnotation(Mockery.class); if (mockeryAnnotation == null) continue; Metadata metadata = new Metadata<>(mockingClass, method, args, annotation, type); Mockery.Behaviour mockery = instantiateInterface.from(mockeryAnnotation.value()); mockeriesMetadata.add(new MockeryMetadata(metadata, mockery)); } if (mockeriesMetadata.isEmpty()) return null; if (mockeriesMetadata.size() > 1) { String message = Messages .multipleMockeryOnMethodOrParam(mockingClass, method); throw new IllegalArgumentException(message); } return mockeriesMetadata.get(0); }
public MockeryMetadata fromMethod(Class<?> mockingClass, Method method, Type typeMethod, Object[] args) { Annotation[] annotationsMethod = method.getAnnotations(); MockeryMetadata mockery = mockeryMetadata(mockingClass, method, args, typeMethod, annotationsMethod); if (mockery == null) { String message = Messages .noMockeryFoundOnMethod(mockingClass, method); throw new RuntimeException(message); } return mockery; }
@Test public void When_No_Interceptor_Annotated_Then_Throw_Exception() throws NoSuchMethodException { Method method = ProvidersWithoutInterceptor.class.getDeclaredMethod("string"); try { getInterceptorMetadata.with(ProvidersWithoutInterceptor.class, method, null); fail(); } catch (RuntimeException e) { String expectedMessage = Messages .noInterceptorsFoundOnClass(ProvidersWithoutInterceptor.class); assertThat(expectedMessage, is(e.getMessage())); } }
@Test public void When_Multiple_Interceptor_Annotated_Then_Throw_Exception() throws NoSuchMethodException { Method method = ProvidersMultipleInterceptor.class.getDeclaredMethod("string"); try { getInterceptorMetadata.with(ProvidersMultipleInterceptor.class, method, null); fail(); } catch (RuntimeException e) { String expectedMessage = Messages .multipleInterceptorsFoundOnClass(ProvidersMultipleInterceptor.class); assertThat(expectedMessage, is(e.getMessage())); } }
public void from(Class mockingClass, Method method, MockeryMetadata mockery, Type candidate) { Type[] supportedTypes = mockery.supportedTypes(); for (Type supportedType : supportedTypes) { if (supportedType.toString().replace("? extends ", "") .equals(candidate.toString())) { return; } } String message = Messages.notSupportedTypeForMockery(mockingClass, method, mockery, candidate, supportedTypes); throw new RuntimeException(message); } }
private RealRobot(Class<?> providersClass, String nameMethod) { this.providersClass = providersClass; for (Method method : providersClass.getDeclaredMethods()) { if (method.getName().equals(nameMethod)) this.method = method; } if (method == null) { String message = Messages .noMethodFoundForMethodName(providersClass, nameMethod); throw new RuntimeException(message); } providersClass.getDeclaredMethods(); this.interceptorMetadata = new GetInterceptorMetadata() .with(providersClass, method, null); this.mockeryMetadata = new GetMockeryMetadata(); this.checkSupportType = new CheckSupportType(); }
private void applyValidationsToParams(Method method, Object[] args) { Type[] typeParams = method.getGenericParameterTypes(); for (int i = 0; i < typeParams.length; i++) { Type typeParam = typeParams[i]; MockeryMetadata mockery = getMockeryMetadata .fromParam(mockingClass, method, args, i); if (mockery == null) { String message = Messages.noMockeryFoundOnParam(mockingClass, method, i); throw new RuntimeException(message); } checkSupportType.from(mockingClass, method, mockery, typeParam); Object valueParam = args[i]; mockery.validate(valueParam); } }
private JolyglotGenerics jolyglot(Metadata<DTOJson> metadata) { JsonConverter jsonConverter = metadata.getMockingClass() .getAnnotation(JsonConverter.class); if (jsonConverter == null) { String message = Messages.noJsonConverterFound( metadata.getMockingClass(), metadata.getMethod(), DTOJson.class); throw new RuntimeException(message); } JolyglotGenerics jolyglot = instantiateInterface.from(jsonConverter.value()); return jolyglot; }
@Test public void When_Get_Mockery_Metadata_From_Method_Without_Mockery_Get_Exception() throws NoSuchMethodException { Method method = Providers.class.getMethod("noMockeryMethod"); Type type = method.getGenericReturnType(); try { getMockeryMetadata.fromMethod(Providers.class, method, type, null); fail(); } catch (RuntimeException e) { String expectedMessage = Messages .noMockeryFoundOnMethod(Providers.class, method); assertThat(expectedMessage, is(e.getMessage())); } }
@Test public void When_Get_Mockery_Metadata_From_Method_With_Multiple_Mockery_Get_Exception() throws NoSuchMethodException { Method method = Providers.class.getMethod("multipleMockeryMethod"); Type type = method.getGenericReturnType(); try { getMockeryMetadata.fromMethod(Providers.class, method, type, null); fail(); } catch (RuntimeException e) { String expectedMessage = Messages .multipleMockeryOnMethodOrParam(Providers.class, method); assertThat(expectedMessage, is(e.getMessage())); } }
@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_Get_Mockery_Metadata_From_Param_With_Multiple_Mockery_Get_Exception() throws NoSuchMethodException { Method method = Providers.class.getMethod("mockeryParams", String.class, String.class, String.class); try { getMockeryMetadata.fromParam(Providers.class, method, null, 1); fail(); } catch (RuntimeException e) { String expectedMessage = Messages .multipleMockeryOnMethodOrParam(Providers.class, method); assertThat(expectedMessage, is(e.getMessage())); } }