001/*
002 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.mxtool.junit;
024
025import org.junit.runner.*;
026import org.junit.runner.notification.*;
027
028/**
029 * Color support for JUnit test output using ANSI escapes codes.
030 */
031public class AnsiTerminalDecorator extends MxRunListenerDecorator {
032
033    /** Foreground black. */
034    public static final String BLACK = "\u001b[30m";
035    /** Foreground red. */
036    public static final String RED = "\u001b[31m";
037    /** Foreground green. */
038    public static final String GREEN = "\u001b[32m";
039    /** Foreground yellow. */
040    public static final String YELLOW = "\u001b[33m";
041    /** Foreground blue. */
042    public static final String BLUE = "\u001b[34m";
043    /** Foreground magenta. */
044    public static final String MAGENTA = "\u001b[35m";
045    /** Foreground cyan. */
046    public static final String CYAN = "\u001b[36m";
047    /** Foreground white. */
048    public static final String WHITE = "\u001b[37m";
049
050    /** Foreground bold black. */
051    public static final String BOLD_BLACK = "\u001b[30;1m";
052    /** Foreground bold red. */
053    public static final String BOLD_RED = "\u001b[31;1m";
054    /** Foreground bold green. */
055    public static final String BOLD_GREEN = "\u001b[32;1m";
056    /** Foreground bold yellow. */
057    public static final String BOLD_YELLOW = "\u001b[33;1m";
058    /** Foreground bold blue. */
059    public static final String BOLD_BLUE = "\u001b[34;1m";
060    /** Foreground bold magenta. */
061    public static final String BOLD_MAGENTA = "\u001b[35;1m";
062    /** Foreground bold cyan. */
063    public static final String BOLD_CYAN = "\u001b[36;1m";
064    /** Foreground bold white. */
065    public static final String BOLD_WHITE = "\u001b[37;1m";
066
067    /** Background black. */
068    public static final String BG_BLACK = "\u001b[40m";
069    /** Background red. */
070    public static final String BG_RED = "\u001b[41m";
071    /** Background green. */
072    public static final String BG_GREEN = "\u001b[42m";
073    /** Background yellow. */
074    public static final String BG_YELLOW = "\u001b[43m";
075    /** Background blue. */
076    public static final String BG_BLUE = "\u001b[44m";
077    /** Background magenta. */
078    public static final String BG_MAGENTA = "\u001b[45m";
079    /** Background cyan. */
080    public static final String BG_CYAN = "\u001b[46m";
081    /** Background white. */
082    public static final String BG_WHITE = "\u001b[47m";
083
084    /** Reset. */
085    public static final String RESET = "\u001b[0m";
086    /** Underline. */
087    public static final String UNDERLINED = "\u001b[4m";
088
089    public AnsiTerminalDecorator(MxRunListener l) {
090        super(l);
091    }
092
093    @Override
094    public void testSucceeded(Description description) {
095        getWriter().print(GREEN);
096        super.testSucceeded(description);
097        getWriter().print(RESET);
098    }
099
100    @Override
101    public void testAssumptionFailure(Failure failure) {
102        getWriter().print(BLUE);
103        super.testAssumptionFailure(failure);
104        getWriter().print(RESET);
105    }
106
107    @Override
108    public void testFailed(Failure failure) {
109        getWriter().print(RED);
110        super.testFailed(failure);
111        getWriter().print(RESET);
112    }
113
114    @Override
115    public void testIgnored(Description description) {
116        getWriter().print(MAGENTA);
117        super.testIgnored(description);
118        getWriter().print(RESET);
119    }
120}