diff --git a/blade/pom.xml b/blade/pom.xml index 6d73913e25..f463c7f077 100644 --- a/blade/pom.xml +++ b/blade/pom.xml @@ -106,7 +106,7 @@ java -jar - sample-blade-app.jar + blade.jar diff --git a/libraries-3/pom.xml b/libraries-3/pom.xml index 66b48733f2..4bb1a4fba1 100644 --- a/libraries-3/pom.xml +++ b/libraries-3/pom.xml @@ -73,6 +73,28 @@ ${cache2k.version} pom + + com.squareup.moshi + moshi + ${moshi.version} + + + com.squareup.moshi + moshi-adapters + ${moshi.version} + + + + com.jcabi + jcabi-aspects + ${jcabi-aspects.version} + + + org.aspectj + aspectjrt + ${aspectjrt.version} + runtime + @@ -81,6 +103,36 @@ https://jitpack.io + + + libraries-3 + + + com.jcabi + jcabi-maven-plugin + ${jcabi-maven-plugin.version} + + + + ajc + + + + + + org.aspectj + aspectjtools + ${aspectjtools.version} + + + org.aspectj + aspectjweaver + ${aspectjweaver.version} + + + + + 1.78 @@ -93,5 +145,11 @@ 0.43 2.7.2 1.2.3.Final + 1.9.2 + 0.22.6 + 1.9.2 + 0.14.1 + 1.9.2 + 1.9.2 diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java index dc984e5f0b..cc646c9e17 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java @@ -4,38 +4,33 @@ import java.util.Objects; import org.cache2k.Cache; import org.cache2k.Cache2kBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class ProductHelper { - final Logger LOGGER = LoggerFactory.getLogger(ProductHelper.class); - private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelper() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount") .eternal(true) .entryCapacity(100) .build(); - - initDiscountCache("Sports", 20); - } - - public void initDiscountCache(String productType, Integer value) { - cachedDiscounts.put(productType, value); } public Integer getDiscount(String productType) { Integer discount = cachedDiscounts.get(productType); if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); + cacheMissCount++; + discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10; + cachedDiscounts.put(productType, discount); } return discount; } + public int getCacheMissCount() { + return cacheMissCount; + } + } diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java index 787a78cd36..7b2ac4caa1 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java @@ -1,6 +1,5 @@ package com.baeldung.cache2k; -import java.util.Objects; import java.util.concurrent.TimeUnit; import org.cache2k.Cache; @@ -14,28 +13,26 @@ public class ProductHelperUsingLoader { private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelperUsingLoader() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount-loader") - .eternal(false) .expireAfterWrite(10, TimeUnit.MILLISECONDS) .entryCapacity(100) .loader((key) -> { - LOGGER.info("Calculating discount for {}.", key); + cacheMissCount++; return "Sports".equalsIgnoreCase(key) ? 20 : 10; }) .build(); } public Integer getDiscount(String productType) { - Integer discount = cachedDiscounts.get(productType); - if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); - } - return discount; + return cachedDiscounts.get(productType); + } + + public int getCacheMissCount() { + return cacheMissCount; } } diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java index 5b9eb28c68..90c6ee3adf 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java @@ -1,6 +1,5 @@ package com.baeldung.cache2k; -import java.util.Objects; import java.util.concurrent.TimeUnit; import org.cache2k.Cache; @@ -16,14 +15,15 @@ public class ProductHelperWithEventListener { private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelperWithEventListener() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount-listener") - .eternal(false) .expireAfterWrite(10, TimeUnit.MILLISECONDS) .entryCapacity(100) .loader((key) -> { - LOGGER.info("Calculating discount for {}.", key); + cacheMissCount++; return "Sports".equalsIgnoreCase(key) ? 20 : 10; }) .addListener(new CacheEntryCreatedListener() { @@ -36,14 +36,11 @@ public class ProductHelperWithEventListener { } public Integer getDiscount(String productType) { - Integer discount = cachedDiscounts.get(productType); - if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); - } - return discount; + return cachedDiscounts.get(productType); + } + + public int getCacheMissCount() { + return cacheMissCount; } } diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java index b0bf8f90de..22b656fead 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java @@ -5,39 +5,34 @@ import java.util.concurrent.TimeUnit; import org.cache2k.Cache; import org.cache2k.Cache2kBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class ProductHelperWithExpiry { - final Logger LOGGER = LoggerFactory.getLogger(ProductHelperWithExpiry.class); - private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelperWithExpiry() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount-expiry") - .eternal(false) .expireAfterWrite(5, TimeUnit.MILLISECONDS) .entryCapacity(100) .build(); - initDiscountCache("Sports", 20); - } - - public void initDiscountCache(String productType, Integer value) { - cachedDiscounts.put(productType, value); } public Integer getDiscount(String productType) { Integer discount = cachedDiscounts.get(productType); if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); + cacheMissCount++; + discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10; + cachedDiscounts.put(productType, discount); } return discount; } + public int getCacheMissCount() { + return cacheMissCount; + } + } diff --git a/libraries-3/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java b/libraries-3/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java new file mode 100644 index 0000000000..6bd345c59c --- /dev/null +++ b/libraries-3/src/main/java/com/baeldung/jcabi/JcabiAspectJ.java @@ -0,0 +1,112 @@ +package com.baeldung.jcabi; + + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import com.jcabi.aspects.Async; +import com.jcabi.aspects.Cacheable; +import com.jcabi.aspects.LogExceptions; +import com.jcabi.aspects.Loggable; +import com.jcabi.aspects.Quietly; +import com.jcabi.aspects.RetryOnFailure; +import com.jcabi.aspects.UnitedThrow; + +public class JcabiAspectJ { + + public static void main(String[] args) { + try { + displayFactorial(10); + getFactorial(10).get(); + + String result = cacheExchangeRates(); + if (result != cacheExchangeRates()) { + System.out.println(result); + } + + divideByZero(); + } catch(Exception e) { + e.printStackTrace(); + } + + divideByZeroQuietly(); + try { + processFile(); + } catch(Exception e) { + e.printStackTrace(); + } + + } + + @Loggable + @Async + public static void displayFactorial(int number) { + long result = factorial(number); + System.out.println(result); + } + + @Loggable + @Async + public static Future getFactorial(int number) { + Future factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number)); + return factorialFuture; + } + + /** + * Finds factorial of a number + * @param number + * @return + */ + public static long factorial(int number) { + long result = 1; + for(int i=number;i>0;i--) { + result *= i; + } + return result; + } + + @Loggable + @Cacheable(lifetime = 2, unit = TimeUnit.SECONDS) + public static String cacheExchangeRates() { + String result = null; + try { + URL exchangeRateUrl = new URL("https://api.exchangeratesapi.io/latest"); + URLConnection con = exchangeRateUrl.openConnection(); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + result = in.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } + + @LogExceptions + public static void divideByZero() { + int x = 1/0; + } + + @RetryOnFailure(attempts = 2, types = {java.lang.NumberFormatException.class}) + @Quietly + public static void divideByZeroQuietly() { + int x = 1/0; + } + + @UnitedThrow(IllegalStateException.class) + public static void processFile() throws IOException, InterruptedException { + BufferedReader reader = new BufferedReader(new FileReader("baeldung.txt")); + reader.readLine(); + + Thread thread = new Thread(); + thread.wait(2000); + } + +} diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java index 69da2591dd..e9b495279a 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java @@ -6,12 +6,13 @@ import org.junit.Test; public class ProductHelperUnitTest { - ProductHelper productHelper = new ProductHelper(); - @Test - public void whenInvokedGetDiscount_thenGetItFromCache() { + public void whenInvokedGetDiscountTwice_thenGetItFromCache() { + ProductHelper productHelper = new ProductHelper(); + assertTrue(productHelper.getCacheMissCount() == 0); assertTrue(productHelper.getDiscount("Sports") == 20); - assertTrue(productHelper.getDiscount("Electronics") == 0); + assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 1); } } diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java index 2656e75cab..3ad77aa2de 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java @@ -6,12 +6,16 @@ import org.junit.Test; public class ProductHelperUsingLoaderUnitTest { - ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader(); - @Test - public void whenInvokedGetDiscount_thenPopulateCache() { + public void whenInvokedGetDiscount_thenPopulateCacheUsingLoader() { + ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader(); + assertTrue(productHelper.getCacheMissCount() == 0); + assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 1); + assertTrue(productHelper.getDiscount("Electronics") == 10); + assertTrue(productHelper.getCacheMissCount() == 2); } } diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java index 7bf08232f4..9aeb9f0552 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java @@ -6,10 +6,9 @@ import org.junit.Test; public class ProductHelperWithEventListenerUnitTest { - ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener(); - @Test public void whenEntryAddedInCache_thenEventListenerCalled() { + ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener(); assertTrue(productHelper.getDiscount("Sports") == 20); } diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java index 65feba2c70..a3303ca0aa 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java @@ -6,13 +6,17 @@ import org.junit.Test; public class ProductHelperWithExpiryUnitTest { - ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry(); - @Test - public void whenInvokedGetDiscountForExpiredProduct_thenNoDiscount() throws InterruptedException { + public void whenInvokedGetDiscountAfterExpiration_thenDiscountCalculatedAgain() throws InterruptedException { + ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry(); + assertTrue(productHelper.getCacheMissCount() == 0); assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 1); + Thread.sleep(20); - assertTrue(productHelper.getDiscount("Sports") == 0); + + assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 2); } } diff --git a/libraries-3/src/test/java/com/baeldung/moshi/AlternativeAdapterUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/AlternativeAdapterUnitTest.java new file mode 100644 index 0000000000..63d80bfe58 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/AlternativeAdapterUnitTest.java @@ -0,0 +1,105 @@ +package com.baeldung.moshi; + +import java.io.IOException; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.time.Instant; + +import com.squareup.moshi.FromJson; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.JsonQualifier; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.ToJson; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; + +public class AlternativeAdapterUnitTest { + @Test + public void whenSerializing_thenAlternativeAdapterUsed() { + Moshi moshi = new Moshi.Builder() + .add(new EpochMillisAdapter()) + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + String json = jsonAdapter.toJson(new Post("Introduction to Moshi Json", "Baeldung", Instant.now())); + System.out.println(json); + } + + @Test + public void whenDeserializing_thenAlternativeAdapterUsed() throws IOException { + Moshi moshi = new Moshi.Builder() + .add(new EpochMillisAdapter()) + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + String json = "{\"author\":\"Baeldung\",\"posted\":1582095269204,\"title\":\"Introduction to Moshi Json\"}"; + Post post = jsonAdapter.fromJson(json); + System.out.println(post); + + } + + public static class Post { + String title; + String author; + @EpochMillis Instant posted; + + public Post() { + } + + public Post(String title, String author, Instant posted) { + this.title = title; + this.author = author; + this.posted = posted; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public Instant getPosted() { + return posted; + } + + public void setPosted(Instant posted) { + this.posted = posted; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("title", title).append("author", author).append("posted", posted) + .toString(); + } + } + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) + @JsonQualifier + public @interface EpochMillis { + } + + public static class EpochMillisAdapter { + @ToJson + public Long toJson(@EpochMillis Instant input) { + return input.toEpochMilli(); + } + @FromJson + @EpochMillis + public Instant fromJson(Long input) { + return Instant.ofEpochMilli(input); + } + + } +} diff --git a/libraries-3/src/test/java/com/baeldung/moshi/ArrayUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/ArrayUnitTest.java new file mode 100644 index 0000000000..83bb2bb128 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/ArrayUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.moshi; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.List; + +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.Types; +import org.junit.Test; + +public class ArrayUnitTest { + @Test + public void whenSerializingList_thenJsonArrayProduced() { + Moshi moshi = new Moshi.Builder() + .build(); + Type type = Types.newParameterizedType(List.class, String.class); + JsonAdapter> jsonAdapter = moshi.adapter(type); + + String json = jsonAdapter.toJson(Arrays.asList("One", "Two", "Three")); + System.out.println(json); + } + + @Test + public void whenDeserializingJsonArray_thenListProduced() throws IOException { + Moshi moshi = new Moshi.Builder() + .build(); + Type type = Types.newParameterizedType(List.class, String.class); + JsonAdapter> jsonAdapter = moshi.adapter(type); + + String json = "[\"One\",\"Two\",\"Three\"]"; + List result = jsonAdapter.fromJson(json); + System.out.println(result); + } +} diff --git a/libraries-3/src/test/java/com/baeldung/moshi/ComplexAdapterUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/ComplexAdapterUnitTest.java new file mode 100644 index 0000000000..f0f8e9a95d --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/ComplexAdapterUnitTest.java @@ -0,0 +1,94 @@ +package com.baeldung.moshi; + +import java.io.IOException; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import com.squareup.moshi.FromJson; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.ToJson; +import org.junit.Test; + +public class ComplexAdapterUnitTest { + @Test + public void whenSerializing_thenCorrectJsonProduced() { + Moshi moshi = new Moshi.Builder() + .add(new JsonDateTimeAdapter()) + .build(); + JsonAdapter jsonAdapter = moshi.adapter(ZonedDateTime.class); + + String json = jsonAdapter.toJson(ZonedDateTime.now()); + System.out.println(json); + } + + @Test + public void whenDeserializing_thenCorrectJsonConsumed() throws IOException { + Moshi moshi = new Moshi.Builder() + .add(new JsonDateTimeAdapter()) + .build(); + JsonAdapter jsonAdapter = moshi.adapter(ZonedDateTime.class); + + String json = "{\"date\":\"2020-02-17\",\"time\":\"07:53:27.064\",\"timezone\":\"Europe/London\"}"; + ZonedDateTime now = jsonAdapter.fromJson(json); + System.out.println(now); + + } + + public static class JsonDateTimeAdapter { + @ToJson + public JsonDateTime toJson(ZonedDateTime input) { + String date = input.toLocalDate().toString(); + String time = input.toLocalTime().toString(); + String timezone = input.getZone().toString(); + return new JsonDateTime(date, time, timezone); + } + @FromJson + public ZonedDateTime fromJson(JsonDateTime input) { + LocalDate date = LocalDate.parse(input.getDate()); + LocalTime time = LocalTime.parse(input.getTime()); + ZoneId timezone = ZoneId.of(input.getTimezone()); + return ZonedDateTime.of(date, time, timezone); + } + } + public static class JsonDateTime { + private String date; + private String time; + private String timezone; + + public JsonDateTime() { + } + + public JsonDateTime(String date, String time, String timezone) { + this.date = date; + this.time = time; + this.timezone = timezone; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + } +} diff --git a/libraries-3/src/test/java/com/baeldung/moshi/DefaultUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/DefaultUnitTest.java new file mode 100644 index 0000000000..0b4ecc23a5 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/DefaultUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.moshi; + +import java.io.IOException; +import java.time.Instant; + +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; + +public class DefaultUnitTest { + + @Test + public void whenDeserializing_thenFieldsGetDefaultValues() throws IOException { + Moshi moshi = new Moshi.Builder() + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + String json = "{\"title\":\"My Post\"}"; + Post post = jsonAdapter.fromJson(json); + System.out.println(post); + } + public static class Post { + private String title; + private String author; + private String posted; + + public Post() { + posted = Instant.now().toString(); + } + + public Post(String title, String author, String posted) { + this.title = title; + this.author = author; + this.posted = posted; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getPosted() { + return posted; + } + + public void setPosted(String posted) { + this.posted = posted; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("title", title).append("author", author).append("posted", posted) + .toString(); + } + } +} diff --git a/libraries-3/src/test/java/com/baeldung/moshi/PrimitiveUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/PrimitiveUnitTest.java new file mode 100644 index 0000000000..e26e93ba8c --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/PrimitiveUnitTest.java @@ -0,0 +1,77 @@ +package com.baeldung.moshi; + +import java.io.IOException; + +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; + +public class PrimitiveUnitTest { + @Test + public void whenSerializing_thenCorrectJsonProduced() { + Moshi moshi = new Moshi.Builder() + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + Post post = new Post("My Post", "Baeldung", "This is my post"); + String json = jsonAdapter.toJson(post); + System.out.println(json); + } + + @Test + public void whenDeserializing_thenCorrectJsonConsumed() throws IOException { + Moshi moshi = new Moshi.Builder() + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + String json = "{\"author\":\"Baeldung\",\"text\":\"This is my post\",\"title\":\"My Post\"}"; + Post post = jsonAdapter.fromJson(json); + System.out.println(post); + } + + public static class Post { + private String title; + private String author; + private String text; + + public Post() { + } + + public Post(String title, String author, String text) { + this.title = title; + this.author = author; + this.text = text; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("title", title).append("author", author).append("text", text) + .toString(); + } + } +} diff --git a/libraries-3/src/test/java/com/baeldung/moshi/RenameUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/RenameUnitTest.java new file mode 100644 index 0000000000..2118538a19 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/RenameUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.moshi; + +import java.io.IOException; + +import com.squareup.moshi.Json; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.jupiter.api.Test; + +public class RenameUnitTest { + + @Test + public void whenSerializing_thenFieldsGetRenamed() { + Moshi moshi = new Moshi.Builder() + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + Post post = new Post("My Post", "Baeldung"); + String json = jsonAdapter.toJson(post); + System.out.println(json); + } + + @Test + public void whenSerializing_thenRenamedFieldsGetConsumed() throws IOException { + Moshi moshi = new Moshi.Builder() + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + String json = "{\"authored_by\":\"Baeldung\",\"title\":\"My Post\"}"; + Post post = jsonAdapter.fromJson(json); + System.out.println(post); + } + public static class Post { + private String title; + @Json(name = "authored_by") + private String author; + + public Post() { + } + + public Post(String title, String author) { + this.title = title; + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("title", title).append("author", author).toString(); + } + } +} diff --git a/libraries-3/src/test/java/com/baeldung/moshi/SimpleAdapterUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/SimpleAdapterUnitTest.java new file mode 100644 index 0000000000..e0be2f8a66 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/SimpleAdapterUnitTest.java @@ -0,0 +1,129 @@ +package com.baeldung.moshi; + +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.squareup.moshi.FromJson; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.ToJson; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.Test; + +public class SimpleAdapterUnitTest { + @Test + public void whenSerializing_thenAdapterUsed() { + Moshi moshi = new Moshi.Builder() + .add(new AuthorAdapter()) + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + Post post = new Post("My Post", new Author("Baeldung", "baeldung@example.com"), "This is my post"); + String json = jsonAdapter.toJson(post); + System.out.println(json); + } + + @Test + public void whenDeserializing_thenAdapterUsed() throws IOException { + Moshi moshi = new Moshi.Builder() + .add(new AuthorAdapter()) + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + String json = "{\"author\":\"Baeldung \",\"text\":\"This is my post\",\"title\":\"My Post\"}"; + Post post = jsonAdapter.fromJson(json); + System.out.println(post); + } + public static class AuthorAdapter { + private Pattern pattern = Pattern.compile("^(.*) <(.*)>$"); + @ToJson + public String toJson(Author author) { + return author.name + " <" + author.email + ">"; + } + + @FromJson + public Author fromJson(String author) { + Matcher matcher = pattern.matcher(author); + return matcher.find() ? new Author(matcher.group(1), matcher.group(2)) : null; + } + } + + public static class Author { + private String name; + private String email; + + public Author() { + } + + public Author(String name, String email) { + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("name", name).append("email", email).toString(); + } + } + public static class Post { + private String title; + private Author author; + private String text; + + public Post() { + } + + public Post(String title, Author author, String text) { + this.title = title; + this.author = author; + this.text = text; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Author getAuthor() { + return author; + } + + public void setAuthor(Author author) { + this.author = author; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("title", title).append("author", author).append("text", text) + .toString(); + } + } +} diff --git a/libraries-3/src/test/java/com/baeldung/moshi/TransientUnitTest.java b/libraries-3/src/test/java/com/baeldung/moshi/TransientUnitTest.java new file mode 100644 index 0000000000..2554e937b3 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/moshi/TransientUnitTest.java @@ -0,0 +1,66 @@ +package com.baeldung.moshi; + +import java.io.IOException; + +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.junit.jupiter.api.Test; + +public class TransientUnitTest { + + @Test + public void whenSerializing_thenTransientFieldIgnored() { + Moshi moshi = new Moshi.Builder() + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + Post post = new Post("My Post", "Baeldung"); + String json = jsonAdapter.toJson(post); + System.out.println(json); + } + + @Test + public void whenDeserializing_thenTransientFieldIgnored() throws IOException { + Moshi moshi = new Moshi.Builder() + .build(); + JsonAdapter jsonAdapter = moshi.adapter(Post.class); + + String json = "{\"authored_by\":\"Baeldung\",\"title\":\"My Post\"}"; + Post post = jsonAdapter.fromJson(json); + System.out.println(post); + } + public static class Post { + private String title; + private transient String author; + + public Post() { + } + + public Post(String title, String author) { + this.title = title; + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("title", title).append("author", author).toString(); + } + } +} diff --git a/persistence-modules/spring-hibernate-3/README.md b/persistence-modules/spring-hibernate-3/README.md index 898c6522a9..610d586f85 100644 --- a/persistence-modules/spring-hibernate-3/README.md +++ b/persistence-modules/spring-hibernate-3/README.md @@ -4,8 +4,8 @@ This module contains articles about Spring with Hibernate 3 ### Relevant Articles: -- [Hibernate 3 with Spring](http://www.baeldung.com/hibernate3-spring) -- [HibernateException: No Hibernate Session Bound to Thread in Hibernate 3](http://www.baeldung.com/no-hibernate-session-bound-to-thread-exception) +- [Hibernate 3 with Spring](https://www.baeldung.com/hibernate3-spring) +- [HibernateException: No Hibernate Session Bound to Thread in Hibernate 3](https://www.baeldung.com/no-hibernate-session-bound-to-thread-exception) ### Quick Start diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/AbstractHibernateDao.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/AbstractHibernateDao.java similarity index 97% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/AbstractHibernateDao.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/AbstractHibernateDao.java index 0f8b132891..701c6c43c7 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/AbstractHibernateDao.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/AbstractHibernateDao.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; import java.io.Serializable; import java.util.List; diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/EventDao.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/EventDao.java similarity index 70% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/EventDao.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/EventDao.java index 74da643f35..e6519d54c8 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/EventDao.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/EventDao.java @@ -1,18 +1,18 @@ -package org.baeldung.persistence.dao; - - -import org.baeldung.persistence.model.Event; -import org.springframework.stereotype.Repository; - -@Repository -public class EventDao extends AbstractHibernateDao implements IEventDao { - - public EventDao() { - super(); - - setClazz(Event.class); - } - - // API - -} +package com.baeldung.persistence.dao; + + +import com.baeldung.persistence.model.Event; +import org.springframework.stereotype.Repository; + +@Repository +public class EventDao extends AbstractHibernateDao implements IEventDao { + + public EventDao() { + super(); + + setClazz(Event.class); + } + + // API + +} diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/FooDao.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/FooDao.java similarity index 73% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/FooDao.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/FooDao.java index 23de04169e..afcd958bc2 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/FooDao.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/FooDao.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; -import org.baeldung.persistence.model.Foo; +import com.baeldung.persistence.model.Foo; import org.springframework.stereotype.Repository; @Repository diff --git a/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IEventDao.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IEventDao.java new file mode 100644 index 0000000000..da2be1ce1e --- /dev/null +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IEventDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Event; + + +public interface IEventDao extends IOperations { + // +} diff --git a/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IFooDao.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IFooDao.java new file mode 100644 index 0000000000..f79883980a --- /dev/null +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IFooDao.java @@ -0,0 +1,7 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Foo; + +public interface IFooDao extends IOperations { + // +} diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IOperations.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IOperations.java similarity index 89% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IOperations.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IOperations.java index 8c5a5e1aed..66f8008f27 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IOperations.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/dao/IOperations.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; import java.io.Serializable; import java.util.List; diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/model/Event.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/model/Event.java similarity index 89% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/model/Event.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/model/Event.java index 1d659ed75c..37a9758cd5 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/model/Event.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/model/Event.java @@ -1,45 +1,45 @@ -package org.baeldung.persistence.model; - -import java.io.Serializable; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table(name = "EVENTS") -public class Event implements Serializable { - - @Id - @GeneratedValue - private Long id; - - private String description; - - public Event() { - } - - - public Event(String description) { - this.description = description; - } - - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - +package com.baeldung.persistence.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "EVENTS") +public class Event implements Serializable { + + @Id + @GeneratedValue + private Long id; + + private String description; + + public Event() { + } + + + public Event(String description) { + this.description = description; + } + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + } \ No newline at end of file diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/model/Foo.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/model/Foo.java similarity index 97% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/model/Foo.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/model/Foo.java index 8e1dee33e8..9af3d07bed 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/model/Foo.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package com.baeldung.persistence.model; import java.io.Serializable; diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/service/EventService.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/service/EventService.java similarity index 72% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/service/EventService.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/service/EventService.java index 6171751cc5..bf9afed38b 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/service/EventService.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/service/EventService.java @@ -1,27 +1,27 @@ -package org.baeldung.persistence.service; - - -import org.baeldung.persistence.dao.IEventDao; -import org.baeldung.persistence.model.Event; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional -public class EventService { - - @Autowired - private IEventDao dao; - - public EventService() { - super(); - } - - // API - - public void create(final Event entity) { - dao.create(entity); - } - -} +package com.baeldung.persistence.service; + + +import com.baeldung.persistence.model.Event; +import com.baeldung.persistence.dao.IEventDao; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class EventService { + + @Autowired + private IEventDao dao; + + public EventService() { + super(); + } + + // API + + public void create(final Event entity) { + dao.create(entity); + } + +} diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/service/FooService.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/service/FooService.java similarity index 75% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/service/FooService.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/service/FooService.java index b07698c438..ad88062216 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/service/FooService.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/persistence/service/FooService.java @@ -1,7 +1,7 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; -import org.baeldung.persistence.dao.IFooDao; -import org.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.model.Foo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceConfig.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceConfig.java similarity index 94% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceConfig.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceConfig.java index 03b9bfac33..29472174b2 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceConfig.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import java.util.Properties; @@ -22,7 +22,7 @@ import com.google.common.base.Preconditions; @Configuration @EnableTransactionManagement @PropertySource({ "classpath:persistence-h2.properties" }) -@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" }) +@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" }) public class PersistenceConfig { @Autowired @@ -36,7 +36,7 @@ public class PersistenceConfig { public AnnotationSessionFactoryBean sessionFactory() { final AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); - sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); + sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory; diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceConfigHibernate3.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceConfigHibernate3.java similarity index 97% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceConfigHibernate3.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceConfigHibernate3.java index f38da21dc0..08ae9a76df 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceConfigHibernate3.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceConfigHibernate3.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import java.util.Properties; @@ -24,7 +24,7 @@ import com.google.common.base.Preconditions; @Configuration @EnableTransactionManagement @PropertySource({ "classpath:persistence-h2.properties" }) -@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" }) +@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" }) public class PersistenceConfigHibernate3 { @Autowired diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceXmlConfig.java b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java similarity index 82% rename from persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceXmlConfig.java rename to persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java index 5831d2745c..798fb740a7 100644 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/spring/PersistenceXmlConfig.java +++ b/persistence-modules/spring-hibernate-3/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; @@ -6,7 +6,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; // @Configuration @EnableTransactionManagement -@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" }) +@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" }) @ImportResource({ "classpath:persistenceConfig.xml" }) public class PersistenceXmlConfig { diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IEventDao.java b/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IEventDao.java deleted file mode 100644 index f7be705905..0000000000 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IEventDao.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.model.Event; - - - -public interface IEventDao extends IOperations { - // -} diff --git a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IFooDao.java b/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IFooDao.java deleted file mode 100644 index 6a3bd95d71..0000000000 --- a/persistence-modules/spring-hibernate-3/src/main/java/org/baeldung/persistence/dao/IFooDao.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.model.Foo; - -public interface IFooDao extends IOperations { - // -} diff --git a/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemo.cfg.xml b/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemo.cfg.xml index 8a710cc559..896c080983 100644 --- a/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemo.cfg.xml +++ b/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemo.cfg.xml @@ -4,6 +4,6 @@ "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> - + diff --git a/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemoPersistenceConfig.xml b/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemoPersistenceConfig.xml index 263e902e7c..5f9e8c0107 100644 --- a/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemoPersistenceConfig.xml +++ b/persistence-modules/spring-hibernate-3/src/main/resources/exceptionDemoPersistenceConfig.xml @@ -16,7 +16,7 @@ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> - + diff --git a/persistence-modules/spring-hibernate-3/src/main/resources/persistenceConfig.xml b/persistence-modules/spring-hibernate-3/src/main/resources/persistenceConfig.xml index f39817383b..03d4d022e6 100644 --- a/persistence-modules/spring-hibernate-3/src/main/resources/persistenceConfig.xml +++ b/persistence-modules/spring-hibernate-3/src/main/resources/persistenceConfig.xml @@ -9,7 +9,7 @@ - + ${hibernate.hbm2ddl.auto} diff --git a/persistence-modules/spring-hibernate-3/src/main/webapp/WEB-INF/web.xml b/persistence-modules/spring-hibernate-3/src/main/webapp/WEB-INF/web.xml index ad56bf28c7..69ce04fe06 100644 --- a/persistence-modules/spring-hibernate-3/src/main/webapp/WEB-INF/web.xml +++ b/persistence-modules/spring-hibernate-3/src/main/webapp/WEB-INF/web.xml @@ -11,7 +11,7 @@ contextConfigLocation - org.baeldung.spring + com.baeldung.spring org.springframework.web.context.ContextLoaderListener diff --git a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/SpringContextTest.java b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/SpringContextTest.java similarity index 88% rename from persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/SpringContextTest.java rename to persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/SpringContextTest.java index 956d5279a3..8b352b490d 100644 --- a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/SpringContextTest.java +++ b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/SpringContextTest.java @@ -1,6 +1,6 @@ -package org.baeldung; +package com.baeldung; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.spring.PersistenceConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; diff --git a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java similarity index 91% rename from persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java rename to persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java index b6e4a5592f..7874f7c679 100644 --- a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java +++ b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java @@ -1,10 +1,9 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; -import org.baeldung.persistence.model.Foo; -import org.baeldung.persistence.service.FooService; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Foo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; diff --git a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/HibernateExceptionScen1MainIntegrationTest.java b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/HibernateExceptionScen1MainIntegrationTest.java similarity index 90% rename from persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/HibernateExceptionScen1MainIntegrationTest.java rename to persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/HibernateExceptionScen1MainIntegrationTest.java index 08032660c0..99dc7b42be 100644 --- a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/HibernateExceptionScen1MainIntegrationTest.java +++ b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/HibernateExceptionScen1MainIntegrationTest.java @@ -1,7 +1,7 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; -import org.baeldung.persistence.model.Event; -import org.baeldung.spring.PersistenceConfigHibernate3; +import com.baeldung.persistence.model.Event; +import com.baeldung.spring.PersistenceConfigHibernate3; import org.hamcrest.core.IsInstanceOf; import org.hibernate.HibernateException; import org.junit.Ignore; diff --git a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/HibernateExceptionScen2MainIntegrationTest.java b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/HibernateExceptionScen2MainIntegrationTest.java similarity index 91% rename from persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/HibernateExceptionScen2MainIntegrationTest.java rename to persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/HibernateExceptionScen2MainIntegrationTest.java index 44cc6ca010..d3e475a244 100644 --- a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/HibernateExceptionScen2MainIntegrationTest.java +++ b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/HibernateExceptionScen2MainIntegrationTest.java @@ -1,7 +1,7 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; -import org.baeldung.persistence.model.Event; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Event; +import com.baeldung.spring.PersistenceConfig; import org.hamcrest.core.IsInstanceOf; import org.hibernate.HibernateException; import org.junit.Ignore; diff --git a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java similarity index 88% rename from persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java rename to persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java index 2b29dcb7a9..d1c5cc04e6 100644 --- a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java +++ b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java @@ -1,42 +1,42 @@ -package org.baeldung.persistence.service; - -import org.baeldung.persistence.model.Event; -import org.baeldung.spring.PersistenceXmlConfig; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.orm.hibernate3.HibernateSystemException; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class) -public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest { - - @Autowired - EventService service; - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public final void whenEntityIsCreated_thenNoExceptions() { - service.create(new Event("from Annotation Session Bean Factory")); - } - - @Test - @Ignore - public final void whenNoTransBoundToSession_thenException() { - expectedEx.expect(HibernateSystemException.class); - expectedEx.expectMessage("No Hibernate Session bound to thread, " - + "and configuration does not allow creation of " - + "non-transactional one here"); - service.create(new Event("from Annotation Session Bean Factory")); - } - -} +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Event; +import com.baeldung.spring.PersistenceXmlConfig; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.orm.hibernate3.HibernateSystemException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class) +public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest { + + @Autowired + EventService service; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + service.create(new Event("from Annotation Session Bean Factory")); + } + + @Test + @Ignore + public final void whenNoTransBoundToSession_thenException() { + expectedEx.expect(HibernateSystemException.class); + expectedEx.expectMessage("No Hibernate Session bound to thread, " + + "and configuration does not allow creation of " + + "non-transactional one here"); + service.create(new Event("from Annotation Session Bean Factory")); + } + +} diff --git a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java similarity index 86% rename from persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java rename to persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java index 1bc6c07b18..1df6aa172c 100644 --- a/persistence-modules/spring-hibernate-3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java +++ b/persistence-modules/spring-hibernate-3/src/test/java/com/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java @@ -1,39 +1,38 @@ -package org.baeldung.persistence.service; - -import org.baeldung.persistence.model.Event; -import org.hibernate.HibernateException; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.orm.hibernate3.HibernateSystemException; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" }) -public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest { - - @Autowired - EventService service; - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public final void whenEntityIsCreated_thenNoExceptions() { - service.create(new Event("from local session bean factory")); - } - - @Test - @Ignore - public final void whenNoTransBoundToSession_thenException() { - expectedEx.expect(HibernateException.class); - expectedEx.expectMessage("No Hibernate Session bound to thread, " - + "and configuration does not allow creation " - + "of non-transactional one here"); - service.create(new Event("from local session bean factory")); - } -} +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Event; +import org.hibernate.HibernateException; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" }) +public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest { + + @Autowired + EventService service; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + service.create(new Event("from local session bean factory")); + } + + @Test + @Ignore + public final void whenNoTransBoundToSession_thenException() { + expectedEx.expect(HibernateException.class); + expectedEx.expectMessage("No Hibernate Session bound to thread, " + + "and configuration does not allow creation " + + "of non-transactional one here"); + service.create(new Event("from local session bean factory")); + } +} diff --git a/persistence-modules/spring-hibernate-5/README.md b/persistence-modules/spring-hibernate-5/README.md index a74600a47f..c3c8fa7b9f 100644 --- a/persistence-modules/spring-hibernate-5/README.md +++ b/persistence-modules/spring-hibernate-5/README.md @@ -4,11 +4,11 @@ This module contains articles about Hibernate 5 with Spring. ### Relevant articles -- [Hibernate Many to Many Annotation Tutorial](http://www.baeldung.com/hibernate-many-to-many) -- [Programmatic Transactions in the Spring TestContext Framework](http://www.baeldung.com/spring-test-programmatic-transactions) -- [JPA Criteria Queries](http://www.baeldung.com/hibernate-criteria-queries) -- [Introduction to Hibernate Search](http://www.baeldung.com/hibernate-search) +- [Hibernate Many to Many Annotation Tutorial](https://www.baeldung.com/hibernate-many-to-many) +- [Programmatic Transactions in the Spring TestContext Framework](https://www.baeldung.com/spring-test-programmatic-transactions) +- [JPA Criteria Queries](https://www.baeldung.com/hibernate-criteria-queries) +- [Introduction to Hibernate Search](https://www.baeldung.com/hibernate-search) - [@DynamicUpdate with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-dynamicupdate) -- [Hibernate Second-Level Cache](http://www.baeldung.com/hibernate-second-level-cache) -- [Deleting Objects with Hibernate](http://www.baeldung.com/delete-with-hibernate) -- [Spring, Hibernate and a JNDI Datasource](http://www.baeldung.com/spring-persistence-jpa-jndi-datasource) +- [Hibernate Second-Level Cache](https://www.baeldung.com/hibernate-second-level-cache) +- [Deleting Objects with Hibernate](https://www.baeldung.com/delete-with-hibernate) +- [Spring, Hibernate and a JNDI Datasource](https://www.baeldung.com/spring-persistence-jpa-jndi-datasource) diff --git a/persistence-modules/spring-hibernate-5/src/test/java/org/baeldung/SpringContextTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/SpringContextTest.java similarity index 96% rename from persistence-modules/spring-hibernate-5/src/test/java/org/baeldung/SpringContextTest.java rename to persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/SpringContextTest.java index 6815ebf304..e19965773e 100644 --- a/persistence-modules/spring-hibernate-5/src/test/java/org/baeldung/SpringContextTest.java +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/SpringContextTest.java @@ -1,4 +1,4 @@ -package org.baeldung; +package com.baeldung; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/persistence-modules/spring-hibernate4/README.md b/persistence-modules/spring-hibernate4/README.md index c9e2b5abfc..cfa13ca3b0 100644 --- a/persistence-modules/spring-hibernate4/README.md +++ b/persistence-modules/spring-hibernate4/README.md @@ -3,13 +3,13 @@ This module contains articles about Spring with Hibernate 4 ### Relevant Articles: -- [Guide to Hibernate 4 with Spring](http://www.baeldung.com/hibernate-4-spring) -- [Hibernate Pagination](http://www.baeldung.com/hibernate-pagination) -- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort) -- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial) -- [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate) -- [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading) -- [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate) +- [Guide to Hibernate 4 with Spring](https://www.baeldung.com/hibernate-4-spring) +- [Hibernate Pagination](https://www.baeldung.com/hibernate-pagination) +- [Sorting with Hibernate](https://www.baeldung.com/hibernate-sort) +- [Stored Procedures with Hibernate](https://www.baeldung.com/stored-procedures-with-hibernate-tutorial) +- [Hibernate: save, persist, update, merge, saveOrUpdate](https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate) +- [Eager/Lazy Loading In Hibernate](https://www.baeldung.com/hibernate-lazy-eager-loading) +- [The DAO with Spring and Hibernate](https://www.baeldung.com/persistence-layer-with-spring-and-hibernate) - [Auditing with JPA, Hibernate, and Spring Data JPA](https://www.baeldung.com/database-auditing-jpa) ### Quick Start diff --git a/persistence-modules/spring-hibernate4/src/test/java/org/baeldung/SpringContextTest.java b/persistence-modules/spring-hibernate4/src/test/java/com/baeldung/SpringContextTest.java similarity index 96% rename from persistence-modules/spring-hibernate4/src/test/java/org/baeldung/SpringContextTest.java rename to persistence-modules/spring-hibernate4/src/test/java/com/baeldung/SpringContextTest.java index 6815ebf304..e19965773e 100644 --- a/persistence-modules/spring-hibernate4/src/test/java/org/baeldung/SpringContextTest.java +++ b/persistence-modules/spring-hibernate4/src/test/java/com/baeldung/SpringContextTest.java @@ -1,4 +1,4 @@ -package org.baeldung; +package com.baeldung; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/persistence-modules/spring-jpa/README.md b/persistence-modules/spring-jpa/README.md index 489d990fc3..27a2fe8368 100644 --- a/persistence-modules/spring-jpa/README.md +++ b/persistence-modules/spring-jpa/README.md @@ -4,13 +4,13 @@ ### Relevant Articles: -- [The DAO with JPA and Spring](http://www.baeldung.com/spring-dao-jpa) -- [JPA Pagination](http://www.baeldung.com/jpa-pagination) -- [Sorting with JPA](http://www.baeldung.com/jpa-sort) -- [Self-Contained Testing Using an In-Memory Database](http://www.baeldung.com/spring-jpa-test-in-memory-database) -- [A Guide to Spring AbstractRoutingDatasource](http://www.baeldung.com/spring-abstract-routing-data-source) -- [Obtaining Auto-generated Keys in Spring JDBC](http://www.baeldung.com/spring-jdbc-autogenerated-keys) -- [Transactions with Spring 4 and JPA](http://www.baeldung.com/transaction-configuration-with-jpa-and-spring) +- [The DAO with JPA and Spring](https://www.baeldung.com/spring-dao-jpa) +- [JPA Pagination](https://www.baeldung.com/jpa-pagination) +- [Sorting with JPA](https://www.baeldung.com/jpa-sort) +- [Self-Contained Testing Using an In-Memory Database](https://www.baeldung.com/spring-jpa-test-in-memory-database) +- [A Guide to Spring AbstractRoutingDatasource](https://www.baeldung.com/spring-abstract-routing-data-source) +- [Obtaining Auto-generated Keys in Spring JDBC](https://www.baeldung.com/spring-jdbc-autogenerated-keys) +- [Transactions with Spring 4 and JPA](https://www.baeldung.com/transaction-configuration-with-jpa-and-spring) - [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries) - [Many-To-Many Relationship in JPA](https://www.baeldung.com/jpa-many-to-many) - [Spring Persistence (Hibernate and JPA) with a JNDI datasource](https://www.baeldung.com/spring-persistence-hibernate-and-jpa-with-a-jndi-datasource/) diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/MyUtilityRepository.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/MyUtilityRepository.java similarity index 90% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/MyUtilityRepository.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/MyUtilityRepository.java index 5fe54b80d9..a23e9d9fe4 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/MyUtilityRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/MyUtilityRepository.java @@ -1,4 +1,4 @@ -package org.baeldung.annotations; +package com.baeldung.annotations; import java.io.Serializable; import java.util.Optional; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/Person.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/Person.java similarity index 93% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/Person.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/Person.java index 309a4f43e1..b881535e05 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/Person.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/Person.java @@ -1,4 +1,4 @@ -package org.baeldung.annotations; +package com.baeldung.annotations; import java.util.Date; @@ -8,7 +8,7 @@ import javax.persistence.NamedStoredProcedureQuery; import javax.persistence.ParameterMode; import javax.persistence.StoredProcedureParameter; -import org.baeldung.persistence.multiple.model.user.User; +import com.baeldung.persistence.multiple.model.user.User; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.Id; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/PersonRepository.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/PersonRepository.java similarity index 96% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/PersonRepository.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/PersonRepository.java index 58558860ff..77b7067f11 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/annotations/PersonRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/annotations/PersonRepository.java @@ -1,4 +1,4 @@ -package org.baeldung.annotations; +package com.baeldung.annotations; import javax.persistence.LockModeType; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/PersistenceJPAConfig.java similarity index 94% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/config/PersistenceJPAConfig.java index ec0d4bca3c..4eda4d9274 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/PersistenceJPAConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.config; +package com.baeldung.config; import java.util.Properties; @@ -25,8 +25,8 @@ import com.google.common.base.Preconditions; @Configuration @EnableTransactionManagement @PropertySource({ "classpath:persistence-h2.properties" }) -@ComponentScan({ "org.baeldung.persistence" }) -@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") +@ComponentScan({ "com.baeldung.persistence" }) +@EnableJpaRepositories(basePackages = "com.baeldung.persistence.dao") public class PersistenceJPAConfig { @Autowired @@ -42,7 +42,7 @@ public class PersistenceJPAConfig { public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); + em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfigXml.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/PersistenceJPAConfigXml.java similarity index 84% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfigXml.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/config/PersistenceJPAConfigXml.java index 3df36e62ee..95224a4662 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfigXml.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/PersistenceJPAConfigXml.java @@ -1,4 +1,4 @@ -package org.baeldung.config; +package com.baeldung.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; @@ -6,7 +6,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; // @Configuration @EnableTransactionManagement -@ComponentScan({ "org.baeldung.persistence" }) +@ComponentScan({ "com.baeldung.persistence" }) @ImportResource({ "classpath:jpaConfig.xml" }) public class PersistenceJPAConfigXml { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/SpringWebConfig.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/SpringWebConfig.java similarity index 92% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/config/SpringWebConfig.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/config/SpringWebConfig.java index 6afb271b10..475970d1f0 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/SpringWebConfig.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/SpringWebConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.config; +package com.baeldung.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -10,7 +10,7 @@ import org.springframework.web.servlet.view.JstlView; @EnableWebMvc @Configuration -@ComponentScan({ "org.baeldung.web" }) +@ComponentScan({ "com.baeldung.web" }) public class SpringWebConfig extends WebMvcConfigurerAdapter { @Bean diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/StudentJpaConfig.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/StudentJpaConfig.java similarity index 94% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/config/StudentJpaConfig.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/config/StudentJpaConfig.java index 17047cbab2..08f37ea806 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/StudentJpaConfig.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/StudentJpaConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.config; +package com.baeldung.config; import java.util.Properties; @@ -18,7 +18,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration -@EnableJpaRepositories(basePackages = "org.baeldung.inmemory.persistence.dao") +@EnableJpaRepositories(basePackages = "com.baeldung.inmemory.persistence.dao") @PropertySource("persistence-student.properties") @EnableTransactionManagement public class StudentJpaConfig { @@ -41,7 +41,7 @@ public class StudentJpaConfig { public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" }); + em.setPackagesToScan(new String[] { "com.baeldung.inmemory.persistence.model" }); em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); em.setJpaProperties(additionalProperties()); return em; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/WebInitializer.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/WebInitializer.java similarity index 95% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/config/WebInitializer.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/config/WebInitializer.java index cf6e69eb39..be81cca76b 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/WebInitializer.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/config/WebInitializer.java @@ -1,4 +1,4 @@ -package org.baeldung.config; +package com.baeldung.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDao.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDao.java similarity index 95% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDao.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDao.java index 9e19cf4ed9..e8117ba4fa 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDao.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDao.java @@ -1,4 +1,4 @@ -package org.baeldung.dsrouting; +package com.baeldung.dsrouting; import javax.sql.DataSource; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDataSourceRouter.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDataSourceRouter.java similarity index 91% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDataSourceRouter.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDataSourceRouter.java index 997e461cde..a9f5d83b55 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDataSourceRouter.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDataSourceRouter.java @@ -1,4 +1,4 @@ -package org.baeldung.dsrouting; +package com.baeldung.dsrouting; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDatabase.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDatabase.java similarity index 64% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDatabase.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDatabase.java index 619b8707d8..d8d178a4d5 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDatabase.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDatabase.java @@ -1,4 +1,4 @@ -package org.baeldung.dsrouting; +package com.baeldung.dsrouting; public enum ClientDatabase { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDatabaseContextHolder.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDatabaseContextHolder.java similarity index 95% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDatabaseContextHolder.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDatabaseContextHolder.java index c08559e877..ae5d16cad2 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientDatabaseContextHolder.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientDatabaseContextHolder.java @@ -1,4 +1,4 @@ -package org.baeldung.dsrouting; +package com.baeldung.dsrouting; import org.springframework.util.Assert; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientService.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientService.java similarity index 94% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientService.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientService.java index 4b63c6333c..59b3d1660a 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/dsrouting/ClientService.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/dsrouting/ClientService.java @@ -1,4 +1,4 @@ -package org.baeldung.dsrouting; +package com.baeldung.dsrouting; /** * Service layer code for datasource routing example. Here, the service methods are responsible diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/ManyStudentRepository.java similarity index 68% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/ManyStudentRepository.java index a03b2950a0..a2aa0c5780 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/ManyStudentRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.inmemory.persistence.dao; +package com.baeldung.inmemory.persistence.dao; -import org.baeldung.inmemory.persistence.model.ManyStudent; +import com.baeldung.inmemory.persistence.model.ManyStudent; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/ManyTagRepository.java similarity index 57% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/ManyTagRepository.java index b7d991de32..63337f3cb9 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/ManyTagRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.inmemory.persistence.dao; +package com.baeldung.inmemory.persistence.dao; -import org.baeldung.inmemory.persistence.model.ManyTag; +import com.baeldung.inmemory.persistence.model.ManyTag; import org.springframework.data.jpa.repository.JpaRepository; public interface ManyTagRepository extends JpaRepository { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/StudentRepository.java similarity index 90% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/StudentRepository.java index ffe1a68558..8ac91fbf0c 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/dao/StudentRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.inmemory.persistence.dao; +package com.baeldung.inmemory.persistence.dao; -import org.baeldung.inmemory.persistence.model.Student; +import com.baeldung.inmemory.persistence.model.Student; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/KVTag.java similarity index 91% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/KVTag.java index 1522744116..1fc186f4ce 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/KVTag.java @@ -1,4 +1,4 @@ -package org.baeldung.inmemory.persistence.model; +package com.baeldung.inmemory.persistence.model; import javax.persistence.Embeddable; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/LocationTag.java similarity index 92% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/LocationTag.java index 3acdbbe6fe..b12ad9fbd1 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/LocationTag.java @@ -1,4 +1,4 @@ -package org.baeldung.inmemory.persistence.model; +package com.baeldung.inmemory.persistence.model; import javax.persistence.Embeddable; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/ManyStudent.java similarity index 94% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/ManyStudent.java index 8343edc9cd..190740d582 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/ManyStudent.java @@ -1,4 +1,4 @@ -package org.baeldung.inmemory.persistence.model; +package com.baeldung.inmemory.persistence.model; import javax.persistence.*; import java.util.HashSet; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/ManyTag.java similarity index 93% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/ManyTag.java index e820506544..5af898f7df 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/ManyTag.java @@ -1,4 +1,4 @@ -package org.baeldung.inmemory.persistence.model; +package com.baeldung.inmemory.persistence.model; import javax.persistence.*; import java.util.HashSet; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/SkillTag.java similarity index 90% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/SkillTag.java index 490ee0a18e..738b5d0b36 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/SkillTag.java @@ -1,4 +1,4 @@ -package org.baeldung.inmemory.persistence.model; +package com.baeldung.inmemory.persistence.model; import javax.persistence.Embeddable; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/Student.java similarity index 96% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/Student.java index 07aa3ef9ef..f0e824e165 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/inmemory/persistence/model/Student.java @@ -1,4 +1,4 @@ -package org.baeldung.inmemory.persistence.model; +package com.baeldung.inmemory.persistence.model; import java.util.ArrayList; import java.util.List; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/AbstractJpaDAO.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/AbstractJpaDAO.java similarity index 96% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/AbstractJpaDAO.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/AbstractJpaDAO.java index 13007d9fcc..b4827cc7e1 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/AbstractJpaDAO.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/AbstractJpaDAO.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; import java.io.Serializable; import java.util.List; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepository.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepository.java similarity index 76% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepository.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepository.java index 114cf48c7c..48620f4ff1 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; -import org.baeldung.persistence.model.Book; +import com.baeldung.persistence.model.Book; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryCustom.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepositoryCustom.java similarity index 65% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryCustom.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepositoryCustom.java index b939907572..1b46d0ecc3 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryCustom.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepositoryCustom.java @@ -1,8 +1,8 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; import java.util.List; -import org.baeldung.persistence.model.Book; +import com.baeldung.persistence.model.Book; public interface BookRepositoryCustom { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryImpl.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepositoryImpl.java similarity index 93% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryImpl.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepositoryImpl.java index 6e9aa998d7..241a4c05aa 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryImpl.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookRepositoryImpl.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; import java.util.ArrayList; import java.util.List; @@ -10,7 +10,7 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; -import org.baeldung.persistence.model.Book; +import com.baeldung.persistence.model.Book; import org.springframework.stereotype.Repository; @Repository diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookService.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookService.java similarity index 71% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookService.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookService.java index 88b769e9bf..f02f9daa11 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookService.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookService.java @@ -1,12 +1,12 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; -import static org.baeldung.persistence.dao.BookSpecifications.hasAuthor; -import static org.baeldung.persistence.dao.BookSpecifications.titleContains; +import static com.baeldung.persistence.dao.BookSpecifications.hasAuthor; +import static com.baeldung.persistence.dao.BookSpecifications.titleContains; import static org.springframework.data.jpa.domain.Specifications.where; import java.util.List; -import org.baeldung.persistence.model.Book; +import com.baeldung.persistence.model.Book; import org.springframework.stereotype.Service; @Service diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookSpecifications.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookSpecifications.java similarity index 82% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookSpecifications.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookSpecifications.java index ed9540060d..16646a5b4b 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookSpecifications.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/BookSpecifications.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; -import org.baeldung.persistence.model.Book; +import com.baeldung.persistence.model.Book; import org.springframework.data.jpa.domain.Specification; public class BookSpecifications { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/FooDao.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/FooDao.java similarity index 73% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/FooDao.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/FooDao.java index 77978c5cf2..a4ed7d97b4 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/FooDao.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/FooDao.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; -import org.baeldung.persistence.model.Foo; +import com.baeldung.persistence.model.Foo; import org.springframework.stereotype.Repository; @Repository diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/IFooDao.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/IFooDao.java similarity index 74% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/IFooDao.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/IFooDao.java index f5799522e2..8a4877aa79 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/IFooDao.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/dao/IFooDao.java @@ -1,8 +1,8 @@ -package org.baeldung.persistence.dao; +package com.baeldung.persistence.dao; import java.util.List; -import org.baeldung.persistence.model.Foo; +import com.baeldung.persistence.model.Foo; public interface IFooDao { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Bar.java similarity index 98% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Bar.java index b602e57562..5a88ecc6cf 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Bar.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package com.baeldung.persistence.model; import java.io.Serializable; import java.util.List; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Book.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Book.java similarity index 92% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Book.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Book.java index 754bd179d1..507043dd56 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Book.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Book.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package com.baeldung.persistence.model; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Foo.java similarity index 98% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Foo.java index 5294860311..7897bba4e4 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/model/Foo.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package com.baeldung.persistence.model; import org.hibernate.annotations.CacheConcurrencyStrategy; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/multiple/model/user/Possession.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/multiple/model/user/Possession.java similarity index 96% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/multiple/model/user/Possession.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/multiple/model/user/Possession.java index 7aff5f51d0..6ad6b8e603 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/multiple/model/user/Possession.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/multiple/model/user/Possession.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.multiple.model.user; +package com.baeldung.persistence.multiple.model.user; import javax.persistence.*; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/multiple/model/user/User.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/multiple/model/user/User.java similarity index 96% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/multiple/model/user/User.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/multiple/model/user/User.java index 6fb9de4a47..3ebb1e0156 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/multiple/model/user/User.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/multiple/model/user/User.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.multiple.model.user; +package com.baeldung.persistence.multiple.model.user; import javax.persistence.*; import java.util.List; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/service/FooService.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/service/FooService.java similarity index 81% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/service/FooService.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/service/FooService.java index 6d1bb0adbe..ace36dd03b 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/service/FooService.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/persistence/service/FooService.java @@ -1,9 +1,9 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; import java.util.List; -import org.baeldung.persistence.dao.IFooDao; -import org.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.model.Foo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/sqlfiles/Country.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/sqlfiles/Country.java similarity index 95% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/sqlfiles/Country.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/sqlfiles/Country.java index 922f55cbf6..f2be33adfd 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/sqlfiles/Country.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/sqlfiles/Country.java @@ -1,4 +1,4 @@ -package org.baeldung.sqlfiles; +package com.baeldung.sqlfiles; import static javax.persistence.GenerationType.IDENTITY; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/web/MainController.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/web/MainController.java similarity index 85% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/web/MainController.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/web/MainController.java index 6900482de8..c0d4743ae7 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/web/MainController.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/web/MainController.java @@ -1,6 +1,6 @@ -package org.baeldung.web; +package com.baeldung.web; -import org.baeldung.persistence.service.FooService; +import com.baeldung.persistence.service.FooService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; diff --git a/persistence-modules/spring-jpa/src/main/resources/persistence.xml b/persistence-modules/spring-jpa/src/main/resources/persistence.xml index 6304fa0a65..57687c306d 100644 --- a/persistence-modules/spring-jpa/src/main/resources/persistence.xml +++ b/persistence-modules/spring-jpa/src/main/resources/persistence.xml @@ -11,7 +11,7 @@ - + + + org.springframework.boot + spring-boot-starter-test + test + + + diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Bike.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Bike.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Bike.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Bike.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Biker.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Biker.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Biker.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Biker.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Car.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Car.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Car.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Car.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarMechanic.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/CarMechanic.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarMechanic.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/CarMechanic.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarUtility.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/CarUtility.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarUtility.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/CarUtility.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomException.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/CustomException.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomException.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/CustomException.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Driver.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Driver.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Driver.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Driver.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Engine.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Engine.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Engine.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Engine.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/PerformanceAspect.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/PerformanceAspect.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/PerformanceAspect.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/PerformanceAspect.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Vehicle.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Vehicle.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/Vehicle.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/Vehicle.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleController.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleController.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleController.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleController.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRepository.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleRepository.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRepository.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleRepository.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRestController.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleRestController.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRestController.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleRestController.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleService.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleService.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleService.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/VehicleService.java diff --git a/spring-boot-modules/spring-boot-mvc/README.md b/spring-boot-modules/spring-boot-mvc/README.md index a83ea3ee25..b4f87d1d40 100644 --- a/spring-boot-modules/spring-boot-mvc/README.md +++ b/spring-boot-modules/spring-boot-mvc/README.md @@ -6,16 +6,8 @@ This module contains articles about Spring Web MVC in Spring Boot projects. - [Guide to the Favicon in Spring Boot](https://www.baeldung.com/spring-boot-favicon) - [Custom Validation MessageSource in Spring Boot](https://www.baeldung.com/spring-custom-validation-message-source) -- [Spring Boot Annotations](https://www.baeldung.com/spring-boot-annotations) -- [Spring Scheduling Annotations](https://www.baeldung.com/spring-scheduling-annotations) -- [Spring Web Annotations](https://www.baeldung.com/spring-mvc-annotations) -- [Spring Core Annotations](https://www.baeldung.com/spring-core-annotations) - [Display RSS Feed with Spring MVC](https://www.baeldung.com/spring-mvc-rss-feed) - [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao) -- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache) - [Setting Up Swagger 2 with a Spring REST API](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) -- [Conditionally Enable Scheduled Jobs in Spring](https://www.baeldung.com/spring-scheduled-enabled-conditionally) -- [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js) - [Using Spring ResponseEntity to Manipulate the HTTP Response](https://www.baeldung.com/spring-response-entity) -- [Spring Bean Annotations](https://www.baeldung.com/spring-bean-annotations) - More articles: [[next -->]](/spring-boot-mvc-2) diff --git a/spring-boot-rest/pom.xml b/spring-boot-rest/pom.xml index 2483aab6be..10dacf99e8 100644 --- a/spring-boot-rest/pom.xml +++ b/spring-boot-rest/pom.xml @@ -95,7 +95,6 @@ 27.0.1-jre 1.4.11.1 2.3.5 - 2.1.9.RELEASE diff --git a/spring-boot-rest/src/main/java/com/baeldung/persistence/config/CustomH2Dialect.java b/spring-boot-rest/src/main/java/com/baeldung/persistence/config/CustomH2Dialect.java new file mode 100644 index 0000000000..0108f92b2c --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/persistence/config/CustomH2Dialect.java @@ -0,0 +1,26 @@ +package com.baeldung.persistence.config; + +import org.hibernate.dialect.H2Dialect; + +/** + * Since H2 1.4.200. the behavior of the drop table commands has changed. + * Tables are not dropped in a correct order. + * Until this is properly fixed directly in Hibernate project, + * let's use this custom H2Dialect class to solve this issue. + * + * @see https://hibernate.atlassian.net/browse/HHH-13711 + * @see https://github.com/hibernate/hibernate-orm/pull/3093 + */ +public class CustomH2Dialect extends H2Dialect { + + @Override + public boolean dropConstraints() { + return true; + } + + @Override + public boolean supportsIfExistsAfterAlterTable() { + return true; + } + +} diff --git a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java index 10da2e10f0..06b2485c7b 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java +++ b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Customer.java @@ -1,14 +1,13 @@ package com.baeldung.persistence.model; -import java.util.Map; - -import org.springframework.hateoas.ResourceSupport; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; +import org.springframework.hateoas.RepresentationModel; + +import java.util.Map; @JsonInclude(Include.NON_NULL) -public class Customer extends ResourceSupport { +public class Customer extends RepresentationModel { private String customerId; private String customerName; private String companyName; diff --git a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java index 7aea9bce5c..b5e1a2cee2 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java +++ b/spring-boot-rest/src/main/java/com/baeldung/persistence/model/Order.java @@ -1,8 +1,8 @@ package com.baeldung.persistence.model; -import org.springframework.hateoas.ResourceSupport; +import org.springframework.hateoas.RepresentationModel; -public class Order extends ResourceSupport { +public class Order extends RepresentationModel { private String orderId; private double price; private int quantity; diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java b/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java index 91aa9f2144..2b7dc1eee1 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java +++ b/spring-boot-rest/src/main/java/com/baeldung/web/controller/CustomerController.java @@ -1,13 +1,13 @@ package com.baeldung.web.controller; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.Link; -import org.springframework.hateoas.Resources; +import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.config.EnableHypermediaSupport; import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; import org.springframework.web.bind.annotation.GetMapping; @@ -41,7 +41,7 @@ public class CustomerController { } @GetMapping(value = "/{customerId}/orders", produces = { "application/hal+json" }) - public Resources getOrdersForCustomer(@PathVariable final String customerId) { + public CollectionModel getOrdersForCustomer(@PathVariable final String customerId) { final List orders = orderService.getAllOrdersForCustomer(customerId); for (final Order order : orders) { final Link selfLink = linkTo( @@ -50,12 +50,12 @@ public class CustomerController { } Link link = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withSelfRel(); - Resources result = new Resources<>(orders, link); + CollectionModel result = new CollectionModel<>(orders, link); return result; } @GetMapping(produces = { "application/hal+json" }) - public Resources getAllCustomers() { + public CollectionModel getAllCustomers() { final List allCustomers = customerService.allCustomers(); for (final Customer customer : allCustomers) { @@ -72,7 +72,7 @@ public class CustomerController { } Link link = linkTo(CustomerController.class).withSelfRel(); - Resources result = new Resources<>(allCustomers, link); + CollectionModel result = new CollectionModel<>(allCustomers, link); return result; } diff --git a/spring-boot-rest/src/main/resources/persistence-h2.properties b/spring-boot-rest/src/main/resources/persistence-h2.properties index 839a466533..efbf3f7db7 100644 --- a/spring-boot-rest/src/main/resources/persistence-h2.properties +++ b/spring-boot-rest/src/main/resources/persistence-h2.properties @@ -17,6 +17,6 @@ jdbc.user=sa jdbc.pass= # hibernate.X -hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.dialect=com.baeldung.persistence.config.CustomH2Dialect hibernate.show_sql=false hibernate.hbm2ddl.auto=create-drop diff --git a/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java b/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java index 23b5d60b6b..0f89e0a9de 100644 --- a/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java +++ b/spring-boot-rest/src/test/java/com/baeldung/test/JacksonMarshaller.java @@ -60,7 +60,7 @@ public final class JacksonMarshaller implements IMarshaller { List entities = null; try { if (clazz.equals(Foo.class)) { - entities = objectMapper.readValue(resourcesAsString, new TypeReference>() { + entities = objectMapper.readValue(resourcesAsString, new TypeReference>() { // ... }); } else { diff --git a/spring-caching/README.md b/spring-caching/README.md index 14dfe94041..3efbfe3eaa 100644 --- a/spring-caching/README.md +++ b/spring-caching/README.md @@ -2,3 +2,4 @@ - [Introduction To Ehcache](http://www.baeldung.com/ehcache) - [A Guide To Caching in Spring](http://www.baeldung.com/spring-cache-tutorial) - [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator) +- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache) diff --git a/spring-ejb/ejb-beans/pom.xml b/spring-ejb/ejb-beans/pom.xml index b8978a0cc3..eecf8c1d23 100644 --- a/spring-ejb/ejb-beans/pom.xml +++ b/spring-ejb/ejb-beans/pom.xml @@ -37,6 +37,42 @@ tomee-embedded ${tomee-embedded.version} + + org.springframework + spring-context + ${springframework.version} + + + + javax.ejb + javax.ejb-api + ${javax.ejb-api.version} + provided + + + + org.springframework + spring-jms + ${springframework.version} + + + commons-logging + commons-logging + + + + + + org.apache.activemq + activemq-broker + ${activemq.broker.version} + + + org.apache.activemq.tooling + activemq-junit + ${activemq.junit.version} + test + org.jboss.arquillian.junit arquillian-junit-container @@ -81,6 +117,10 @@ 1.7.5 3.1.2 1.0.0.CR4 + 3.2 + 5.2.3.RELEASE + 5.10.2 + 5.13.1 diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/messagedriven/RecieverMDB.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/messagedriven/RecieverMDB.java new file mode 100644 index 0000000000..7a6b750ce5 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/messagedriven/RecieverMDB.java @@ -0,0 +1,63 @@ +package com.baeldung.ejb.spring.comparison.ejb.messagedriven; + +import javax.annotation.Resource; +import javax.ejb.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; + +@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destination", propertyValue = "myQueue"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) +public class RecieverMDB implements MessageListener { + + @Resource + private ConnectionFactory connectionFactory; + + @Resource(name = "ackQueue") + private Queue ackQueue; + + public void onMessage(Message message) { + try { + + TextMessage textMessage = (TextMessage) message; + String producerPing = textMessage.getText(); + + if (producerPing.equals("marco")) { + acknowledge("polo"); + } + } catch (JMSException e) { + throw new IllegalStateException(e); + } + } + + private void acknowledge(String text) throws JMSException { + + Connection connection = null; + Session session = null; + + try { + connection = connectionFactory.createConnection(); + connection.start(); + + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageProducer ackSender = session.createProducer(ackQueue); + ackSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + + TextMessage message = session.createTextMessage(text); + + ackSender.send(message); + } finally { + session.close(); + connection.close(); + } + } + +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/singleton/CounterEJB.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/singleton/CounterEJB.java new file mode 100644 index 0000000000..b64005aed8 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/singleton/CounterEJB.java @@ -0,0 +1,14 @@ +package com.baeldung.ejb.spring.comparison.ejb.singleton; + +import javax.ejb.Singleton; + +@Singleton +public class CounterEJB implements CounterEJBRemote { + + private int count = 1; + + public int count() { + return count++; + } + +} \ No newline at end of file diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/singleton/CounterEJBRemote.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/singleton/CounterEJBRemote.java new file mode 100644 index 0000000000..ec3b9e9f6b --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/singleton/CounterEJBRemote.java @@ -0,0 +1,8 @@ +package com.baeldung.ejb.spring.comparison.ejb.singleton; + +import javax.ejb.Remote; + +@Remote +public interface CounterEJBRemote { + int count(); +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateful/ShoppingCartEJB.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateful/ShoppingCartEJB.java new file mode 100644 index 0000000000..5bdccea994 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateful/ShoppingCartEJB.java @@ -0,0 +1,25 @@ +package com.baeldung.ejb.spring.comparison.ejb.stateful; + +import java.util.ArrayList; +import java.util.List; + +import javax.ejb.Stateful; + +@Stateful +public class ShoppingCartEJB implements ShoppingCartEJBRemote { + + private List shoppingCart; + + public ShoppingCartEJB() { + shoppingCart = new ArrayList(); + } + + public void addItem(String item) { + shoppingCart.add(item); + } + + public List getItems() { + return shoppingCart; + } + +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateful/ShoppingCartEJBRemote.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateful/ShoppingCartEJBRemote.java new file mode 100644 index 0000000000..a8d7a15d25 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateful/ShoppingCartEJBRemote.java @@ -0,0 +1,13 @@ +package com.baeldung.ejb.spring.comparison.ejb.stateful; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface ShoppingCartEJBRemote { + + void addItem(String item); + + List getItems(); +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateless/FinderEJB.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateless/FinderEJB.java new file mode 100644 index 0000000000..fb45e4e7ce --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateless/FinderEJB.java @@ -0,0 +1,25 @@ +package com.baeldung.ejb.spring.comparison.ejb.stateless; + +import java.util.HashMap; +import java.util.Map; + +import javax.ejb.Stateless; + +@Stateless +public class FinderEJB implements FinderEJBRemote { + + private Map alphabet; + + public FinderEJB() { + alphabet = new HashMap(); + alphabet.put("A", "Apple"); + alphabet.put("B", "Ball"); + alphabet.put("C", "Cat"); + alphabet.put("D", "Dog"); + } + + public String search(String keyword) { + return alphabet.get(keyword); + } + +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateless/FinderEJBRemote.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateless/FinderEJBRemote.java new file mode 100644 index 0000000000..36b4c9ae04 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/ejb/stateless/FinderEJBRemote.java @@ -0,0 +1,9 @@ +package com.baeldung.ejb.spring.comparison.ejb.stateless; + +import javax.ejb.Remote; + +@Remote +public interface FinderEJBRemote { + + String search(String keyword); +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/config/ApplicationConfig.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/config/ApplicationConfig.java new file mode 100644 index 0000000000..6ec14dc098 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/config/ApplicationConfig.java @@ -0,0 +1,36 @@ +package com.baeldung.ejb.spring.comparison.spring.config; + +import javax.jms.ConnectionFactory; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.annotation.EnableJms; +import org.springframework.jms.config.DefaultJmsListenerContainerFactory; +import org.springframework.jms.core.JmsTemplate; + +@Configuration +@ComponentScan(basePackages = "com.baeldung.ejb.spring.comparison.spring") +@EnableJms +public class ApplicationConfig { + + @Bean + public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() { + DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory()); + return factory; + } + + @Bean + public ConnectionFactory connectionFactory() { + return new ActiveMQConnectionFactory("tcp://localhost:61616"); + } + + @Bean + public JmsTemplate jmsTemplate() { + JmsTemplate template = new JmsTemplate(connectionFactory()); + template.setConnectionFactory(connectionFactory()); + return template; + } +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/messagedriven/Producer.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/messagedriven/Producer.java new file mode 100644 index 0000000000..a60dc4db8e --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/messagedriven/Producer.java @@ -0,0 +1,19 @@ +package com.baeldung.ejb.spring.comparison.spring.messagedriven; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.stereotype.Component; + +@Component +public class Producer { + @Autowired + private JmsTemplate jmsTemplate; + + public void sendMessageToDefaultDestination(final String message) { + jmsTemplate.convertAndSend("myQueue", message); + } + + public String receiveAck() { + return (String) jmsTemplate.receiveAndConvert("ackQueue"); + } +} \ No newline at end of file diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/messagedriven/Receiver.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/messagedriven/Receiver.java new file mode 100644 index 0000000000..a3b36dd587 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/messagedriven/Receiver.java @@ -0,0 +1,22 @@ +package com.baeldung.ejb.spring.comparison.spring.messagedriven; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.annotation.JmsListener; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.stereotype.Component; + +@Component +public class Receiver { + @Autowired + private JmsTemplate jmsTemplate; + + @JmsListener(destination = "myQueue") + public void receiveMessage(String msg) { + sendAck(); + } + + private void sendAck() { + jmsTemplate.convertAndSend("ackQueue", "polo"); + } + +} \ No newline at end of file diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/singleton/CounterBean.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/singleton/CounterBean.java new file mode 100644 index 0000000000..6456cfc007 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/singleton/CounterBean.java @@ -0,0 +1,12 @@ +package com.baeldung.ejb.spring.comparison.spring.singleton; + +import org.springframework.stereotype.Component; + +@Component +public class CounterBean { + private int count = 1; + + public int count() { + return count++; + } +} diff --git a/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/stateful/ShoppingCartBean.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/stateful/ShoppingCartBean.java new file mode 100644 index 0000000000..9286ff9950 --- /dev/null +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/ejb/spring/comparison/spring/stateful/ShoppingCartBean.java @@ -0,0 +1,28 @@ +package com.baeldung.ejb.spring.comparison.spring.stateful; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class ShoppingCartBean { + + private List shoppingCart; + + public ShoppingCartBean() { + shoppingCart = new ArrayList(); + } + + public void addItem(String item) { + shoppingCart.add(item); + } + + public List getItems() { + return shoppingCart; + } + +} diff --git a/spring-ejb/ejb-beans/src/test/java/com/baeldung/ejb/spring/comparison/ejb/EJBUnitTest.java b/spring-ejb/ejb-beans/src/test/java/com/baeldung/ejb/spring/comparison/ejb/EJBUnitTest.java new file mode 100644 index 0000000000..1f0f1bf8a4 --- /dev/null +++ b/spring-ejb/ejb-beans/src/test/java/com/baeldung/ejb/spring/comparison/ejb/EJBUnitTest.java @@ -0,0 +1,142 @@ +package com.baeldung.ejb.spring.comparison.ejb; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.annotation.Resource; +import javax.ejb.EJB; +import javax.ejb.embeddable.EJBContainer; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.NamingException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.ejb.spring.comparison.ejb.singleton.CounterEJBRemote; +import com.baeldung.ejb.spring.comparison.ejb.stateful.ShoppingCartEJBRemote; +import com.baeldung.ejb.spring.comparison.ejb.stateless.FinderEJBRemote; + +public class EJBUnitTest { + + private static EJBContainer ejbContainer = null; + + private static Context context = null; + + @Resource + private ConnectionFactory connectionFactory; + + @EJB + private FinderEJBRemote alphabetFinder; + + @Resource(name = "myQueue") + private Queue myQueue; + + @Resource(name = "ackQueue") + private Queue ackQueue; + + @BeforeClass + public static void start() throws NamingException { + ejbContainer = EJBContainer.createEJBContainer(); + } + + @Before + public void initializeContext() throws NamingException { + context = ejbContainer.getContext(); + context.bind("inject", this); + } + + @Test + public void givenSingletonBean_whenCounterInvoked_thenCountIsIncremented() throws NamingException { + + int count = 0; + CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB"); + + for (int i = 0; i < 10; i++) + count = counterEJB.count(); + + assertThat(count, is(not(1))); + } + + @Test + public void givenSingletonBean_whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException { + + CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB"); + + int count = 0; + for (int i = 0; i < 10; i++) + count = counterEJB.count(); + + assertThat(count, is(not(1))); + } + + @Test + public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException { + ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB"); + + bathingCart.addItem("soap"); + bathingCart.addItem("shampoo"); + bathingCart.addItem("oil"); + + assertEquals(3, bathingCart.getItems() + .size()); + } + + @Test + public void givenStatefulBean_whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException { + ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB"); + + fruitCart.addItem("apples"); + fruitCart.addItem("oranges"); + + assertEquals(2, fruitCart.getItems() + .size()); + } + + @Test + public void givenStatelessBean_whenSearchForA_thenApple() throws NamingException { + + assertEquals("Apple", alphabetFinder.search("A")); + } + + @Test + public void givenMDB_whenMessageSent_thenAcknowledgementReceived() throws InterruptedException, JMSException, NamingException { + + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = session.createProducer(myQueue); + producer.send(session.createTextMessage("marco")); + MessageConsumer response = session.createConsumer(ackQueue); + + assertEquals("polo", ((TextMessage) response.receive(1000)).getText()); + + } + + @After + public void reset() throws NamingException { + context.unbind("inject"); + } + + @AfterClass + public static void checkTotalCountAndcloseContext() throws NamingException { + CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB"); + assertEquals(21, counterEJB.count()); + + context.close(); + ejbContainer.close(); + } + +} diff --git a/spring-ejb/ejb-beans/src/test/java/com/baeldung/ejb/spring/comparison/spring/SpringUnitTest.java b/spring-ejb/ejb-beans/src/test/java/com/baeldung/ejb/spring/comparison/spring/SpringUnitTest.java new file mode 100644 index 0000000000..a346baba38 --- /dev/null +++ b/spring-ejb/ejb-beans/src/test/java/com/baeldung/ejb/spring/comparison/spring/SpringUnitTest.java @@ -0,0 +1,108 @@ +package com.baeldung.ejb.spring.comparison.spring; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.naming.NamingException; + +import org.apache.activemq.junit.EmbeddedActiveMQBroker; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.ejb.spring.comparison.spring.config.ApplicationConfig; +import com.baeldung.ejb.spring.comparison.spring.messagedriven.Producer; +import com.baeldung.ejb.spring.comparison.spring.singleton.CounterBean; +import com.baeldung.ejb.spring.comparison.spring.stateful.ShoppingCartBean; + +public class SpringUnitTest { + + private static AnnotationConfigApplicationContext context = null; + + @ClassRule + public static EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker() { + @Override + protected void configure() { + this.getBrokerService() + .setUseJmx(true); + try { + this.getBrokerService() + .addConnector("tcp://localhost:61616"); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + } + }; + + @BeforeClass + public static void init() { + context = new AnnotationConfigApplicationContext(ApplicationConfig.class); + } + + @Test + public void whenCounterInvoked_thenCountIsIncremented() throws NamingException { + CounterBean counterBean = context.getBean(CounterBean.class); + + int count = 0; + for (int i = 0; i < 10; i++) + count = counterBean.count(); + + assertThat(count, is(not(1))); + } + + @Test + public void whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException { + CounterBean counterBean = context.getBean(CounterBean.class); + + int count = 0; + for (int i = 0; i < 10; i++) + count = counterBean.count(); + + assertThat(count, is(not(1))); + } + + @Test + public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException { + ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class); + + bathingCart.addItem("soap"); + bathingCart.addItem("shampoo"); + bathingCart.addItem("oil"); + + assertEquals(3, bathingCart.getItems() + .size()); + } + + @Test + public void whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException { + ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class); + + fruitCart.addItem("apples"); + fruitCart.addItem("oranges"); + + assertEquals(2, fruitCart.getItems() + .size()); + } + + @Test + public void givenJMSBean_whenMessageSent_thenAcknowledgementReceived() throws NamingException { + Producer producer = context.getBean(Producer.class); + producer.sendMessageToDefaultDestination("marco"); + + assertEquals("polo", producer.receiveAck()); + } + + @AfterClass + public static void checkTotalCountAndcloseContext() throws NamingException { + CounterBean counterBean = context.getBean(CounterBean.class); + int count = counterBean.count(); + assertEquals(21, count); + context.close(); + } + +} diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index ff1f7830b6..f1263860f9 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -16,3 +16,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring MVC @PathVariable with a dot (.) gets truncated](https://www.baeldung.com/spring-mvc-pathvariable-dot) - [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters) - [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml) +- [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js) diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 0f3a1d65b9..079a664a5d 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -223,8 +223,6 @@ - 2.1.9.RELEASE - 3.0.9.RELEASE diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/accessparamsjs/App.java b/spring-mvc-java/src/main/java/com/baeldung/accessparamsjs/App.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/accessparamsjs/App.java rename to spring-mvc-java/src/main/java/com/baeldung/accessparamsjs/App.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/accessparamsjs/Controller.java b/spring-mvc-java/src/main/java/com/baeldung/accessparamsjs/Controller.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/accessparamsjs/Controller.java rename to spring-mvc-java/src/main/java/com/baeldung/accessparamsjs/Controller.java index 8759f1bcd6..157dcf54da 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/accessparamsjs/Controller.java +++ b/spring-mvc-java/src/main/java/com/baeldung/accessparamsjs/Controller.java @@ -1,11 +1,11 @@ package com.baeldung.accessparamsjs; -import java.util.Map; - import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; +import java.util.Map; + /** * Sample rest controller for the tutorial article * "Access Spring MVC Model object in JavaScript". diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java index 4a9f6a3431..a0dd7358d0 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java @@ -1,17 +1,17 @@ package com.baeldung.spring.web.config; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @Configuration public class CustomWebMvcConfigurationSupport extends WebMvcConfigurationSupport { - @Bean - public RequestMappingHandlerMapping requestMappingHandlerMapping() { - RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping(); - handlerMapping.setUseSuffixPatternMatch(false); - return handlerMapping; + @Override + protected PathMatchConfigurer getPathMatchConfigurer() { + PathMatchConfigurer pathMatchConfigurer = super.getPathMatchConfigurer(); + pathMatchConfigurer.setUseSuffixPatternMatch(false); + + return pathMatchConfigurer; } } diff --git a/spring-boot-modules/spring-boot-mvc/src/main/resources/templates/thymeleaf/index.html b/spring-mvc-java/src/main/resources/templates/thymeleaf/index.html similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/resources/templates/thymeleaf/index.html rename to spring-mvc-java/src/main/resources/templates/thymeleaf/index.html diff --git a/spring-boot-modules/spring-boot-mvc/src/main/webapp/js/jquery.js b/spring-mvc-java/src/main/webapp/js/jquery.js similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/webapp/js/jquery.js rename to spring-mvc-java/src/main/webapp/js/jquery.js diff --git a/spring-boot-modules/spring-boot-mvc/src/main/webapp/js/script-async-jquery.js b/spring-mvc-java/src/main/webapp/js/script-async-jquery.js similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/webapp/js/script-async-jquery.js rename to spring-mvc-java/src/main/webapp/js/script-async-jquery.js diff --git a/spring-boot-modules/spring-boot-mvc/src/main/webapp/js/script-async.js b/spring-mvc-java/src/main/webapp/js/script-async.js similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/webapp/js/script-async.js rename to spring-mvc-java/src/main/webapp/js/script-async.js diff --git a/spring-boot-modules/spring-boot-mvc/src/main/webapp/js/script.js b/spring-mvc-java/src/main/webapp/js/script.js similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/main/webapp/js/script.js rename to spring-mvc-java/src/main/webapp/js/script.js diff --git a/spring-boot-modules/spring-boot-mvc/src/test/java/com/baeldung/accessparamsjs/ControllerUnitTest.java b/spring-mvc-java/src/test/java/com/baeldung/accessparamsjs/ControllerUnitTest.java similarity index 100% rename from spring-boot-modules/spring-boot-mvc/src/test/java/com/baeldung/accessparamsjs/ControllerUnitTest.java rename to spring-mvc-java/src/test/java/com/baeldung/accessparamsjs/ControllerUnitTest.java index 2dc62a20f6..f05d34b654 100644 --- a/spring-boot-modules/spring-boot-mvc/src/test/java/com/baeldung/accessparamsjs/ControllerUnitTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/accessparamsjs/ControllerUnitTest.java @@ -1,7 +1,5 @@ package com.baeldung.accessparamsjs; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -12,6 +10,8 @@ import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java index 079ea3e5cd..3d34a46791 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java @@ -32,7 +32,7 @@ public class GreetControllerIntegrationTest { private MockMvc mockMvc; - private static final String CONTENT_TYPE = "application/json;charset=UTF-8"; + private static final String CONTENT_TYPE = "application/json"; @Before public void setup() throws Exception { diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerUnitTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerUnitTest.java index 0475bd933d..eacd256438 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerUnitTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerUnitTest.java @@ -16,7 +16,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; public class GreetControllerUnitTest { private MockMvc mockMvc; - private static final String CONTENT_TYPE = "application/json;charset=UTF-8"; + private static final String CONTENT_TYPE = "application/json"; @Before public void setup() { diff --git a/spring-rest-http/pom.xml b/spring-rest-http/pom.xml index c5c6b8d44c..32d2804220 100644 --- a/spring-rest-http/pom.xml +++ b/spring-rest-http/pom.xml @@ -41,12 +41,18 @@ org.springframework.boot spring-boot-starter-test + + com.github.java-json-tools + json-patch + ${jsonpatch.version} + 1.4.9 + 1.12 diff --git a/spring-rest-http/src/main/java/com/baeldung/CustomerSpringBootRestApplication.java b/spring-rest-http/src/main/java/com/baeldung/CustomerSpringBootRestApplication.java new file mode 100644 index 0000000000..2995d4d0dc --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/CustomerSpringBootRestApplication.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CustomerSpringBootRestApplication { + + public static void main(String[] args) { + SpringApplication.run(CustomerSpringBootRestApplication.class, args); + } +} diff --git a/spring-rest-http/src/main/java/com/baeldung/model/Customer.java b/spring-rest-http/src/main/java/com/baeldung/model/Customer.java new file mode 100644 index 0000000000..e65446c389 --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/model/Customer.java @@ -0,0 +1,79 @@ +package com.baeldung.model; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +public class Customer { + private String id; + private String telephone; + private List favorites; + private Map communicationPreferences; + + public Customer() { + } + + public Customer(String id, String telephone, List favorites, Map communicationPreferences) { + this(telephone, favorites, communicationPreferences); + this.id = id; + } + + public Customer(String telephone, List favorites, Map communicationPreferences) { + this.telephone = telephone; + this.favorites = favorites; + this.communicationPreferences = communicationPreferences; + } + + public static Customer fromCustomer(Customer customer) { + return new Customer(customer.getId(), customer.getTelephone(), customer.getFavorites(), customer.getCommunicationPreferences()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTelephone() { + return telephone; + } + + public void setTelephone(String telephone) { + this.telephone = telephone; + } + + public Map getCommunicationPreferences() { + return communicationPreferences; + } + + public void setCommunicationPreferences(Map communicationPreferences) { + this.communicationPreferences = communicationPreferences; + } + + public List getFavorites() { + return favorites; + } + + public void setFavorites(List favorites) { + this.favorites = favorites; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Customer)) { + return false; + } + Customer customer = (Customer) o; + return Objects.equals(id, customer.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/spring-rest-http/src/main/java/com/baeldung/service/CustomerIdGenerator.java b/spring-rest-http/src/main/java/com/baeldung/service/CustomerIdGenerator.java new file mode 100644 index 0000000000..f4afb79852 --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/service/CustomerIdGenerator.java @@ -0,0 +1,6 @@ +package com.baeldung.service; + +public interface CustomerIdGenerator { + int generateNextId(); +} + diff --git a/spring-rest-http/src/main/java/com/baeldung/service/CustomerService.java b/spring-rest-http/src/main/java/com/baeldung/service/CustomerService.java new file mode 100644 index 0000000000..51a6bc7474 --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/service/CustomerService.java @@ -0,0 +1,14 @@ +package com.baeldung.service; + +import com.baeldung.model.Customer; + +import java.util.Optional; + +public interface CustomerService { + + Customer createCustomer(Customer customer); + + Optional findCustomer(String id); + + void updateCustomer(Customer customer); +} diff --git a/spring-rest-http/src/main/java/com/baeldung/service/impl/CustomerIdGeneratorImpl.java b/spring-rest-http/src/main/java/com/baeldung/service/impl/CustomerIdGeneratorImpl.java new file mode 100644 index 0000000000..471fe880b8 --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/service/impl/CustomerIdGeneratorImpl.java @@ -0,0 +1,17 @@ +package com.baeldung.service.impl; + +import com.baeldung.service.CustomerIdGenerator; + +import org.springframework.stereotype.Component; + +import java.util.concurrent.atomic.AtomicInteger; + +@Component +public class CustomerIdGeneratorImpl implements CustomerIdGenerator { + private static final AtomicInteger SEQUENCE = new AtomicInteger(); + + @Override + public int generateNextId() { + return SEQUENCE.incrementAndGet(); + } +} diff --git a/spring-rest-http/src/main/java/com/baeldung/service/impl/CustomerServiceImpl.java b/spring-rest-http/src/main/java/com/baeldung/service/impl/CustomerServiceImpl.java new file mode 100644 index 0000000000..c57e95deed --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/service/impl/CustomerServiceImpl.java @@ -0,0 +1,42 @@ +package com.baeldung.service.impl; + +import com.baeldung.model.Customer; +import com.baeldung.service.CustomerIdGenerator; +import com.baeldung.service.CustomerService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Service +public class CustomerServiceImpl implements CustomerService { + private CustomerIdGenerator customerIdGenerator; + private List customers = new ArrayList<>(); + + @Autowired + public CustomerServiceImpl(CustomerIdGenerator customerIdGenerator) { + this.customerIdGenerator = customerIdGenerator; + } + + @Override + public Customer createCustomer(Customer customer) { + customer.setId(Integer.toString(customerIdGenerator.generateNextId())); + customers.add(customer); + return customer; + } + + @Override + public Optional findCustomer(String id) { + return customers.stream() + .filter(customer -> customer.getId().equals(id)) + .findFirst(); + } + + @Override + public void updateCustomer(Customer customer) { + customers.set(customers.indexOf(customer), customer); + } +} diff --git a/spring-rest-http/src/main/java/com/baeldung/web/controller/CustomerRestController.java b/spring-rest-http/src/main/java/com/baeldung/web/controller/CustomerRestController.java new file mode 100644 index 0000000000..9c248b6d2d --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/web/controller/CustomerRestController.java @@ -0,0 +1,69 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.Customer; +import com.baeldung.service.CustomerService; +import com.baeldung.web.exception.CustomerNotFoundException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.fge.jsonpatch.JsonPatch; +import com.github.fge.jsonpatch.JsonPatchException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import java.net.URI; + +import javax.validation.Valid; + +@RestController +@RequestMapping(value = "/customers") +public class CustomerRestController { + private CustomerService customerService; + private ObjectMapper objectMapper = new ObjectMapper(); + + @Autowired + public CustomerRestController(CustomerService customerService) { + this.customerService = customerService; + } + + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity createCustomer(@RequestBody Customer customer) { + Customer customerCreated = customerService.createCustomer(customer); + URI location = ServletUriComponentsBuilder.fromCurrentRequest() + .path("/{id}") + .buildAndExpand(customerCreated.getId()) + .toUri(); + return ResponseEntity.created(location).build(); + } + + @PatchMapping(path = "/{id}", consumes = "application/json-patch+json") + public ResponseEntity updateCustomer(@PathVariable String id, + @RequestBody JsonPatch patch) { + try { + Customer customer = customerService.findCustomer(id).orElseThrow(CustomerNotFoundException::new); + Customer customerPatched = applyPatchToCustomer(patch, customer); + customerService.updateCustomer(customerPatched); + + return ResponseEntity.ok(customerPatched); + } catch (CustomerNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } catch (JsonPatchException | JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + private Customer applyPatchToCustomer(JsonPatch patch, Customer targetCustomer) throws JsonPatchException, JsonProcessingException { + JsonNode patched = patch.apply(objectMapper.convertValue(targetCustomer, JsonNode.class)); + return objectMapper.treeToValue(patched, Customer.class); + } +} diff --git a/spring-rest-http/src/main/java/com/baeldung/web/exception/CustomerNotFoundException.java b/spring-rest-http/src/main/java/com/baeldung/web/exception/CustomerNotFoundException.java new file mode 100644 index 0000000000..c843696402 --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/web/exception/CustomerNotFoundException.java @@ -0,0 +1,5 @@ +package com.baeldung.web.exception; + +public class CustomerNotFoundException extends RuntimeException { + +} diff --git a/spring-rest-http/src/test/java/com/baeldung/service/impl/CustomerIdGeneratorImplUnitTest.java b/spring-rest-http/src/test/java/com/baeldung/service/impl/CustomerIdGeneratorImplUnitTest.java new file mode 100644 index 0000000000..a855c1cda2 --- /dev/null +++ b/spring-rest-http/src/test/java/com/baeldung/service/impl/CustomerIdGeneratorImplUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.service.impl; + +import com.baeldung.service.CustomerIdGenerator; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CustomerIdGeneratorImplUnitTest { + + @Test + public void givenIdGeneratedPreviously_whenGenerated_thenIdIsIncremented(){ + CustomerIdGenerator customerIdGenerator = new CustomerIdGeneratorImpl(); + int firstId = customerIdGenerator.generateNextId(); + assertThat(customerIdGenerator.generateNextId()).isEqualTo(++firstId); + } +} diff --git a/spring-rest-http/src/test/java/com/baeldung/service/impl/CustomerServiceImplUnitTest.java b/spring-rest-http/src/test/java/com/baeldung/service/impl/CustomerServiceImplUnitTest.java new file mode 100644 index 0000000000..27f0b35714 --- /dev/null +++ b/spring-rest-http/src/test/java/com/baeldung/service/impl/CustomerServiceImplUnitTest.java @@ -0,0 +1,94 @@ +package com.baeldung.service.impl; + + +import com.baeldung.model.Customer; +import com.baeldung.service.CustomerIdGenerator; +import com.baeldung.service.CustomerService; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.HashMap; +import java.util.Map; + + +import static com.baeldung.model.Customer.fromCustomer; +import static java.util.Arrays.asList; +import static java.util.Optional.empty; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; + +@RunWith(MockitoJUnitRunner.class) +public class CustomerServiceImplUnitTest { + + @Mock + private CustomerIdGenerator mockCustomerIdGenerator; + + private CustomerService customerService; + + @Before + public void setup() { + customerService = new CustomerServiceImpl(mockCustomerIdGenerator); + } + + @Test + public void whenCustomerIsCreated_thenNewCustomerDetailsAreCorrect() { + Map communicationPreferences = new HashMap<>(); + communicationPreferences.put("post", true); + communicationPreferences.put("email", true); + Customer customer = new Customer("001-555-1234", asList("Milk", "Eggs"), communicationPreferences); + given(mockCustomerIdGenerator.generateNextId()).willReturn(1); + + Customer newCustomer = customerService.createCustomer(customer); + + assertThat(newCustomer.getId()).isEqualTo("1"); + assertThat(newCustomer.getTelephone()).isEqualTo("001-555-1234"); + assertThat(newCustomer.getFavorites()).containsExactly("Milk", "Eggs"); + assertThat(newCustomer.getCommunicationPreferences()).isEqualTo(communicationPreferences); + } + + @Test + public void givenNonExistentCustomer_whenCustomerIsLookedUp_thenCustomerCanNotBeFound() { + assertThat(customerService.findCustomer("CUST12345")).isEqualTo(empty()); + } + + @Test + public void whenCustomerIsCreated_thenCustomerCanBeFound() { + Map communicationPreferences = new HashMap<>(); + communicationPreferences.put("post", true); + communicationPreferences.put("email", true); + Customer customer = new Customer("001-555-1234", asList("Milk", "Eggs"), communicationPreferences); + given(mockCustomerIdGenerator.generateNextId()).willReturn(7890); + + customerService.createCustomer(customer); + Customer lookedUpCustomer = customerService.findCustomer("7890").get(); + + assertThat(lookedUpCustomer.getId()).isEqualTo("7890"); + assertThat(lookedUpCustomer.getTelephone()).isEqualTo("001-555-1234"); + assertThat(lookedUpCustomer.getFavorites()).containsExactly("Milk", "Eggs"); + assertThat(lookedUpCustomer.getCommunicationPreferences()).isEqualTo(communicationPreferences); + } + + @Test + public void whenCustomerUpdated_thenDetailsUpdatedCorrectly() { + given(mockCustomerIdGenerator.generateNextId()).willReturn(7890); + Map communicationPreferences = new HashMap<>(); + communicationPreferences.put("post", true); + communicationPreferences.put("email", true); + Customer customer = new Customer("001-555-1234", asList("Milk", "Eggs"), communicationPreferences); + Customer newCustomer = customerService.createCustomer(customer); + + Customer customerWithUpdates = fromCustomer(newCustomer); + customerWithUpdates.setTelephone("001-555-6789"); + customerService.updateCustomer(customerWithUpdates); + Customer lookedUpCustomer = customerService.findCustomer("7890").get(); + + assertThat(lookedUpCustomer.getId()).isEqualTo("7890"); + assertThat(lookedUpCustomer.getTelephone()).isEqualTo("001-555-6789"); + assertThat(lookedUpCustomer.getFavorites()).containsExactly("Milk", "Eggs"); + assertThat(lookedUpCustomer.getCommunicationPreferences()).isEqualTo(communicationPreferences); + } +} \ No newline at end of file diff --git a/spring-rest-http/src/test/java/com/baeldung/web/controller/CustomerRestControllerIntegrationTest.java b/spring-rest-http/src/test/java/com/baeldung/web/controller/CustomerRestControllerIntegrationTest.java new file mode 100644 index 0000000000..f4d9ff1b92 --- /dev/null +++ b/spring-rest-http/src/test/java/com/baeldung/web/controller/CustomerRestControllerIntegrationTest.java @@ -0,0 +1,72 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.Customer; +import com.baeldung.service.CustomerService; + +import org.junit.Before; +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.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static java.util.Collections.emptyList; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class CustomerRestControllerIntegrationTest { + + @Autowired + private CustomerService customerService; + + @Autowired + private TestRestTemplate testRestTemplate; + + @Before + public void setup() { + testRestTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory()); + } + + @Test + public void givenExistingCustomer_whenPatched_thenOnlyPatchedFieldsUpdated() { + Map communicationPreferences = new HashMap<>(); + communicationPreferences.put("post", true); + communicationPreferences.put("email", true); + Customer newCustomer = new Customer("001-555-1234", Arrays.asList("Milk", "Eggs"), + communicationPreferences); + Customer customer = customerService.createCustomer(newCustomer); + + + String patchBody = "[ { \"op\": \"replace\", \"path\": \"/telephone\", \"value\": \"001-555-5678\" },\n" + + "{\"op\": \"add\", \"path\": \"/favorites/0\", \"value\": \"Bread\" }]"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.valueOf("application/json-patch+json")); + ResponseEntity patchResponse + = testRestTemplate.exchange("/customers/{id}", + HttpMethod.PATCH, + new HttpEntity<>(patchBody, headers), + Customer.class, + customer.getId()); + + Customer customerPatched = patchResponse.getBody(); + assertThat(patchResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(customerPatched.getId()).isEqualTo(customer.getId()); + assertThat(customerPatched.getTelephone()).isEqualTo("001-555-5678"); + assertThat(customerPatched.getCommunicationPreferences().get("post")).isTrue(); + assertThat(customerPatched.getCommunicationPreferences().get("email")).isTrue(); + assertThat(customerPatched.getFavorites()).containsExactly("Bread", "Milk", "Eggs"); + } +} diff --git a/spring-rest-http/src/test/java/com/baeldung/web/controller/CustomerRestControllerUnitTest.java b/spring-rest-http/src/test/java/com/baeldung/web/controller/CustomerRestControllerUnitTest.java new file mode 100644 index 0000000000..d0fb255a5d --- /dev/null +++ b/spring-rest-http/src/test/java/com/baeldung/web/controller/CustomerRestControllerUnitTest.java @@ -0,0 +1,101 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.Customer; +import com.baeldung.service.CustomerService; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.HashMap; +import java.util.Map; + +import static java.util.Arrays.asList; +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; +import static org.springframework.http.MediaType.APPLICATION_JSON; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc +public class CustomerRestControllerUnitTest { + + private static final String APPLICATION_JSON_PATCH_JSON = "application/json-patch+json"; + + @Autowired + private MockMvc mvc; + + @MockBean + private CustomerService mockCustomerService; + + @Autowired + ApplicationContext context; + + @Test + public void whenCustomerCreated_then201ReturnedWithNewCustomerLocation() throws Exception { + Map communicationPreferences = new HashMap<>(); + communicationPreferences.put("post", true); + communicationPreferences.put("email", true); + Customer customer = new Customer("001-555-1234", asList("Milk", "Eggs"), communicationPreferences); + + Customer persistedCustomer = Customer.fromCustomer(customer); + persistedCustomer.setId("1"); + + given(mockCustomerService.createCustomer(customer)).willReturn(persistedCustomer); + + String createCustomerRequestBody = "{" + + "\"telephone\": \"001-555-1234\",\n" + + "\"favorites\": [\"Milk\", \"Eggs\"],\n" + + "\"communicationPreferences\": {\"post\":true, \"email\":true}\n" + + "}"; + mvc.perform(post("/customers") + .contentType(APPLICATION_JSON) + .content(createCustomerRequestBody)) + .andExpect(status().isCreated()) + .andExpect(redirectedUrlPattern("http://*/customers/1")); + } + + @Test + public void givenNonExistentCustomer_whenPatched_then404Returned() throws Exception { + given(mockCustomerService.findCustomer("1")).willReturn(empty()); + + String patchInstructions = "[{\"op\":\"replace\",\"path\": \"/telephone\",\"value\":\"001-555-5678\"}]"; + mvc.perform(patch("/customers/1") + .contentType(APPLICATION_JSON_PATCH_JSON) + .content(patchInstructions)) + .andExpect(status().isNotFound()); + } + + @Test + public void givenExistingCustomer_whenPatched_thenReturnPatchedCustomer() throws Exception { + Map communicationPreferences = new HashMap<>(); + communicationPreferences.put("post", true); + communicationPreferences.put("email", true); + Customer customer = new Customer("1", "001-555-1234", asList("Milk", "Eggs"), communicationPreferences); + + given(mockCustomerService.findCustomer("1")).willReturn(of(customer)); + + String patchInstructions = "[{\"op\":\"replace\",\"path\": \"/telephone\",\"value\":\"001-555-5678\"}]"; + mvc.perform(patch("/customers/1") + .contentType(APPLICATION_JSON_PATCH_JSON) + .content(patchInstructions)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is("1"))) + .andExpect(jsonPath("$.telephone", is("001-555-5678"))) + .andExpect(jsonPath("$.favorites", is(asList("Milk", "Eggs")))) + .andExpect(jsonPath("$.communicationPreferences", is(communicationPreferences))); + } +} diff --git a/spring-scheduling/README.md b/spring-scheduling/README.md index 2e3bb2b5e5..ddc540bafb 100644 --- a/spring-scheduling/README.md +++ b/spring-scheduling/README.md @@ -3,4 +3,5 @@ - [The @Scheduled Annotation in Spring](https://www.baeldung.com/spring-scheduled-tasks) - [Guide to Spring Retry](http://www.baeldung.com/spring-retry) - [How To Do @Async in Spring](http://www.baeldung.com/spring-async) +- [Conditionally Enable Scheduled Jobs in Spring](https://www.baeldung.com/spring-scheduled-enabled-conditionally) diff --git a/spring-scheduling/pom.xml b/spring-scheduling/pom.xml index 8726fea438..b27f33196d 100644 --- a/spring-scheduling/pom.xml +++ b/spring-scheduling/pom.xml @@ -27,6 +27,10 @@ org.springframework spring-aspects + + org.springframework.boot + spring-boot-starter-web + javax.annotation javax.annotation-api diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduleJobsByProfile.java b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduleJobsByProfile.java similarity index 91% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduleJobsByProfile.java rename to spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduleJobsByProfile.java index 33cd44331f..7783d0e6ff 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduleJobsByProfile.java +++ b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduleJobsByProfile.java @@ -1,4 +1,4 @@ -package com.baeldung.scheduling; +package com.baeldung.scheduling.conditional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJob.java b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJob.java similarity index 91% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJob.java rename to spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJob.java index df7cefcd3c..b56cb9ed5b 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJob.java +++ b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJob.java @@ -1,4 +1,4 @@ -package com.baeldung.scheduling; +package com.baeldung.scheduling.conditional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithBoolean.java b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithBoolean.java similarity index 94% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithBoolean.java rename to spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithBoolean.java index b03de61641..69c0efb7b2 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithBoolean.java +++ b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithBoolean.java @@ -1,4 +1,4 @@ -package com.baeldung.scheduling; +package com.baeldung.scheduling.conditional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithConditional.java b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithConditional.java similarity index 93% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithConditional.java rename to spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithConditional.java index 081c8d990a..2a90300c20 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithConditional.java +++ b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithConditional.java @@ -1,4 +1,4 @@ -package com.baeldung.scheduling; +package com.baeldung.scheduling.conditional; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithExpression.java b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithExpression.java similarity index 93% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithExpression.java rename to spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithExpression.java index 577a01f241..4842e649c7 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/ScheduledJobsWithExpression.java +++ b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/ScheduledJobsWithExpression.java @@ -1,4 +1,4 @@ -package com.baeldung.scheduling; +package com.baeldung.scheduling.conditional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/SchedulingApplication.java b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/SchedulingApplication.java similarity index 89% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/SchedulingApplication.java rename to spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/SchedulingApplication.java index 913e2137f8..4777230341 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/scheduling/SchedulingApplication.java +++ b/spring-scheduling/src/main/java/com/baeldung/scheduling/conditional/SchedulingApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.scheduling; +package com.baeldung.scheduling.conditional; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/spring-security-modules/pom.xml b/spring-security-modules/pom.xml index 7de3009f47..85b86f00fc 100644 --- a/spring-security-modules/pom.xml +++ b/spring-security-modules/pom.xml @@ -25,13 +25,12 @@ spring-security-mvc-custom spring-security-mvc-digest-auth spring-security-mvc-jsonview - spring-security-mvc-ldap + spring-security-ldap spring-security-mvc-login spring-security-mvc-persisted-remember-me spring-security-mvc-socket spring-security-oidc spring-security-react - spring-security-rest spring-security-rest-basic-auth spring-security-rest-custom diff --git a/spring-security-modules/spring-security-mvc-ldap/README.md b/spring-security-modules/spring-security-ldap/README.md similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/README.md rename to spring-security-modules/spring-security-ldap/README.md diff --git a/spring-security-modules/spring-security-mvc-ldap/pom.xml b/spring-security-modules/spring-security-ldap/pom.xml similarity index 95% rename from spring-security-modules/spring-security-mvc-ldap/pom.xml rename to spring-security-modules/spring-security-ldap/pom.xml index 2f227d7908..f5e8856648 100644 --- a/spring-security-modules/spring-security-mvc-ldap/pom.xml +++ b/spring-security-modules/spring-security-ldap/pom.xml @@ -2,9 +2,9 @@ 4.0.0 - spring-security-mvc-ldap + spring-security-ldap 0.1-SNAPSHOT - spring-security-mvc-ldap + spring-security-ldap war diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java b/spring-security-modules/spring-security-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java rename to spring-security-modules/spring-security-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java b/spring-security-modules/spring-security-ldap/src/main/java/org/baeldung/controller/MyController.java similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java rename to spring-security-modules/spring-security-ldap/src/main/java/org/baeldung/controller/MyController.java diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/java/org/baeldung/security/SecurityConfig.java b/spring-security-modules/spring-security-ldap/src/main/java/org/baeldung/security/SecurityConfig.java similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/java/org/baeldung/security/SecurityConfig.java rename to spring-security-modules/spring-security-ldap/src/main/java/org/baeldung/security/SecurityConfig.java diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/resources/logback.xml b/spring-security-modules/spring-security-ldap/src/main/resources/logback.xml similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/resources/logback.xml rename to spring-security-modules/spring-security-ldap/src/main/resources/logback.xml diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/resources/static/css/bootstrap.min.css b/spring-security-modules/spring-security-ldap/src/main/resources/static/css/bootstrap.min.css similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/resources/static/css/bootstrap.min.css rename to spring-security-modules/spring-security-ldap/src/main/resources/static/css/bootstrap.min.css diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/resources/templates/error.html b/spring-security-modules/spring-security-ldap/src/main/resources/templates/error.html similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/resources/templates/error.html rename to spring-security-modules/spring-security-ldap/src/main/resources/templates/error.html diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/resources/templates/home.html b/spring-security-modules/spring-security-ldap/src/main/resources/templates/home.html similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/resources/templates/home.html rename to spring-security-modules/spring-security-ldap/src/main/resources/templates/home.html diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/resources/templates/login.html b/spring-security-modules/spring-security-ldap/src/main/resources/templates/login.html similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/resources/templates/login.html rename to spring-security-modules/spring-security-ldap/src/main/resources/templates/login.html diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/resources/users.ldif b/spring-security-modules/spring-security-ldap/src/main/resources/users.ldif similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/resources/users.ldif rename to spring-security-modules/spring-security-ldap/src/main/resources/users.ldif diff --git a/spring-security-modules/spring-security-mvc-ldap/src/main/resources/webSecurityConfig.xml b/spring-security-modules/spring-security-ldap/src/main/resources/webSecurityConfig.xml similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/main/resources/webSecurityConfig.xml rename to spring-security-modules/spring-security-ldap/src/main/resources/webSecurityConfig.xml diff --git a/spring-security-modules/spring-security-mvc-ldap/src/test/java/org/baeldung/SpringContextTest.java b/spring-security-modules/spring-security-ldap/src/test/java/org/baeldung/SpringContextTest.java similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/test/java/org/baeldung/SpringContextTest.java rename to spring-security-modules/spring-security-ldap/src/test/java/org/baeldung/SpringContextTest.java diff --git a/spring-security-modules/spring-security-mvc-ldap/src/test/resources/.gitignore b/spring-security-modules/spring-security-ldap/src/test/resources/.gitignore similarity index 100% rename from spring-security-modules/spring-security-mvc-ldap/src/test/resources/.gitignore rename to spring-security-modules/spring-security-ldap/src/test/resources/.gitignore diff --git a/testing-modules/junit5-annotations/pom.xml b/testing-modules/junit5-annotations/pom.xml index 080844c30b..d0fba4d21b 100644 --- a/testing-modules/junit5-annotations/pom.xml +++ b/testing-modules/junit5-annotations/pom.xml @@ -49,8 +49,8 @@ - 5.4.2 - 1.4.2 + 5.6.0 + 1.6.0 2.8.2 diff --git a/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/conditional/ConditionalAnnotationsUnitTest.java b/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/conditional/ConditionalAnnotationsUnitTest.java index ddceb78cac..ba840a1c33 100644 --- a/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/conditional/ConditionalAnnotationsUnitTest.java +++ b/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/conditional/ConditionalAnnotationsUnitTest.java @@ -10,6 +10,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class ConditionalAnnotationsUnitTest { + @Test @EnabledOnOs({OS.WINDOWS, OS.MAC}) public void shouldRunBothWindowsAndMac() { @@ -28,6 +29,18 @@ public class ConditionalAnnotationsUnitTest { System.out.println("runs with java 10 and 11"); } + @Test + @EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_13) + public void shouldOnlyRunOnJava8UntilJava13() { + System.out.println("runs with Java 8, 9, 10, 11, 12 and 13!"); + } + + @Test + @DisabledForJreRange(min = JRE.JAVA_14, max = JRE.JAVA_15) + public void shouldNotBeRunOnJava14AndJava15() { + System.out.println("Shouldn't be run on Java 14 and 15."); + } + @Test @DisabledOnJre(JRE.OTHER) public void thisTestOnlyRunsWithUpToDateJREs() { @@ -58,36 +71,38 @@ public class ConditionalAnnotationsUnitTest { System.out.println("will not run if environment variable LC_TIME is UTF-8"); } + // Commented codes are going to work prior JUnit 5.5 + @Test - @EnabledIf("'FR' == systemProperty.get('user.country')") + // @EnabledIf("'FR' == systemProperty.get('user.country')") public void onlyFrenchPeopleWillRunThisMethod() { System.out.println("will run only if user.country is FR"); } @Test - @DisabledIf("java.lang.System.getProperty('os.name').toLowerCase().contains('mac')") + // @DisabledIf("java.lang.System.getProperty('os.name').toLowerCase().contains('mac')") public void shouldNotRunOnMacOS() { System.out.println("will not run if our os.name is mac"); } @Test - @EnabledIf(value = { - "load('nashorn:mozilla_compat.js')", - "importPackage(java.time)", - "", - "var thisMonth = LocalDate.now().getMonth().name()", - "var february = Month.FEBRUARY.name()", - "thisMonth.equals(february)" + /*@EnabledIf(value = { + "load('nashorn:mozilla_compat.js')", + "importPackage(java.time)", + "", + "var thisMonth = LocalDate.now().getMonth().name()", + "var february = Month.FEBRUARY.name()", + "thisMonth.equals(february)" }, - engine = "nashorn", - reason = "Self-fulfilling: {result}") + engine = "nashorn", + reason = "Self-fulfilling: {result}")*/ public void onlyRunsInFebruary() { System.out.println("this test only runs in February"); } @Test - @DisabledIf("systemEnvironment.get('XPC_SERVICE_NAME') != null " + - "&& systemEnvironment.get('XPC_SERVICE_NAME').contains('intellij')") + /*@DisabledIf("systemEnvironment.get('XPC_SERVICE_NAME') != null " + + "&& systemEnvironment.get('XPC_SERVICE_NAME').contains('intellij')")*/ public void notValidForIntelliJ() { System.out.println("this test will run if our ide is INTELLIJ"); } @@ -107,7 +122,7 @@ public class ConditionalAnnotationsUnitTest { @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) - @DisabledIf("Math.random() >= 0.5") + // @DisabledIf("Math.random() >= 0.5") @interface CoinToss { }