1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.lang.builder; 19 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.List; 23 24 import junit.framework.Assert; 25 import junit.framework.TestCase; 26 27 import org.apache.commons.lang.ArrayUtils; 28 29 /** 30 * @author <a href="mailto:ggregory@seagullsw.com">ggregory</a> 31 * @version $Id: ReflectionToStringBuilderExcludeTest.java 437554 2006-08-28 06:21:41Z bayard $ 32 */ 33 public class ReflectionToStringBuilderExcludeTest extends TestCase { 34 35 class TestFixture { 36 private String secretField = SECRET_VALUE; 37 38 private String showField = NOT_SECRET_VALUE; 39 } 40 41 private static final String NOT_SECRET_FIELD = "showField"; 42 43 private static final String NOT_SECRET_VALUE = "Hello World!"; 44 45 private static final String SECRET_FIELD = "secretField"; 46 47 private static final String SECRET_VALUE = "secret value"; 48 test_toStringExclude()49 public void test_toStringExclude() { 50 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), SECRET_FIELD); 51 this.validateSecretFieldAbsent(toString); 52 } 53 test_toStringExcludeArray()54 public void test_toStringExcludeArray() { 55 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), new String[]{SECRET_FIELD}); 56 this.validateSecretFieldAbsent(toString); 57 } 58 test_toStringExcludeArrayWithNull()59 public void test_toStringExcludeArrayWithNull() { 60 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), new String[]{null}); 61 this.validateSecretFieldPresent(toString); 62 } 63 test_toStringExcludeArrayWithNulls()64 public void test_toStringExcludeArrayWithNulls() { 65 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), new String[]{null, null}); 66 this.validateSecretFieldPresent(toString); 67 } 68 test_toStringExcludeCollection()69 public void test_toStringExcludeCollection() { 70 List excludeList = new ArrayList(); 71 excludeList.add(SECRET_FIELD); 72 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), excludeList); 73 this.validateSecretFieldAbsent(toString); 74 } 75 test_toStringExcludeCollectionWithNull()76 public void test_toStringExcludeCollectionWithNull() { 77 List excludeList = new ArrayList(); 78 excludeList.add(null); 79 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), excludeList); 80 this.validateSecretFieldPresent(toString); 81 } 82 test_toStringExcludeCollectionWithNulls()83 public void test_toStringExcludeCollectionWithNulls() { 84 List excludeList = new ArrayList(); 85 excludeList.add(null); 86 excludeList.add(null); 87 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), excludeList); 88 this.validateSecretFieldPresent(toString); 89 } 90 test_toStringExcludeEmptyArray()91 public void test_toStringExcludeEmptyArray() { 92 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), ArrayUtils.EMPTY_STRING_ARRAY); 93 this.validateSecretFieldPresent(toString); 94 } 95 test_toStringExcludeEmptyCollection()96 public void test_toStringExcludeEmptyCollection() { 97 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), new ArrayList()); 98 this.validateSecretFieldPresent(toString); 99 } 100 test_toStringExcludeNullArray()101 public void test_toStringExcludeNullArray() { 102 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), (String[]) null); 103 this.validateSecretFieldPresent(toString); 104 } 105 test_toStringExcludeNullCollection()106 public void test_toStringExcludeNullCollection() { 107 String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), (Collection) null); 108 this.validateSecretFieldPresent(toString); 109 } 110 validateNonSecretField(String toString)111 private void validateNonSecretField(String toString) { 112 Assert.assertTrue(toString.indexOf(NOT_SECRET_FIELD) > ArrayUtils.INDEX_NOT_FOUND); 113 Assert.assertTrue(toString.indexOf(NOT_SECRET_VALUE) > ArrayUtils.INDEX_NOT_FOUND); 114 } 115 validateSecretFieldAbsent(String toString)116 private void validateSecretFieldAbsent(String toString) { 117 Assert.assertEquals(ArrayUtils.INDEX_NOT_FOUND, toString.indexOf(SECRET_VALUE)); 118 this.validateNonSecretField(toString); 119 } 120 validateSecretFieldPresent(String toString)121 private void validateSecretFieldPresent(String toString) { 122 Assert.assertTrue(toString.indexOf(SECRET_VALUE) > 0); 123 this.validateNonSecretField(toString); 124 } 125 } 126