This commit is contained in:
ajay74984 2020-02-15 09:36:34 +01:00
commit fde3940afb
11 changed files with 319 additions and 117 deletions

View File

@ -0,0 +1,27 @@
package com.baeldung.headlessmode;
import java.awt.GraphicsEnvironment;
import javax.swing.JOptionPane;
public class FlexibleApp {
public static final int HEADLESS = 0;
public static final int HEADED = 1;
public FlexibleApp() {
if (GraphicsEnvironment.isHeadless()) {
System.out.println("Hello World");
} else {
JOptionPane.showMessageDialog(null, "Hello World");
}
}
public static int iAmFlexible() {
if (GraphicsEnvironment.isHeadless()) {
return HEADLESS;
} else {
return HEADED;
}
}
}

View File

@ -0,0 +1,87 @@
package com.baeldung.headlessmode;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.awt.Canvas;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
public class HeadlessModeUnitTest {
private static final String IN_FILE = "/product.png";
private static final String OUT_FILE = System.getProperty("java.io.tmpdir") + "/product.jpg";
private static final String FORMAT = "jpg";
@Before
public void setUpHeadlessMode() {
System.setProperty("java.awt.headless", "true");
}
@Test
public void whenJavaAwtHeadlessSetToTrue_thenIsHeadlessReturnsTrue() {
assertThat(GraphicsEnvironment.isHeadless()).isTrue();
}
@Test
public void whenHeadlessMode_thenFontsWork() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fonts[] = ge.getAvailableFontFamilyNames();
assertThat(fonts).isNotEmpty();
Font font = new Font(fonts[0], Font.BOLD, 14);
FontMetrics fm = (new Canvas()).getFontMetrics(font);
assertThat(fm.getHeight()).isGreaterThan(0);
assertThat(fm.getAscent()).isGreaterThan(0);
assertThat(fm.getDescent()).isGreaterThan(0);
}
@Test
public void whenHeadlessMode_thenImagesWork() throws IOException {
boolean result = false;
try (InputStream inStream = HeadlessModeUnitTest.class.getResourceAsStream(IN_FILE); FileOutputStream outStream = new FileOutputStream(OUT_FILE)) {
BufferedImage inputImage = ImageIO.read(inStream);
result = ImageIO.write(inputImage, FORMAT, outStream);
}
assertThat(result).isTrue();
}
@Test
public void whenHeadlessmode_thenFrameThrowsHeadlessException() {
assertThatExceptionOfType(HeadlessException.class).isThrownBy(() -> {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(120, 120);
});
}
@Test
public void whenHeadless_thenFlexibleAppAdjustsItsBehavior() {
assertThat(FlexibleApp.iAmFlexible()).isEqualTo(FlexibleApp.HEADLESS);
}
@Test
public void whenHeaded_thenFlexibleAppAdjustsItsBehavior() {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
assertThat(FlexibleApp.iAmFlexible()).isEqualTo(FlexibleApp.HEADED);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -57,11 +57,11 @@ public class AggregationLiveTest {
} }
@Test @Test
public void givenCountryCollection_whenNAFTACountriesCounted_thenThree() { public void givenCountryCollection_whenEnglishSpeakingCountriesCounted_thenNinetyOne() {
Document naftaCountries = collection.aggregate(Arrays.asList(match(Filters.eq("regionalBlocs.acronym", "NAFTA")), count())) Document englishSpeakingCountries = collection.aggregate(Arrays.asList(match(Filters.eq("languages.name", "English")), count()))
.first(); .first();
assertEquals(3, naftaCountries.get("count")); assertEquals(91, englishSpeakingCountries.get("count"));
} }
@Test @Test

View File

@ -146,4 +146,9 @@ public class RedisClient {
} }
} }
public void destroyInstance() {
jedisPool = null;
instance = null;
}
} }

View File

@ -25,13 +25,14 @@ public class NaiveApproachIntegrationTest {
s.close(); s.close();
redisServer = new RedisServer(port); redisServer = new RedisServer(port);
redisServer.start();
} }
@AfterClass @AfterClass
public static void destroy() { public static void destroy() {
if (redisServer.isActive()) if (redisServer.isActive()) {
redisServer.stop(); redisServer.stop();
redisClient.destroyInstance();
}
} }
@Before @Before

View File

@ -32,13 +32,14 @@ public class ScanStrategyIntegrationTest {
s.close(); s.close();
redisServer = new RedisServer(port); redisServer = new RedisServer(port);
redisServer.start();
} }
@AfterClass @AfterClass
public static void destroy() { public static void destroy() {
if (redisServer.isActive()) if (redisServer.isActive()) {
redisServer.stop(); redisServer.stop();
redisClient.destroyInstance();
}
} }
@Before @Before

View File

@ -0,0 +1,26 @@
package com.baeldung.beandefinitionoverrideexception;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TestConfiguration1.class, TestConfiguration2.class}, properties = {"spring.main.allow-bean-definition-overriding=true"})
public class SpringBootBeanDefinitionOverrideExceptionIntegrationTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void whenBeanOverridingAllowed_thenTestBean2OverridesTestBean1() {
Object testBean = applicationContext.getBean("testBean");
assertThat(testBean.getClass()).isEqualTo(TestConfiguration2.TestBean2.class);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.beandefinitionoverrideexception;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration1 {
class TestBean1 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Bean
public TestBean1 testBean() {
return new TestBean1();
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.beandefinitionoverrideexception;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration2 {
class TestBean2 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Bean
public TestBean2 testBean() {
return new TestBean2();
}
}

View File

@ -49,8 +49,8 @@ public class ResponseLogFilter extends ZuulFilter {
context.setResponseBody(responseData); context.setResponseBody(responseData);
} }
catch (Throwable e) { catch (Exception e) {
e.printStackTrace(); logger.error("error occurred at response log filter", e);
} }
return null; return null;