This commit is contained in:
commit
fde3940afb
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 |
|
@ -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<Document> 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<Document> 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<Document> 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<Document> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -145,5 +145,10 @@ public class RedisClient {
|
|||
log.error("Exception caught in flushAll", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroyInstance() {
|
||||
jedisPool = null;
|
||||
instance = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue