View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils.logging;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.PrintStream;
23  import java.nio.charset.StandardCharsets;
24  
25  import org.fusesource.jansi.AnsiColors;
26  import org.fusesource.jansi.AnsiConsole;
27  import org.fusesource.jansi.AnsiMode;
28  import org.fusesource.jansi.AnsiPrintStream;
29  import org.fusesource.jansi.AnsiType;
30  import org.fusesource.jansi.io.AnsiOutputStream;
31  import org.junit.Test;
32  
33  import static org.hamcrest.CoreMatchers.not;
34  import static org.hamcrest.CoreMatchers.sameInstance;
35  import static org.hamcrest.MatcherAssert.assertThat;
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assume.assumeNoException;
38  
39  public class MessageUtilsTest {
40      @Test
41      public void testSystem() {
42          PrintStream currentOut = System.out;
43          try {
44              MessageUtils.systemInstall();
45              assertThat(System.out, not(sameInstance(currentOut)));
46          } catch (LinkageError e) {
47              assumeNoException("JAnsi not supported for this platform", e);
48          } finally {
49              try {
50                  // uninstall is always necessary due to https://github.com/fusesource/jansi/issues/242
51                  // but might throw exceptions
52                  MessageUtils.systemUninstall();
53              } catch (Throwable t) {
54                  // ignore any thrown exception like NPE here
55              }
56          }
57          assertThat(System.out, sameInstance(currentOut));
58      }
59  
60      @Test
61      public void testTerminalWidth() {
62          AnsiOutputStream.WidthSupplier width = new AnsiOutputStream.WidthSupplier() {
63              @Override
64              public int getTerminalWidth() {
65                  return 33;
66              }
67          };
68          AnsiOutputStream aos = new AnsiOutputStream(
69                  new ByteArrayOutputStream(),
70                  width,
71                  AnsiMode.Default,
72                  null,
73                  AnsiType.Emulation,
74                  AnsiColors.Colors256,
75                  StandardCharsets.UTF_8,
76                  null,
77                  null,
78                  false);
79          try {
80              AnsiConsole.systemInstall();
81              AnsiConsole.out = new AnsiPrintStream(aos, true);
82              assertEquals(33, MessageUtils.getTerminalWidth());
83          } catch (LinkageError e) {
84              assumeNoException("JAnsi not supported for this platform", e);
85          } finally {
86              try {
87                  // uninstall is always necessary due to https://github.com/fusesource/jansi/issues/242
88                  // but might throw exceptions
89                  AnsiConsole.systemUninstall();
90              } catch (Throwable t) {
91                  // ignore any thrown exception like NPE here
92              }
93          }
94      }
95  }