@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( event.getEnvironment(), "spring.output.ansi."); if (resolver.containsProperty("enabled")) { String enabled = resolver.getProperty("enabled"); AnsiOutput.setEnabled(Enum.valueOf(Enabled.class, enabled.toUpperCase())); } if (resolver.containsProperty("console-available")) { AnsiOutput.setConsoleAvailable( resolver.getProperty("console-available", Boolean.class)); } }
@Test public void enabled() { SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); Map<String, Object> props = new HashMap<String, Object>(); props.put("spring.output.ansi.enabled", "ALWAYS"); application.setDefaultProperties(props); this.context = application.run(); assertThat(AnsiOutputEnabledValue.get()).isEqualTo(Enabled.ALWAYS); }
@Test public void disabled() throws Exception { SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); Map<String, Object> props = new HashMap<String, Object>(); props.put("spring.output.ansi.enabled", "never"); application.setDefaultProperties(props); this.context = application.run(); assertThat(AnsiOutputEnabledValue.get()).isEqualTo(Enabled.NEVER); }
@Test public void disabledViaApplicationProperties() throws Exception { ConfigurableEnvironment environment = new StandardEnvironment(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, "spring.config.name=ansi"); SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); application.setEnvironment(environment); this.context = application.run(); assertThat(AnsiOutputEnabledValue.get()).isEqualTo(Enabled.NEVER); }
@Test public void printBannerShouldRenderGradient() throws Exception { AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER); String banner = printBanner("gradient.gif", "banner.image.width=10", "banner.image.margin=0"); assertThat(banner).contains("@#8&o:*. "); }
@Test public void printBannerShouldCapWidthAndCalculateHeight() throws Exception { AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER); String banner = printBanner("large.gif", "banner.image.margin=0"); assertThat(getBannerWidth(banner)).isEqualTo(76); assertThat(getBannerHeight(banner)).isEqualTo(37); }
@Test public void printBannerShouldPrintMargin() throws Exception { AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER); String banner = printBanner("large.gif"); String[] lines = banner.split(NEW_LINE); for (int i = 2; i < lines.length - 1; i++) { assertThat(lines[i]).startsWith(" @"); } }
@Test public void printBannerWhenHasMarginPropertyShouldPrintSizedMargin() throws Exception { AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER); String banner = printBanner("large.gif", "banner.image.margin=4"); String[] lines = banner.split(NEW_LINE); for (int i = 2; i < lines.length - 1; i++) { assertThat(lines[i]).startsWith(" @"); } }
@Test public void renderWithColors() throws Exception { Resource resource = new ByteArrayResource( "${Ansi.RED}This is red.${Ansi.NORMAL}".getBytes()); AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS); String banner = printBanner(resource, null, null, null); assertThat(banner).startsWith("\u001B[31mThis is red.\u001B[0m"); }
@Test public void renderWithColorsButDisabled() throws Exception { Resource resource = new ByteArrayResource( "${Ansi.RED}This is red.${Ansi.NORMAL}".getBytes()); AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER); String banner = printBanner(resource, null, null, null); assertThat(banner).startsWith("This is red."); }
protected void captureOutput() { AnsiOutput.setEnabled(Enabled.NEVER); this.copy = new ByteArrayOutputStream(); this.captureOut = new CaptureOutputStream(System.out, this.copy); this.captureErr = new CaptureOutputStream(System.err, this.copy); System.setOut(new PrintStream(this.captureOut)); System.setErr(new PrintStream(this.captureErr)); }
@Test public void enabled() { SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); Map<String, Object> props = new HashMap<String, Object>(); props.put("spring.output.ansi.enabled", "ALWAYS"); application.setDefaultProperties(props); this.context = application.run(); assertThat(AnsiOutputEnabledValue.get(), equalTo(Enabled.ALWAYS)); }
@Test public void disabled() throws Exception { SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); Map<String, Object> props = new HashMap<String, Object>(); props.put("spring.output.ansi.enabled", "never"); application.setDefaultProperties(props); this.context = application.run(); assertThat(AnsiOutputEnabledValue.get(), equalTo(Enabled.NEVER)); }
@Test public void disabledViaApplicationProperties() throws Exception { ConfigurableEnvironment environment = new StandardEnvironment(); EnvironmentTestUtils.addEnvironment(environment, "spring.config.name:ansi"); SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); application.setEnvironment(environment); this.context = application.run(); assertThat(AnsiOutputEnabledValue.get(), equalTo(Enabled.NEVER)); }
@Test public void renderWithColors() throws Exception { Resource resource = new ByteArrayResource( "${Ansi.RED}This is red.${Ansi.NORMAL}".getBytes()); AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS); String banner = printBanner(resource, null, null); assertThat(banner, startsWith("\u001B[31mThis is red.\u001B[0m")); }
@Test public void renderWithColorsButDisabled() throws Exception { Resource resource = new ByteArrayResource( "${Ansi.RED}This is red.${Ansi.NORMAL}".getBytes()); AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER); String banner = printBanner(resource, null, null); assertThat(banner, startsWith("This is red.")); }
public static Enabled get() { return AnsiOutput.getEnabled(); }
@After public void reset() { AnsiOutput.setEnabled(Enabled.DETECT); }
@Test public void encodeEnabled() throws Exception { AnsiOutput.setEnabled(Enabled.ALWAYS); AnsiPropertySource source = new AnsiPropertySource("ansi", true); assertThat(source.getProperty("Ansi.RED")).isEqualTo("\033[31m"); }
@Test public void encodeDisabled() throws Exception { AnsiOutput.setEnabled(Enabled.NEVER); AnsiPropertySource source = new AnsiPropertySource("ansi", true); assertThat(source.getProperty("Ansi.RED")).isEqualTo(""); }
@BeforeClass public static void enable() { AnsiOutput.setEnabled(Enabled.ALWAYS); }
@AfterClass public static void reset() { AnsiOutput.setEnabled(Enabled.DETECT); }
@Before public void resetAnsi() { AnsiOutput.setEnabled(Enabled.DETECT); }
@Before public void setup() { AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS); }
@After public void cleanup() { AnsiOutput.setEnabled(Enabled.DETECT); }
protected void releaseOutput() { AnsiOutput.setEnabled(Enabled.DETECT); System.setOut(this.captureOut.getOriginal()); System.setErr(this.captureErr.getOriginal()); this.copy = null; }
@Override public void disableAnsiOutput() { AnsiOutput.setEnabled(Enabled.NEVER); }