public class Country { private String name; private Currency currency; private List<City> cities; public Country() { setCities(new ArrayList<City>()); } //... getters setters }
public class City { private String name; private int population; private List<Street> streets; public City(String name) { this.name = name; } //... getters setters }
public class Street { private String name; public Street(String name) { this.name = name; } //... getters setters }
public enum Currency { RUB, EUR, USD; }
public class RecursiveToStringStyle extends ToStringStyle { private static final long serialVersionUID = 1L; private int offset; public RecursiveToStringStyle() { this(0); } private RecursiveToStringStyle(int offset) { setUseShortClassName(true); setUseFieldNames(true); setUseIdentityHashCode(false); this.offset = offset; String off = ""; for (int i = 0; i < offset; i++) off += "\t"; this.setContentStart("["); this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + off + " "); this.setFieldSeparatorAtStart(true); this.setContentEnd(SystemUtils.LINE_SEPARATOR + off + "]"); } protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> col) { buffer.append('['); for (Object obj : col) { buffer.append(ReflectionToStringBuilder.toString(obj, new RecursiveToStringStyle(offset + 1))); buffer.append(','); } if (buffer.charAt(buffer.length() - 1) == ',') buffer.setCharAt(buffer.length() - 1, ']'); } protected void appendDetail(StringBuffer buffer, String fieldName, Object value) { if (value instanceof String) { buffer.append("\"" + value.toString() + "\""); } else if (value instanceof BigDecimal) { buffer.append(value.getClass().getSimpleName() + "[" + value.toString() + "]"); } else if (value instanceof BigInteger) { buffer.append(value.getClass().getSimpleName() + "[" + value.toString() + "]"); } else if (!value.getClass().getName().startsWith("java.lang.")) { try { buffer.append(ReflectionToStringBuilder.toString(value, new RecursiveToStringStyle(offset + 1))); } catch (Throwable t) { } } else { super.appendDetail(buffer, fieldName, value); } } }
public class CountryCreatorSimple { public static void main(String[] args) { /** */ PodamFactory factory = new PodamFactoryImpl(); /** */ Country myPojo = factory.manufacturePojo(Country.class); /** "" */ System.out.println(ReflectionToStringBuilder.toString(myPojo,new RecursiveToStringStyle())); } }
Country[ name="2n_BNdJOpE" currency=Currency[ name="USD" ordinal=2 ] cities=[City[ name="7_BmoRTDab" population=-1863637717 streets=[Street[ name="XV_q7SPbvk" ],Street[ name="GkNGKj6B9J" ],Street[ name="y9GNakRAsW" ],Street[ name="Mwo09nQx0R" ],Street[ name="n4_EDMGNUR" ]] ],City[ name="1sifHwujvo" population=1832262487 streets=[Street[ name="xpZiJH2sce" ],Street[ name="ns8DRJDi4e" ],Street[ name="7Ijv_UVZrF" ],Street[ name="CYruDEhe2M" ],Street[ name="4HFzN0v5mc" ]] ],City[ name="qJlUWEPoxp" population=1979728140 streets=[Street[ name="_LbqmCPgWC" ],Street[ name="yS6jX8vRqI" ],Street[ name="yFysWkntdh" ],Street[ name="RvP93uJphY" ],Street[ name="WjARSGWfxB" ]] ],City[ name="W1J9mWpEFH" population=493149274 streets=[Street[ name="8bFRRbPmqO" ],Street[ name="ORJ4rP1i41" ],Street[ name="qD9XU0I0K2" ],Street[ name="I75Wt5cK9v" ],Street[ name="viT8t5FkPq" ]] ],City[ name="33cPIh6go9" population=693664641 streets=[Street[ name="kvPtj1GIL4" ],Street[ name="aVv1taDA0j" ],Street[ name="iQ6ZriwuZK" ],Street[ name="fcf6JICEQ9" ],Street[ name="1Pbdnc_7R6" ]] ]] ]
@Override public int getNumberOfCollectionElements(Class<?> type)
@Override public String getStringValue(AttributeMetadata attributeMetadata)
@Override public Integer getInteger(AttributeMetadata attributeMetadata)
attributeMetadata.getAttributeName() /** */ attributeMetadata.getPojoClass() /** , */
public class CountryDataProviderStrategy extends AbstractRandomDataProviderStrategy { private static final Random random = new Random(System.currentTimeMillis()); public CountryDataProviderStrategy() { super(); } @Override public String getStringValue(AttributeMetadata attributeMetadata) { /** * name, , * , */ if ("name".equals(attributeMetadata.getAttributeName())) { if (Street.class.equals(attributeMetadata.getPojoClass())) { return Streets.randomStreet(); } else if (City.class.equals(attributeMetadata.getPojoClass())) { return Cities.randomCity(); } else if (Country.class.equals(attributeMetadata.getPojoClass())) { return "Podam States of Mockitia"; } } return super.getStringValue(attributeMetadata); }; @Override public int getNumberOfCollectionElements(Class<?> type) { /** * , 1 2. , * 1 10 */ if (City.class.getName().equals(type.getName())) { return 1 + random.nextInt(2); } else if (Street.class.getName().equals(type.getName())) { return 1 + random.nextInt(10); } return super.getNumberOfCollectionElements(type); }; @Override public Integer getInteger(AttributeMetadata attributeMetadata) { /** */ if (City.class.equals(attributeMetadata.getPojoClass())) { if ("population".equals(attributeMetadata.getAttributeName())) { return 1_000_000 + random.nextInt(9_000_000); } } return super.getInteger(attributeMetadata); } private enum Cities { MOSCOW, SAINT_PETERSBURG, LONDON, NEW_YORK, SHANGHAI, KARACHI, BEIJING, DELHI, PARIS, NAIROBI; private static final List<Cities> values = Collections.unmodifiableList(Arrays.asList(values())); private static final int size = values.size(); private static final Random random = new Random(); public static String randomCity() { return values.get(random.nextInt(size)).toString(); } } private enum Streets { RUE_ABEL, RUE_AMPERE, AVENUE_PAUL_APPELL, BOULEVARD_ARAGO, JARDINS_ARAGO, SQUARE_ARAGO, RUE_ANTOINE_ARNAULD, SQUARE_ANTOINE_ARNAULD, RUE_BERNOULLI, RUE_BEZOUT, RUE_BIOT, RUE_BORDA, SQUARE_BOREL, RUE_CHARLES_BOSSUT, RUE_DE_BROGLIE, RUE_BUFFON, AVENUE_CARNOT, BOULEVARD_CARNOT, VILLA_SADI_CARNOT, RUE_CASSINI, RUE_CAUCHY, RUE_MICHEL_CHASLES, RUE_NICOLAS_CHUQUET, RUE_CLAIRAUT, RUE_CLAPEYRON, RUE_CONDORCET, RUE_CORIOLIS, RUE_COURNOT, RUE_GASTON_DARBOUX, RUE_DELAMBRE, SQUARE_DELAMBRE, RUE_DEPARCIEUX, RUE_DE_PRONY, RUE_DESARGUES, RUE_DESCARTES, RUE_ESCLANGON, RUE_EULER; private static final List<Streets> values = Collections.unmodifiableList(Arrays.asList(values())); private static final int size = values.size(); private static final Random random = new Random(); public static String randomStreet() { return values.get(random.nextInt(size)).toString(); } } }
JARDINS_ARAGO, SQUARE_ARAGO, RUE_ANTOINE_ARNAULD, SQUARE_ANTOINE_ARNAULD, RUE_BERNOULLI, RUE_BEZOUT, RUE_BIOT, RUE_BORDA, SQUARE_BOREL, RUE_CHARLES_BOSSUT, RUE_DE_BROGLIE, RUE_BUFFON, AVENUE_CARNOT, BOULEVARD_CARNOT, VILLA_SADI_CARNOT, RUE_CASSINI, RUE_CAUCHY, RUE_MICHEL_CHASLES, RUE_NICOLAS_CHUQUET, RUE_CLAIRAUT, RUE_CLAPEYRON, public class CountryDataProviderStrategy extends AbstractRandomDataProviderStrategy { private static final Random random = new Random(System.currentTimeMillis()); public CountryDataProviderStrategy() { super(); } @Override public String getStringValue(AttributeMetadata attributeMetadata) { /** * name, , * , */ if ("name".equals(attributeMetadata.getAttributeName())) { if (Street.class.equals(attributeMetadata.getPojoClass())) { return Streets.randomStreet(); } else if (City.class.equals(attributeMetadata.getPojoClass())) { return Cities.randomCity(); } else if (Country.class.equals(attributeMetadata.getPojoClass())) { return "Podam States of Mockitia"; } } return super.getStringValue(attributeMetadata); }; @Override public int getNumberOfCollectionElements(Class<?> type) { /** * , 1 2. , * 1 10 */ if (City.class.getName().equals(type.getName())) { return 1 + random.nextInt(2); } else if (Street.class.getName().equals(type.getName())) { return 1 + random.nextInt(10); } return super.getNumberOfCollectionElements(type); }; @Override public Integer getInteger(AttributeMetadata attributeMetadata) { /** */ if (City.class.equals(attributeMetadata.getPojoClass())) { if ("population".equals(attributeMetadata.getAttributeName())) { return 1_000_000 + random.nextInt(9_000_000); } } return super.getInteger(attributeMetadata); } private enum Cities { MOSCOW, SAINT_PETERSBURG, LONDON, NEW_YORK, SHANGHAI, KARACHI, BEIJING, DELHI, PARIS, NAIROBI; private static final List<Cities> values = Collections.unmodifiableList(Arrays.asList(values())); private static final int size = values.size(); private static final Random random = new Random(); public static String randomCity() { return values.get(random.nextInt(size)).toString(); } } private enum Streets { RUE_ABEL, RUE_AMPERE, AVENUE_PAUL_APPELL, BOULEVARD_ARAGO, JARDINS_ARAGO, SQUARE_ARAGO, RUE_ANTOINE_ARNAULD, SQUARE_ANTOINE_ARNAULD, RUE_BERNOULLI, RUE_BEZOUT, RUE_BIOT, RUE_BORDA, SQUARE_BOREL, RUE_CHARLES_BOSSUT, RUE_DE_BROGLIE, RUE_BUFFON, AVENUE_CARNOT, BOULEVARD_CARNOT, VILLA_SADI_CARNOT, RUE_CASSINI, RUE_CAUCHY, RUE_MICHEL_CHASLES, RUE_NICOLAS_CHUQUET, RUE_CLAIRAUT, RUE_CLAPEYRON, RUE_CONDORCET, RUE_CORIOLIS, RUE_COURNOT, RUE_GASTON_DARBOUX, RUE_DELAMBRE, SQUARE_DELAMBRE, RUE_DEPARCIEUX, RUE_DE_PRONY, RUE_DESARGUES, RUE_DESCARTES, RUE_ESCLANGON, RUE_EULER; private static final List<Streets> values = Collections.unmodifiableList(Arrays.asList(values())); private static final int size = values.size(); private static final Random random = new Random(); public static String randomStreet() { return values.get(random.nextInt(size)).toString(); } } }
public class CountryCreatotWithStrategy { public static void main(String[] args) { /** */ DataProviderStrategy strategy = new CountryDataProviderStrategy(); /** */ PodamFactory factory = new PodamFactoryImpl(strategy); /** */ Country myPojo = factory.manufacturePojo(Country.class); /** */ System.out.println(ReflectionToStringBuilder.toString(myPojo,new RecursiveToStringStyle())); } }
Country[ name="Podam States of Mockitia" currency=Currency[ name="RUB" ordinal=0 ] cities=[City[ name="NAIROBI" population=9563403 streets=[Street[ name="RUE_BORDA" ],Street[ name="RUE_DE_PRONY" ],Street[ name="SQUARE_ANTOINE_ARNAULD" ],Street[ name="RUE_CASSINI" ],Street[ name="RUE_DE_PRONY" ]] ],City[ name="PARIS" population=7602177 streets=[Street[ name="RUE_DESCARTES" ],Street[ name="RUE_CHARLES_BOSSUT" ]] ]] ]
Source: https://habr.com/ru/post/255425/
All Articles