private static String generateKey(final int size) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < size; i++) { int idx = rand.nextInt(ALPHANUMERIC.length()); sb.append(ALPHANUMERIC.substring(idx, idx + 1)); } return (sb.toString()); }
public static Shipment randomShipment() { Shipment s = create(Shipment.class); s.setExternalReferenceId(generateKey(7)); s.setFromAddress(randomAddress()); s.setToAddress(randomAddress()); s.setShippingServiceName(randomShippingService().getName()); for (int i = 0; i < rand.nextInt(5) + 1; i++) { Parcel p = create(Parcel.class); p.setShipmentId(rand.nextInt(5000)); p.setContents(randomize(CONTENTS)); p.setWidth(rand.nextInt(5) + 1); p.setHeight(rand.nextInt(5) + 1); p.setLength(rand.nextInt(5) + 1); p.setWeight(rand.nextInt(5) + 1); p.setParcelStatus(randomEnum(ParcelStatus.values())); s.getParcels().add(p); } return (s); }
public static <T extends BaseEntity> T create(Class<T> baseEntityClass) { T obj = null; try { obj = baseEntityClass.newInstance(); } catch (Exception e) { logger.log(Level.SEVERE, "Instantiating " + baseEntityClass.getName(), e); return (null); } obj.setId(rand.nextInt(5000)); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); obj.setCreatedDate(cal); obj.setModifiedDate(cal); return (obj); }
private static <T extends Enum> T randomEnum(T[] values) { return (values[rand.nextInt(values.length)]); }
private static String randomize(final String[] vals) { return (vals[rand.nextInt(vals.length)]); }