1 package junitparams.internal; 2 3 import java.lang.reflect.Method; 4 import java.util.ArrayList; 5 import java.util.Arrays; 6 7 /** 8 * Some String utils to handle parameterised tests' results. 9 * 10 * @author Pawel Lipinski 11 */ 12 public class Utils { 13 public static final String REGEX_ALL_NEWLINES = "(\\r\\n|\\n|\\r)"; 14 stringify(Object paramSet, int paramIdx)15 public static String stringify(Object paramSet, int paramIdx) { 16 String result = "[" + paramIdx + "] "; 17 18 return result + stringify(paramSet); 19 } 20 stringify(Object paramSet)21 public static String stringify(Object paramSet) { 22 String result; 23 if (paramSet == null) 24 result = "null"; 25 else if (paramSet instanceof String) 26 result = paramSet.toString(); 27 else 28 result = asCsvString(safelyCastParamsToArray(paramSet)); 29 30 return trimSpecialChars(result); 31 } 32 getParameterStringByIndexOrEmpty(Object paramSet, int parameterIndex)33 public static String getParameterStringByIndexOrEmpty(Object paramSet, int parameterIndex) { 34 Object[] params = safelyCastParamsToArray(paramSet); 35 if (paramSet instanceof String) { 36 params = splitAtCommaOrPipe((String) paramSet); 37 } 38 if (parameterIndex >= 0 && parameterIndex < params.length) { 39 return addParamToResult("", params[parameterIndex]); 40 } 41 42 return ""; 43 } 44 splitAtCommaOrPipe(String input)45 public static String[] splitAtCommaOrPipe(String input) { 46 ArrayList<String> result = new ArrayList<String>(); 47 48 char character = '\0'; 49 char previousCharacter; 50 51 StringBuilder value = new StringBuilder(); 52 for (int i = 0; i < input.length(); i++) { 53 previousCharacter = character; 54 character = input.charAt(i); 55 56 if (character == ',' || character == '|') { 57 if (previousCharacter == '\\') { 58 value.setCharAt(value.length() - 1, character); 59 continue; 60 } 61 result.add(value.toString().trim()); 62 value = new StringBuilder(); 63 continue; 64 } 65 66 value.append(character); 67 } 68 result.add(value.toString().trim()); 69 70 return result.toArray(new String[]{}); 71 } 72 trimSpecialChars(String result)73 private static String trimSpecialChars(String result) { 74 return result.replace('(', '[').replace(')', ']').replaceAll(REGEX_ALL_NEWLINES, " "); 75 } 76 safelyCastParamsToArray(Object paramSet)77 static Object[] safelyCastParamsToArray(Object paramSet) { 78 final Object[] params; 79 if (paramSet instanceof Object[]) { 80 params = (Object[]) paramSet; 81 } else { 82 params = new Object[]{paramSet}; 83 } 84 return params; 85 } 86 asCsvString(Object[] params)87 private static String asCsvString(Object[] params) { 88 if (params == null) 89 return "null"; 90 91 if (params.length == 0) 92 return ""; 93 94 String result = ""; 95 96 for (int i = 0; i < params.length - 1; i++) { 97 Object param = params[i]; 98 result = addParamToResult(result, param) + ", "; 99 } 100 result = addParamToResult(result, params[params.length - 1]); 101 102 return result; 103 } 104 addParamToResult(String result, Object param)105 private static String addParamToResult(String result, Object param) { 106 if (param == null) 107 result += "null"; 108 else if (param.getClass().isArray()) 109 result += convertAnyArrayToString(param); 110 else if (hasOverridenToStringMethod(param)) 111 result += param.toString(); 112 else 113 result += param.getClass().getSimpleName(); 114 115 return result; 116 } 117 hasOverridenToStringMethod(Object param)118 private static boolean hasOverridenToStringMethod(Object param) { 119 Method[] methods = param.getClass().getMethods(); 120 for (Method method : methods) { 121 if (method.getName().equals("toString") && overridesMethod(method)) { 122 return true; 123 } 124 } 125 return false; 126 } 127 overridesMethod(Method method)128 private static boolean overridesMethod(Method method) { 129 return !method.getDeclaringClass().equals(Object.class); 130 } 131 uniqueMethodId(int index, Object paramSet, String methodName)132 static String uniqueMethodId(int index, Object paramSet, String methodName) { 133 return stringify(paramSet, index) + " (" + methodName + ")"; 134 } 135 convertAnyArrayToString(Object arrayAsObject)136 private static String convertAnyArrayToString(Object arrayAsObject) { 137 if (arrayAsObject.getClass().getComponentType().isPrimitive()) { 138 return convertFromArrayOfPrimitives(arrayAsObject); 139 } else { 140 return Arrays.toString((Object[]) arrayAsObject); 141 } 142 } 143 convertFromArrayOfPrimitives(Object arrayOfPrimitives)144 private static final String convertFromArrayOfPrimitives(Object arrayOfPrimitives) { 145 String componentType = arrayOfPrimitives.getClass().getComponentType().getName(); 146 if ("byte".equals(componentType)) { 147 return Arrays.toString((byte[]) arrayOfPrimitives); 148 } else if ("short".equals(componentType)) { 149 return Arrays.toString((short[]) arrayOfPrimitives); 150 } else if ("int".equals(componentType)) { 151 return Arrays.toString((int[]) arrayOfPrimitives); 152 } else if ("long".equals(componentType)) { 153 return Arrays.toString((long[]) arrayOfPrimitives); 154 } else if ("float".equals(componentType)) { 155 return Arrays.toString((float[]) arrayOfPrimitives); 156 } else if ("double".equals(componentType)) { 157 return Arrays.toString((double[]) arrayOfPrimitives); 158 } else if ("boolean".equals(componentType)) { 159 return Arrays.toString((boolean[]) arrayOfPrimitives); 160 } else { 161 return Arrays.toString((char[]) arrayOfPrimitives); 162 } 163 } 164 } 165