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); } }
/** * 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); }
@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); }
@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); } }