001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau;
018
019import static org.apache.juneau.common.utils.StringUtils.*;
020
021import java.text.*;
022
023/**
024 * An extension of {@link AssertionError} with helper constructors for messages with message-style arguments.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 * </ul>
028 *
029 * @serial exclude
030 */
031public class BasicAssertionError extends AssertionError {
032
033   private static final long serialVersionUID = 1L;
034
035   /**
036    * Constructor.
037    *
038    * @param message The {@link MessageFormat}-style message.
039    * @param args Optional {@link MessageFormat}-style arguments.
040    */
041   public BasicAssertionError(String message, Object...args) {
042      super(format(message, args));
043   }
044
045   /**
046    * Constructor.
047    *
048    * @param cause The cause of this exception.
049    * @param message The {@link MessageFormat}-style message.
050    * @param args Optional {@link MessageFormat}-style arguments.
051    */
052   public BasicAssertionError(Throwable cause, String message, Object...args) {
053      this(getMessage(cause, message, null), args);
054      initCause(cause);
055   }
056
057   /**
058    * Finds the message.
059    *
060    * @param cause The cause.
061    * @param msg The message.
062    * @param def The default value if both above are <jk>null</jk>.
063    * @return The resolved message.
064    */
065   protected static final String getMessage(Throwable cause, String msg, String def) {
066      if (msg != null)
067         return msg;
068      if (cause != null)
069         return cause.getMessage();
070      return def;
071   }
072}