/** * Wraps {@code obj} in a {@link Formatter} that standardizes formatting for certain objects. */ static Formattable fmt(Object obj) { return new Formattable() { @Override public void formatTo(Formatter buf, int flags, int width, int precision) { if (obj instanceof Throwable) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ((Throwable) obj).printStackTrace(new PrintStream(baos)); buf.format("%s", baos.toString()); } else if (obj instanceof StackTraceElement[]) { for (StackTraceElement e : (StackTraceElement[]) obj) { buf.format("\t%s%n", e); } } else if (obj instanceof JavaMethod) { buf.format("%s", str((JavaMethod) obj)); } else { buf.format("%s", obj); } } }; }
/** * Find decor. * @param key Key for the formatter to be used to fmt the arguments * @return The type of decor found * @throws DecorException If some problem */ @SuppressWarnings("unchecked") private static Class<? extends Formattable> find(final String key) throws DecorException { final Class<? extends Formattable> type; if (DecorsManager.DECORS.containsKey(key)) { type = DecorsManager.DECORS.get(key); } else { try { type = (Class<Formattable>) Class.forName(key); } catch (final ClassNotFoundException ex) { throw new DecorException( ex, "Decor '%s' not found and class can't be instantiated", key ); } } return type; }
/** * Test for a long text. */ @Test public void compressesLongText() { final int len = 1000; final String text = StringUtils.repeat('x', len); final Formattable fmt = new TextDecor(text); final StringBuilder output = new StringBuilder(100); fmt.formatTo(new Formatter(output), 0, 0, 0); MatcherAssert.assertThat( output.length(), Matchers.describedAs( output.toString(), Matchers.equalTo(TextDecor.MAX) ) ); }
/** * ExceptionDecor can transform exception to text. * @throws Exception If some problem */ @Test public void convertsExceptionToText() throws Exception { final Formattable decor = new ExceptionDecor(new IOException("ouch!")); final Appendable dest = Mockito.mock(Appendable.class); final Formatter fmt = new Formatter(dest); decor.formatTo(fmt, 0, 0, 0); Mockito.verify(dest).append( MockitoHamcrest.argThat( Matchers.allOf( Matchers.containsString( "java.io.IOException: ouch!" ), Matchers.containsString( "at com.jcabi.log.ExceptionDecorTest." ) ) ) ); }
private void printString(Object arg, Locale l) throws IOException { if (arg == null) { print("null"); } else if (arg instanceof Formattable) { Formatter fmt = new Formatter(formatter.out(), l); /* if (formatter.locale() != l) fmt = new F3Formatter(formatter.out(), l); */ ((Formattable)arg).formatTo(fmt, f.valueOf(), width, precision); } else { print(arg.toString()); } }
/** * Perform Python-style string formatting, lazily. * * @param pattern a format string. * @param arguments positional arguments. * @return the formatted string. */ public static Formattable formattable(final String pattern, Object... arguments) { final List<Object> args = Arrays.asList(arguments); return new Formattable() { @Override public String toString() { return formatWithList(pattern, args); } @Override public void formatTo(Formatter formatter, int flags, int width, int precision) { Printer.getPrinter(formatter.out()).formatWithList(pattern, args); } }; }
/** * AbstractDecor can convert object to text. * @throws Exception If some problem inside */ @Test public final void convertsDifferentFormats() throws Exception { final Formattable decor = this.decor(); final Appendable dest = Mockito.mock(Appendable.class); final Formatter fmt = new Formatter(dest); decor.formatTo(fmt, this.flags, this.width, this.precision); Mockito.verify(dest).append(this.text); }
/** * DocumentDecor can transform Document to text. * @throws Exception If some problem */ @Test public void convertsDocumentToText() throws Exception { final Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); doc.appendChild(doc.createElement("root")); final Formattable decor = new DomDecor(doc); final Appendable dest = Mockito.mock(Appendable.class); final Formatter fmt = new Formatter(dest); decor.formatTo(fmt, 0, 0, 0); Mockito.verify(dest).append( MockitoHamcrest.argThat(Matchers.containsString("<root/>")) ); }
/** * DocumentDecor can handle NULL properly. * @throws Exception If some problem */ @Test public void convertsNullToText() throws Exception { final Formattable decor = new DomDecor(null); final Appendable dest = Mockito.mock(Appendable.class); final Formatter fmt = new Formatter(dest); decor.formatTo(fmt, 0, 0, 0); Mockito.verify(dest).append("NULL"); }
/** * ExceptionDecor can handle NULL properly. * @throws Exception If some problem */ @Test public void convertsNullToText() throws Exception { final Formattable decor = new ExceptionDecor(null); final Appendable dest = Mockito.mock(Appendable.class); final Formatter fmt = new Formatter(dest); decor.formatTo(fmt, 0, 0, 0); Mockito.verify(dest).append("NULL"); }
/** * @tests java.util.Formatter#format(String, Object...) for general * conversion other cases */ public void test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionOther() { /* * In Turkish locale, the upper case of '\u0069' is '\u0130'. The * following test indicate that '\u0069' is coverted to upper case * without using the turkish locale. */ Formatter f = new Formatter(new Locale("tr")); f.format("%S", "\u0069"); assertEquals("\u0049", f.toString()); final Object[] input = { Boolean.FALSE, Boolean.TRUE, new Character('c'), new Byte((byte) 0x01), new Short((short) 0x0001), new Integer(1), new Float(1.1f), new Double(1.1d), "", "string content", new MockFormattable(), (Object) null, }; f = new Formatter(Locale.GERMAN); for (int i = 0; i < input.length; i++) { if (!(input[i] instanceof Formattable)) { try { f.format("%#s", input[i]); /* * fail on RI, spec says if the '#' flag is present and the * argument is not a Formattable , then a * FormatFlagsConversionMismatchException will be thrown. */ fail("should throw FormatFlagsConversionMismatchException"); } catch (FormatFlagsConversionMismatchException e) { // expected } } else { f.format("%#s%<-#8s", input[i]); assertEquals( "customized format function width: -1 precision: -1customized format function width: 8 precision: -1", f.toString()); } } }
private Mutability(Formattable annotation) { this.isFrozen = false; // Seems unlikely that we'll often lock more than 10 things at once. this.lockedItems = new IdentityHashMap<>(10); this.annotation = Preconditions.checkNotNull(annotation); }
@Test public void testSimplestFormat() { final Formattable formattable = new SimplestFormattable("foo"); assertThat(FormattableUtils.toString(formattable)).isEqualTo("foo"); }
@Override public Formattable decor() { return new NanoDecor((Long) this.object()); }
@Override public Formattable decor() { return new TextDecor(this.object()); }
@Override public Formattable decor() { return new TypeDecor(this.object()); }
@Override public Formattable decor() { return new SizeDecor((Long) this.object()); }
@Override public Formattable decor() { return new SecretDecor(this.object()); }
@Override public Formattable decor() { return new MsDecor((Long) this.object()); }
@Override public Formattable decor() { return new ObjectDecor(this.object()); }
@Override public Formattable decor() throws Exception { return new ListDecor(this.object()); }
/** * Get the default formatted representation of the specified * {@code Formattable}. * * @param formattable the instance to convert to a string, not null * @return the resulting string, not null */ public static String toString(final Formattable formattable) { return String.format(SIMPLEST_FORMAT, formattable); }
/** * Get the default formatted representation of the specified * {@code Formattable}. * * @param formattable the instance to convert to a string, not null * @return the resulting string, not null */ public static String toString(Formattable formattable) { return String.format(SIMPLEST_FORMAT, formattable); }
/** * Get the default formatted representation of the specified * {@code Formattable}. * * @param formattable the instance to convert to a string, not null * @return the resulting string, not null */ @GwtIncompatible("incompatible method") public static String toString(final Formattable formattable) { return String.format(SIMPLEST_FORMAT, formattable); }
/** * Get decor with the object. * @return The decor to test * @throws Exception If some problem */ protected abstract Formattable decor() throws Exception;