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    * Creator.
091    *
092    * @return A new {@link MockConsole} object.
093    */
094   public static MockConsole create() {
095      return new MockConsole();
096   }
097
098   /**
099    * Constructor.
100    */
101   public MockConsole() {
102      super(baos);
103   }
104
105   /**
106    * Allows you to perform fluent-style assertions on the contents of this buffer.
107    *
108    * <h5 class='figure'>Example:</h5>
109    * <p class='bjava'>
110    *    MockConsole <jv>console</jv> = MockConsole.<jsf>create</jsf>();
111    *
112    *    MockRestClient
113    *       .<jsm>create</jsm>(MyRest.<jk>class</jk>)
114    *       .console(<jv>console</jv>)
115    *       .debug()
116    *       .json5()
117    *       .build()
118    *       .get(<js>"/url"</js>)
119    *       .run();
120    *
121    *    <jv>console</jv>.assertContents().isContains(<js>"HTTP GET /url"</js>);
122    * </p>
123    *
124    * @return A new fluent-style assertion object.
125    */
126   public synchronized FluentStringAssertion<MockConsole> assertContents() {
127      return new FluentStringAssertion<>(toString(), this);
128   }
129
130   /**
131    * Allows you to perform fluent-style assertions on the size of this buffer.
132    *
133    * <h5 class='figure'>Example:</h5>
134    * <p class='bjava'>
135    *    MockConsole <jv>console</jv> = MockConsole.<jsf>create</jsf>();
136    *
137    *    MockRestClient
138    *       .<jsm>create</jsm>(MyRest.<jk>class</jk>)
139    *       .console(<jv>console</jv>)
140    *       .debug()
141    *       .json5()
142    *       .build()
143    *       .get(<js>"/url"</js>)
144    *       .run();
145    *
146    *    <jv>console</jv>.assertSize().isGreaterThan(0);
147    * </p>
148    *
149    * @return A new fluent-style assertion object.
150    */
151   public synchronized FluentIntegerAssertion<MockConsole> assertSize() {
152      return new FluentIntegerAssertion<>(baos.size(), this);
153   }
154
155   /**
156    * Resets the contents of this buffer.
157    *
158    * @return This object.
159    */
160   public synchronized MockConsole reset() {
161      baos.reset();
162      return 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}