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.rest.mock;
018
019import java.io.*;
020
021import org.apache.juneau.assertions.*;
022
023/**
024 * A capturing {@link PrintStream} that allows you to easily capture console output.
025 *
026 * <p>
027 * Stores output into an internal {@link ByteArrayOutputStream}.  Note that this means you could run into memory
028 * constraints if you heavily use this class.
029 *
030 * <p>
031 * Typically used in conjunction with the {@link org.apache.juneau.rest.client.RestClient.Builder#console(PrintStream)} to capture console output for
032 * testing purposes.
033 *
034 * <h5 class='figure'>Example:</h5>
035 * <p class='bjava'>
036 *    <jc>// A simple REST API that echos a posted bean.</jc>
037 *    <ja>@Rest</ja>
038 *    <jk>public class</jk> MyRest <jk>extends</jk> BasicRestObject {
039 *       <ja>@RestPost</ja>(<js>"/bean"</js>)
040 *       <jk>public</jk> Bean postBean(<ja>@Content</ja> Bean <jv>bean</jv>) {
041 *          <jk>return</jk> <jv>bean</jv>;
042 *       }
043 *    }
044 *
045 * <jc>// Our mock console.</jc>
046 *    MockConsole <jv>console</jv> = MockConsole.<jsm>create</jsm>();
047 *
048 * <jc>// Make a call against our REST API and log the call.</jc>
049 *    MockRestClient
050 *       .<jsm>create</jsm>(MyRest.<jk>class</jk>)
051 *       .json5()
052 *       .logRequests(DetailLevel.<jsf>FULL</jsf>, Level.<jsf>SEVERE</jsf>)
053 *       .logToConsole()
054 *       .console(<jv>console</jv>)
055 *       .build()
056 *       .post(<js>"/bean"</js>, <jv>bean</jv>)
057 *       .run();
058 *
059 *    <jv>console</jv>.assertContents().is(
060 *       <js>""</js>,
061 *       <js>"=== HTTP Call (outgoing) ======================================================"</js>,
062 *       <js>"=== REQUEST ==="</js>,
063 *       <js>"POST http://localhost/bean"</js>,
064 *       <js>"---request headers---"</js>,
065 *       <js>" Accept: application/json5"</js>,
066 *       <js>"---request entity---"</js>,
067 *       <js>" Content-Type: application/json5"</js>,
068 *       <js>"---request content---"</js>,
069 *       <js>"{f:1}"</js>,
070 *       <js>"=== RESPONSE ==="</js>,
071 *       <js>"HTTP/1.1 200 "</js>,
072 *       <js>"---response headers---"</js>,
073 *       <js>" Content-Type: application/json"</js>,
074 *       <js>"---response content---"</js>,
075 *       <js>"{f:1}"</js>,
076 *       <js>"=== END ======================================================================="</js>,
077 *       <js>""</js>
078 *    );
079 * </p>
080 *
081 * <h5 class='section'>See Also:</h5><ul>
082 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestMockBasics">juneau-rest-mock Basics</a>
083 * </ul>
084 */
085public class MockConsole extends PrintStream {
086
087   private static final ByteArrayOutputStream baos = new ByteArrayOutputStream();
088
089   /**
090    * Constructor.
091    */
092   public MockConsole() {
093      super(baos);
094   }
095
096   /**
097    * Creator.
098    *
099    * @return A new {@link MockConsole} object.
100    */
101   public static MockConsole create() {
102      return new MockConsole();
103   }
104
105   /**
106    * Resets the contents of this buffer.
107    *
108    * @return This object.
109    */
110   public synchronized MockConsole reset() {
111      baos.reset();
112      return this;
113   }
114
115   /**
116    * Allows you to perform fluent-style assertions on the contents of this buffer.
117    *
118    * <h5 class='figure'>Example:</h5>
119    * <p class='bjava'>
120    *    MockConsole <jv>console</jv> = MockConsole.<jsf>create</jsf>();
121    *
122    *    MockRestClient
123    *       .<jsm>create</jsm>(MyRest.<jk>class</jk>)
124    *       .console(<jv>console</jv>)
125    *       .debug()
126    *       .json5()
127    *       .build()
128    *       .get(<js>"/url"</js>)
129    *       .run();
130    *
131    *    <jv>console</jv>.assertContents().isContains(<js>"HTTP GET /url"</js>);
132    * </p>
133    *
134    * @return A new fluent-style assertion object.
135    */
136   public synchronized FluentStringAssertion<MockConsole> assertContents() {
137      return new FluentStringAssertion<>(toString(), this);
138   }
139
140   /**
141    * Allows you to perform fluent-style assertions on the size of this buffer.
142    *
143    * <h5 class='figure'>Example:</h5>
144    * <p class='bjava'>
145    *    MockConsole <jv>console</jv> = MockConsole.<jsf>create</jsf>();
146    *
147    *    MockRestClient
148    *       .<jsm>create</jsm>(MyRest.<jk>class</jk>)
149    *       .console(<jv>console</jv>)
150    *       .debug()
151    *       .json5()
152    *       .build()
153    *       .get(<js>"/url"</js>)
154    *       .run();
155    *
156    *    <jv>console</jv>.assertSize().isGreaterThan(0);
157    * </p>
158    *
159    * @return A new fluent-style assertion object.
160    */
161   public synchronized FluentIntegerAssertion<MockConsole> assertSize() {
162      return new FluentIntegerAssertion<>(baos.size(), this);
163   }
164
165   /**
166    * Returns the contents of this buffer as a string.
167    */
168   @Override
169   public String toString() {
170      return baos.toString();
171   }
172}