public void test_of_reasonMessage() { FailureItem test = FailureItem.of(FailureReason.INVALID, "my {} {} failure", "big", "bad"); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my big bad failure"); assertFalse(test.getCauseType().isPresent()); assertFalse(test.getStackTrace().contains(".FailureItem.of(")); assertFalse(test.getStackTrace().contains(".Failure.of(")); assertTrue(test.getStackTrace().startsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure")); assertTrue(test.getStackTrace().contains(".test_of_reasonMessage(")); assertEquals(test.toString(), "INVALID: my big bad failure"); }
@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -934964668: // reason return ((FailureItem) bean).getReason(); case 954925063: // message return ((FailureItem) bean).getMessage(); case 405645655: // attributes return ((FailureItem) bean).getAttributes(); case 2026279837: // stackTrace return ((FailureItem) bean).getStackTrace(); case -1443456189: // causeType return ((FailureItem) bean).causeType; } return super.propertyGet(bean, propertyName, quiet); }
/** * Obtains a failure from a reason, throwable and message. * <p> * The message is produced using a template that contains zero to many "{}" placeholders. * Each placeholder is replaced by the next available argument. * If there are too few arguments, then the message will be left with placeholders. * If there are too many arguments, then the excess arguments are appended to the * end of the message. No attempt is made to format the arguments. * See {@link Messages#formatWithAttributes(String, Object...)} for more details. * * @param reason the reason * @param cause the cause * @param message a message explaining the failure, not empty, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return the failure */ public static FailureItem of(FailureReason reason, Throwable cause, String message, Object... messageArgs) { ArgChecker.notNull(reason, "reason"); ArgChecker.notNull(cause, "cause"); Pair<String, Map<String, String>> msg = Messages.formatWithAttributes(message, messageArgs); String stackTrace = Throwables.getStackTraceAsString(cause).replace(System.lineSeparator(), "\n"); FailureItem base = new FailureItem(reason, msg.getFirst(), msg.getSecond(), stackTrace, cause.getClass()); String causeMessage = cause.getMessage(); if (!base.getAttributes().containsKey(EXCEPTION_MESSAGE_ATTRIBUTE) && !Strings.isNullOrEmpty(causeMessage)) { return base.withAttribute(EXCEPTION_MESSAGE_ATTRIBUTE, causeMessage); } return base; }
/** * Obtains a failure for a single failure item. * * @param item the failure item * @return the failure */ public static Failure of(FailureItem item) { return new Failure(item.getReason(), item.getMessage(), ImmutableSet.of(item)); }
/** * Obtains a failure from a reason and message. * <p> * The failure will still have a stack trace, but the cause type will not be present. * * @param reason the reason * @param message the failure message, not empty * @param attributes the attributes associated with this failure * @return the failure */ private static FailureItem of(FailureReason reason, String message, Map<String, String> attributes) { ArgChecker.notNull(reason, "reason"); ArgChecker.notEmpty(message, "message"); String stackTrace = localGetStackTraceAsString(message, 1); return new FailureItem(reason, message, attributes, stackTrace, null); }
public void failure_fromResult_failure() { Result<String> failure = Result.failure(ERROR, "my failure"); Result<Integer> test = Result.failure(failure); assertTrue(test.isFailure()); assertEquals(test.getFailure().getMessage(), "my failure"); assertEquals(test.getFailure().getItems().size(), 1); FailureItem item = test.getFailure().getItems().iterator().next(); assertEquals(item.getReason(), ERROR); assertEquals(item.getMessage(), "my failure"); assertEquals(item.getCauseType().isPresent(), false); assertEquals(item.getStackTrace().contains(".FailureItem.of("), false); assertEquals(item.getStackTrace().contains(".Failure.of("), false); assertEquals(item.getStackTrace().contains(".Result.failure("), false); }
/** * Obtains a failure from a reason and exception. * * @param reason the reason * @param cause the cause * @return the failure */ public static Failure of(FailureReason reason, Exception cause) { return Failure.of(FailureItem.of(reason, cause)); }
public void generatedStackTrace_Failure() { Failure test = Failure.of(FailureReason.INVALID, "my {} {} failure", "big", "bad"); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my big bad failure"); assertEquals(test.getItems().size(), 1); FailureItem item = test.getItems().iterator().next(); assertFalse(item.getCauseType().isPresent()); assertFalse(item.getStackTrace().contains(".FailureItem.of(")); assertFalse(item.getStackTrace().contains(".Failure.of(")); assertTrue(item.getStackTrace().startsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure")); assertTrue(item.getStackTrace().contains(".generatedStackTrace_Failure(")); assertEquals(item.toString(), "INVALID: my big bad failure"); }
public void test_toValueWithFailures() { List<Double> testList = ImmutableList.of(5d, 6d, 7d); ValueWithFailures<Double> result = testList.stream() .map(value -> mockCalc(value)) .collect(ValueWithFailures.toValueWithFailures(1d, (val1, val2) -> val1 * val2)); assertEquals(result.getValue(), 210d); //5 * 6 * 7 = 210 List<FailureItem> failures = result.getFailures(); assertEquals(failures.size(), 3); //One failure item for each element in testList. assertEquals(failures.get(0).getMessage(), Messages.format("Error calculating result for input value {}", 5d)); assertEquals(failures.get(1).getMessage(), Messages.format("Error calculating result for input value {}", 6d)); assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); }
public void test_flatMap() { ValueWithFailures<List<String>> base = ValueWithFailures.of(ImmutableList.of("1", "a", "2"), ImmutableList.of(FAILURE1)); ValueWithFailures<List<Integer>> test = base.flatMap(this::flatMapFunction); assertEquals(test.getValue(), ImmutableList.of(Integer.valueOf(1), Integer.valueOf(2))); assertEquals(test.getFailures().size(), 2); assertEquals(test.getFailures().get(0), FAILURE1); assertEquals(test.getFailures().get(1).getReason(), FailureReason.INVALID); }
public void test_of_reasonMessageShortStackTrace() { FailureItem test = FailureItem.meta().builder() .set("reason", FailureReason.INVALID) .set("message", "my issue") .set("stackTrace", "Short stack trace") .set("causeType", IllegalArgumentException.class) .build(); assertEquals(test.toString(), "INVALID: my issue: Short stack trace"); }
/** * Returns a string summary of the failures, as a single line excluding the stack traces. * * @return the summary string */ @Override public String toString() { return failures.stream() .map(f -> f.toString()) .collect(joining(", ", "FailureItems[", "]")); }
@Override public FailureItem build() { return new FailureItem( reason, message, attributes, stackTrace, causeType); }
public void test_parse_list_ioException() { CharSource source = new CharSource() { @Override public Reader openStream() throws IOException { throw new IOException("Oops"); } }; ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER.parse(ImmutableList.of(source)); assertEquals(test.getFailures().size(), 1); assertEquals(test.getFailures().get(0).getReason(), FailureReason.PARSING); assertEquals(test.getFailures().get(0).getMessage().startsWith("CSV file could not be parsed: "), true); assertEquals(test.getFailures().get(0).getMessage().contains("Oops"), true); }
public void ofNullable_null() { Result<Integer> test = Result.ofNullable(null); assertTrue(test.isFailure()); assertEquals(test.getFailure().getMessage(), "Found null where a value was expected"); assertEquals(test.getFailure().getItems().size(), 1); FailureItem item = test.getFailure().getItems().iterator().next(); assertEquals(item.getReason(), MISSING_DATA); assertEquals(item.getMessage(), "Found null where a value was expected"); assertEquals(item.getCauseType().isPresent(), false); assertTrue(item.getStackTrace() != null); }
/** * Obtains a failure from a reason, message and exception. * <p> * The message is produced using a template that contains zero to many "{}" placeholders. * Each placeholder is replaced by the next available argument. * If there are too few arguments, then the message will be left with placeholders. * If there are too many arguments, then the excess arguments are appended to the * end of the message. No attempt is made to format the arguments. * See {@link Messages#format(String, Object...)} for more details. * * @param reason the reason * @param cause the cause * @param message the failure message, possibly containing placeholders, formatted using {@link Messages#format} * @param messageArgs arguments used to create the failure message * @return the failure */ public static Failure of(FailureReason reason, Exception cause, String message, Object... messageArgs) { return Failure.of(FailureItem.of(reason, cause, message, messageArgs)); }
public void generatedStackTrace() { Result<Object> test = Result.failure(FailureReason.INVALID, "my {} {} failure", "big", "bad"); assertEquals(test.getFailure().getReason(), FailureReason.INVALID); assertEquals(test.getFailure().getMessage(), "my big bad failure"); assertEquals(test.getFailure().getItems().size(), 1); FailureItem item = test.getFailure().getItems().iterator().next(); assertFalse(item.getCauseType().isPresent()); assertFalse(item.getStackTrace().contains(".FailureItem.of(")); assertFalse(item.getStackTrace().contains(".Failure.of(")); assertTrue(item.getStackTrace().startsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure")); assertTrue(item.getStackTrace().contains(".generatedStackTrace(")); assertEquals(item.toString(), "INVALID: my big bad failure"); }
public void test_load_filtered() { TradeCsvLoader test = TradeCsvLoader.standard(); ValueWithFailures<List<Trade>> trades = test.parse( ImmutableList.of(FILE.getCharSource()), ImmutableList.of(FraTrade.class, TermDepositTrade.class)); assertEquals(trades.getValue().size(), 6); assertEquals(trades.getFailures().size(), 10); assertEquals(trades.getFailures().get(0).getMessage(), "Trade type not allowed " + SwapTrade.class.getName() + ", only these types are supported: FraTrade, TermDepositTrade"); }
public void test_of_supplier_failure() { ValueWithFailures<String> test = ValueWithFailures.of("", () -> { throw new IllegalArgumentException(); }); assertEquals(test.hasFailures(), true); assertEquals(test.getValue(), ""); assertEquals(test.getFailures().size(), 1); assertEquals(test.getFailures().get(0).getReason(), FailureReason.ERROR); }
/** * Obtains a failure from a reason and message. * <p> * The failure will still have a stack trace, but the cause type will not be present. * * @param reason the reason * @param message the failure message, not empty * @param skipFrames the number of caller frames to skip, not including this one * @return the failure */ static FailureItem of(FailureReason reason, String message, int skipFrames) { ArgChecker.notNull(reason, "reason"); ArgChecker.notEmpty(message, "message"); String stackTrace = localGetStackTraceAsString(message, skipFrames); return new FailureItem(reason, message, ImmutableMap.of(), stackTrace, null); }