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

import net.ranides.assira.generic.Wrapper;
import net.ranides.assira.reflection.GenericField;
import net.ranides.assira.reflection.InspectException;
import net.ranides.assira.reflection.mock.ForFieldInspector;
import net.ranides.assira.reflection.mock.ForFieldInspector.*;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class FieldInspectorTest {
    
    @Test
    public void testFind() {
        ForFieldInspector that = new ForFieldInspector();
        assertEquals(3, FieldInspector.find(ForFieldInspector.class, "A").get(null));
        assertEquals(4, FieldInspector.find(ForFieldInspector.class, "B").get(null));
        assertEquals(6, FieldInspector.find(ForFieldInspector.class, "width").get(that));
        assertEquals("List<Number>", FieldInspector.find(ForFieldInspector.class, "list").type().toString());
        
        try {
            FieldInspector.find(ForFieldInspector.class, "Q");
            fail("InspectException expected");
        } catch(InspectException ex) {
            assertTrue(true);
        }
    }
    
    
    @Test
    public void testCheck() {
        ForFieldInspector that = new ForFieldInspector();
        
        GenericField<?> field1 = FieldInspector.find(ForFieldInspector.class, "width");
        GenericField<Integer> field2 = FieldInspector.check(int.class, field1);
        Wrapper<Integer> wrapper = field2.bind(that);
                
        assertEquals( (Integer)6, wrapper.get());
        
        try {
            FieldInspector.find(ForFieldInspector.class, "Q");
            fail("InspectException expected");
        } catch(InspectException ex) {
            assertTrue(true);
        }
        
        try {
            FieldInspector.check(float.class, field1);
            fail("InspectException expected");
        } catch(InspectException ex) {
            assertTrue(true);
        }
    }
    
    @Test
    public void testCopy() {
        A a = new A();
        B b = new B();
        
        FieldInspector.copy(b, a);
        assertEquals(10, b.a);
        assertEquals(20, b.b);
        assertEquals(40, b.d);
    }
    
}
