/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.Test;
import org.junit.Before;
import org.junit.BeforeClass;
import stubs.cglib.SimpleCollectionEntity;
import stubs.cglib.SimpleEntity;
import stubs.cglib.SimpleInnerEntity;
import static org.junit.Assert.*;
/**
*
* @author Jose Noheda
* @since 1.0
*/
public class BeanUtilsTest {
private static EntityManagerFactory factory;
private static EntityManager entityManager;
private static String pk;
@BeforeClass
public static void setUp() throws Exception {
factory = Persistence.createEntityManagerFactory("CGLIB");
entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
SimpleInnerEntity sub = new SimpleInnerEntity();
sub.setName("simple inner entity");
SimpleCollectionEntity col = new SimpleCollectionEntity();
SimpleCollectionEntity col2 = new SimpleCollectionEntity();
Collection<SimpleCollectionEntity> cols = new ArrayList<SimpleCollectionEntity>();
cols.add(col);
cols.add(col2);
SimpleEntity e = new SimpleEntity();
col.setSimpleEntity(e);
col2.setSimpleEntity(e);
sub.setEntity(new ArrayList<SimpleEntity>());
sub.getEntity().add(e);
e.setName("a name");
e.setSimpleCollection(cols);
e.setCglibEntity(sub);
entityManager.persist(e);
pk = e.getId();
entityManager.getTransaction().commit();
entityManager.close();
}
@Before
public void init() {
entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
}
@Test
public void testCopyProperties() {
assertNull("Null is cloned as null", BeanUtils.copyProperties(null));
SimpleEntity e = entityManager.find(SimpleEntity.class, pk);
SimpleEntity target = new SimpleEntity();
BeanUtils.copyProperties(e, target);
assertEquals("String copied", "a name", target.getName());
assertNull("Non initialized object is not copied", target.getCglibEntity());
assertNull("Non initialized collection is not copied", target.getSimpleCollection());
SimpleInnerEntity inner = BeanUtils.copyProperties(e.getCglibEntity());
assertNull("Non initialized CGLIB object is not cloned", inner);
e.getSimpleCollection().size();
e.getCglibEntity().getName();
target = new SimpleEntity();
BeanUtils.copyProperties(e, target);
assertTrue("Initialized collection is copied", target.getSimpleCollection().size() == 2);
assertNotNull("Initialized object is copied", target.getCglibEntity().getId());
assertEquals("Initialized object is copied", "simple inner entity", target.getCglibEntity().getName());
inner = BeanUtils.copyProperties(e.getCglibEntity());
assertEquals("CGLIB objects are copied", "simple inner entity", inner.getName());
target = new SimpleEntity();
BeanUtils.copyProperties(e, target, Arrays.asList("name"));
assertNull("Properties are ignored", target.getName());
}
@After
public void tearDown() {
entityManager.getTransaction().rollback();
entityManager.close();
}
}