/** * Builds a reflection object with since added properties. * @return the built */ public DataModelReflection build() { return new DataModelReflection(properties); } }
public Object getValue(PropertyName name) { assert name != null; return model.getValue(name); } }
/** * Creates a new instance. * @param properties current property list * @throws IllegalArgumentException if some parameters were {@code null} */ public DataModelReflection(Map<PropertyName, ?> properties) { if (properties == null) { throw new IllegalArgumentException("properties must not be null"); //$NON-NLS-1$ } this.properties = normalize(properties); }
/** * return formatted value. * This method is equivalent to {@link DataModelReflection#toStringRepresentation(Object)}. * @param value target value. * @return formatted value. */ public static String format(Object value) { return DataModelReflection.toStringRepresentation(value); } }
/** * Returns formatted string representation of values. * @param value original value * @return formatted string representation * @since 0.5.1 */ public static String toStringRepresentation(Object value) { if (value instanceof String) { return toStringLiteral((String) value); } else if (value instanceof Calendar) { Calendar c = (Calendar) value; if (c.isSet(Calendar.HOUR_OF_DAY)) { return new SimpleDateFormat(DateTime.FORMAT).format(c.getTime()); } else { return new SimpleDateFormat(Date.FORMAT).format(c.getTime()); } } else if (value instanceof BigDecimal) { return ((BigDecimal) value).toPlainString(); } return String.valueOf(value); }
@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append('{'); if (properties.isEmpty() == false) { for (Map.Entry<PropertyName, ?> entry : properties.entrySet()) { buf.append(entry.getKey()); buf.append('='); buf.append(toStringRepresentation(entry.getValue())); buf.append(','); buf.append(' '); } buf.delete(buf.length() - 2, buf.length()); } buf.append('}'); return buf.toString(); }
@Override public Map<PropertyName, Object> getKey(DataModelReflection target) { Map<PropertyName, Object> results = new LinkedHashMap<>(); for (PropertyName name : keys) { results.put(name, target.getValue(name)); } return results; }
private Difference createDifference(long actual) { PropertyName name = PropertyName.newInstance("count"); return new Difference( new DataModelReflection(Collections.singletonMap(name, expected)), new DataModelReflection(Collections.singletonMap(name, actual)), "count verification was failed"); }
@Override public T toObject(DataModelReflection reflection) { try { T instance = modelClass.newInstance(); for (Map.Entry<PropertyName, Field> entry : fields.entrySet()) { PropertyName name = entry.getKey(); Field field = entry.getValue(); Object value = reflection.getValue(name); try { field.set(instance, value); } catch (IllegalAccessException e) { throw new AssertionError(e); } } return instance; } catch (Exception e) { throw new IllegalStateException(e); } } }
private Difference createDifference(long actual) { PropertyName name = PropertyName.newInstance("count"); return new Difference( new DataModelReflection(Collections.singletonMap(name, expected)), new DataModelReflection(Collections.singletonMap(name, actual)), "count verification was failed"); }
@Override public T toObject(DataModelReflection reflection) { try { T instance = modelClass.newInstance(); for (Map.Entry<PropertyName, Method> entry : accessors.entrySet()) { PropertyName property = entry.getKey(); Method accessor = entry.getValue(); Object value = reflection.getValue(property); set(instance, accessor, value); } return instance; } catch (Exception e) { throw new AssertionError(e); } }
private Difference createDifference(long actual) { PropertyName name = PropertyName.newInstance("count"); return new Difference( new DataModelReflection(Collections.singletonMap(name, expected)), new DataModelReflection(Collections.singletonMap(name, actual)), "count verification was failed"); }
@SuppressWarnings("unchecked") private static <T> Comparator<DataModelReflection> toComparator(DataModelDefinition<T> definition, String term) { if (term.isEmpty()) { throw new IllegalArgumentException("order term must not be empty"); //$NON-NLS-1$ } Ordering order = parseOrder(term); checkProperty(definition, order.propertyName); Comparator<DataModelReflection> comparator = (a, b) -> { Comparable<Object> aValue = (Comparable<Object>) a.getValue(order.propertyName); Comparable<Object> bValue = (Comparable<Object>) b.getValue(order.propertyName); if (aValue == null) { if (bValue == null) { return 0; } else { return -1; } } else if (bValue == null) { return +1; } return aValue.compareTo(bValue); }; if (order.direction == Direction.ASCENDANT) { return comparator; } else { return comparator.reversed(); } }
private Difference createDifference(long actual) { PropertyName name = PropertyName.newInstance("count"); return new Difference( new DataModelReflection(Collections.singletonMap(name, expected)), new DataModelReflection(Collections.singletonMap(name, actual)), "count verification was failed"); }
private Map<List<Object>, List<DataModelReflection>> asRefMap() { Map<List<Object>, List<DataModelReflection>> results = new LinkedHashMap<>(); try (DataModelSource source = factory.createSource(definition, context)) { while (true) { DataModelReflection ref = source.next(); if (ref == null) { break; } List<Object> key = new ArrayList<>(grouping.size()); for (PropertyName name : grouping) { Object value = ref.getValue(name); key.add(value); } results.computeIfAbsent(key, k -> new ArrayList<>()).add(ref); } } catch (IOException e) { throw new IllegalStateException(e); } if (refComparator != null) { for (List<DataModelReflection> refs : results.values()) { refs.sort(refComparator); } } return results; }
private Object describeProperty(DataModelReflection object, PropertyName property) { assert property != null; if (object == null) { return null; } Object value = object.getValue(property); if (value == null) { return null; } PropertyType type = definition.getType(property); switch (type) { case DATE: return dateFormat.format(((Calendar) value).getTime()); case TIME: return timeFormat.format(((Calendar) value).getTime()); case DATETIME: return datetimeFormat.format(((Calendar) value).getTime()); case DECIMAL: return String.format( "%s(scale=%d)", //$NON-NLS-1$ ((BigDecimal) value).toPlainString(), ((BigDecimal) value).scale()); case STRING: return toStringLiteral((String) value); default: return value; } }
private Object checkProperties(DataModelReflection expected, DataModelReflection actual) { List<String> differences = new ArrayList<>(1); for (PropertyCondition<?> condition : propertyConditions) { Object e = expected.getValue(condition.getPropertyName()); Object a = actual.getValue(condition.getPropertyName()); if (condition.accepts(e, a) == false) { differences.add(MessageFormat.format( Messages.getString("VerifyRuleInterpretor.messageWrongProperty"), //$NON-NLS-1$ condition.getPropertyName(), Util.format(a), condition.describeExpected(e, a))); } } return differences.isEmpty() ? null : differences; } }