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

import static net.ranides.assira.reflection.mockup.ForMemberTraits.*;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class MemberTraitsTest {

    @Test
    public void testModifiersTrue() throws Exception {
        assertTrue(MemberTraits.isPublic(PUBLIC) );
        assertTrue(MemberTraits.isProtected(PROTECTED) );
        assertTrue(MemberTraits.isPrivate(PRIVATE) );
        assertTrue(MemberTraits.isStatic(STATIC) );
        assertTrue(MemberTraits.isVolatile(VOLATILE) );
        assertTrue(MemberTraits.isTransient(TRANSIENT) );
        assertTrue(MemberTraits.isFinal(FINAL) );
        assertTrue(MemberTraits.isAbstract(ABSTRACT) );
        assertTrue(MemberTraits.isSynchronized(SYNCHRONIZED) );
        assertTrue(MemberTraits.isStrict(STRICT) );
        assertTrue(MemberTraits.isNative(NATIVE) );
    }
    
    @Test
    public void testModifiersFalse() throws Exception {
        assertFalse(MemberTraits.isPublic(NOTHING) );
        assertFalse(MemberTraits.isProtected(NOTHING) );
        assertFalse(MemberTraits.isPrivate(NOTHING) );
        assertFalse(MemberTraits.isStatic(NOTHING) );
        assertFalse(MemberTraits.isVolatile(NOTHING) );
        assertFalse(MemberTraits.isTransient(NOTHING) );
        assertFalse(MemberTraits.isFinal(NOTHING) );
        assertFalse(MemberTraits.isAbstract(NOTHING) );
        assertFalse(MemberTraits.isAbstract(METHOD) );
        assertFalse(MemberTraits.isAbstract(FINAL) );
        assertFalse(MemberTraits.isSynchronized(METHOD) );
        assertFalse(MemberTraits.isStrict(METHOD) );
        assertFalse(MemberTraits.isNative(METHOD) );
    }
    
    @Test
    public void testModifiersPackage() throws Exception {
        assertTrue(MemberTraits.isPackage(NOTHING) );
        assertFalse(MemberTraits.isPackage(PRIVATE) );
        assertFalse(MemberTraits.isPackage(PUBLIC) );
        assertFalse(MemberTraits.isPackage(PROTECTED) );
    }
    
    @Test
    public void testGetter() throws NoSuchMethodException {
        assertTrue( MemberTraits.isGetter($method(Bean.class, "getName4")));
        assertTrue( MemberTraits.isGetter($method(Bean.class, "isVisible2")));
        assertTrue( MemberTraits.isGetter($method(Bean.class, "isVisible3")));
        assertTrue( MemberTraits.isGetter($method(Bean.class, "isVisible3")));
        
        assertFalse( MemberTraits.isGetter($method(Bean.class, "getName1")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "getName2")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "getName3")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "get")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "name6")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "getname5")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "isVisible1")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "isvisible4")));
        assertFalse( MemberTraits.isGetter($method(Bean.class, "is")));
        
        assertFalse( MemberTraits.isGetter($method(Object.class, "getClass")));
        assertTrue( MemberTraits.isGetter(String.class.getDeclaredMethod("getBytes")));
        
        assertFalse( MemberTraits.isGetter(GETTER));
    }
    
    @Test
    public void testSetter() throws NoSuchMethodException {
        assertTrue( MemberTraits.isSetter($method(Bean.class, "setName1")));
        assertTrue( MemberTraits.isSetter($method(Bean.class, "setName2")));
        assertTrue( MemberTraits.isSetter($method(Bean.class, "setName8")));
        
        assertFalse( MemberTraits.isSetter($method(Bean.class, "getName3")));
        assertFalse( MemberTraits.isSetter($method(Bean.class, "name5")));
        assertFalse( MemberTraits.isSetter($method(Bean.class, "setName6")));
        assertFalse( MemberTraits.isSetter($method(Bean.class, "set")));
        assertFalse( MemberTraits.isSetter($method(Bean.class, "setname7")));
        
        assertFalse( MemberTraits.isSetter($method(Object.class, "getClass")));
        assertFalse( MemberTraits.isGetter(SETTER));
    }
    
    @Test
    public void testSetterSelf() throws NoSuchMethodException {
        assertTrue( MemberTraits.isSetter($method(Bean.class, "setName3")));
        
        // overloading
        assertFalse( MemberTraits.isSetter($method(Bean2.class, "setName2")));
        assertTrue( MemberTraits.isSetter(Bean2.class.getDeclaredMethod("setName3", int.class)));
        assertTrue( MemberTraits.isSetter($method(Bean2.class, "setName4")));
    }
    
}
