@Test public void nestedSelfRefsInStringContentWithoutParentFile() throws NoSuchMethodException, ClassNotFoundException, IOException { String schemaContents = IOUtils.toString( CodeGenerationHelper.class.getResource("/schema/ref/nestedSelfRefsReadAsString.json")); JCodeModel codeModel = new JCodeModel(); new SchemaMapper().generate(codeModel, "NestedSelfRefsInString", "com.example", schemaContents); codeModel.build(schemaRule.getGenerateDir()); ClassLoader classLoader = schemaRule.compile(); Class<?> nestedSelfRefs = classLoader.loadClass("com.example.NestedSelfRefsInString"); assertThat( nestedSelfRefs.getMethod("getThings").getReturnType().getSimpleName(), equalTo("List")); Class<?> listEntryType = (Class<?>) ((ParameterizedType) nestedSelfRefs.getMethod("getThings").getGenericReturnType()) .getActualTypeArguments()[0]; assertThat(listEntryType.getName(), equalTo("com.example.Thing")); Class<?> thingClass = classLoader.loadClass("com.example.Thing"); assertThat( thingClass.getMethod("getNamespace").getReturnType().getSimpleName(), equalTo("String")); assertThat(thingClass.getMethod("getName").getReturnType().getSimpleName(), equalTo("String")); assertThat( thingClass.getMethod("getVersion").getReturnType().getSimpleName(), equalTo("String")); }
@Test public void stringToInt() throws Throwable { Method str_to_int = moduleClass.getMethod("str_to_int"); assertThat((Integer) str_to_int.invoke(null), is(42)); Method str_to_integer = moduleClass.getMethod("str_to_integer"); assertThat((Integer) str_to_integer.invoke(null), is(42)); }
@Test public void stringFormat() throws Throwable { Method str_format1 = moduleClass.getMethod("str_format1"); assertThat((String) str_format1.invoke(null), is("plop")); Method str_format2 = moduleClass.getMethod("str_format2"); assertThat((String) str_format2.invoke(null), is("plop da plop")); }
@Test public void detectsMethodsOnPagingAndSortingRepository() throws Exception { Class<DomainPagingAndSortingRepository> type = DomainPagingAndSortingRepository.class; assertFindAllMethodOn(type, type.getMethod("findAll", Pageable.class)); assertDeleteMethodOn(type, type.getMethod("delete", Serializable.class)); assertSaveMethodPresent(type, true); }
@Test public void detectsMethodsOnCustomRepository() throws Exception { Class<RepositoryWithCustomSortingAndPagingFindAll> type = RepositoryWithCustomSortingAndPagingFindAll.class; assertFindAllMethodOn(type, type.getMethod("findAll", Pageable.class)); Class<RepositoryWithIterableDeleteOnly> type1 = RepositoryWithIterableDeleteOnly.class; assertDeleteMethodOn(type1, type1.getMethod("delete", Iterable.class)); }
@Test public void bindings() throws Throwable { Method lbind = moduleClass.getMethod("lbind"); MethodHandle mh = (MethodHandle) lbind.invoke(null); Integer result = (Integer) mh.invokeWithArguments(2); assertThat(result, is(8)); Method rbind = moduleClass.getMethod("rbind"); mh = (MethodHandle) rbind.invoke(null); result = (Integer) mh.invokeWithArguments(2); assertThat(result, is(-8)); }
@Test public void sets_each() throws Throwable { Method sets_each = moduleClass.getMethod("sets_each"); Object result = sets_each.invoke(null); assertThat(result, instanceOf(Integer.class)); assertThat((Integer) result, is(10)); }
@Test public void lists_reduce() throws Throwable { Method lists_reduce = moduleClass.getMethod("lists_reduce"); Object result = lists_reduce.invoke(null); assertThat(result, instanceOf(Integer.class)); assertThat((Integer) result, is(10)); }
@Test public void refToFragmentOfAnotherSchemaIsReadSuccessfully() throws NoSuchMethodException { Class<?> aClass = fragmentRefsClass.getMethod("getFragmentOfA").getReturnType(); assertThat(aClass.getName(), is("com.example.AdditionalPropertyValue")); }
@Test public void selfRefUsedInAPropertyIsReadSuccessfully() throws NoSuchMethodException { Class<?> aClass = selfRefsClass.getMethod("getChildOfSelf").getReturnType(); assertThat(aClass.getName(), is("com.example.SelfRefs")); }
@Test public void chaining() throws Throwable { Method chaining = moduleClass.getMethod("chaining"); MethodHandle mh = (MethodHandle) chaining.invoke(null); Integer result = (Integer) mh.invokeWithArguments(4); assertThat(result, is(-500)); }
@Test public void detectsFindAllWithSortParameterOnSortingRepository() throws Exception { Class<RepositoryWithCustomSortingFindAll> type = RepositoryWithCustomSortingFindAll.class; assertFindAllMethodOn(type, type.getMethod("findAll", Sort.class)); assertSaveMethodPresent(type, false); }
@Test public void method_handle_to() throws Throwable { Method method_handle_to = moduleClass.getMethod("method_handle_to"); Object result = method_handle_to.invoke(null); assertThat(result, instanceOf(Callable.class)); Callable<?> callable = (Callable<?>) result; assertThat((String) callable.call(), is("ok")); }
@Test public void refToFragmentOfSelfIsReadSuccessfully() throws NoSuchMethodException { Class<?> aClass = fragmentRefsClass.getMethod("getFragmentOfSelf").getReturnType(); assertThat(aClass.getName(), is("com.example.A")); assertThat(aClass.getMethods(), hasItemInArray(hasProperty("name", equalTo("getPropertyOfA")))); }
@Test @SuppressWarnings("unchecked") public void sets_map() throws Throwable { Method sets_map = moduleClass.getMethod("sets_map"); Object result = sets_map.invoke(null); assertThat(result, instanceOf(Set.class)); Set<Integer> set = (Set<Integer>) result; assertThat(set, containsInAnyOrder(0, 10, 20, 30, 40)); }
@Test @SuppressWarnings("unchecked") public void list_sorted_reverse() throws Throwable { Method list_sorted_reverse = moduleClass.getMethod("list_sorted_reverse"); Object result = list_sorted_reverse.invoke(null); assertThat(result, instanceOf(List.class)); List<Integer> list = (List<Integer>) result; assertThat(list, contains(4, 3, 2, 1, 0)); }
@Test @SuppressWarnings("unchecked") public void maps_find() throws Throwable { Method maps_find = moduleClass.getMethod("maps_find"); Object result = maps_find.invoke(null); Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) result; assertThat(entry.getKey(), is("a")); assertThat(entry.getValue(), is(1)); }
@Test @SuppressWarnings("unchecked") public void lists_filter() throws Throwable { Method lists_filter = moduleClass.getMethod("lists_filter"); Object result = lists_filter.invoke(null); assertThat(result, instanceOf(List.class)); List<Integer> list = (List<Integer>) result; assertThat(list, containsInAnyOrder(0, 2, 4)); }
@Test public void selfRefUsedAsArrayItemsIsReadSuccessfully() throws NoSuchMethodException { Type listOfAType = selfRefsClass.getMethod("getArrayOfSelf").getGenericReturnType(); Class<?> listEntryClass = (Class<?>) ((ParameterizedType) listOfAType).getActualTypeArguments()[0]; assertThat(listEntryClass.getName(), is("com.example.SelfRefs")); }
/** @see DATACMNS-393 */ @Test public void selectsFindAllWithSortParameterOnRepositoryAmongUnsuitableAlternatives() throws Exception { Class<RepositoryWithInvalidPagingFallbackToSortFindAll> type = RepositoryWithInvalidPagingFallbackToSortFindAll.class; assertFindAllMethodOn(type, type.getMethod("findAll", Sort.class)); assertSaveMethodPresent(type, false); }
@Test public void selfRefUsedForAdditionalPropertiesIsReadSuccessfully() throws NoSuchMethodException { Type additionalPropertiesType = selfRefsClass.getMethod("getAdditionalProperties").getGenericReturnType(); Class<?> mapEntryClass = (Class<?>) ((ParameterizedType) additionalPropertiesType).getActualTypeArguments()[1]; assertThat(mapEntryClass.getName(), is("com.example.SelfRefs")); }
@Test @SuppressWarnings("unchecked") public void maps_filter() throws Throwable { Method maps_filter = moduleClass.getMethod("maps_filter"); Object result = maps_filter.invoke(null); assertThat(result, instanceOf(Map.class)); Map<String, Integer> map = (Map<String, Integer>) result; assertThat(map, hasEntry("a", 1)); assertThat(map, hasEntry("c", 3)); assertThat(map.size(), is(2)); }
@Test public void tuple_count() throws Throwable { Method tuple_count = moduleClass.getMethod("tuple_count"); Object result = tuple_count.invoke(null); assertThat((Integer) result, is(4)); }
@Test public void number_repeaters() throws Throwable { Method number_repeaters = moduleClass.getMethod("number_repeaters"); assertThat((String) number_repeaters.invoke(null), is("..012101112121110")); }
@Test public void tupled() throws Throwable { Method tupled = moduleClass.getMethod("tupled"); assertThat((Integer) tupled.invoke(null), is(60)); }
@Test public void list_not_find() throws Throwable { Method list_not_find = moduleClass.getMethod("list_not_find"); Object result = list_not_find.invoke(null); assertThat(result, nullValue()); }
@Test public void list_find() throws Throwable { Method list_find = moduleClass.getMethod("list_find"); Object result = list_find.invoke(null); assertThat((Integer) result, is(2)); }
@Test public void tuple_exists() throws Throwable { Method tuple_exists = moduleClass.getMethod("tuple_exists"); Object result = tuple_exists.invoke(null); assertThat((Boolean) result, is(true)); }
@Test public void vector_not_exists() throws Throwable { Method vector_not_exists = moduleClass.getMethod("vector_not_exists"); Object result = vector_not_exists.invoke(null); assertThat((Boolean) result, is(false)); }
@Test public void vector_count_zero() throws Throwable { Method vector_count_zero = moduleClass.getMethod("vector_count_zero"); Object result = vector_count_zero.invoke(null); assertThat((Integer) result, is(0)); }