package eu.dnetlib.contract.runner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;

/**
 * Tests for rpimitives recognition.
 * 
 * @author mhorst
 *
 */
public class PrimitivesRecognitionTest {

	@Test
	public void testRecognize() {
		int intValue = 1;
		Class<?> primitiveIntClass = int.class;
		assertNotNull(primitiveIntClass);
		assertTrue(primitiveIntClass.isPrimitive());
		
		Integer integerValue = 1;
//		core of the problem, Integer is not an instance of int;
		assertFalse(primitiveIntClass.isInstance(integerValue));
		assertFalse(primitiveIntClass.isInstance(null));
		
		assertFalse(Integer.class.isInstance(null));

//		checking the other way if int isInstance of Integer
//		it works because of autoboxing
		assertTrue(Integer.class.isInstance(intValue));
		
//		using isAssignable, same result for int vs Integer
		assertFalse(primitiveIntClass.isAssignableFrom(integerValue.getClass()));
//		throws an exception!
		try {
			assertFalse(primitiveIntClass.isAssignableFrom(null));
			fail("exception should be thrown");
		} catch (NullPointerException e) {
//			ok
		}
	}
	
	
}
