/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.reflection.util;

import java.lang.reflect.Method;
import net.ranides.assira.reflection.inspect.MethodInspector;
import net.ranides.assira.reflection.mock.Bean2;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class BeanUtilsTest {
    
    private static Method fm(Class<?> clazz, String name) {
        return MethodInspector.inclass(clazz).published().name(name).first().reflective();
    }

    @Test
    public void testAsPropertyName() {
        
        assertEquals("name", BeanUtils.asPropertyName(fm(Bean2.class, "isName")));
        assertEquals("size", BeanUtils.asPropertyName(fm(Bean2.class, "getSize")));
        
        assertEquals("name", BeanUtils.asPropertyName(fm(Bean2.class, "setName")));
        assertEquals("list", BeanUtils.asPropertyName(fm(Bean2.class, "setList")));
        
        
        // invalid for some reason should be unchanged
        assertEquals("isValue", BeanUtils.asPropertyName(fm(Bean2.class, "isValue")));
        assertEquals("isPair", BeanUtils.asPropertyName(fm(Bean2.class, "isPair")));
        
        assertEquals("getLength", BeanUtils.asPropertyName(fm(Bean2.class, "getLength")));
        assertEquals("getError", BeanUtils.asPropertyName(fm(Bean2.class, "getError")));
    }
    
    @Test
    public void testIntAsPropertyName() {
        
        assertEquals("name", BeanUtils.$asPropertyName(fm(Bean2.class, "isName")));
        assertEquals("size", BeanUtils.$asPropertyName(fm(Bean2.class, "getSize")));
        
        assertEquals("name", BeanUtils.$asPropertyName(fm(Bean2.class, "setName")));
        assertEquals("list", BeanUtils.$asPropertyName(fm(Bean2.class, "setList")));
        
        
        // invalid method, but name is correct
        assertEquals("value", BeanUtils.$asPropertyName(fm(Bean2.class, "isValue")));
        assertEquals("pair", BeanUtils.$asPropertyName(fm(Bean2.class, "isPair")));
        
        assertEquals("length", BeanUtils.$asPropertyName(fm(Bean2.class, "getLength")));
        assertEquals("error", BeanUtils.$asPropertyName(fm(Bean2.class, "getError")));
    }
    
}
