Commit 43d1d926 authored by Phillip Webb's avatar Phillip Webb

Rework ImageBanner Support

Refactor several aspects of the ImageBanner:

- Extract a few new classes and methods from the previous code
- Directly encode ANSI rather than using `${}` properties
- Rework the scaling algorithm to prefer a fixed width
- Allow ImageBanner and TextBanner to be used together
- Rename several of the `banner.image` properties
- Add support for a left hand margin
- Add property meta-data

See gh-4647
parent 60500aef
${Ansi.GREEN} :: Sample application build with Spring Boot${spring-boot.formatted-version} ::${Ansi.DEFAULT}
...@@ -18,26 +18,24 @@ package org.springframework.boot; ...@@ -18,26 +18,24 @@ package org.springframework.boot;
import java.awt.Color; import java.awt.Color;
import java.awt.Image; import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.ansi.AnsiPropertySource; import org.springframework.boot.ansi.AnsiBackground;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiColors;
import org.springframework.boot.ansi.AnsiElement;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -46,271 +44,137 @@ import org.springframework.util.Assert; ...@@ -46,271 +44,137 @@ import org.springframework.util.Assert;
* {@link Resource}. * {@link Resource}.
* *
* @author Craig Burke * @author Craig Burke
* @author Phillip Webb
* @since 1.4.0
*/ */
public class ImageBanner implements Banner { public class ImageBanner implements Banner {
private static final Log log = LogFactory.getLog(ImageBanner.class); private static final Log log = LogFactory.getLog(ImageBanner.class);
private static final double RED_WEIGHT = 0.2126d; private static final double[] RGB_WEIGHT = { 0.2126d, 0.7152d, 0.0722d };
private static final double GREEN_WEIGHT = 0.7152d;
private static final double BLUE_WEIGHT = 0.0722d; private static final char[] PIXEL = { ' ', '.', '*', ':', 'o', '&', '8', '#', '@' };
private static final int DEFAULT_MAX_WIDTH = 72; private static final int LUMINANCE_INCREMENT = 10;
private static final double DEFAULT_ASPECT_RATIO = 0.5d;
private static final boolean DEFAULT_DARK = false;
private Resource image; private static final int LUMINANCE_START = LUMINANCE_INCREMENT * PIXEL.length;
private Map<String, Color> colors = new HashMap<String, Color>();
private final Resource image;
public ImageBanner(Resource image) { public ImageBanner(Resource image) {
Assert.notNull(image, "Image must not be null"); Assert.notNull(image, "Image must not be null");
Assert.isTrue(image.exists(), "Image must exist"); Assert.isTrue(image.exists(), "Image must exist");
this.image = image; this.image = image;
colorsInit();
}
private void colorsInit() {
this.colors.put("BLACK", new Color(0, 0, 0));
this.colors.put("RED", new Color(170, 0, 0));
this.colors.put("GREEN", new Color(0, 170, 0));
this.colors.put("YELLOW", new Color(170, 85, 0));
this.colors.put("BLUE", new Color(0, 0, 170));
this.colors.put("MAGENTA", new Color(170, 0, 170));
this.colors.put("CYAN", new Color(0, 170, 170));
this.colors.put("WHITE", new Color(170, 170, 170));
this.colors.put("BRIGHT_BLACK", new Color(85, 85, 85));
this.colors.put("BRIGHT_RED", new Color(255, 85, 85));
this.colors.put("BRIGHT_GREEN", new Color(85, 255, 85));
this.colors.put("BRIGHT_YELLOW", new Color(255, 255, 85));
this.colors.put("BRIGHT_BLUE", new Color(85, 85, 255));
this.colors.put("BRIGHT_MAGENTA", new Color(255, 85, 255));
this.colors.put("BRIGHT_CYAN", new Color(85, 255, 255));
this.colors.put("BRIGHT_WHITE", new Color(255, 255, 255));
} }
@Override @Override
public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) { public void printBanner(Environment environment, Class<?> sourceClass,
String headlessProperty = System.getProperty("java.awt.headless"); PrintStream out) {
String headless = System.getProperty("java.awt.headless");
try { try {
System.setProperty("java.awt.headless", "true"); System.setProperty("java.awt.headless", "true");
BufferedImage sourceImage = ImageIO.read(this.image.getInputStream()); printBanner(environment, out);
int maxWidth = environment.getProperty("banner.image.max-width",
Integer.class, DEFAULT_MAX_WIDTH);
Double aspectRatio = environment.getProperty("banner.image.aspect-ratio",
Double.class, DEFAULT_ASPECT_RATIO);
boolean invert = environment.getProperty("banner.image.dark", Boolean.class,
DEFAULT_DARK);
BufferedImage resizedImage = resizeImage(sourceImage, maxWidth, aspectRatio);
String banner = imageToBanner(resizedImage, invert);
PropertyResolver ansiResolver = getAnsiResolver();
banner = ansiResolver.resolvePlaceholders(banner);
out.println(banner);
} }
catch (Exception ex) { catch (Exception ex) {
log.warn("Image banner not printable: " + this.image + " (" + ex.getClass() log.warn("Image banner not printable: " + this.image + " (" + ex.getClass()
+ ": '" + ex.getMessage() + "')", ex); + ": '" + ex.getMessage() + "')", ex);
} }
finally { finally {
System.setProperty("java.awt.headless", headlessProperty); if (headless == null) {
} System.clearProperty("java.awt.headless");
}
private PropertyResolver getAnsiResolver() {
MutablePropertySources sources = new MutablePropertySources();
sources.addFirst(new AnsiPropertySource("ansi", true));
return new PropertySourcesPropertyResolver(sources);
}
private String imageToBanner(BufferedImage image, boolean dark) {
StringBuilder banner = new StringBuilder();
for (int y = 0; y < image.getHeight(); y++) {
if (dark) {
banner.append("${AnsiBackground.BLACK}");
} }
else { else {
banner.append("${AnsiBackground.DEFAULT}"); System.setProperty("java.awt.headless", headless);
}
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y), false);
banner.append(getFormatString(color, dark));
} }
if (dark) {
banner.append("${AnsiBackground.DEFAULT}");
} }
banner.append("${AnsiColor.DEFAULT}\n");
} }
return banner.toString(); private void printBanner(Environment environment, PrintStream out)
throws IOException {
PropertyResolver properties = new RelaxedPropertyResolver(environment,
"banner.image.");
int width = properties.getProperty("width", Integer.class, 76);
int heigth = properties.getProperty("height", Integer.class, 0);
int margin = properties.getProperty("margin", Integer.class, 2);
boolean invert = properties.getProperty("invert", Boolean.class, false);
BufferedImage image = readImage(width, heigth);
printBanner(image, margin, invert, out);
} }
protected String getFormatString(Color color, boolean dark) { private BufferedImage readImage(int width, int heigth) throws IOException {
String matchedColorName = null; InputStream inputStream = this.image.getInputStream();
Double minColorDistance = null; try {
BufferedImage image = ImageIO.read(inputStream);
for (Entry<String, Color> colorOption : this.colors.entrySet()) { return resizeImage(image, width, heigth);
double distance = getColorDistance(color, colorOption.getValue());
if (minColorDistance == null || distance < minColorDistance) {
minColorDistance = distance;
matchedColorName = colorOption.getKey();
} }
finally {
inputStream.close();
} }
return "${AnsiColor." + matchedColorName + "}" + getAsciiCharacter(color, dark);
} }
private static int getLuminance(Color color, boolean inverse) { private BufferedImage resizeImage(BufferedImage image, int width, int height) {
double red = color.getRed(); if (width < 1) {
double green = color.getGreen(); width = 1;
double blue = color.getBlue();
double luminance;
if (inverse) {
luminance = (RED_WEIGHT * (255.0d - red)) + (GREEN_WEIGHT * (255.0d - green))
+ (BLUE_WEIGHT * (255.0d - blue));
} }
else { if (height <= 0) {
luminance = (RED_WEIGHT * red) + (GREEN_WEIGHT * green) double aspectRatio = (double) width / image.getWidth() * 0.5;
+ (BLUE_WEIGHT * blue); height = (int) Math.ceil(image.getHeight() * aspectRatio);
} }
BufferedImage resized = new BufferedImage(width, height,
return (int) Math.ceil((luminance / 255.0d) * 100); BufferedImage.TYPE_INT_RGB);
Image scaled = image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
resized.getGraphics().drawImage(scaled, 0, 0, null);
return resized;
} }
private static char getAsciiCharacter(Color color, boolean dark) { private void printBanner(BufferedImage image, int margin, boolean invert,
double luminance = getLuminance(color, dark); PrintStream out) {
AnsiElement background = (invert ? AnsiBackground.BLACK : AnsiBackground.DEFAULT);
if (luminance >= 90) { out.print(AnsiOutput.encode(AnsiColor.DEFAULT));
return ' '; out.print(AnsiOutput.encode(background));
} out.println();
else if (luminance >= 80) { out.println();
return '.'; AnsiColor lastColor = AnsiColor.DEFAULT;
} for (int y = 0; y < image.getHeight(); y++) {
else if (luminance >= 70) { for (int i = 0; i < margin; i++) {
return '*'; out.print(" ");
}
else if (luminance >= 60) {
return ':';
}
else if (luminance >= 50) {
return 'o';
}
else if (luminance >= 40) {
return '&';
} }
else if (luminance >= 30) { for (int x = 0; x < image.getWidth(); x++) {
return '8'; Color color = new Color(image.getRGB(x, y), false);
AnsiColor ansiColor = AnsiColors.getClosest(color);
if (ansiColor != lastColor) {
out.print(AnsiOutput.encode(ansiColor));
lastColor = ansiColor;
} }
else if (luminance >= 20) { out.print(getAsciiPixel(color, invert));
return '#';
} }
else { out.println();
return '@';
} }
out.print(AnsiOutput.encode(AnsiColor.DEFAULT));
out.print(AnsiOutput.encode(AnsiBackground.DEFAULT));
out.println();
} }
private static BufferedImage resizeImage(BufferedImage sourceImage, int maxWidth, private char getAsciiPixel(Color color, boolean dark) {
double aspectRatio) { double luminance = getLuminance(color, dark);
int width; for (int i = 0; i < PIXEL.length; i++) {
double resizeRatio; if (luminance >= (LUMINANCE_START - (i * LUMINANCE_INCREMENT))) {
if (sourceImage.getWidth() > maxWidth) { return PIXEL[i];
resizeRatio = (double) maxWidth / (double) sourceImage.getWidth();
width = maxWidth;
} }
else {
resizeRatio = 1.0d;
width = sourceImage.getWidth();
} }
return PIXEL[PIXEL.length - 1];
int height = (int) (Math.ceil(resizeRatio * aspectRatio
* (double) sourceImage.getHeight()));
Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage resizedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
resizedImage.getGraphics().drawImage(image, 0, 0, null);
return resizedImage;
} }
/** private int getLuminance(Color color, boolean inverse) {
* Computes the CIE94 distance between two colors. double luminance = 0.0;
* luminance += getLuminance(color.getRed(), inverse, RGB_WEIGHT[0]);
* Contributed by michael-simons luminance += getLuminance(color.getGreen(), inverse, RGB_WEIGHT[1]);
* (original implementation https://github.com/michael-simons/dfx-mosaic/blob/public/src/main/java/de/dailyfratze/mosaic/images/CIE94ColorDistance.java) luminance += getLuminance(color.getBlue(), inverse, RGB_WEIGHT[2]);
* return (int) Math.ceil((luminance / 0xFF) * 100);
* @param color1 the first color
* @param color2 the second color
* @return the distance between the colors
*/
private static double getColorDistance(final Color color1, final Color color2) {
// Convert to L*a*b* color space
float[] lab1 = toLab(color1);
float[] lab2 = toLab(color2);
// Make it more readable
double L1 = lab1[0];
double a1 = lab1[1];
double b1 = lab1[2];
double L2 = lab2[0];
double a2 = lab2[1];
double b2 = lab2[2];
// CIE94 coefficients for graphic arts
double kL = 1;
double K1 = 0.045;
double K2 = 0.015;
// Weighting factors
double sl = 1.0;
double kc = 1.0;
double kh = 1.0;
// See http://en.wikipedia.org/wiki/Color_difference#CIE94
double c1 = Math.sqrt(a1 * a1 + b1 * b1);
double deltaC = c1 - Math.sqrt(a2 * a2 + b2 * b2);
double deltaA = a1 - a2;
double deltaB = b1 - b2;
double deltaH = Math.sqrt(Math.max(0.0, deltaA * deltaA + deltaB * deltaB - deltaC * deltaC));
return Math.sqrt(Math.max(0.0, Math.pow((L1 - L2) / (kL * sl), 2) + Math.pow(deltaC / (kc * (1 + K1 * c1)), 2) + Math.pow(deltaH / (kh * (1 + K2 * c1)), 2.0)));
} }
/** private double getLuminance(int component, boolean inverse, double weight) {
* Returns the CIE L*a*b* values of this color. return (inverse ? 0xFF - component : component) * weight;
*
* Implements the forward transformation described in
* https://en.wikipedia.org/wiki/Lab_color_space
*
* @param color the color to convert
* @return the xyz color components
*/
static float[] toLab(Color color) {
float[] xyz = color.getColorComponents(
ColorSpace.getInstance(ColorSpace.CS_CIEXYZ), null);
return xyzToLab(xyz);
}
static float[] xyzToLab(float[] colorvalue) {
double l = f(colorvalue[1]);
double L = 116.0 * l - 16.0;
double a = 500.0 * (f(colorvalue[0]) - l);
double b = 200.0 * (l - f(colorvalue[2]));
return new float[]{(float) L, (float) a, (float) b};
} }
private static double f(double t) {
if (t > 216.0 / 24389.0) {
return Math.cbrt(t);
}
else {
return (1.0 / 3.0) * Math.pow(29.0 / 6.0, 2) * t + (4.0 / 29.0);
}
}
} }
...@@ -38,7 +38,7 @@ import org.springframework.util.Assert; ...@@ -38,7 +38,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
/** /**
* Banner implementation that prints from a source {@link Resource}. * Banner implementation that prints from a source text {@link Resource}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Vedran Pavic * @author Vedran Pavic
......
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
package org.springframework.boot; package org.springframework.boot;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.security.AccessControlException; import java.security.AccessControlException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -41,6 +38,7 @@ import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; ...@@ -41,6 +38,7 @@ import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.diagnostics.FailureAnalyzers; import org.springframework.boot.diagnostics.FailureAnalyzers;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
...@@ -167,19 +165,17 @@ public class SpringApplication { ...@@ -167,19 +165,17 @@ public class SpringApplication {
/** /**
* Default banner location. * Default banner location.
*/ */
public static final String BANNER_LOCATION_PROPERTY_VALUE = "banner.txt"; public static final String BANNER_LOCATION_PROPERTY_VALUE = SpringApplicationBannerPrinter.DEFAULT_BANNER_LOCATION;
/** /**
* Banner location property key. * Banner location property key.
*/ */
public static final String BANNER_LOCATION_PROPERTY = "banner.location"; public static final String BANNER_LOCATION_PROPERTY = SpringApplicationBannerPrinter.BANNER_LOCATION_PROPERTY;
private static final String CONFIGURABLE_WEB_ENVIRONMENT_CLASS = "org.springframework.web.context.ConfigurableWebEnvironment"; private static final String CONFIGURABLE_WEB_ENVIRONMENT_CLASS = "org.springframework.web.context.ConfigurableWebEnvironment";
private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless"; private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
private static final Banner DEFAULT_BANNER = new SpringBootBanner();
private static final Set<String> SERVLET_ENVIRONMENT_SOURCE_NAMES; private static final Set<String> SERVLET_ENVIRONMENT_SOURCE_NAMES;
static { static {
...@@ -543,60 +539,16 @@ public class SpringApplication { ...@@ -543,60 +539,16 @@ public class SpringApplication {
* @see #setBannerMode * @see #setBannerMode
*/ */
protected void printBanner(Environment environment) { protected void printBanner(Environment environment) {
Banner selectedBanner = selectBanner(environment);
if (this.bannerMode == Banner.Mode.LOG) {
try {
logger.info(createStringFromBanner(selectedBanner, environment));
}
catch (UnsupportedEncodingException ex) {
logger.warn("Failed to create String for banner", ex);
}
}
else {
selectedBanner.printBanner(environment, this.mainApplicationClass,
System.out);
}
}
private Banner selectBanner(Environment environment) {
String location = environment.getProperty(BANNER_LOCATION_PROPERTY,
BANNER_LOCATION_PROPERTY_VALUE);
ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader
: new DefaultResourceLoader(getClassLoader()); : new DefaultResourceLoader(getClassLoader());
Resource resource = resourceLoader.getResource(location); SpringApplicationBannerPrinter banner = new SpringApplicationBannerPrinter(resourceLoader,
if (resource.exists()) { this.banner);
return new ResourceBanner(resource); if (this.bannerMode == Mode.LOG) {
} banner.print(environment, this.mainApplicationClass, logger);
if (this.banner != null) {
return this.banner;
} }
else {
Resource image = getBannerImage(environment, resourceLoader); banner.print(environment, this.mainApplicationClass, System.out);
if (image.exists()) {
return new ImageBanner(image);
}
return DEFAULT_BANNER;
}
private Resource getBannerImage(Environment environment, ResourceLoader resourceLoader) {
String imageLocation = environment.getProperty("banner.image", "banner.gif");
Resource image = resourceLoader.getResource(imageLocation);
if (!image.exists()) {
image = resourceLoader.getResource("banner.jpg");
}
if (!image.exists()) {
image = resourceLoader.getResource("banner.png");
}
return image;
} }
private String createStringFromBanner(Banner banner, Environment environment)
throws UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
banner.printBanner(environment, this.mainApplicationClass, new PrintStream(baos));
String charset = environment.getProperty("banner.charset", "UTF-8");
return baos.toString(charset);
} }
/** /**
......
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;
/**
* Class used by {@link SpringApplication} to print the application banner.
*
* @author Phillip Webb
*/
class SpringApplicationBannerPrinter {
static final String BANNER_LOCATION_PROPERTY = "banner.location";
static final String BANNER_IMAGE_LOCATION_PROPERTY = "banner.image.location";
static final String DEFAULT_BANNER_LOCATION = "banner.txt";
static final String[] IMAGE_EXTENSION = { "gif", "jpg", "png" };
private static final Banner DEFAULT_BANNER = new SpringBootBanner();
private final ResourceLoader resourceLoader;
private final Banner fallbackBanner;
SpringApplicationBannerPrinter(ResourceLoader resourceLoader, Banner fallbackBanner) {
this.resourceLoader = resourceLoader;
this.fallbackBanner = fallbackBanner;
}
public void print(Environment environment, Class<?> sourceClass, Log logger) {
Banner banner = getBanner(environment, this.fallbackBanner);
try {
logger.info(createStringFromBanner(banner, environment, sourceClass));
}
catch (UnsupportedEncodingException ex) {
logger.warn("Failed to create String for banner", ex);
}
}
public void print(Environment environment, Class<?> sourceClass, PrintStream out) {
Banner banner = getBanner(environment, this.fallbackBanner);
banner.printBanner(environment, sourceClass, out);
}
private Banner getBanner(Environment environment, Banner definedBanner) {
Banners banners = new Banners();
banners.addIfNotNull(getImageBanner(environment));
banners.addIfNotNull(getTextBanner(environment));
if (banners.hasAtLeastOneBanner()) {
return banners;
}
if (this.fallbackBanner != null) {
return this.fallbackBanner;
}
return DEFAULT_BANNER;
}
private Banner getTextBanner(Environment environment) {
String location = environment.getProperty(BANNER_LOCATION_PROPERTY,
DEFAULT_BANNER_LOCATION);
Resource resource = this.resourceLoader.getResource(location);
if (resource.exists()) {
return new ResourceBanner(resource);
}
return null;
}
private Banner getImageBanner(Environment environment) {
String location = environment.getProperty(BANNER_IMAGE_LOCATION_PROPERTY);
if (StringUtils.hasLength(location)) {
Resource resource = this.resourceLoader.getResource(location);
return (resource.exists() ? new ImageBanner(resource) : null);
}
for (String ext : IMAGE_EXTENSION) {
Resource resource = this.resourceLoader.getResource("banner." + ext);
if (resource.exists()) {
return new ImageBanner(resource);
}
}
return null;
}
private String createStringFromBanner(Banner banner, Environment environment,
Class<?> mainApplicationClass) throws UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
banner.printBanner(environment, mainApplicationClass, new PrintStream(baos));
String charset = environment.getProperty("banner.charset", "UTF-8");
return baos.toString(charset);
}
/**
* {@link Banner} comprised of other {@link Banner Banners}.
*/
private static class Banners implements Banner {
private final List<Banner> banners = new ArrayList<Banner>();
public void addIfNotNull(Banner banner) {
if (banner != null) {
this.banners.add(banner);
}
}
public boolean hasAtLeastOneBanner() {
return !this.banners.isEmpty();
}
@Override
public void printBanner(Environment environment, Class<?> sourceClass,
PrintStream out) {
for (Banner banner : this.banners) {
banner.printBanner(environment, sourceClass, out);
}
}
}
}
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.ansi;
import java.awt.Color;
import java.awt.color.ColorSpace;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.util.Assert;
/**
* Utility for working with {@link AnsiColor} in the context of {@link Color AWT Colors}.
*
* @author Craig Burke
* @author Ruben Dijkstra
* @author Phillip Webb
* @author Michael Simons
* @since 1.4.0
*/
public final class AnsiColors {
private static final Map<AnsiColor, LabColor> ANSI_COLOR_MAP;
static {
Map<AnsiColor, LabColor> colorMap = new LinkedHashMap<AnsiColor, LabColor>();
colorMap.put(AnsiColor.BLACK, new LabColor(0x000000));
colorMap.put(AnsiColor.RED, new LabColor(0xAA0000));
colorMap.put(AnsiColor.GREEN, new LabColor(0x00AA00));
colorMap.put(AnsiColor.YELLOW, new LabColor(0xAA5500));
colorMap.put(AnsiColor.BLUE, new LabColor(0x0000AA));
colorMap.put(AnsiColor.MAGENTA, new LabColor(0xAA00AA));
colorMap.put(AnsiColor.CYAN, new LabColor(0x00AAAA));
colorMap.put(AnsiColor.WHITE, new LabColor(0xAAAAAA));
colorMap.put(AnsiColor.BRIGHT_BLACK, new LabColor(0x555555));
colorMap.put(AnsiColor.BRIGHT_RED, new LabColor(0xFF5555));
colorMap.put(AnsiColor.BRIGHT_GREEN, new LabColor(0x55FF00));
colorMap.put(AnsiColor.BRIGHT_YELLOW, new LabColor(0xFFFF55));
colorMap.put(AnsiColor.BRIGHT_BLUE, new LabColor(0x5555FF));
colorMap.put(AnsiColor.BRIGHT_MAGENTA, new LabColor(0xFF55FF));
colorMap.put(AnsiColor.BRIGHT_CYAN, new LabColor(0x55FFFF));
colorMap.put(AnsiColor.BRIGHT_WHITE, new LabColor(0xFFFFFF));
ANSI_COLOR_MAP = Collections.unmodifiableMap(colorMap);
}
private AnsiColors() {
}
public static AnsiColor getClosest(Color color) {
return getClosest(new LabColor(color));
}
private static AnsiColor getClosest(LabColor color) {
AnsiColor result = null;
double resultDistance = Float.MAX_VALUE;
for (Entry<AnsiColor, LabColor> entry : ANSI_COLOR_MAP.entrySet()) {
double distance = color.getDistance(entry.getValue());
if (result == null || distance < resultDistance) {
resultDistance = distance;
result = entry.getKey();
}
}
return result;
}
/**
* Represents a color stored in LAB form.
*/
private static final class LabColor {
private static final ColorSpace XYZ_COLOR_SPACE = ColorSpace
.getInstance(ColorSpace.CS_CIEXYZ);
private final double l;
private final double a;
private final double b;
LabColor(Integer rgb) {
this(rgb == null ? (Color) null : new Color(rgb));
}
LabColor(Color color) {
Assert.notNull(color, "Color must not be null");
float[] lab = fromXyz(color.getColorComponents(XYZ_COLOR_SPACE, null));
this.l = lab[0];
this.a = lab[1];
this.b = lab[2];
}
private float[] fromXyz(float[] xyz) {
return fromXyz(xyz[0], xyz[1], xyz[2]);
}
private float[] fromXyz(float x, float y, float z) {
double l = (f(y) - 16.0) * 116.0;
double a = (f(x) - f(y)) * 500.0;
double b = (f(y) - f(z)) * 200.0;
return new float[] { (float) l, (float) a, (float) b };
}
private double f(double t) {
return (t > (216.0 / 24389.0) ? Math.cbrt(t)
: (1.0 / 3.0) * Math.pow(29.0 / 6.0, 2) * t + (4.0 / 29.0));
}
// See http://en.wikipedia.org/wiki/Color_difference#CIE94
public double getDistance(LabColor other) {
double c1 = Math.sqrt(this.a * this.a + this.b * this.b);
double deltaC = c1 - Math.sqrt(other.a * other.a + other.b * other.b);
double deltaA = this.a - other.a;
double deltaB = this.b - other.b;
double deltaH = Math.sqrt(
Math.max(0.0, deltaA * deltaA + deltaB * deltaB - deltaC * deltaC));
return Math.sqrt(Math.max(0.0,
Math.pow((this.l - other.l) / (1.0), 2)
+ Math.pow(deltaC / (1 + 0.045 * c1), 2)
+ Math.pow(deltaH / (1 + 0.015 * c1), 2.0)));
}
}
}
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.ansi;
import java.awt.Color;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link AnsiColors}.
*
* @author Phillip Webb
*/
public class AnsiColorsTests {
@Test
public void getClosestWhenExactMatchShouldReturnAnsiColor() throws Exception {
assertThat(getClosest(0x000000)).isEqualTo(AnsiColor.BLACK);
assertThat(getClosest(0xAA0000)).isEqualTo(AnsiColor.RED);
assertThat(getClosest(0x00AA00)).isEqualTo(AnsiColor.GREEN);
assertThat(getClosest(0xAA5500)).isEqualTo(AnsiColor.YELLOW);
assertThat(getClosest(0x0000AA)).isEqualTo(AnsiColor.BLUE);
assertThat(getClosest(0xAA00AA)).isEqualTo(AnsiColor.MAGENTA);
assertThat(getClosest(0x00AAAA)).isEqualTo(AnsiColor.CYAN);
assertThat(getClosest(0xAAAAAA)).isEqualTo(AnsiColor.WHITE);
assertThat(getClosest(0x555555)).isEqualTo(AnsiColor.BRIGHT_BLACK);
assertThat(getClosest(0xFF5555)).isEqualTo(AnsiColor.BRIGHT_RED);
assertThat(getClosest(0x55FF00)).isEqualTo(AnsiColor.BRIGHT_GREEN);
assertThat(getClosest(0xFFFF55)).isEqualTo(AnsiColor.BRIGHT_YELLOW);
assertThat(getClosest(0x5555FF)).isEqualTo(AnsiColor.BRIGHT_BLUE);
assertThat(getClosest(0xFF55FF)).isEqualTo(AnsiColor.BRIGHT_MAGENTA);
assertThat(getClosest(0x55FFFF)).isEqualTo(AnsiColor.BRIGHT_CYAN);
assertThat(getClosest(0xFFFFFF)).isEqualTo(AnsiColor.BRIGHT_WHITE);
}
@Test
public void getClosestWhenCloseShouldReturnAnsiColor() throws Exception {
assertThat(getClosest(0x292424)).isEqualTo(AnsiColor.BLACK);
assertThat(getClosest(0x8C1919)).isEqualTo(AnsiColor.RED);
assertThat(getClosest(0x0BA10B)).isEqualTo(AnsiColor.GREEN);
assertThat(getClosest(0xB55F09)).isEqualTo(AnsiColor.YELLOW);
assertThat(getClosest(0x0B0BA1)).isEqualTo(AnsiColor.BLUE);
assertThat(getClosest(0xA312A3)).isEqualTo(AnsiColor.MAGENTA);
assertThat(getClosest(0x0BB5B5)).isEqualTo(AnsiColor.CYAN);
assertThat(getClosest(0xBAB6B6)).isEqualTo(AnsiColor.WHITE);
assertThat(getClosest(0x615A5A)).isEqualTo(AnsiColor.BRIGHT_BLACK);
assertThat(getClosest(0xF23333)).isEqualTo(AnsiColor.BRIGHT_RED);
assertThat(getClosest(0x55E80C)).isEqualTo(AnsiColor.BRIGHT_GREEN);
assertThat(getClosest(0xF5F54C)).isEqualTo(AnsiColor.BRIGHT_YELLOW);
assertThat(getClosest(0x5656F0)).isEqualTo(AnsiColor.BRIGHT_BLUE);
assertThat(getClosest(0xFA50FA)).isEqualTo(AnsiColor.BRIGHT_MAGENTA);
assertThat(getClosest(0x56F5F5)).isEqualTo(AnsiColor.BRIGHT_CYAN);
assertThat(getClosest(0xEDF5F5)).isEqualTo(AnsiColor.BRIGHT_WHITE);
}
private AnsiColor getClosest(int rgb) {
return AnsiColors.getClosest(new Color(rgb));
}
}
...@@ -13,9 +13,36 @@ ...@@ -13,9 +13,36 @@
{ {
"name": "banner.location", "name": "banner.location",
"type": "org.springframework.core.io.Resource", "type": "org.springframework.core.io.Resource",
"description": "Banner file location.", "description": "Banner text resource location.",
"defaultValue": "classpath:banner.txt" "defaultValue": "classpath:banner.txt"
}, },
{
"name": "banner.image.location",
"type": "org.springframework.core.io.Resource",
"description": "Banner image file location.",
"defaultValue": "banner.gif"
},
{
"name": "banner.image.width",
"type": "java.lang.Integer",
"description": "Banner image width (in chars)."
},
{
"name": "banner.image.height",
"type": "java.lang.Integer",
"description": "Banner image height (in chars)."
},
{
"name": "banner.image.margin",
"type": "java.lang.Integer",
"description": "Left hand image height (in chars)."
},
{
"name": "banner.image.invert",
"type": "java.lang.Boolean",
"description": "Invert images for dark console themes.",
"defaultValue": false
},
{ {
"name": "debug", "name": "debug",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
......
...@@ -18,219 +18,171 @@ package org.springframework.boot; ...@@ -18,219 +18,171 @@ package org.springframework.boot;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.ansi.AnsiBackground; import org.springframework.boot.ansi.AnsiBackground;
import org.springframework.boot.ansi.AnsiColor; import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiElement;
import org.springframework.boot.ansi.AnsiOutput; import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.hamcrest.Matchers.containsString; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link ImageBanner}. * Tests for {@link ImageBanner}.
* *
* @author Craig Burke * @author Craig Burke
* @author Phillip Webb
*/ */
public class ImageBannerTests { public class ImageBannerTests {
private static final String IMAGE_BANNER_BLACK_AND_WHITE = "banners/black-and-white.gif"; private static final String NEW_LINE = System.getProperty("line.separator");
private static final String IMAGE_BANNER_LARGE = "banners/large.gif";
private static final String IMAGE_BANNER_ALL_COLORS = "banners/colors.gif";
private static final String IMAGE_BANNER_GRADIENT = "banners/gradient.gif";
private static final String BACKGROUND_DEFAULT_ANSI = getAnsiOutput(AnsiBackground.DEFAULT);
private static final String BACKGROUND_DARK_ANSI = getAnsiOutput(AnsiBackground.BLACK);
private static final char HIGH_LUMINANCE_CHARACTER = ' '; private static final char HIGH_LUMINANCE_CHARACTER = ' ';
private static final char LOW_LUMINANCE_CHARACTER = '@'; private static final char LOW_LUMINANCE_CHARACTER = '@';
private static Map<String, Object> properties; private static final String INVERT_TRUE = "banner.image.invert=true";
@Before @Before
public void setup() { public void setup() {
AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS); AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS);
properties = new HashMap<String, Object>();
} }
@Test @After
public void renderDefaultBackground() { public void cleanup() {
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); AnsiOutput.setEnabled(Enabled.DETECT);
assertThat(banner, startsWith(BACKGROUND_DEFAULT_ANSI));
} }
@Test @Test
public void renderDarkBackground() { public void printBannerShouldResetForegroundAndBackground() {
setDark(true); String banner = printBanner("black-and-white.gif");
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); String expected = AnsiOutput.encode(AnsiColor.DEFAULT)
+ AnsiOutput.encode(AnsiBackground.DEFAULT);
assertThat(banner).startsWith(expected);
}
assertThat(banner, startsWith(BACKGROUND_DARK_ANSI)); @Test
public void printBannerWhenInvertedShouldResetForegroundAndBackground() {
String banner = printBanner("black-and-white.gif", INVERT_TRUE);
String expected = AnsiOutput.encode(AnsiColor.DEFAULT)
+ AnsiOutput.encode(AnsiBackground.BLACK);
assertThat(banner).startsWith(expected);
} }
@Test @Test
public void renderWhiteCharactersWithColors() { public void printBannerShouldPrintWhiteAsBrightWhiteHighLuminance() {
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); String banner = printBanner("black-and-white.gif");
String expectedFirstLine = getAnsiOutput(AnsiColor.BRIGHT_WHITE) String expected = AnsiOutput.encode(AnsiColor.BRIGHT_WHITE)
+ HIGH_LUMINANCE_CHARACTER; + HIGH_LUMINANCE_CHARACTER;
assertThat(banner).contains(expected);
assertThat(banner, containsString(expectedFirstLine));
} }
@Test @Test
public void renderWhiteCharactersOnDarkBackground() { public void printBannerWhenInvertedShouldPrintWhiteAsBrightWhiteLowLuminance() {
setDark(true); String banner = printBanner("black-and-white.gif", INVERT_TRUE);
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); String expected = AnsiOutput.encode(AnsiColor.BRIGHT_WHITE)
String expectedFirstLine = getAnsiOutput(AnsiColor.BRIGHT_WHITE)
+ LOW_LUMINANCE_CHARACTER; + LOW_LUMINANCE_CHARACTER;
assertThat(banner).contains(expected);
assertThat(banner, containsString(expectedFirstLine));
} }
@Test @Test
public void renderBlackCharactersOnDefaultBackground() { public void printBannerShouldPrintBlackAsBlackLowLuminance() {
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); String banner = printBanner("black-and-white.gif");
String blackCharacter = getAnsiOutput(AnsiColor.BLACK) + LOW_LUMINANCE_CHARACTER; String expected = AnsiOutput.encode(AnsiColor.BLACK) + LOW_LUMINANCE_CHARACTER;
assertThat(banner).contains(expected);
assertThat(banner, containsString(blackCharacter));
} }
@Test @Test
public void renderBlackCharactersOnDarkBackground() { public void printBannerWhenInvertedShouldPrintBlackAsBlackHighLuminance() {
setDark(true); String banner = printBanner("black-and-white.gif", INVERT_TRUE);
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); String expected = AnsiOutput.encode(AnsiColor.BLACK) + HIGH_LUMINANCE_CHARACTER;
String blackCharacter = getAnsiOutput(AnsiColor.BLACK) + HIGH_LUMINANCE_CHARACTER; assertThat(banner).contains(expected);
assertThat(banner, containsString(blackCharacter));
} }
@Test @Test
public void renderBannerWithAllColors() { public void printBannerWhenShouldPrintAllColors() {
String banner = printBanner(IMAGE_BANNER_ALL_COLORS); String banner = printBanner("colors.gif");
for (AnsiColor color : AnsiColor.values()) {
assertThat("Banner contains BLACK", banner, if (color != AnsiColor.DEFAULT) {
containsString(getAnsiOutput(AnsiColor.BLACK))); assertThat(banner).contains(AnsiOutput.encode(color));
assertThat("Banner contains RED", banner, }
containsString(getAnsiOutput(AnsiColor.RED))); }
assertThat("Banner contains GREEN", banner,
containsString(getAnsiOutput(AnsiColor.GREEN)));
assertThat("Banner contains YELLOW", banner,
containsString(getAnsiOutput(AnsiColor.YELLOW)));
assertThat("Banner contains BLUE", banner,
containsString(getAnsiOutput(AnsiColor.BLUE)));
assertThat("Banner contains MAGENTA", banner,
containsString(getAnsiOutput(AnsiColor.MAGENTA)));
assertThat("Banner contains CYAN", banner,
containsString(getAnsiOutput(AnsiColor.CYAN)));
assertThat("Banner contains WHITE", banner,
containsString(getAnsiOutput(AnsiColor.WHITE)));
assertThat("Banner contains BRIGHT_BLACK", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_BLACK)));
assertThat("Banner contains BRIGHT_RED", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_RED)));
assertThat("Banner contains BRIGHT_GREEN", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_GREEN)));
assertThat("Banner contains BRIGHT_YELLOW", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_YELLOW)));
assertThat("Banner contains BRIGHT_BLUE", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_BLUE)));
assertThat("Banner contains BRIGHT_MAGENTA", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_MAGENTA)));
assertThat("Banner contains BRIGHT_CYAN", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_CYAN)));
assertThat("Banner contains BRIGHT_WHITE", banner,
containsString(getAnsiOutput(AnsiColor.BRIGHT_WHITE)));
} }
@Test @Test
public void renderSimpleGradient() { public void printBannerShouldRenderGradient() throws Exception {
AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER); AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER);
String banner = printBanner(IMAGE_BANNER_GRADIENT); String banner = printBanner("gradient.gif", "banner.image.width=10",
String expectedResult = "@#8&o:*. "; "banner.image.margin=0");
System.out.println(banner);
assertThat(banner, startsWith(expectedResult)); assertThat(banner).contains("@#8&o:*. ");
} }
@Test @Test
public void renderBannerWithDefaultAspectRatio() { public void printBannerShouldCalculateHeight() throws Exception {
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); String banner = printBanner("large.gif", "banner.image.width=20");
int bannerHeight = getBannerHeight(banner); assertThat(getBannerHeight(banner)).isEqualTo(10);
assertThat(bannerHeight, equalTo(2));
} }
@Test @Test
public void renderBannerWithCustomAspectRatio() { public void printBannerWhenHasHeightPropertyShouldSetHeight() throws Exception {
setAspectRatio(1.0d); String banner = printBanner("large.gif", "banner.image.width=20",
String banner = printBanner(IMAGE_BANNER_BLACK_AND_WHITE); "banner.image.height=30");
int bannerHeight = getBannerHeight(banner); assertThat(getBannerHeight(banner)).isEqualTo(30);
assertThat(bannerHeight, equalTo(4));
} }
@Test @Test
public void renderLargeBanner() { public void printBannerShouldCapWidthAndCalculateHeight() throws Exception {
String banner = printBanner(IMAGE_BANNER_LARGE); AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER);
int bannerWidth = getBannerWidth(banner); String banner = printBanner("large.gif", "banner.image.margin=0");
assertThat(getBannerWidth(banner)).isEqualTo(76);
assertThat(bannerWidth, equalTo(72)); assertThat(getBannerHeight(banner)).isEqualTo(37);
} }
@Test @Test
public void renderLargeBannerWithACustomWidth() { public void printBannerShouldPrintMargin() throws Exception {
setMaxWidth(60); AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER);
String banner = printBanner(IMAGE_BANNER_LARGE); String banner = printBanner("large.gif");
int bannerWidth = getBannerWidth(banner); String[] lines = banner.split(NEW_LINE);
for (int i = 2; i < lines.length - 1; i++) {
assertThat(bannerWidth, equalTo(60)); assertThat(lines[i]).startsWith(" @");
} }
private int getBannerHeight(String banner) {
return banner.split("\n").length;
} }
private int getBannerWidth(String banner) { @Test
String strippedBanner = banner.replaceAll("\u001B\\[.*?m", ""); public void printBannerWhenHasMarginPropertShouldPrintSizedMargin() throws Exception {
String firstLine = strippedBanner.split("\n")[0]; AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER);
return firstLine.length(); 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(" @");
} }
private static String getAnsiOutput(AnsiElement ansi) {
return "\u001B[" + ansi.toString() + "m";
} }
private void setDark(boolean dark) { private int getBannerHeight(String banner) {
properties.put("banner.image.dark", dark); return banner.split(NEW_LINE).length - 3;
} }
private void setMaxWidth(int maxWidth) { private int getBannerWidth(String banner) {
properties.put("banner.image.max-width", maxWidth); int width = 0;
for (String line : banner.split(NEW_LINE)) {
width = Math.max(width, line.length());
} }
return width;
private void setAspectRatio(double aspectRatio) {
properties.put("banner.image.aspect-ratio", aspectRatio);
} }
private String printBanner(String imagePath) { private String printBanner(String path, String... properties) {
Resource image = new ClassPathResource(imagePath); ImageBanner banner = new ImageBanner(new ClassPathResource(path, getClass()));
ImageBanner banner = new ImageBanner(image);
ConfigurableEnvironment environment = new MockEnvironment(); ConfigurableEnvironment environment = new MockEnvironment();
environment.getPropertySources().addLast( TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment,
new MapPropertySource("testConfig", properties)); properties);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
banner.printBanner(environment, getClass(), new PrintStream(out)); banner.printBanner(environment, getClass(), new PrintStream(out));
return out.toString(); return out.toString();
......
...@@ -67,6 +67,7 @@ import org.springframework.core.env.Environment; ...@@ -67,6 +67,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
...@@ -196,29 +197,26 @@ public class SpringApplicationTests { ...@@ -196,29 +197,26 @@ public class SpringApplicationTests {
} }
@Test @Test
public void textBannerTakesPrecedence() throws Exception { public void imageBannerAndTextBanner() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class); SpringApplication application = new SpringApplication(ExampleConfig.class);
BannerResourceLoaderStub resourceLoader = new BannerResourceLoaderStub(); MockResourceLoader resourceLoader = new MockResourceLoader();
resourceLoader.addResource("banner.gif", "banners/black-and-white.gif"); resourceLoader.addResource("banner.gif", "black-and-white.gif");
resourceLoader.addResource("banner.txt", "banners/foobar.txt"); resourceLoader.addResource("banner.txt", "foobar.txt");
application.setWebEnvironment(false); application.setWebEnvironment(false);
application.setResourceLoader(resourceLoader); application.setResourceLoader(resourceLoader);
application.run(); application.run();
assertThat(this.output.toString()).contains("@@@@").contains("Foo Bar");
assertThat(this.output.toString()).startsWith("Foo Bar");
} }
@Test @Test
public void imageBannerLoads() throws Exception { public void imageBannerLoads() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class); SpringApplication application = new SpringApplication(ExampleConfig.class);
BannerResourceLoaderStub resourceLoader = new BannerResourceLoaderStub(); MockResourceLoader resourceLoader = new MockResourceLoader();
resourceLoader.addResource("banner.gif", "banners/black-and-white.gif"); resourceLoader.addResource("banner.gif", "black-and-white.gif");
application.setWebEnvironment(false); application.setWebEnvironment(false);
application.setResourceLoader(resourceLoader); application.setResourceLoader(resourceLoader);
application.run(); application.run();
assertThat(this.output.toString()).contains("@@@@@@");
assertThat(this.output.toString()).startsWith("@");
} }
@Test @Test
...@@ -1120,28 +1118,23 @@ public class SpringApplicationTests { ...@@ -1120,28 +1118,23 @@ public class SpringApplicationTests {
} }
private static class BannerResourceLoaderStub extends DefaultResourceLoader { private static class MockResourceLoader implements ResourceLoader {
private Map<String, String> resources = new HashMap<String, String>(); private final Map<String, Resource> resources = new HashMap<String, Resource>();
Resource notFoundResource;
BannerResourceLoaderStub() { public void addResource(String source, String path) {
this.notFoundResource = super.getResource("classpath:foo/bar/foobar"); this.resources.put(source, new ClassPathResource(path, getClass()));
assert !this.notFoundResource.exists();
} }
public void addResource(String file, String realPath) { @Override
this.resources.put(file, realPath); public Resource getResource(String path) {
Resource resource = this.resources.get(path);
return (resource == null ? new ClassPathResource("doesnotexit") : resource);
} }
@Override @Override
public Resource getResource(String s) { public ClassLoader getClassLoader() {
if (this.resources.containsKey(s)) { return getClass().getClassLoader();
return super.getResource("classpath:" + this.resources.get(s));
}
else {
return this.notFoundResource;
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment