/*
 * Copyright 2015-2018 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.params.aggregator;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.PreconditionViolationException;

/**
 * Unit tests for {@link ArgumentsAccessor}.
 *
 * @since 5.2
 */
class ArgumentsAccessorTests {

	@Test
	void preconditions() {
		assertThrows(PreconditionViolationException.class, () -> new DefaultArgumentsAccessor((Object[]) null));

		ArgumentsAccessor arguments = new DefaultArgumentsAccessor(1, 2);
		Exception exception = assertThrows(PreconditionViolationException.class, () -> arguments.get(-1));
		assertThat(exception.getMessage()).isEqualTo("index must be >= 0 and < 2");
	}

	@Test
	void getNull() {
		assertNull(new DefaultArgumentsAccessor(new Object[] { null }).get(0));
	}

	@Test
	void getWithNullCastToWrapperType() {
		assertNull(new DefaultArgumentsAccessor((Object[]) new Integer[] { null }).get(0, Integer.class));
	}

	@Test
	void get() {
		assertEquals(1, new DefaultArgumentsAccessor(1).get(0));
	}

	@Test
	void getWithCast() {
		assertEquals(Integer.valueOf(1), new DefaultArgumentsAccessor(1).get(0, Integer.class));
		assertEquals(Character.valueOf('A'), new DefaultArgumentsAccessor('A').get(0, Character.class));
	}

	@Test
	void getWithCastToPrimitiveType() {
		Exception exception = assertThrows(ArgumentsAccessorException.class,
			() -> new DefaultArgumentsAccessor(1).get(0, int.class));
		assertThat(exception.getMessage()).isEqualTo(
			"Argument at index [0] with value [1] and type [java.lang.Integer] could not be converted or cast to type [int].");
	}

	@Test
	void getWithCastToIncompatibleType() {
		Exception exception = assertThrows(ArgumentsAccessorException.class,
			() -> new DefaultArgumentsAccessor(1).get(0, Character.class));
		assertThat(exception.getMessage()).isEqualTo(
			"Argument at index [0] with value [1] and type [java.lang.Integer] could not be converted or cast to type [java.lang.Character].");
	}

	@Test
	void getCharacter() {
		assertEquals(Character.valueOf('A'), new DefaultArgumentsAccessor('A', 'B').getCharacter(0));
	}

	@Test
	void getBoolean() {
		assertEquals(Boolean.TRUE, new DefaultArgumentsAccessor(true, false).getBoolean(0));
	}

	@Test
	void getByte() {
		assertEquals(Byte.valueOf((byte) 42), new DefaultArgumentsAccessor((byte) 42).getByte(0));
	}

	@Test
	void getShort() {
		assertEquals(Short.valueOf((short) 42), new DefaultArgumentsAccessor((short) 42).getShort(0));
	}

	@Test
	void getInteger() {
		assertEquals(Integer.valueOf(42), new DefaultArgumentsAccessor(42).getInteger(0));
	}

	@Test
	void getLong() {
		assertEquals(Long.valueOf(42L), new DefaultArgumentsAccessor(42L).getLong(0));
	}

	@Test
	void getFloat() {
		assertEquals(Float.valueOf(42.0f), new DefaultArgumentsAccessor(42.0f).getFloat(0));
	}

	@Test
	void getDouble() {
		assertEquals(Double.valueOf(42.0), new DefaultArgumentsAccessor(42.0).getDouble(0));
	}

	@Test
	void getString() {
		assertEquals("foo", new DefaultArgumentsAccessor("foo", "bar").getString(0));
	}

	@Test
	void toArray() {
		DefaultArgumentsAccessor arguments = new DefaultArgumentsAccessor("foo", "bar");
		Object[] copy = arguments.toArray();
		assertArrayEquals(new String[] { "foo", "bar" }, copy);

		// Modify local copy:
		copy[0] = "Boom!";
		assertEquals("foo", arguments.toArray()[0]);
	}

	@Test
	void toList() {
		DefaultArgumentsAccessor arguments = new DefaultArgumentsAccessor("foo", "bar");
		List<Object> copy = arguments.toList();
		assertIterableEquals(Arrays.asList("foo", "bar"), copy);

		// Modify local copy:
		assertThrows(UnsupportedOperationException.class, () -> copy.set(0, "Boom!"));
	}

	@Test
	void size() {
		assertEquals(0, new DefaultArgumentsAccessor().size());
		assertEquals(1, new DefaultArgumentsAccessor(42).size());
		assertEquals(5, new DefaultArgumentsAccessor('a', 'b', 'c', 'd', 'e').size());
	}

}

@@ -2275,24 +2275,324 @@
 pe %5Bint%5D.%22);
+%0A%0A%09%09exception = assertThrows(ArgumentsAccessorException.class,%0A%09%09%09() -%3E new DefaultArgumentsAccessor(new Object%5B%5D %7B null %7D).get(0, int.class));%0A%09%09assertThat(exception.getMessage()).isEqualTo(%0A%09%09%09%22Argument at index %5B0%5D with value %5Bnull%5D and type %5Bnull%5D could not be converted or cast to type %5Bint%5D.%22);
 %0A%09%7D%0A%0A%09@Test%0A

Test for null to primitive cast in ArgumentsAccessor