Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added EnumValuesInvoker; simplified SetterGetterInvoker

  • Loading branch information...
commit db8782b3e8fbdf8449b4de5b972861acc3f893e9 1 parent e1dd176
Duc M. Quoc authored
120 jutil/src/main/java/vn/ducquoc/jutil/EnumValuesInvoker.java
View
@@ -0,0 +1,120 @@
+package vn.ducquoc.jutil;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+/**
+ * Helper class for invoking trivial enum methods (to increase coverage).
+ *
+ * @author ducquoc
+ * @see vn.ducquoc.jutil.SetterGetterInvoker
+ * @see org.reflection.Reflections
+ */
+@SuppressWarnings({ "unchecked", "rawtypes" })
+public class EnumValuesInvoker<T> {
+
+ private T testTarget;
+
+ public EnumValuesInvoker(T targetClass) {
+ super();
+ this.testTarget = targetClass;
+ }
+
+ public T[] invokeEnumValues() {
+ T[] returnedObjects = null;
+ try {
+ Class<?> enumClass = Class.forName(testTarget.toString().replaceAll("class ", ""));
+ if (enumClass.isEnum()) {
+// Class<?> magicClass = Class.forName(enumClass.toString().replaceAll("class ", ""));
+// Method method = magicClass.Method("values()");
+// returnedObjects = (T[]) method.invoke(testTarget); // .invoke(magicClass.newInstance());
+ returnedObjects = (T[]) enumClass.getEnumConstants();
+ }
+ } catch (Exception ex) {
+ throw new RuntimeException("Failed to invoke enum values()", ex);
+ }
+ return returnedObjects;
+ }
+
+ public static List getEnumClasses(String packageName) throws ClassNotFoundException, IOException {
+ List enumClasses = new ArrayList();
+ for (Class clazz : getClasses(packageName)) {
+ if (clazz.isEnum()) {
+ enumClasses.add(clazz);
+ }
+ }
+ return enumClasses;
+ }
+
+ /**
+ * Scans all classes accessible from the context class loader which belong to
+ * the given package and subpackages.
+ *
+ * @param packageName
+ * The base package
+ * @return list of classes
+ * @throws ClassNotFoundException
+ * @throws IOException
+ */
+ public static List<Class> getClasses(String packageName) throws ClassNotFoundException, IOException {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ assert classLoader != null;
+ String path = packageName.replace('.', '/');
+ Enumeration<URL> resources = classLoader.getResources(path);
+ List<File> dirs = new ArrayList<File>();
+ while (resources.hasMoreElements()) {
+ URL resource = resources.nextElement();
+ String fileName = resource.getFile();
+ String fileNameDecoded = URLDecoder.decode(fileName, "UTF-8");
+ dirs.add(new File(fileNameDecoded));
+ }
+ ArrayList<Class> classes = new ArrayList<Class>();
+ for (File directory : dirs) {
+ classes.addAll(findClasses(directory, packageName));
+ }
+ return classes;
+ }
+
+ /**
+ * Finds all classes in a given directory and subdirs, using recursion. <br/>
+ * This method is not supposed to work with JAR files, but possible to do so
+ * if you customize it using some unzip classes, such as ZipInputStream.
+ *
+ * @param directory
+ * The base directory
+ * @param packageName
+ * The package name for classes found inside the base directory
+ * @return The classes
+ * @throws ClassNotFoundException
+ */
+ private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
+ List<Class> classes = new ArrayList<Class>();
+ if (!directory.exists()) {
+ return classes;
+ }
+ File[] files = directory.listFiles();
+ for (File file : files) {
+ String fileName = file.getName();
+ if (file.isDirectory()) {
+ assert !fileName.contains(".");
+ classes.addAll(findClasses(file, packageName + "." + fileName));
+ } else if (fileName.endsWith(".class")) { // && !fileName.contains("$")
+ Class clazz;
+ try {
+ clazz = Class.forName(packageName + '.' + fileName.substring(0, fileName.length() - 6));
+ } catch (ExceptionInInitializerError e) { // may happen with injections
+ clazz = Class.forName(packageName + '.' + fileName.substring(0, fileName.length() - 6), false, Thread
+ .currentThread().getContextClassLoader());
+ }
+ classes.add(clazz);
+ }
+ }
+ return classes;
+ }
+
+}
18 jutil/src/main/java/vn/ducquoc/jutil/SetterGetterInvoker.java
View
@@ -53,7 +53,7 @@ public void invokeSettersAndGetters(List<String> excludedMethodNames) {
if (parameterTypes.length == 1) {
Object testValue = valueFactory.createValue(parameterTypes[0]);
try {
- System.out.println("Invoking set method: " + method.toString() + " with argument: " + testValue);
+// System.out.println("Invoking set method: " + method.toString() + " with argument: " + testValue);
method.invoke(testTarget, testValue);
if (testValue instanceof Boolean) {
@@ -62,9 +62,9 @@ public void invokeSettersAndGetters(List<String> excludedMethodNames) {
invokeGetter(targetClass, testValue, "get" + method.getName().substring(3));
}
} catch (IllegalAccessException ex) {
- throw new UtilException("Failed to access setter method: " + method.toString(), ex);
+ throw new RuntimeException("Failed to access setter method: " + method.toString(), ex);
} catch (InvocationTargetException ex) {
- throw new UtilException("Failed to invoke setter method: " + method.toString(), ex);
+ throw new RuntimeException("Failed to invoke setter method: " + method.toString(), ex);
}
}
}
@@ -82,15 +82,15 @@ public void invokeSettersAndGetters(List<String> excludedMethodNames) {
private void invokeGetter(Class<?> targetClass, Object expectedValue, String getterName) {
try {
Method getterMethod = targetClass.getMethod(getterName);
- System.out.println("Invoke get method: " + getterMethod.toString());
+// System.out.println("Invoking get method: " + getterMethod.toString());
getterMethod.invoke(testTarget);
} catch (NoSuchMethodException ignore) {
- System.out.println("This can be ignored - Getter does not exist: " + getterName);
+ System.out.println("[can be ignored] Getter does not exist: " + getterName + "() of " + targetClass.getSimpleName());
} catch (IllegalAccessException ex) {
- throw new UtilException("Failed to access getter method: " + getterName, ex);
+ throw new RuntimeException("Failed to access getter method: " + getterName, ex);
} catch (InvocationTargetException ex) {
- throw new UtilException("Failed to invoke getter method: " + getterName, ex);
+ throw new RuntimeException("Failed to invoke getter method: " + getterName, ex);
}
}
@@ -106,8 +106,8 @@ public ValueFactory() {
typeValues.put(String.class, "test");
typeValues.put(Integer.class, Integer.valueOf(42));
typeValues.put(Integer.TYPE, Integer.valueOf(42));
- typeValues.put(Long.class, Long.valueOf(Integer.MAX_VALUE + 42));
- typeValues.put(Long.TYPE, Long.valueOf(Integer.MAX_VALUE + 42));
+ typeValues.put(Long.class, Long.valueOf(42L + Integer.MAX_VALUE));
+ typeValues.put(Long.TYPE, Long.valueOf(42L + Integer.MAX_VALUE));
typeValues.put(Boolean.class, Boolean.TRUE);
typeValues.put(Boolean.TYPE, Boolean.TRUE);
}
Please sign in to comment.
Something went wrong with that request. Please try again.