@Test void typedInterfaceMapTypesMightBePopulated() throws Exception { when(context.getParameters()).thenReturn(parameters); when(easyRandom.doPopulateBean(String.class, context)).thenReturn(FOO, BAR); Field field = Foo.class.getDeclaredField("typedMap"); Map<String, String> randomMap = (Map<String, String>) mapPopulator.getRandomMap(field, context); assertThat(randomMap).containsExactly(entry(FOO, BAR)); }
@SuppressWarnings("unchecked") Map<?, ?> getRandomMap(final Field field, final RandomizationContext context) { int randomSize = getRandomMapSize(context.getParameters()); Class<?> fieldType = field.getType(); Type fieldGenericType = field.getGenericType(); Map<Object, Object> map; if (isInterface(fieldType)) { map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType); } else { try { map = (Map<Object, Object>) fieldType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { if (fieldType.isAssignableFrom(EnumMap.class)) { if (isParameterizedType(fieldGenericType)) { Type type = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0]; map = new EnumMap((Class<?>)type); } else { return null; } } else { map = (Map<Object, Object>) objectFactory.createInstance(fieldType, context); } } } if (isParameterizedType(fieldGenericType)) { ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (isPopulatable(keyType) && isPopulatable(valueType)) { for (int index = 0; index < randomSize; index++) { Object randomKey = easyRandom.doPopulateBean((Class<?>) keyType, context); Object randomValue = easyRandom.doPopulateBean((Class<?>) valueType, context); if(randomKey != null) { map.put(randomKey, randomValue); } } } } return map; }
MapPopulator { @SuppressWarnings("unchecked") Map<?, ?> getRandomMap(final Field field, final RandomizationContext context) { int randomSize = getRandomMapSize(context.getParameters()); Class<?> fieldType = field.getType(); Type fieldGenericType = field.getGenericType(); Map<Object, Object> map; if (isInterface(fieldType)) { map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType); } else { try { map = (Map<Object, Object>) fieldType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { if (fieldType.isAssignableFrom(EnumMap.class)) { if (isParameterizedType(fieldGenericType)) { Type type = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0]; map = new EnumMap((Class<?>)type); } else { return null; } } else { map = (Map<Object, Object>) objectFactory.createInstance(fieldType, context); } } } if (isParameterizedType(fieldGenericType)) { ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (isPopulatable(keyType) && isPopulatable(valueType)) { for (int index = 0; index < randomSize; index++) { Object randomKey = easyRandom.doPopulateBean((Class<?>) keyType, context); Object randomValue = easyRandom.doPopulateBean((Class<?>) valueType, context); if(randomKey != null) { map.put(randomKey, randomValue); } } } } return map; } }
MapPopulator { @SuppressWarnings("unchecked") Map<?, ?> getRandomMap(final Field field, final RandomizationContext context) { int randomSize = getRandomMapSize(context.getParameters()); Class<?> fieldType = field.getType(); Type fieldGenericType = field.getGenericType(); Map<Object, Object> map; if (isInterface(fieldType)) { map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType); } else { try { map = (Map<Object, Object>) fieldType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { if (fieldType.isAssignableFrom(EnumMap.class)) { if (isParameterizedType(fieldGenericType)) { Type type = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0]; map = new EnumMap((Class<?>)type); } else { return null; } } else { map = (Map<Object, Object>) objectFactory.createInstance(fieldType, context); } } } if (isParameterizedType(fieldGenericType)) { ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (isPopulatable(keyType) && isPopulatable(valueType)) { for (int index = 0; index < randomSize; index++) { Object randomKey = easyRandom.doPopulateBean((Class<?>) keyType, context); Object randomValue = easyRandom.doPopulateBean((Class<?>) valueType, context); if(randomKey != null) { map.put(randomKey, randomValue); } } } } return map; } MapPopulator(final EasyRandom easyRandom, final ObjectFactory objectFactory); }
MapPopulator { @SuppressWarnings("unchecked") Map<?, ?> getRandomMap(final Field field, final RandomizationContext context) { int randomSize = getRandomMapSize(context.getParameters()); Class<?> fieldType = field.getType(); Type fieldGenericType = field.getGenericType(); Map<Object, Object> map; if (isInterface(fieldType)) { map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType); } else { try { map = (Map<Object, Object>) fieldType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { if (fieldType.isAssignableFrom(EnumMap.class)) { if (isParameterizedType(fieldGenericType)) { Type type = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0]; map = new EnumMap((Class<?>)type); } else { return null; } } else { map = (Map<Object, Object>) objectFactory.createInstance(fieldType, context); } } } if (isParameterizedType(fieldGenericType)) { ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (isPopulatable(keyType) && isPopulatable(valueType)) { for (int index = 0; index < randomSize; index++) { Object randomKey = easyRandom.doPopulateBean((Class<?>) keyType, context); Object randomValue = easyRandom.doPopulateBean((Class<?>) valueType, context); if(randomKey != null) { map.put(randomKey, randomValue); } } } } return map; } MapPopulator(final EasyRandom easyRandom, final ObjectFactory objectFactory);  }
MapPopulator { @SuppressWarnings("unchecked") Map<?, ?> getRandomMap(final Field field, final RandomizationContext context) { int randomSize = getRandomMapSize(context.getParameters()); Class<?> fieldType = field.getType(); Type fieldGenericType = field.getGenericType(); Map<Object, Object> map; if (isInterface(fieldType)) { map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType); } else { try { map = (Map<Object, Object>) fieldType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { if (fieldType.isAssignableFrom(EnumMap.class)) { if (isParameterizedType(fieldGenericType)) { Type type = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0]; map = new EnumMap((Class<?>)type); } else { return null; } } else { map = (Map<Object, Object>) objectFactory.createInstance(fieldType, context); } } } if (isParameterizedType(fieldGenericType)) { ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (isPopulatable(keyType) && isPopulatable(valueType)) { for (int index = 0; index < randomSize; index++) { Object randomKey = easyRandom.doPopulateBean((Class<?>) keyType, context); Object randomValue = easyRandom.doPopulateBean((Class<?>) valueType, context); if(randomKey != null) { map.put(randomKey, randomValue); } } } } return map; } MapPopulator(final EasyRandom easyRandom, final ObjectFactory objectFactory);   }