/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.bytecode.javassist;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.bytecode.internal.javassist.BytecodeProviderImpl;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.test.bytecode.Bean;
import org.hibernate.test.bytecode.BeanReflectionHelper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Steve Ebersole
*/
public class ReflectionOptimizerTest extends BaseUnitTestCase {
@Test
public void testReflectionOptimization() {
BytecodeProviderImpl provider = new BytecodeProviderImpl();
ReflectionOptimizer optimizer = provider.getReflectionOptimizer(
Bean.class,
BeanReflectionHelper.getGetterNames(),
BeanReflectionHelper.getSetterNames(),
BeanReflectionHelper.getTypes()
);
assertNotNull( optimizer );
assertNotNull( optimizer.getInstantiationOptimizer() );
assertNotNull( optimizer.getAccessOptimizer() );
Object instance = optimizer.getInstantiationOptimizer().newInstance();
assertEquals( instance.getClass(), Bean.class );
Bean bean = ( Bean ) instance;
optimizer.getAccessOptimizer().setPropertyValues( bean, BeanReflectionHelper.TEST_VALUES );
assertEquals( bean.getSomeString(), BeanReflectionHelper.TEST_VALUES[0] );
Object[] values = optimizer.getAccessOptimizer().getPropertyValues( bean );
assertEquivalent( values, BeanReflectionHelper.TEST_VALUES );
}
private void assertEquivalent(Object[] checkValues, Object[] values) {
assertEquals( "Different lengths", checkValues.length, values.length );
for ( int i = 0; i < checkValues.length; i++ ) {
assertEquals( "different values at index [" + i + "]", checkValues[i], values[i] );
}
}
}