if (isSetter(method)) { if (takesPrimitive(method)) { random(method, model); } else if (takesEnum(method)) { } else if(takesBigDecimal(method)) { populateBigDecimal(method, model); } else if (takesXmlGregorianCalendar(method)) { populateXmlGregorianCalendar(method, model); } else { descend(method, model, scope(seen), level); } else if (isGetter(method) && returnsList(method)) { fill(method, model, seen, level);
private static void random(Method m, Object model) throws Exception { Object value = null; if (takesString(m)) { value = garble(m); } else if (takesShort(m)) { value = (short) rand(100); } else if (takesInteger(m)) { value = rand(100); } else if (takesLong(m)) { value = (long) rand(1000000000); } else if (takesBoolean(m)) { value = RandomUtils.instance().nextBoolean(); } else if (takesDouble(m)) { value = RandomUtils.instance().nextDouble(); } if (value != null) { m.invoke(model, value); } }
/** * Populate a JAXB model type by recursively walking element tree and * setting leaf nodes to randomized values. * * @param clz * the model type * @return a populated instance */ public static Object populate(Class<?> clz) throws Exception { List<Class<?>> seen = getSetMethodTypes(clz); return populate(instantiate(clz), clz, seen, 1); }
private static void descend(Method method, Object model, List<Class<?>> seen, int level) throws Exception { Object child = method.getParameterTypes()[0].newInstance(); method.invoke(model, child); if (level == 1 || (unseen(method, seen) && (level <= 3))) { populate(child, child.getClass(), seen, ++level); } }
@SuppressWarnings("unchecked") private static void fill(Method method, Object model, List<Class<?>> seen, int level) throws Exception { ParameterizedType returnType = (ParameterizedType) method.getGenericReturnType(); Class<?> childType = (Class<?>) returnType.getActualTypeArguments()[0]; if (level == 1 || unseen(childType, seen)) { List<Object> list = (List<Object>) method.invoke(model); Object child = null; if (childType.isEnum()) { Object[] labels = childType.getEnumConstants(); child = labels[rand(labels.length)]; } else { child = childType.newInstance(); } list.add(child); populate(child, child.getClass(), seen, ++level); } }
@Test @Override public void testRoundtrip() throws Exception { Application model = Application.class.cast(populate(Application.class)); model = postPopulate(model); model.setName(NAMES[0]); model.setId(new Guid(NAMES[0].getBytes()).toString()); Mapper<String, Application> back = getMappingLocator().getMapper(String.class, Application.class); Application transform = back.map(NAMES[0], null); verify(model, transform); }
private static List<Class<?>> getSetMethodTypes(Class<?> clz) { List<Class<?>> types = new ArrayList<>(); for (Method method : clz.getMethods()) { if (isSetter(method)) { Class<?> type = method.getParameterTypes()[0]; if (BaseResource.class.isAssignableFrom(type) && !types.contains(type)) { types.add(type); } } } return types; }
@Test public void testRoundtrip() throws Exception { F model = fromClass.cast(populate(fromClass)); model = postPopulate(model); Mapper<F, T> out = mappingLocator.getMapper(fromClass, toClass); Mapper<I, F> back = mappingLocator.getMapper(inverseClass, fromClass); T to = out.map(model, null); I inverse = getInverse(to); F transform = back.map(inverse, getModel(null)); verify(model, transform); }
@Test @Override public void testRoundtrip() throws Exception { DiskSnapshot model = DiskSnapshot.class.cast(populate(DiskSnapshot.class)); model = postPopulate(model); Mapper<DiskSnapshot, Disk> out = getMappingLocator().getMapper(DiskSnapshot.class, Disk.class); Mapper<Disk, DiskSnapshot> back = getMappingLocator().getMapper(Disk.class, DiskSnapshot.class); DiskImage to = (DiskImage) out.map(model, null); DiskImage inverse = getInverse(to); DiskSnapshot transform = back.map(inverse, null); verify(model, transform); }
@Test @Override public void testRoundtrip() throws Exception { Disk model = Disk.class.cast(populate(Disk.class)); model = postPopulate(model); Mapper<Disk, org.ovirt.engine.core.common.businessentities.storage.Disk> out = getMappingLocator().getMapper(Disk.class, org.ovirt.engine.core.common.businessentities.storage.Disk.class); Mapper<org.ovirt.engine.core.common.businessentities.storage.Disk, Disk> back = getMappingLocator().getMapper(org.ovirt.engine.core.common.businessentities.storage.Disk.class, Disk.class); DiskImage to = (DiskImage) out.map(model, null); DiskImage inverse = getInverse(to); Disk transform = back.map(inverse, null); verify(model, transform); }
@Test @Override public void testRoundtrip() throws Exception { Disk model = Disk.class.cast(populate(Disk.class)); model = postPopulate(model); Mapper<Disk, org.ovirt.engine.core.common.businessentities.storage.Disk> out = getMappingLocator().getMapper(Disk.class, org.ovirt.engine.core.common.businessentities.storage.Disk.class); Mapper<org.ovirt.engine.core.common.businessentities.storage.Disk, Disk> back = getMappingLocator().getMapper(org.ovirt.engine.core.common.businessentities.storage.Disk.class, Disk.class); LunDisk to = (LunDisk) out.map(model, null); LunDisk inverse = getInverse(to); Disk transform = back.map(inverse, null); verify(model, transform); } }