Skip to content

Instantly share code, notes, and snippets.

@compwron
Last active September 12, 2017 15:48
Show Gist options
  • Save compwron/4acd4c7bf897f1fadfbb7950a26bccde to your computer and use it in GitHub Desktop.
Save compwron/4acd4c7bf897f1fadfbb7950a26bccde to your computer and use it in GitHub Desktop.
try to test any spring crud codebase to make sure that timestamps are present on db tables
@Test
public void foo() throws Exception {
// find all classes that extend CrudRepository
// find the object that the repository is for
// check that object has .getCreatedAt and .getUpdatedAt with the Timestamp type
// https://stackoverflow.com/questions/492184/how-do-you-find-all-subclasses-of-a-given-class-in-java
// ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
// provider.addIncludeFilter(new AssignableTypeFilter(CrudRepository.class));
//
//// scan in org.example.package
// Set<BeanDefinition> components = provider.findCandidateComponents("com/sonicdrivein/api");
// for (BeanDefinition component : components) {
// Class cls = Class.forName(component.getBeanClassName());
// // use class cls found
// System.out.println(cls);
// }
List<String> methodNames = Arrays.asList("getCreatedAt", "getUpdatedAt");
Reflections reflections = new Reflections("com.sonicdrivein");
Set<Class<? extends CrudRepository>> classes = reflections.getSubTypesOf(CrudRepository.class);
assertThat(classes.size(), is(1));
Arrays.stream(classes.toArray()).forEach(clazz -> {
Method[] methods = ((Class) clazz).getMethods();
Arrays.stream(methods).forEach(method -> {
if (method.getName().equals("save")) {
Class<?>[] parameterTypes = method.getParameterTypes();
Class<?> parameterType = parameterTypes[0];
long count = Arrays.stream(parameterType.getMethods()).filter(m -> methodNames.contains(m.getName())).count();
// if theyre found, check that they return timestamp
}
});
});
// OfferRepository.class.getMethods()[0].getName()
// classes.toArray()[0].getDeclaredMethods()
// classes.stream().forEach(clazz -> {
// Class<?> componentType = clazz.getComponentType();
// Method[] declaredMethods = componentType.getDeclaredMethods();
// System.out.println(declaredMethods);
// });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment