001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.assertions;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.internal.*;
019
020/**
021 * Base class for all assertion objects.
022 */
023public class Assertion {
024
025   String msg;
026   Object[] msgArgs;
027   boolean stdout, stderr;
028
029   /**
030    * Constructor used when this assertion is being created from within another assertion.
031    * @param creator The creator of this assertion.
032    */
033   protected Assertion(Assertion creator) {
034      if (creator != null) {
035         this.msg = creator.msg;
036         this.msgArgs = creator.msgArgs;
037         this.stdout = creator.stdout;
038         this.stderr = creator.stderr;
039      }
040   }
041
042   /**
043    * Allows to to specify the assertion failure message.
044    *
045    * <p>
046    * String can contain <js>"{msg}"</js> to represent the original message.
047    *
048    * @param msg The assertion failure message.
049    * @param args Optional message arguments.
050    * @return This object (for method chaining).
051    */
052   @FluentSetter
053   public Assertion msg(String msg, Object...args) {
054      this.msg = msg.replace("{msg}", "<<<MSG>>>");
055      this.msgArgs = args;
056      return this;
057   }
058
059   /**
060    * If an error occurs, send the error message to STDOUT.
061    *
062    * @return This object (for method chaining).
063    */
064   @FluentSetter
065   public Assertion stdout() {
066      this.stdout = true;
067      return this;
068   }
069
070   /**
071    * If an error occurs, send the error message to STDERR.
072    *
073    * @return This object (for method chaining).
074    */
075   @FluentSetter
076   public Assertion stderr() {
077      this.stderr = true;
078      return this;
079   }
080
081   /**
082    * Creates a new {@link BasicAssertionError}.
083    *
084    * @param msg The message.
085    * @param args The message arguments.
086    * @return A new {@link BasicAssertionError}.
087    */
088   protected BasicAssertionError error(String msg, Object...args) {
089      msg = format(msg, args);
090      if (this.msg != null)
091         msg = format(this.msg, this.msgArgs).replace("<<<MSG>>>", msg);
092      if (stdout)
093         System.out.println(msg);
094      if (stderr)
095         System.err.println(msg);
096      return new BasicAssertionError(msg);
097   }
098
099   /**
100    * Convenience method for getting the class name for an object.
101    *
102    * @param o The object to get the class name for.
103    * @return The class name for an object.
104    */
105   protected static String className(Object o) {
106      return ClassUtils.className(o);
107   }
108
109   /**
110    * Asserts the specified value is not null.
111    *
112    * @param value The value to check.
113    * @param msg The message.
114    * @param args The message arguments.
115    * @return The value.
116    */
117   protected Object assertNotNull(Object value, String msg, Object...args) {
118      if (value == null)
119         throw new BasicAssertionError(format(msg, args));
120      return value;
121   }
122
123   /**
124    * Asserts the specified parameter is not null.
125    *
126    * @param parameter The parameter name.
127    * @param value The value to check.
128    * @return The value.
129    */
130   protected Object assertNotNull(String parameter, Object value) {
131      return assertNotNull(value, "Parameter ''{0}'' cannot be null.", parameter);
132   }
133
134   // <FluentSetters>
135
136   // </FluentSetters>
137}