/** * <p>Uses <code>ReflectionToStringBuilder</code> to generate a * <code>toString</code> for the specified object.</p> * * @param object the Object to be output * @return the String result * @see ReflectionToStringBuilder#toString(Object) */ public static String reflectionToString(final Object object) { return ReflectionToStringBuilder.toString(object); }
/** * <p>Uses <code>ReflectionToStringBuilder</code> to generate a * <code>toString</code> for the specified object.</p> * * @param object the Object to be output * @param style the style of the <code>toString</code> to create, may be <code>null</code> * @return the String result * @see ReflectionToStringBuilder#toString(Object,ToStringStyle) */ public static String reflectionToString(final Object object, final ToStringStyle style) { return ReflectionToStringBuilder.toString(object, style); }
/** * <p>Uses <code>ReflectionToStringBuilder</code> to generate a * <code>toString</code> for the specified object.</p> * * @param object the Object to be output * @param style the style of the <code>toString</code> to create, may be <code>null</code> * @param outputTransients whether to include transient fields * @return the String result * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean) */ public static String reflectionToString(final Object object, final ToStringStyle style, final boolean outputTransients) { return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null); }
@Override public String toString() { return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); } }
@Override public void run() { ReflectionToStringBuilder.toString(testFixture); } }
@Override public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) { if (!ClassUtils.isPrimitiveWrapper(value.getClass()) && !String.class.equals(value.getClass()) && accept(value.getClass())) { buffer.append(ReflectionToStringBuilder.toString(value, this)); } else { super.appendDetail(buffer, fieldName, value); } }
/** * Builds a String for a toString method excluding the given field names. * * @param object * The object to "toString". * @param excludeFieldNames * The field names to exclude * @return The toString value. */ public static String toStringExclude(final Object object, final String... excludeFieldNames) { return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString(); }
@Override public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) { if (!ClassUtils.isPrimitiveWrapper(value.getClass()) && !String.class.equals(value.getClass()) && accept(value.getClass())) { spaces += INDENT; resetIndent(); buffer.append(ReflectionToStringBuilder.toString(value, this)); spaces -= INDENT; resetIndent(); } else { super.appendDetail(buffer, fieldName, value); } }
public SELF hasCookie(String path, String name, String value, int maxAge, boolean secured, boolean httpOnly) { Cookie actualCookie = actual.getCookie(name); Cookie expectedCookie = new Cookie(name, value); expectedCookie.setDomain(""); expectedCookie.setPath(path); expectedCookie.setMaxAge(maxAge); expectedCookie.setSecure(secured); expectedCookie.setHttpOnly(httpOnly); if (!EqualsBuilder.reflectionEquals(expectedCookie, actualCookie)) { this.as("cookie"); throw Failures.instance().failure(info, shouldBeEqual(ReflectionToStringBuilder.toString(actualCookie, ToStringStyle.MULTI_LINE_STYLE), ReflectionToStringBuilder.toString(expectedCookie, ToStringStyle.MULTI_LINE_STYLE), info.representation())); } return myself; }
@Override public Integer call() { for (int i = 0; i < REPEAT; i++) { final String s = ReflectionToStringBuilder.toString(holder); Assert.assertNotNull(s); } return Integer.valueOf(REPEAT); } };
@Test(expected=IllegalArgumentException.class) public void testReflectionNull() { assertEquals("<null>", ReflectionToStringBuilder.toString(null)); }
@Test public void test_NonExclude(){ //normal case= String toString = ReflectionToStringBuilder.toString(BOTH_NON_NULL, null, false, false, false, null); assertTrue(toString.contains(INTEGER_FIELD_NAME)); assertTrue(toString.contains(STRING_FIELD_NAME)); //make one null toString = ReflectionToStringBuilder.toString(FIRST_NULL, null, false, false, false, null); assertTrue(toString.contains(INTEGER_FIELD_NAME)); assertTrue(toString.contains(STRING_FIELD_NAME)); //other one null toString = ReflectionToStringBuilder.toString(SECOND_NULL, null, false, false, false, null); assertTrue(toString.contains(INTEGER_FIELD_NAME)); assertTrue(toString.contains(STRING_FIELD_NAME)); //make the both null toString = ReflectionToStringBuilder.toString(BOTH_NULL, null, false, false, false, null); assertTrue(toString.contains(INTEGER_FIELD_NAME)); assertTrue(toString.contains(STRING_FIELD_NAME)); }
@Test public void test_toStringExclude() { final String toString = ReflectionToStringBuilder.toString(new TestFixture()); assertThat(toString, not(containsString(EXCLUDED_FIELD_NAME))); assertThat(toString, not(containsString(EXCLUDED_FIELD_VALUE))); assertThat(toString, containsString(INCLUDED_FIELD_NAME)); assertThat(toString, containsString(INCLUDED_FIELD_VALUE)); }
@Test public void testPerson() { final Person p = new Person(); p.name = "John Doe"; p.age = 33; p.smoker = false; p.job = new Job(); p.job.title = "Manager"; final String pBaseStr = p.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p)); final String pJobStr = p.job.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p.job)); assertEquals(pBaseStr + "[name=John Doe,age=33,smoker=false,job=" + pJobStr + "[title=Manager]]", new ReflectionToStringBuilder(p, new RecursiveToStringStyle()).toString()); }
/** * Tests ReflectionToStringBuilder setUpToClass(). */ @Test public void test_setUpToClass_valid() { final Integer val = Integer.valueOf(5); final ReflectionToStringBuilder test = new ReflectionToStringBuilder(val); test.setUpToClass(Number.class); test.toString(); }
/** * Tests ReflectionToStringBuilder setUpToClass(). */ @Test(expected=IllegalArgumentException.class) public void test_setUpToClass_invalid() { final Integer val = Integer.valueOf(5); final ReflectionToStringBuilder test = new ReflectionToStringBuilder(val); try { test.setUpToClass(String.class); } finally { test.toString(); } }
private String toString(final Object object) { return new ReflectionToStringBuilder(object, new MultilineRecursiveToStringStyle()).toString(); }
@Test public void test_ConstructorOption_ExcludeNull(){ ReflectionToStringBuilder builder = new ReflectionToStringBuilder(BOTH_NULL, null, null, null, false, false, false); builder.setExcludeNullValues(true); assertTrue(builder.isExcludeNullValues()); String toString = builder.toString(); assertFalse(toString.contains(STRING_FIELD_NAME)); assertFalse(toString.contains(INTEGER_FIELD_NAME)); builder = new ReflectionToStringBuilder(BOTH_NULL, null, null, null, false, false, true); toString = builder.toString(); assertFalse(toString.contains(STRING_FIELD_NAME)); assertFalse(toString.contains(INTEGER_FIELD_NAME)); final ReflectionToStringBuilder oldBuilder = new ReflectionToStringBuilder(BOTH_NULL); oldBuilder.setExcludeNullValues(true); assertTrue(oldBuilder.isExcludeNullValues()); toString = oldBuilder.toString(); assertFalse(toString.contains(STRING_FIELD_NAME)); assertFalse(toString.contains(INTEGER_FIELD_NAME)); }
@Test public void testSimpleReflectionStatics() { final SimpleReflectionStaticFieldsFixture instance1 = new SimpleReflectionStaticFieldsFixture(); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class)); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", ReflectionToStringBuilder.toString(instance1, null, true, true, SimpleReflectionStaticFieldsFixture.class)); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class)); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class)); }
/** * Tests ReflectionToStringBuilder.toString() for statics. */ @Test public void testReflectionStatics() { final ReflectionStaticFieldsFixture instance1 = new ReflectionStaticFieldsFixture(); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]", ReflectionToStringBuilder.toString(instance1, null, false, true, ReflectionStaticFieldsFixture.class)); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,staticTransientString=staticTransientString,staticTransientInt=54321,instanceString=instanceString,instanceInt=67890,transientString=transientString,transientInt=98765]", ReflectionToStringBuilder.toString(instance1, null, true, true, ReflectionStaticFieldsFixture.class)); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]", this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class)); assertEquals( this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]", this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class)); }