001package org.apache.commons.beanutils2.testbeans; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020import static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertSame; 023 024public final class TestBeanUtils 025{ 026 private TestBeanUtils() 027 { 028 // private constructor to prevent instantiation 029 } 030 031 public static void changeArrayProperties( TestBean testBean ) 032 { 033 int intArray[] = new int[] { 123, 456, 789 }; 034 String stringArray[] = new String[] { "New String 0", "New String 1" }; 035 testBean.setIntArray( intArray ); 036 testBean.setStringArray( stringArray ); 037 } 038 039 public static void changeScalarProperties( TestBean testBean ) 040 { 041 testBean.setBooleanProperty( false ); 042 // booleanSecond is left at true 043 testBean.setByteProperty( (byte) 111 ); 044 testBean.setDoubleProperty( 432.0 ); 045 // floatProperty is left at 123.0 046 testBean.setIntProperty( 543 ); 047 testBean.setLongProperty( 123456789l ); 048 testBean.setNullProperty( "Non-null value" ); 049 testBean.setShortProperty( (short) 654 ); 050 // stringProperty is left at "This is a string" 051 } 052 053 /** 054 * Asserts that all properties that are readable and writable on {@code actual} are equal to those on 055 * {@code expected}. 056 * 057 * @param expected the {@code TestBean} with the expected properties. 058 * @param actual the {@code TestBean} with the actual values. 059 */ 060 public static void assertReadWritePropertiesEquals( TestBean expected, TestBean actual ) 061 { 062 assertEquals( "Property 'anotherNested' is not equal!", expected.getAnotherNested(), actual.getAnotherNested() ); 063 assertEquals( "Property 'booleanProperty' is not equal!", expected.getBooleanProperty(), 064 actual.getBooleanProperty() ); 065 assertEquals( "Property 'booleanSecond' is not equal!", expected.isBooleanSecond(), actual.isBooleanSecond() ); 066 assertEquals( "Property 'byteProperty' is not equal!", expected.getByteProperty(), actual.getByteProperty() ); 067 assertArrayEquals( "Property 'DateArrayProperty' is not equal!", expected.getDateArrayProperty(), 068 actual.getDateArrayProperty() ); 069 assertEquals( "Property 'dateProperty' is not equal!", expected.getDateProperty(), actual.getDateProperty() ); 070 assertEquals( "Property 'doubleProperty' is not equal!", expected.getDoubleProperty(), 071 actual.getDoubleProperty(), 0.0 ); 072 assertArrayEquals( "Property 'dupProperty' is not equal!", expected.getDupProperty(), actual.getDupProperty() ); 073 assertEquals( "Property 'floatProperty' is not equal!", expected.getFloatProperty(), actual.getFloatProperty(), 074 0.0 ); 075 assertArrayEquals( "Property 'intArrayProperty' is not equal!", expected.getIntArray(), actual.getIntArray() ); 076 assertEquals( "Property 'intProperty' is not equal!", expected.getIntProperty(), actual.getIntProperty() ); 077 assertEquals( "Property 'invalidBoolean' is not equal!", expected.getInvalidBoolean(), 078 actual.getInvalidBoolean() ); 079 assertEquals( "Property 'listIndexed' is not equal!", expected.getListIndexed(), actual.getListIndexed() ); 080 assertEquals( "Property 'longProperty' is not equal!", expected.getLongProperty(), actual.getLongProperty() ); 081 assertEquals( "Property 'mapProperty' is not equal!", expected.getMapProperty(), actual.getMapProperty() ); 082 assertEquals( "Property 'nested' is not equal!", expected.getNested(), actual.getNested() ); 083 assertEquals( "Property 'nullProperty' is not equal!", expected.getNullProperty(), actual.getNullProperty() ); 084 assertEquals( "Property 'shortProperty' is not equal!", expected.getShortProperty(), actual.getShortProperty() ); 085 assertArrayEquals( "Property 'stringArray' is not equal!", expected.getStringArray(), actual.getStringArray() ); 086 assertEquals( "Property 'stringProperty' is not equal!", expected.getStringProperty(), 087 actual.getStringProperty() ); 088 } 089 090 /** 091 * Asserts, that complex properties of {@code referenced} are the same as in {@code referencing} (that they are just 092 * referenced). 093 */ 094 public static void assertShallow( TestBean referenced, TestBean referencing ) 095 { 096 assertSame( referenced.getAnotherNested(), referencing.getAnotherNested() ); 097 assertSame( referenced.getDateArrayProperty(), referencing.getDateArrayProperty() ); 098 assertSame( referenced.getDateProperty(), referencing.getDateProperty() ); 099 assertSame( referenced.getDateProperty(), referencing.getDateProperty() ); 100 assertSame( referenced.getDupProperty(), referencing.getDupProperty() ); 101 assertSame( referenced.getIntArray(), referencing.getIntArray() ); 102 assertSame( referenced.getMapProperty(), referencing.getMapProperty() ); 103 // mappedNested and nested are lazy initialized, so the following getXXX calls will create new instances on 104 // both, clone and original, causing the assertions to fail. 105 // assertSame(clone.getMappedNested(), original.getMappedNested()); 106 // assertSame(clone.getNested(), original.getNested()); 107 assertSame( referenced.getStringArray(), referencing.getStringArray() ); 108 assertSame( referenced.getStringProperty(), referencing.getStringProperty() ); 109 } 110 111}