Package org.internna.iwebmvc.utils

Source Code of org.internna.iwebmvc.utils.ClassUtilsTest

/*
* 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.lang.reflect.Field;
import org.internna.iwebmvc.model.DomainEntity;
import org.internna.iwebmvc.model.core.I18nText;
import org.internna.iwebmvc.model.core.LocalizedValue;
import org.junit.Test;
import stubs.Trivial;
import stubs.TrivialDomainEntity;
import stubs.validation.FakeDomainEntity;
import stubs.validation.InnerBeanExtension;
import static org.junit.Assert.*;

public class ClassUtilsTest {

    @Test(expected = Exception.class)
    public void testInstantiateClass() throws Exception {
        assertNull("Null class", ClassUtils.instantiateClass((String) null));
        assertNull("Null class", ClassUtils.instantiateClass((Class<?>) null));
        assertNotNull("A simple class", ClassUtils.instantiateClass("stubs.Trivial"));
        assertTrue("Correct instance type", DomainEntity.class.isInstance(ClassUtils.instantiateClass(TrivialDomainEntity.class)));
        assertNotNull("Automatic interface proxy ", ClassUtils.instantiateClass(DomainEntity.class));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetFields() {
        assertTrue("No fields return 0", 0 == ClassUtils.getFields(TrivialDomainEntity.class).size());
        assertTrue("One field", 1 == ClassUtils.getFields(Trivial.class).size());
        assertTrue("Fields in subclass are counted", 3 == ClassUtils.getFields(InnerBeanExtension.class).size());
        assertTrue("One field", 1 == ClassUtils.getFields(null).size());
    }

    @Test
    public void getField() {
        assertNull(ClassUtils.getField(TrivialDomainEntity.class, "heck"));
        assertNotNull(ClassUtils.getField(FakeDomainEntity.class, "color"));
        assertNotNull(ClassUtils.getField(FakeDomainEntity.class, "id"));
    }

    @Test
    public void getGenericType() throws Exception {
        Field field = I18nText.class.getDeclaredField("values");
        assertEquals(ClassUtils.getGenericType(field), LocalizedValue.class);
    }

}
TOP

Related Classes of org.internna.iwebmvc.utils.ClassUtilsTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.