diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/headlessmode/FlexibleApp.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/headlessmode/FlexibleApp.java new file mode 100644 index 0000000000..50f33d239d --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/headlessmode/FlexibleApp.java @@ -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; + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/headlessmode/HeadlessModeUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/headlessmode/HeadlessModeUnitTest.java new file mode 100644 index 0000000000..a17c1600f3 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/headlessmode/HeadlessModeUnitTest.java @@ -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); + } + +} diff --git a/core-java-modules/core-java-lang-2/src/test/resources/product.png b/core-java-modules/core-java-lang-2/src/test/resources/product.png new file mode 100644 index 0000000000..4edd01c0a1 Binary files /dev/null and b/core-java-modules/core-java-lang-2/src/test/resources/product.png differ diff --git a/persistence-modules/java-mongodb/src/test/java/com/baeldung/aggregation/AggregationLiveTest.java b/persistence-modules/java-mongodb/src/test/java/com/baeldung/aggregation/AggregationLiveTest.java index b3f01be566..62ab13563a 100644 --- a/persistence-modules/java-mongodb/src/test/java/com/baeldung/aggregation/AggregationLiveTest.java +++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/aggregation/AggregationLiveTest.java @@ -1,111 +1,111 @@ -package com.baeldung.aggregation; - -import static com.mongodb.client.model.Aggregates.count; -import static com.mongodb.client.model.Aggregates.group; -import static com.mongodb.client.model.Aggregates.limit; -import static com.mongodb.client.model.Aggregates.match; -import static com.mongodb.client.model.Aggregates.out; -import static com.mongodb.client.model.Aggregates.project; -import static com.mongodb.client.model.Aggregates.sort; -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Arrays; - -import org.bson.Document; -import org.bson.conversions.Bson; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.Accumulators; -import com.mongodb.client.model.Filters; -import com.mongodb.client.model.Projections; -import com.mongodb.client.model.Sorts; - -public class AggregationLiveTest { - - private static final String DATABASE = "world"; - private static final String COLLECTION = "countries"; - private static final String DATASET_JSON = "/countrydata.json"; - private static MongoClient mongoClient; - private static MongoDatabase database; - private static MongoCollection collection; - - @BeforeClass - public static void setUpDB() throws IOException { - mongoClient = MongoClients.create(); - database = mongoClient.getDatabase(DATABASE); - collection = database.getCollection(COLLECTION); - - collection.drop(); - - InputStream is = AggregationLiveTest.class.getResourceAsStream(DATASET_JSON); - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - reader.lines() - .forEach(line -> collection.insertOne(Document.parse(line))); - reader.close(); - } - - @Test - public void givenCountryCollection_whenNAFTACountriesCounted_thenThree() { - Document naftaCountries = collection.aggregate(Arrays.asList(match(Filters.eq("regionalBlocs.acronym", "NAFTA")), count())) - .first(); - - assertEquals(3, naftaCountries.get("count")); - } - - @Test - public void givenCountryCollection_whenAreaSortedDescending_thenSuccess() { - - collection.aggregate(Arrays.asList(sort(Sorts.descending("area")), limit(7), out("largest_seven"))) - .toCollection(); - - MongoCollection largestSeven = database.getCollection("largest_seven"); - - assertEquals(7, largestSeven.countDocuments()); - - Document usa = largestSeven.find(Filters.eq("alpha3Code", "USA")) - .first(); - - assertNotNull(usa); - } - - @Test - public void givenCountryCollection_whenCountedRegionWise_thenMaxInAfrica() { - Document maxCountriedRegion = collection.aggregate(Arrays.asList(group("$region", Accumulators.sum("tally", 1)), sort(Sorts.descending("tally")))) - .first(); - assertTrue(maxCountriedRegion.containsValue("Africa")); - } - - @Test - public void givenCountryCollection_whenNeighborsCalculated_thenMaxIsFifteenInChina() { - Bson borderingCountriesCollection = project(Projections.fields(Projections.excludeId(), Projections.include("name"), Projections.computed("borderingCountries", Projections.computed("$size", "$borders")))); - - int maxValue = collection.aggregate(Arrays.asList(borderingCountriesCollection, group(null, Accumulators.max("max", "$borderingCountries")))) - .first() - .getInteger("max"); - - assertEquals(15, maxValue); - - Document maxNeighboredCountry = collection.aggregate(Arrays.asList(borderingCountriesCollection, match(Filters.eq("borderingCountries", maxValue)))) - .first(); - assertTrue(maxNeighboredCountry.containsValue("China")); - - } - - @AfterClass - public static void cleanUp() { - mongoClient.close(); - } - -} +package com.baeldung.aggregation; + +import static com.mongodb.client.model.Aggregates.count; +import static com.mongodb.client.model.Aggregates.group; +import static com.mongodb.client.model.Aggregates.limit; +import static com.mongodb.client.model.Aggregates.match; +import static com.mongodb.client.model.Aggregates.out; +import static com.mongodb.client.model.Aggregates.project; +import static com.mongodb.client.model.Aggregates.sort; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Arrays; + +import org.bson.Document; +import org.bson.conversions.Bson; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Accumulators; +import com.mongodb.client.model.Filters; +import com.mongodb.client.model.Projections; +import com.mongodb.client.model.Sorts; + +public class AggregationLiveTest { + + private static final String DATABASE = "world"; + private static final String COLLECTION = "countries"; + private static final String DATASET_JSON = "/countrydata.json"; + private static MongoClient mongoClient; + private static MongoDatabase database; + private static MongoCollection collection; + + @BeforeClass + public static void setUpDB() throws IOException { + mongoClient = MongoClients.create(); + database = mongoClient.getDatabase(DATABASE); + collection = database.getCollection(COLLECTION); + + collection.drop(); + + InputStream is = AggregationLiveTest.class.getResourceAsStream(DATASET_JSON); + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + reader.lines() + .forEach(line -> collection.insertOne(Document.parse(line))); + reader.close(); + } + + @Test + public void givenCountryCollection_whenEnglishSpeakingCountriesCounted_thenNinetyOne() { + Document englishSpeakingCountries = collection.aggregate(Arrays.asList(match(Filters.eq("languages.name", "English")), count())) + .first(); + + assertEquals(91, englishSpeakingCountries.get("count")); + } + + @Test + public void givenCountryCollection_whenAreaSortedDescending_thenSuccess() { + + collection.aggregate(Arrays.asList(sort(Sorts.descending("area")), limit(7), out("largest_seven"))) + .toCollection(); + + MongoCollection largestSeven = database.getCollection("largest_seven"); + + assertEquals(7, largestSeven.countDocuments()); + + Document usa = largestSeven.find(Filters.eq("alpha3Code", "USA")) + .first(); + + assertNotNull(usa); + } + + @Test + public void givenCountryCollection_whenCountedRegionWise_thenMaxInAfrica() { + Document maxCountriedRegion = collection.aggregate(Arrays.asList(group("$region", Accumulators.sum("tally", 1)), sort(Sorts.descending("tally")))) + .first(); + assertTrue(maxCountriedRegion.containsValue("Africa")); + } + + @Test + public void givenCountryCollection_whenNeighborsCalculated_thenMaxIsFifteenInChina() { + Bson borderingCountriesCollection = project(Projections.fields(Projections.excludeId(), Projections.include("name"), Projections.computed("borderingCountries", Projections.computed("$size", "$borders")))); + + int maxValue = collection.aggregate(Arrays.asList(borderingCountriesCollection, group(null, Accumulators.max("max", "$borderingCountries")))) + .first() + .getInteger("max"); + + assertEquals(15, maxValue); + + Document maxNeighboredCountry = collection.aggregate(Arrays.asList(borderingCountriesCollection, match(Filters.eq("borderingCountries", maxValue)))) + .first(); + assertTrue(maxNeighboredCountry.containsValue("China")); + + } + + @AfterClass + public static void cleanUp() { + mongoClient.close(); + } + +} diff --git a/persistence-modules/redis/src/main/java/com/baeldung/redis_scan/client/RedisClient.java b/persistence-modules/redis/src/main/java/com/baeldung/redis_scan/client/RedisClient.java index 2fe7a787e0..72ff42ff74 100644 --- a/persistence-modules/redis/src/main/java/com/baeldung/redis_scan/client/RedisClient.java +++ b/persistence-modules/redis/src/main/java/com/baeldung/redis_scan/client/RedisClient.java @@ -145,5 +145,10 @@ public class RedisClient { log.error("Exception caught in flushAll", ex); } } + + public void destroyInstance() { + jedisPool = null; + instance = null; + } } diff --git a/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/NaiveApproachIntegrationTest.java b/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/NaiveApproachIntegrationTest.java index c24b88e20c..9bf0b2b086 100644 --- a/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/NaiveApproachIntegrationTest.java +++ b/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/NaiveApproachIntegrationTest.java @@ -25,13 +25,14 @@ public class NaiveApproachIntegrationTest { s.close(); redisServer = new RedisServer(port); - redisServer.start(); } @AfterClass public static void destroy() { - if (redisServer.isActive()) + if (redisServer.isActive()) { redisServer.stop(); + redisClient.destroyInstance(); + } } @Before diff --git a/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/ScanStrategyIntegrationTest.java b/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/ScanStrategyIntegrationTest.java index 828b7a3183..9bde969b58 100644 --- a/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/ScanStrategyIntegrationTest.java +++ b/persistence-modules/redis/src/test/java/com/baeldung/redis_scan/ScanStrategyIntegrationTest.java @@ -32,13 +32,14 @@ public class ScanStrategyIntegrationTest { s.close(); redisServer = new RedisServer(port); - redisServer.start(); } @AfterClass public static void destroy() { - if (redisServer.isActive()) + if (redisServer.isActive()) { redisServer.stop(); + redisClient.destroyInstance(); + } } @Before diff --git a/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/SpringBootBeanDefinitionOverrideExceptionIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/SpringBootBeanDefinitionOverrideExceptionIntegrationTest.java new file mode 100644 index 0000000000..c900bab26c --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/SpringBootBeanDefinitionOverrideExceptionIntegrationTest.java @@ -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); + } +} diff --git a/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration1.java b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration1.java new file mode 100644 index 0000000000..a0e8ea3028 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration1.java @@ -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(); + } + +} diff --git a/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration2.java b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration2.java new file mode 100644 index 0000000000..842b84c866 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration2.java @@ -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(); + } + +} diff --git a/spring-cloud/spring-cloud-zuul/spring-zuul-post-filter/src/main/java/com/baeldung/filters/ResponseLogFilter.java b/spring-cloud/spring-cloud-zuul/spring-zuul-post-filter/src/main/java/com/baeldung/filters/ResponseLogFilter.java index e589e27d46..1fdd89c7f7 100644 --- a/spring-cloud/spring-cloud-zuul/spring-zuul-post-filter/src/main/java/com/baeldung/filters/ResponseLogFilter.java +++ b/spring-cloud/spring-cloud-zuul/spring-zuul-post-filter/src/main/java/com/baeldung/filters/ResponseLogFilter.java @@ -49,8 +49,8 @@ public class ResponseLogFilter extends ZuulFilter { context.setResponseBody(responseData); } - catch (Throwable e) { - e.printStackTrace(); + catch (Exception e) { + logger.error("error occurred at response log filter", e); } return null;