<commit_before>package uk.co.webamoeba.mockito.collections.util;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

/**
 * Unit test for {@link GenericCollectionTypeResolver}. Unlike {@link GenericCollectionTypeResolver} this class has not
 * been ported from Spring.
 * 
 * @author <NAME>
 */
@RunWith(MockitoJUnitRunner.class)
public class GenericCollectionTypeResolverTest {

	private GenericCollectionTypeResolver resolver = new GenericCollectionTypeResolver();

	@Test
	public void shouldGetCollectionFieldTypeGivenCollection() {
		shouldGetCollectionFieldType("collection", String.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenRawCollection() {
		shouldGetCollectionFieldType("rawCollection", null);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenArrayCollection() {
		shouldGetCollectionFieldType("arrayCollection", String[].class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenWildcardCollection() {
		shouldGetCollectionFieldType("wildcardCollection", null);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenWildcardUpperBoundCollection() {
		shouldGetCollectionFieldType("wildcardUpperBoundCollection", InputStream.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenWildcardLowerBoundCollection() {
		shouldGetCollectionFieldType("wildcardLowerBoundCollection", FileInputStream.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenHardCodedTypeCollection() {
		shouldGetCollectionFieldType("hardCodedTypeCollection", Integer.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenTypeCollection() {
		shouldGetCollectionFieldType("typeCollection", Boolean.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenExtendedHardCodedTypeCollection() {
		shouldGetCollectionFieldType("extendedHardCodedTypeCollection", Integer.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenDoubleExtendedHardCodedTypeCollection() {
		shouldGetCollectionFieldType("doubleExtendedHardCodedTypeCollection", Integer.class);
	}

	@Test
	public void shouldFailToGetCollectionFieldTypeGivenTypeVariable() {
		shouldFailToGetCollectionFieldType("typeVariable");
	}

	private void shouldGetCollectionFieldType(String fieldName, Class<?> expectedType) {
		// Given
		Field field = getField(fieldName);

		// When
		Class<?> type = resolver.getCollectionFieldType(field);

		// Then
		assertSame(expectedType, type);
	}

	private void shouldFailToGetCollectionFieldType(String fieldName) {
		// Given
		Field field = getField(fieldName);

		// When
		Class<?> type = resolver.getCollectionFieldType(field);

		// Then
		assertNull(type);
	}

	private Field getField(String name) {
		try {
			return FieldProvider.class.getField(name);
		} catch (Exception e) {
			throw new IllegalArgumentException("Failed to get Field " + name, e);
		}
	}

	/**
	 * Provides a bunch of fields declared such that we can resolve the types associated with the {@link Field}s for the
	 * purpose of this unit test.
	 * 
	 * @author <NAME>
	 */
	private class FieldProvider<T> {

		/**
		 * Expect {@link String}
		 */
		@SuppressWarnings("unused")
		public Collection<String> collection;

		/**
		 * Expect <code>null</code>
		 */
		@SuppressWarnings({ "unused", "rawtypes" })
		public Collection rawCollection;

		/**
		 * Expect {@link String[]}
		 */
		@SuppressWarnings("unused")
		public Collection<String[]> arrayCollection;

		/**
		 * Expect <code>null</code>
		 */
		@SuppressWarnings("unused")
		public Collection<?> wildcardCollection;

		/**
		 * Expect {@link InputStream}
		 */
		@SuppressWarnings("unused")
		public Collection<? extends InputStream> wildcardUpperBoundCollection;

		/**
		 * Expect {@link FileInputStream}
		 * <p>
		 * TODO is this the behaviour we want when given a lower bound?
		 * 
		 * @see <a href="http://docs.oracle.com/javase/tutorial/extra/generics/morefun.html">Generics, bound
		 *      wildcards</a>
		 */
		@SuppressWarnings("unused")
		public Collection<? super FileInputStream> wildcardLowerBoundCollection;

		/**
		 * Expect {@link Integer} as per {@link HardCodedTypeCollection}
		 */
		@SuppressWarnings("unused")
		public HardCodedTypeCollection hardCodedTypeCollection;

		/**
		 * Expect {@link Boolean}
		 */
		@SuppressWarnings("unused")
		public TypeCollection<Boolean> typeCollection;

		/**
		 * Expect {@link Integer} as per {@link HardCodedTypeCollection} (should ignore {@link String} generic declared
		 * here)
		 */
		@SuppressWarnings("unused")
		public ExtendedHardCodedTypeCollection<String> extendedHardCodedTypeCollection;

		/**
		 * Expect {@link Integer} as per {@link HardCodedTypeCollection} (should ignore {@link Reader} generic declared
		 * as part of {@link DoubleExtendedHardCodedTypeCollection})
		 */
		@SuppressWarnings("unused")
		public DoubleExtendedHardCodedTypeCollection doubleExtendedHardCodedTypeCollection;

		public T typeVariable;

	}

	private interface HardCodedTypeCollection extends Collection<Integer> {
	}

	private interface TypeCollection<T> extends Collection<T> {
	}

	private interface ExtendedHardCodedTypeCollection<T> extends HardCodedTypeCollection {
	}

	private interface DoubleExtendedHardCodedTypeCollection extends ExtendedHardCodedTypeCollection<Reader> {
	}
}
<commit_msg>Tidy up warning suppression<commit_after>package uk.co.webamoeba.mockito.collections.util;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

/**
 * Unit test for {@link GenericCollectionTypeResolver}. Unlike {@link GenericCollectionTypeResolver} this class has not
 * been ported from Spring.
 * 
 * @author <NAME>
 */
@RunWith(MockitoJUnitRunner.class)
public class GenericCollectionTypeResolverTest {

	private GenericCollectionTypeResolver resolver = new GenericCollectionTypeResolver();

	@Test
	public void shouldGetCollectionFieldTypeGivenCollection() {
		shouldGetCollectionFieldType("collection", String.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenRawCollection() {
		shouldGetCollectionFieldType("rawCollection", null);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenArrayCollection() {
		shouldGetCollectionFieldType("arrayCollection", String[].class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenWildcardCollection() {
		shouldGetCollectionFieldType("wildcardCollection", null);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenWildcardUpperBoundCollection() {
		shouldGetCollectionFieldType("wildcardUpperBoundCollection", InputStream.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenWildcardLowerBoundCollection() {
		shouldGetCollectionFieldType("wildcardLowerBoundCollection", FileInputStream.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenHardCodedTypeCollection() {
		shouldGetCollectionFieldType("hardCodedTypeCollection", Integer.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenTypeCollection() {
		shouldGetCollectionFieldType("typeCollection", Boolean.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenExtendedHardCodedTypeCollection() {
		shouldGetCollectionFieldType("extendedHardCodedTypeCollection", Integer.class);
	}

	@Test
	public void shouldGetCollectionFieldTypeGivenDoubleExtendedHardCodedTypeCollection() {
		shouldGetCollectionFieldType("doubleExtendedHardCodedTypeCollection", Integer.class);
	}

	@Test
	public void shouldFailToGetCollectionFieldTypeGivenTypeVariable() {
		shouldFailToGetCollectionFieldType("typeVariable");
	}

	private void shouldGetCollectionFieldType(String fieldName, Class<?> expectedType) {
		// Given
		Field field = getField(fieldName);

		// When
		Class<?> type = resolver.getCollectionFieldType(field);

		// Then
		assertSame(expectedType, type);
	}

	private void shouldFailToGetCollectionFieldType(String fieldName) {
		// Given
		Field field = getField(fieldName);

		// When
		Class<?> type = resolver.getCollectionFieldType(field);

		// Then
		assertNull(type);
	}

	private Field getField(String name) {
		try {
			return FieldProvider.class.getField(name);
		} catch (Exception e) {
			throw new IllegalArgumentException("Failed to get Field " + name, e);
		}
	}

	/**
	 * Provides a bunch of fields declared such that we can resolve the types associated with the {@link Field}s for the
	 * purpose of this unit test.
	 * 
	 * @author <NAME>
	 */
	@SuppressWarnings("unused")
	private class FieldProvider<T> {

		/**
		 * Expect {@link String}
		 */
		public Collection<String> collection;

		/**
		 * Expect <code>null</code>
		 */
		@SuppressWarnings("rawtypes")
		public Collection rawCollection;

		/**
		 * Expect {@link String[]}
		 */
		public Collection<String[]> arrayCollection;

		/**
		 * Expect <code>null</code>
		 */
		public Collection<?> wildcardCollection;

		/**
		 * Expect {@link InputStream}
		 */
		public Collection<? extends InputStream> wildcardUpperBoundCollection;

		/**
		 * Expect {@link FileInputStream}
		 * <p>
		 * TODO is this the behaviour we want when given a lower bound?
		 * 
		 * @see <a href="http://docs.oracle.com/javase/tutorial/extra/generics/morefun.html">Generics, bound
		 *      wildcards</a>
		 */
		public Collection<? super FileInputStream> wildcardLowerBoundCollection;

		/**
		 * Expect {@link Integer} as per {@link HardCodedTypeCollection}
		 */
		public HardCodedTypeCollection hardCodedTypeCollection;

		/**
		 * Expect {@link Boolean}
		 */
		public TypeCollection<Boolean> typeCollection;

		/**
		 * Expect {@link Integer} as per {@link HardCodedTypeCollection} (should ignore {@link String} generic declared
		 * here)
		 */
		public ExtendedHardCodedTypeCollection<String> extendedHardCodedTypeCollection;

		/**
		 * Expect {@link Integer} as per {@link HardCodedTypeCollection} (should ignore {@link Reader} generic declared
		 * as part of {@link DoubleExtendedHardCodedTypeCollection})
		 */
		public DoubleExtendedHardCodedTypeCollection doubleExtendedHardCodedTypeCollection;

		public T typeVariable;
	}

	private interface HardCodedTypeCollection extends Collection<Integer> {
	}

	private interface TypeCollection<T> extends Collection<T> {
	}

	private interface ExtendedHardCodedTypeCollection<T> extends HardCodedTypeCollection {
	}

	private interface DoubleExtendedHardCodedTypeCollection extends ExtendedHardCodedTypeCollection<Reader> {
	}
}

{'fraction_non_alphanumeric': 0.0977699963140435, 'fraction_numerical': 0.0, 'mean_word_length': 5.425695677915926, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1701131', 'n_tokens_mistral': 3257, 'n_tokens_neox': 2926, 'n_words': 673}