|
| 1 | +package kaaes.spotify.webapi.android; |
| 2 | + |
| 3 | +import android.os.Parcelable; |
| 4 | + |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.ParameterizedType; |
| 7 | +import java.lang.reflect.Type; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Random; |
| 14 | + |
| 15 | +/* |
| 16 | + * Inspired by the code in the Random Beans project which looks |
| 17 | + * cool but would be a bit of an overkill for these tests. |
| 18 | + * Random Beans repository URL: https://github.com/benas/random-beans |
| 19 | + */ |
| 20 | + |
| 21 | +public class ModelPopulator { |
| 22 | + |
| 23 | + public static final int DEFAULT_STRING_LENGTH = 10; |
| 24 | + public static final int DEFAULT_COLLECTION_SIZE = 5; |
| 25 | + |
| 26 | + public static final Random RANDOM = new Random(); |
| 27 | + public static final Class<String> DEFAULT_GENERIC_CLASS = String.class; |
| 28 | + private final List<String> mExcludeFields; |
| 29 | + |
| 30 | + public static class PopulationException extends RuntimeException { |
| 31 | + public PopulationException(String details, Throwable throwable) { |
| 32 | + super(details, throwable); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public ModelPopulator(String... excludeFields) { |
| 37 | + mExcludeFields = new ArrayList<>(); |
| 38 | + for (String field : excludeFields) { |
| 39 | + mExcludeFields.add(field.toLowerCase()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public <T> T populateWithRandomValues(final Class<T> type) { |
| 44 | + try { |
| 45 | + T instance = type.newInstance(); |
| 46 | + |
| 47 | + ArrayList<Field> fields = new ArrayList<>(Arrays.asList(type.getDeclaredFields())); |
| 48 | + fields.addAll(getInheritedFields(type)); |
| 49 | + |
| 50 | + for (Field field : fields) { |
| 51 | + |
| 52 | + if (shouldSkipField(field)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if (isCollectionType(field.getType())) { |
| 57 | + field.set(instance, populateCollectionField(field)); |
| 58 | + } else { |
| 59 | + field.set(instance, getRandomValueOfType(field.getType())); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return instance; |
| 64 | + } catch (Exception e) { |
| 65 | + throw new PopulationException("Populating object failed", e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private boolean shouldSkipField(Field field) { |
| 70 | + return mExcludeFields.contains(field.getName().toLowerCase()); |
| 71 | + } |
| 72 | + |
| 73 | + private List<Field> getInheritedFields(Class type) { |
| 74 | + List<Field> inheritedFields = new ArrayList<>(); |
| 75 | + while (type.getSuperclass() != null) { |
| 76 | + Class superclass = type.getSuperclass(); |
| 77 | + inheritedFields.addAll(Arrays.asList(superclass.getDeclaredFields())); |
| 78 | + type = superclass; |
| 79 | + } |
| 80 | + return inheritedFields; |
| 81 | + } |
| 82 | + |
| 83 | + private boolean isCollectionType(Class type) { |
| 84 | + return Map.class.isAssignableFrom(type) || List.class.isAssignableFrom(type); |
| 85 | + } |
| 86 | + |
| 87 | + private Object populateCollectionField(Field field) { |
| 88 | + |
| 89 | + Class type = field.getType(); |
| 90 | + |
| 91 | + /* List */ |
| 92 | + if (List.class.isAssignableFrom(type)) { |
| 93 | + Type genericType = field.getGenericType(); |
| 94 | + ParameterizedType pt = (ParameterizedType) genericType; |
| 95 | + Type actualType = pt.getActualTypeArguments()[0]; |
| 96 | + Class elementClass; |
| 97 | + |
| 98 | + if (actualType instanceof Class) { |
| 99 | + elementClass = (Class) actualType; |
| 100 | + } else { |
| 101 | + // Lists with generics will be populated by default type |
| 102 | + elementClass = DEFAULT_GENERIC_CLASS; |
| 103 | + } |
| 104 | + |
| 105 | + List list = new ArrayList(); |
| 106 | + for (int i = 0; i < DEFAULT_COLLECTION_SIZE; i++) { |
| 107 | + list.add(getRandomValueOfType(elementClass)); |
| 108 | + } |
| 109 | + return list; |
| 110 | + } |
| 111 | + |
| 112 | + /* Map */ |
| 113 | + if (Map.class.isAssignableFrom(type)) { |
| 114 | + Type genericType = field.getGenericType(); |
| 115 | + ParameterizedType pt = (ParameterizedType) genericType; |
| 116 | + Type[] arguments = pt.getActualTypeArguments(); |
| 117 | + |
| 118 | + Type key = arguments[0]; |
| 119 | + Type value = arguments[1]; |
| 120 | + |
| 121 | + Class keyClass; |
| 122 | + if (key instanceof Class) { |
| 123 | + keyClass = (Class) key; |
| 124 | + } else { |
| 125 | + // Maps with generics will be populated by default type |
| 126 | + keyClass = DEFAULT_GENERIC_CLASS; |
| 127 | + } |
| 128 | + |
| 129 | + Class valueClass; |
| 130 | + if (value instanceof Class) { |
| 131 | + valueClass = (Class) value; |
| 132 | + } else { |
| 133 | + // Maps with generics will be populated by default type |
| 134 | + valueClass = DEFAULT_GENERIC_CLASS; |
| 135 | + } |
| 136 | + |
| 137 | + Map map = new HashMap(); |
| 138 | + for (int i = 0; i < DEFAULT_COLLECTION_SIZE; i++) { |
| 139 | + map.put(getRandomValueOfType(keyClass), getRandomValueOfType(valueClass)); |
| 140 | + } |
| 141 | + return map; |
| 142 | + } |
| 143 | + |
| 144 | + throw new UnsupportedOperationException("Unsupported collection field type! " + type); |
| 145 | + } |
| 146 | + |
| 147 | + private Object getRandomValueOfType(Class type) { |
| 148 | + |
| 149 | + /* Another model */ |
| 150 | + if (Parcelable.class.isAssignableFrom(type)) { |
| 151 | + return populateWithRandomValues(type); |
| 152 | + } |
| 153 | + |
| 154 | + /* String */ |
| 155 | + if (type.equals(String.class)) { |
| 156 | + StringBuilder builder = new StringBuilder(); |
| 157 | + for (int i = 0; i < DEFAULT_STRING_LENGTH; i++) { |
| 158 | + builder.append((char) (RANDOM.nextInt(26) + 'a')); |
| 159 | + } |
| 160 | + return builder.toString(); |
| 161 | + } |
| 162 | + |
| 163 | + /* Integer */ |
| 164 | + if (type.equals(Integer.TYPE) || type.equals(Integer.class)) { |
| 165 | + return RANDOM.nextInt(); |
| 166 | + } |
| 167 | + |
| 168 | + /* Long */ |
| 169 | + if (type.equals(Long.TYPE) || type.equals(Long.class)) { |
| 170 | + return RANDOM.nextLong(); |
| 171 | + } |
| 172 | + |
| 173 | + /* Boolean */ |
| 174 | + if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) { |
| 175 | + return RANDOM.nextBoolean(); |
| 176 | + } |
| 177 | + |
| 178 | + throw new UnsupportedOperationException("Unsupported field type! " + type); |
| 179 | + } |
| 180 | +} |
0 commit comments