protected PreconditionsTester(Invokable invokable, Class<?>[] parameterTypes) { this.validArguments = new InvokableArguments<>(parameterTypes); this.invalidArguments = new InvokableArguments<>(parameterTypes); this.invokable = invokable; }
/** * Add a list of <b>valid</b> arguments for the parameter. * * @param paramIndex * the index of the parameter * @param arguments * valid arguments for the parameter * @return */ public PreconditionsTester addValidArgs(int paramIndex, Object... arguments) { validArguments.addArg(paramIndex, Argument::new, arguments); return this; }
Set<List<T>> getCartesianProduct() { return getCartesianProduct(argumentsSets); }
/** * Tests all possible combinations valid of arguments on the invokable (constructor or method). * <p> * This requires at least one valid argument for <b>each</b> parameter. * * @throws AssertionError * if invoking the constructor with a valid set of arguments * throws an exception */ public void testValidCombinations() { validArguments.checkAllArgumentsSet(); testArguments(validArguments.getCapturedArgumentsSets(), null); }
public void testEquals() { arguments.checkAllArgumentsSet(); AccessibleRunner.run(constructor, () -> { EqualsTester tester = new EqualsTester(); for (List<Argument> args : arguments.getCartesianProduct()) { try { Object[] invokeArgs = args.stream().map(Argument::getValue).toArray(); Object o1 = constructor.newInstance(invokeArgs); Object o2 = constructor.newInstance(invokeArgs); tester.addEqualityGroup(o1, o2); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { throw new ConstructorInvocationException(e); } } tester.testEquals(); return null; }); }
void addArg(int paramIndex, Function<Object, T> argFactory, Object... arguments) { if (arguments == null) { argumentsSets.get(paramIndex).add(argFactory.apply(null)); } else { for (Object argument : arguments) { checkArgumentType(paramIndex, argument); argumentsSets.get(paramIndex).add(argFactory.apply(argument)); } } }
validArguments.checkAllArgumentsSet(); boolean oneTested = false; for (int i = 0; i < validArguments.getCapturedArgumentsSets().size(); i++) { List<Set<Argument>> argumentsLists = new ArrayList<>(validArguments.getCapturedArgumentsSets()); Set<InvalidArgument> currInvalidArgs = invalidArguments.getCapturedArgumentsSets().get(i); if (!currInvalidArgs.isEmpty()) { for (InvalidArgument invalidParam : currInvalidArgs) {
public CopyConstructorEqualsTester addArguments(int argIndex, Object... args) { this.arguments.addArg(argIndex, Argument::new, args); return this; }
public CopyConstructorEqualsTester(Constructor<?> constructor) { this.constructor = constructor; this.arguments = new InvokableArguments<>(constructor.getParameterTypes()); }
private void testArguments(List<Set<Argument>> argumentsLists, Class<? extends Exception> expectedException) { for (List<Argument> arguments : InvokableArguments.getCartesianProduct(argumentsLists)) { List<Object> argVals = arguments.stream().map(Argument::getValue).collect(Collectors.toList()); try { invokable.invoke(argVals.toArray(new Object[argVals.size()])); if (expectedException != null) { throw new AssertionError( "expected " + expectedException.getName() + " to be thrown for arguments: " + argVals); } } catch (Exception e) { Throwable t = e; if (t instanceof InvocationTargetException) { t = ((InvocationTargetException) t).getTargetException(); } if (expectedException == null) { throw new AssertionError("exception has been thrown for arguments: " + argVals, t); } if (!expectedException.isAssignableFrom(t.getClass())) { throw new AssertionError("expected " + expectedException.getClass() + " to be thrown but got " + t.getClass() + " for arguments: " + argVals, t); } } } }
/** * Add a list of <b>invalid</b> arguments for the parameter and the expected * exception. * * @param paramIndex * @param expectedException * @param arguments * @return */ public PreconditionsTester addInvalidArgs(int paramIndex, Class<? extends Exception> expectedException, Object... arguments) { invalidArguments.addArg(paramIndex, a -> new InvalidArgument(a, expectedException), arguments); return this; }