/** * Converts an object into a long representation as the normal toString * method. E.g. maps are splitted into several lines. * * @param obj * the obj * @return the string * @since 1.4 */ public static String toLongString(final Object obj) { if (obj instanceof Map<?, ?>) { return toLongString((Map<?, ?>) obj); } else if (obj instanceof Object[]) { return toLongString((Object[]) obj); } return toString(obj); }
private static void logMissingElement(Object element, Collection<?> collection) { if (LOG.isTraceEnabled()) { LOG.trace("Element \"{}\" is missing in {}.", Converter.toLongString(element), Converter.toLongString(collection)); } else if (LOG.isDebugEnabled()) { LOG.debug("Element \"{}\" is missing in {}.", Converter.toString(element), Converter.toString(collection)); } }
private static void logMissingElement(Object element, final Collection<?> collection, final String msg) { if (LOG.isTraceEnabled()) { LOG.trace("{}: [{}] is not in {}.", msg, Converter.toLongString(element), Converter.toLongString(collection)); } else if (LOG.isDebugEnabled()) { LOG.debug("{}: [{}] is not in {}.", msg, Converter.toString(element), Converter.toString(collection)); } }
private static void logDifferentElements(int i, Object elem1, Object elem2) { if (LOG.isTraceEnabled()) { LOG.trace("{}. element differs ({} != {}).", i + 1, Converter.toLongString(elem1), Converter.toLongString(elem2)); } else if (LOG.isDebugEnabled()) { LOG.debug("{}. element differs ({} != {}).", i + 1, Converter.toString(elem1), Converter.toString(elem2)); } }
/** * If you want to print each element of an array into a single line you can * use this method here. * * @param array * the array * @return the string * @since 1.4.2 */ public static String toLongString(final Object[] array) { if (array instanceof StackTraceElement[]) { return toLongString((StackTraceElement[]) array); } StringBuilder buf = new StringBuilder(); for (int i = 0; i < array.length; i++) { buf.append('['); buf.append(i); buf.append("]\t"); buf.append(toString(array[i])); buf.append('\n'); } return buf.toString(); }
private static void assertEquals(final byte[] a1, final byte[] a2, final int i) { if (a1[i] != a2[i]) { if (LOG.isTraceEnabled()) { LOG.trace("{}. element differs ({} != {}).", i + 1, Converter.toLongString(a1[i]), Converter.toLongString(a2[i])); } else if (LOG.isDebugEnabled()) { LOG.debug("{}. element differs ({} != {}).", i + 1, Converter.toString(a1[i]), Converter.toString(a2[i])); } throw new AssertionError((i + 1) + ". element differs (" + Converter.toShortString(a1[i]) + " != " + Converter.toShortString(a2[i]) + ")"); } }
/** * Checks if each key-value pair in map m1 is equals to that of map m2. If * not this method will tell you which pair is different. If the maps have * different size this method will tell you which pair is missing. * * @param m1 * the m1 * @param m2 * the m2 */ @SuppressWarnings("unchecked") public static void assertEquals(final Map<?, ?> m1, final Map<?, ?> m2) { Map<Object, Object> map1 = (Map<Object, Object>) m1; if (m1.size() < m2.size()) { assertEquals(m2, m1); } else if (m1.size() > m2.size()) { for (Entry<Object, Object> entry : map1.entrySet()) { if (!(m2.containsKey(entry.getKey()))) { if (LOG.isTraceEnabled()) { LOG.trace("<{}> is missing in {}.", Converter.toLongString(entry), Converter.toLongString(m2)); } else if (LOG.isDebugEnabled()) { LOG.debug("<{}> is missing in {}.", Converter.toString(entry), Converter.toString(m2)); } throw new AssertionError("<" + entry + "> is missing in " + Converter.toShortString(m2)); } } } else { assertEqualSizedMaps(m2, map1); } }
/** * Stops the given 'mon' and logs the given command with the needed time if * debug is enabled. * * @param mon * the mon * @param command * the command * @param returnValue * the return value */ public static void stop(final ProfileMonitor mon, final String command, final Object returnValue) { mon.stop(); if (LOG.isDebugEnabled()) { String msg = '"' + command + "\" returned with " + Converter.toShortString(returnValue) + " after " + mon.getLastTime(); if (LOG.isTraceEnabled()) { StackTraceElement[] stacktrace = StackTraceScanner.getCallerStackTrace(new Pattern[0], SqlStatistic.class, StasiStatement.class, StasiPreparedStatement.class); LOG.trace("{}\n\t{}", msg, Converter.toLongString(stacktrace).trim()); } else { LOG.debug("{}.", msg); } } }