Example #1
0
        public virtual void test_constructor_failure()
        {
            Failure          failure = Failure.of(FailureReason.UNSUPPORTED, "Test");
            FailureException test    = new FailureException(failure);

            assertEquals(test.Failure, failure);
        }
        //-------------------------------------------------------------------------
        public virtual void failure_fromFailure()
        {
            Failure      failure = Failure.of(ERROR, "my failure");
            Result <int> test    = Result.failure(failure);

            assertTrue(test.Failure);
            assertEquals(test.Failure.Message, "my failure");
            assertEquals(test.Failure.Items.size(), 1);
            FailureItem item = test.Failure.Items.GetEnumerator().next();

            assertEquals(item.Reason, ERROR);
            assertEquals(item.Message, "my failure");
            assertEquals(item.CauseType.Present, false);
            assertTrue(!string.ReferenceEquals(item.StackTrace, null));
        }
        //-------------------------------------------------------------------------
        public virtual void generatedStackTrace_Failure()
        {
            Failure test = Failure.of(FailureReason.INVALID, "my {} {} failure", "big", "bad");

            assertEquals(test.Reason, FailureReason.INVALID);
            assertEquals(test.Message, "my big bad failure");
            assertEquals(test.Items.size(), 1);
            FailureItem item = test.Items.GetEnumerator().next();

            assertFalse(item.CauseType.Present);
            assertFalse(item.StackTrace.Contains(".FailureItem.of("));
            assertFalse(item.StackTrace.Contains(".Failure.of("));
            assertTrue(item.StackTrace.StartsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure", StringComparison.Ordinal));
            assertTrue(item.StackTrace.Contains(".generatedStackTrace_Failure("));
            assertEquals(item.ToString(), "INVALID: my big bad failure");
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void createByBuilder_bothValueAndFailure()
        public virtual void createByBuilder_bothValueAndFailure()
        {
            Result.meta().builder().set("value", "A").set("failure", Failure.of(CALCULATION_FAILED, "Fail")).build();
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// Obtains a failure from a reason and message.
        /// <para>
        /// 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 <seealso cref="Messages#format(String, Object...)"/> for more details.
        /// </para>
        /// <para>
        /// An exception will be created internally to obtain a stack trace.
        /// The cause type will not be present in the resulting failure.
        ///
        /// </para>
        /// </summary>
        /// <param name="reason">  the reason </param>
        /// <param name="message">  a message explaining the failure, not empty, uses "{}" for inserting {@code messageArgs} </param>
        /// <param name="messageArgs">  the arguments for the message </param>
        /// <returns> the failure </returns>
        public static Failure of(FailureReason reason, string message, params object[] messageArgs)
        {
            string msg = Messages.format(message, messageArgs);

            return(Failure.of(FailureItem.of(reason, msg, 1)));
        }
 /// <summary>
 /// Obtains a failure from a reason and exception.
 /// </summary>
 /// <param name="reason">  the reason </param>
 /// <param name="cause">  the cause </param>
 /// <returns> the failure </returns>
 public static Failure of(FailureReason reason, Exception cause)
 {
     return(Failure.of(FailureItem.of(reason, cause)));
 }
 /// <summary>
 /// Obtains a failure from a reason, message and exception.
 /// <para>
 /// 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 <seealso cref="Messages#format(String, Object...)"/> for more details.
 ///
 /// </para>
 /// </summary>
 /// <param name="reason">  the reason </param>
 /// <param name="cause">  the cause </param>
 /// <param name="message">  the failure message, possibly containing placeholders, formatted using <seealso cref="Messages#format"/> </param>
 /// <param name="messageArgs">  arguments used to create the failure message </param>
 /// <returns> the failure </returns>
 public static Failure of(FailureReason reason, Exception cause, string message, params object[] messageArgs)
 {
     return(Failure.of(FailureItem.of(reason, cause, message, messageArgs)));
 }