@Test public void shouldFindPossibleAssignmentsFromMethod() throws Throwable { this.assignments = new PotentialAssignments(new TestClass(AssignmentsFromMethod.class)); final List<Assignment> assignments = this.assignments.allPossible().collect(Collectors.toList()); assertThat(assignments).hasSize(2); ArrayList<ParameterSignature> parameterSignature = ParameterSignature.signatures(AssignmentsFromMethod.class.getMethod("a", String.class)); assertThat(assignments.get(0).isValidFor(parameterSignature.get(0))); assertThat(assignments.get(0).parameterProducer().produceParamValue()).isEqualTo("42"); assertThat(assignments.get(1).isValidFor(parameterSignature.get(0))); assertThat(assignments.get(1).parameterProducer().produceParamValue()).isEqualTo("27"); }
@Override public List<PotentialAssignment> getValueSources(final ParameterSignature param) { final ArrayList<PotentialAssignment> list = new ArrayList<>(); list.add(PotentialAssignment.forValue("", "foo")); list.add(PotentialAssignment.forValue("", "bar")); list.add(PotentialAssignment.forValue("", "foo")); list.add(PotentialAssignment.forValue("", "bar")); list.add(PotentialAssignment.forValue("", "foobar")); list.add(PotentialAssignment.forValue("", "foobar")); list.add(PotentialAssignment.forValue("", "foobar")); list.add(PotentialAssignment.forValue("", "whut?")); list.add(PotentialAssignment.forValue("", "OMGWTFBBQ")); list.add(PotentialAssignment.forValue("", "muahhahahahahahahaha")); list.add(PotentialAssignment.forValue("", "ijrobbeoubgt3t9hboebalnpajtih39")); list.add(PotentialAssignment.forValue("", "ijrobbeoubgt3t9hboebalnpajtih39")); return list; }
private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignment> list) throws Throwable { for (FrameworkMethod dataPointsMethod : getDataPointsMethods(sig)) { Class<?> returnType = dataPointsMethod.getReturnType(); if ((returnType.isArray() && sig.canPotentiallyAcceptType(returnType.getComponentType())) || Iterable.class.isAssignableFrom(returnType)) { try { addDataPointsValues(returnType, sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null)); } catch (Throwable throwable) { DataPoints annotation = dataPointsMethod.getAnnotation(DataPoints.class); if (annotation != null && isAssignableToAnyOf(annotation.ignoredExceptions(), throwable)) { return; } else { throw throwable; } } } } }
@Override protected Collection<Field> getSingleDataPointFields(ParameterSignature sig) { Collection<Field> fields = super.getSingleDataPointFields(sig); String requestedName = sig.getAnnotation(FromDataPoints.class).value(); List<Field> fieldsWithMatchingNames = new ArrayList<Field>(); for (Field field : fields) { String[] fieldNames = field.getAnnotation(DataPoint.class).value(); if (Arrays.asList(fieldNames).contains(requestedName)) { fieldsWithMatchingNames.add(field); } } return fieldsWithMatchingNames; }
@Override protected Collection<Field> getDataPointsFields(ParameterSignature sig) { Collection<Field> fields = super.getDataPointsFields(sig); String requestedName = sig.getAnnotation(FromDataPoints.class).value(); List<Field> fieldsWithMatchingNames = new ArrayList<Field>(); for (Field field : fields) { String[] fieldNames = field.getAnnotation(DataPoints.class).value(); if (Arrays.asList(fieldNames).contains(requestedName)) { fieldsWithMatchingNames.add(field); } } return fieldsWithMatchingNames; }
@Override protected Collection<FrameworkMethod> getSingleDataPointMethods(ParameterSignature sig) { Collection<FrameworkMethod> methods = super.getSingleDataPointMethods(sig); String requestedName = sig.getAnnotation(FromDataPoints.class).value(); List<FrameworkMethod> methodsWithMatchingNames = new ArrayList<FrameworkMethod>(); for (FrameworkMethod method : methods) { String[] methodNames = method.getAnnotation(DataPoint.class).value(); if (Arrays.asList(methodNames).contains(requestedName)) { methodsWithMatchingNames.add(method); } } return methodsWithMatchingNames; }
@Override protected Collection<FrameworkMethod> getDataPointsMethods(ParameterSignature sig) { Collection<FrameworkMethod> methods = super.getDataPointsMethods(sig); String requestedName = sig.getAnnotation(FromDataPoints.class).value(); List<FrameworkMethod> methodsWithMatchingNames = new ArrayList<FrameworkMethod>(); for (FrameworkMethod method : methods) { String[] methodNames = method.getAnnotation(DataPoints.class).value(); if (Arrays.asList(methodNames).contains(requestedName)) { methodsWithMatchingNames.add(method); } } return methodsWithMatchingNames; }
private void addFields(ParameterSignature sig, List<PotentialAssignment> list) { for (final Field field : fClass.getJavaClass().getFields()) { if (Modifier.isStatic(field.getModifiers())) { Class<?> type = field.getType(); if (sig.canAcceptArrayType(type) && field.getAnnotation(DataPoints.class) != null) { try { addArrayValues(field.getName(), list, getStaticFieldValue(field)); } catch (Throwable e) { // ignore and move on } } else if (sig.canAcceptType(type) && field.getAnnotation(DataPoint.class) != null) { list.add(PotentialAssignment .forValue(field.getName(), getStaticFieldValue(field))); } } } }
private void addFields(final ParameterSignature sig, final List<PotentialAssignment> list) { for (final Field field : fClass.getJavaClass().getFields()) { if (Modifier.isStatic(field.getModifiers())) { Class<?> type = field.getType(); if (sig.canAcceptArrayType(type) && field.getAnnotation(DataPoints.class) != null) { try { addArrayValues(field.getName(), list, getStaticFieldValue(field)); } catch (Throwable e) { // ignore and move on } } else if (sig.canAcceptType(type) && field.getAnnotation(DataPoint.class) != null) { list.add(PotentialAssignment.forValue(field.getName(), getStaticFieldValue(field))); } } } }
@Override public boolean isValidFor(ParameterSignature parameterSignature) { if (!isProducerNameValid(parameterSignature.getAnnotation(ProducedValue.class))) { return false; } Class<?> type = field.getField().getType(); Class<?> aClass = primitiveSupplierToType.get(type); if (aClass != null && aClass.equals(parameterSignature.getType())) { return true; } else { Type genericType = field.getField().getGenericType(); return isValidGenericType(parameterSignature, genericType); } }
List<ParameterProducer> potentialNextParameterProducers() throws Throwable { ParameterSignature next = nextUnassigned(); return new PotentialAssignments(testClass).allPossible() .filter(assignment -> assignment.isValidFor(next)) .map(Assignment::parameterProducer) .collect(Collectors.toList()); }
boolean isValidGenericType(ParameterSignature parameterSignature, Type genericType) { if (genericType instanceof ParameterizedType) { ParameterizedType parametrizedType = (ParameterizedType) genericType; Type[] actualTypeArguments = parametrizedType.getActualTypeArguments(); Type actualTypeArgument = actualTypeArguments[0]; if (parameterSignature.getType().equals(actualTypeArgument) || parameterSignature.getType().equals(primitiveSupplierToType.get(actualTypeArgument))) { return true; } } return false; }
@Test public void shouldHaveAssignmentsFromField() throws Throwable { this.assignments = new PotentialAssignments(new TestClass(AssignmentsFromField.class)); List<Assignment> assignments = this.assignments.allPossible().collect(Collectors.toList()); assertThat(assignments).hasSize(1); ArrayList<ParameterSignature> parameterSignature = ParameterSignature.signatures(AssignmentsFromField.class.getMethod("a", String.class)); Assignment assignment = assignments.get(0); assertThat(assignment.isValidFor(parameterSignature.get(0))).isTrue(); }
@Test public void shouldNotBeValidIfTypeDoesNotMatch() throws Throwable { this.assignments = new PotentialAssignments(new TestClass(AssignmentsFromField.class)); List<Assignment> assignments = this.assignments.allPossible().collect(Collectors.toList());; ArrayList<ParameterSignature> parameterSignature = ParameterSignature.signatures(AssignmentsFromField.class.getMethod("b", Integer.TYPE)); Assignment assignment = assignments.get(0); assertThat(assignment.isValidFor(parameterSignature.get(0))).isFalse(); }
@Override public List<PotentialAssignment> getValueSources(ParameterSignature sig) throws Throwable { final List<PotentialAssignment> list = new ArrayList<PotentialAssignment>(); list.add(PotentialAssignment.forValue("long", 2L)); list.add(PotentialAssignment.forValue("float", 5.00f)); list.add(PotentialAssignment.forValue("double", 89d)); return list; }
@Override public List<PotentialAssignment> getValueSources(final ParameterSignature signature) { final ArrayList<PotentialAssignment> list = new ArrayList<>(); list.add(PotentialAssignment.forValue("", new Object[]{-100L, 50L, 1L, 11L})); list.add(PotentialAssignment.forValue("", new Object[]{null})); list.add(PotentialAssignment.forValue("", new Object[]{"OMGWTFBBQ", "TEST test!!!"})); list.add(PotentialAssignment.forValue("", new Object[]{-100L})); list.add(PotentialAssignment.forValue("", new Object[]{0.3562})); list.add(PotentialAssignment.forValue("", new Object[]{})); return list; }
@Override public List<PotentialAssignment> getValueSources(final ParameterSignature signature) { final ArrayList<PotentialAssignment> list = new ArrayList<>(); final Random randomGen = new Random(); for (int i = 0; i < 20; ++i) { list.add(PotentialAssignment.forValue("", randomGen.nextLong())); } return list; }
@Override public List<PotentialAssignment> getValueSources(final ParameterSignature signature) { final ArrayList<PotentialAssignment> list = new ArrayList<>(); final Random randomGen = new Random(); for (int i = 0; i < 20; ++i) { list.add(PotentialAssignment.forValue("", randomGen.nextGaussian())); } return list; }
@Override public List<PotentialAssignment> getValueSources(final ParameterSignature arg0) { final ArrayList<PotentialAssignment> list = new ArrayList<>(); list.add(PotentialAssignment.forValue("", new Boolean(true))); list.add(PotentialAssignment.forValue("", new Boolean(true))); list.add(PotentialAssignment.forValue("", new Boolean(true))); list.add(PotentialAssignment.forValue("", new Boolean(false))); list.add(PotentialAssignment.forValue("", new Boolean(false))); list.add(PotentialAssignment.forValue("", new Boolean(false))); list.add(PotentialAssignment.forValue("", new Boolean(false))); return list; }
@Override public List<PotentialAssignment> getValueSources(ParameterSignature sig) { List<PotentialAssignment> list = new ArrayList<PotentialAssignment>(); TestedOn testedOn = sig.getAnnotation(TestedOn.class); int[] ints = testedOn.ints(); for (final int i : ints) { list.add(PotentialAssignment.forValue("ints", i)); } return list; }
@Override public List<PotentialAssignment> getValueSources(ParameterSignature sig) throws Throwable { List<PotentialAssignment> list = new ArrayList<PotentialAssignment>(); addSinglePointFields(sig, list); addMultiPointFields(sig, list); addSinglePointMethods(sig, list); addMultiPointMethods(sig, list); return list; }
private void addSinglePointMethods(ParameterSignature sig, List<PotentialAssignment> list) { for (FrameworkMethod dataPointMethod : getSingleDataPointMethods(sig)) { if (sig.canAcceptType(dataPointMethod.getType())) { list.add(new MethodParameterValue(dataPointMethod)); } } }
private void addSinglePointFields(ParameterSignature sig, List<PotentialAssignment> list) { for (final Field field : getSingleDataPointFields(sig)) { Object value = getStaticFieldValue(field); if (sig.canAcceptValue(value)) { list.add(PotentialAssignment.forValue(field.getName(), value)); } } }
private void addDataPointsValues(Class<?> type, ParameterSignature sig, String name, List<PotentialAssignment> list, Object value) { if (type.isArray()) { addArrayValues(sig, name, list, value); } else if (Iterable.class.isAssignableFrom(type)) { addIterableValues(sig, name, list, (Iterable<?>) value); } }
private void addArrayValues(ParameterSignature sig, String name, List<PotentialAssignment> list, Object array) { for (int i = 0; i < Array.getLength(array); i++) { Object value = Array.get(array, i); if (sig.canAcceptValue(value)) { list.add(PotentialAssignment.forValue(name + "[" + i + "]", value)); } } }
private void addIterableValues(ParameterSignature sig, String name, List<PotentialAssignment> list, Iterable<?> iterable) { Iterator<?> iterator = iterable.iterator(); int i = 0; while (iterator.hasNext()) { Object value = iterator.next(); if (sig.canAcceptValue(value)) { list.add(PotentialAssignment.forValue(name + "[" + i + "]", value)); } i += 1; } }
protected Collection<Field> getSingleDataPointFields(ParameterSignature sig) { List<FrameworkField> fields = fClass.getAnnotatedFields(DataPoint.class); Collection<Field> validFields = new ArrayList<Field>(); for (FrameworkField frameworkField : fields) { validFields.add(frameworkField.getField()); } return validFields; }
protected Collection<Field> getDataPointsFields(ParameterSignature sig) { List<FrameworkField> fields = fClass.getAnnotatedFields(DataPoints.class); Collection<Field> validFields = new ArrayList<Field>(); for (FrameworkField frameworkField : fields) { validFields.add(frameworkField.getField()); } return validFields; }
@Override public List<PotentialAssignment> getValueSources(ParameterSignature sig) { Object[] enumValues = enumType.getEnumConstants(); List<PotentialAssignment> assignments = new ArrayList<PotentialAssignment>(); for (Object value : enumValues) { assignments.add(PotentialAssignment.forValue(value.toString(), value)); } return assignments; }
/** * Returns a new assignment list for {@code testMethod}, with no params * assigned. */ public static Assignments allUnassigned(Method testMethod, TestClass testClass) { List<ParameterSignature> signatures; signatures = ParameterSignature.signatures(testClass .getOnlyConstructor()); signatures.addAll(ParameterSignature.signatures(testMethod)); return new Assignments(new ArrayList<PotentialAssignment>(), signatures, testClass); }
public List<PotentialAssignment> potentialsForNextUnassigned() throws Throwable { ParameterSignature unassigned = nextUnassigned(); List<PotentialAssignment> assignments = getSupplier(unassigned).getValueSources(unassigned); if (assignments.size() == 0) { assignments = generateAssignmentsFromTypeAlone(unassigned); } return assignments; }
private List<PotentialAssignment> generateAssignmentsFromTypeAlone(ParameterSignature unassigned) { Class<?> paramType = unassigned.getType(); if (paramType.isEnum()) { return new EnumSupplier(paramType).getValueSources(unassigned); } else if (paramType.equals(Boolean.class) || paramType.equals(boolean.class)) { return new BooleanSupplier().getValueSources(unassigned); } else { return emptyList(); } }
private ParameterSupplier getSupplier(ParameterSignature unassigned) throws Exception { ParametersSuppliedBy annotation = unassigned .findDeepAnnotation(ParametersSuppliedBy.class); if (annotation != null) { return buildParameterSupplierFromClass(annotation.value()); } else { return new AllMembersSupplier(fClass); } }
private List<PotentialAssignment> allMemberValuesFor(Class<?> testClass, Class<?>... constructorParameterTypes) throws Throwable { return new AllMembersSupplier(new TestClass(testClass)) .getValueSources(ParameterSignature.signatures( testClass.getConstructor(constructorParameterTypes)) .get(0)); }
@Test public void getAnnotations() throws SecurityException, NoSuchMethodException { Method method = getClass().getMethod("foo", int.class); List<Annotation> annotations = ParameterSignature.signatures(method) .get(0).getAnnotations(); assertThat(annotations, CoreMatchers.<TestedOn>hasItem(isA(TestedOn.class))); }
@Test public void primitiveTypesShouldBeAcceptedAsWrapperTypes() throws Exception { List<ParameterSignature> signatures = ParameterSignature .signatures(getClass().getMethod("integerMethod", Integer.class)); ParameterSignature integerSignature = signatures.get(0); assertTrue(integerSignature.canAcceptType(int.class)); }
@Test public void primitiveTypesShouldBeAcceptedAsWrapperTypeAssignables() throws Exception { List<ParameterSignature> signatures = ParameterSignature .signatures(getClass().getMethod("numberMethod", Number.class)); ParameterSignature numberSignature = signatures.get(0); assertTrue(numberSignature.canAcceptType(int.class)); }
@Test public void wrapperTypesShouldBeAcceptedAsPrimitiveTypes() throws Exception { List<ParameterSignature> signatures = ParameterSignature .signatures(getClass().getMethod("intMethod", int.class)); ParameterSignature intSignature = signatures.get(0); assertTrue(intSignature.canAcceptType(Integer.class)); }
private GuesserQueue createGuesserQueue(Assignments incomplete) throws Throwable { ParameterSignature nextUnassigned = incomplete.nextUnassigned(); if (nextUnassigned.hasAnnotation(Stub.class)) { GuesserQueue queue = new GuesserQueue(); queue.add(new Guesser<Object>(nextUnassigned.getType())); return queue; } return GuesserQueue.forSingleValues(incomplete.potentialsForNextUnassigned()); }