diff --git a/checker-plugin/pom.xml b/checker-plugin/pom.xml new file mode 100644 index 0000000000..bc7a669d4f --- /dev/null +++ b/checker-plugin/pom.xml @@ -0,0 +1,114 @@ + + 4.0.0 + com.baeldung + checker-plugin + jar + 1.0-SNAPSHOT + checker-plugin + http://maven.apache.org + + + + + + ${org.checkerframework:jdk8:jar} + + + + + + + + + + org.checkerframework + checker-qual + 2.3.1 + + + org.checkerframework + checker + 2.3.1 + + + org.checkerframework + jdk8 + 2.3.1 + + + + org.checkerframework + compiler + 2.3.1 + + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + + properties + + + + + + + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + 10000 + 10000 + + + + org.checkerframework.checker.nullness.NullnessChecker + org.checkerframework.checker.interning.InterningChecker + org.checkerframework.checker.fenum.FenumChecker + org.checkerframework.checker.formatter.FormatterChecker + org.checkerframework.checker.regex.RegexChecker + + + -AprintErrorStack + + + -Xbootclasspath/p:${annotatedJdk} + + + + + + -Awarns + + + + + + + diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/FakeNumExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/FakeNumExample.java new file mode 100644 index 0000000000..f1769cfdea --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/FakeNumExample.java @@ -0,0 +1,42 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.fenum.qual.Fenum; + +//@SuppressWarnings("fenum:assignment.type.incompatible") +public class FakeNumExample { + + // Here we use some String constants to represents countries. + static final @Fenum("country") String ITALY = "IT"; + static final @Fenum("country") String US = "US"; + static final @Fenum("country") String UNITED_KINGDOM = "UK"; + + // Here we use other String constants to represent planets instead. + static final @Fenum("planet") String MARS = "Mars"; + static final @Fenum("planet") String EARTH = "Earth"; + static final @Fenum("planet") String VENUS = "Venus"; + + // Now we write this method and we want to be sure that + // the String parameter has a value of a country, not that is just a String. + void greetCountries(@Fenum("country") String country) { + System.out.println("Hello " + country); + } + + // Similarly we're enforcing here that the provided + // parameter is a String that represent a planet. + void greetPlanets(@Fenum("planet") String planet) { + System.out.println("Hello " + planet); + } + + public static void main(String[] args) { + + FakeNumExample obj = new FakeNumExample(); + + // This will fail because we pass a planet-String to a method that + // accept a country-String. + obj.greetCountries(MARS); + + // Here the opposite happens. + obj.greetPlanets(US); + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/FormatExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/FormatExample.java new file mode 100644 index 0000000000..2fa53ff2c2 --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/FormatExample.java @@ -0,0 +1,23 @@ +package com.baeldung.typechecker; + +public class FormatExample { + + public static void main(String[] args) { + + // Just enabling org.checkerframework.checker.formatter.FormatterChecker + // we can be sure at compile time that the format strings we pass to format() + // are correct. Normally we would have those errors raised just at runtime. + // All those formats are in fact wrong. + + String.format("%y", 7); // error: invalid format string + + String.format("%d", "a string"); // error: invalid argument type for %d + + String.format("%d %s", 7); // error: missing argument for %s + + String.format("%d", 7, 3); // warning: unused argument 3 + + String.format("{0}", 7); // warning: unused argument 7, because {0} is wrong syntax + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/KeyForExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/KeyForExample.java new file mode 100644 index 0000000000..b3072b72ee --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/KeyForExample.java @@ -0,0 +1,31 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.nullness.qual.KeyFor; + +import java.util.HashMap; +import java.util.Map; + +public class KeyForExample { + + private final Map config = new HashMap<>(); + + KeyForExample() { + + // Here we initialize a map to store + // some config data. + config.put("url", "http://1.2.3.4"); + config.put("name", "foobaz"); + } + + public void dumpPort() { + + // Here, we want to dump the port value stored in the + // config, so we declare that this key has to be + // present in the config map. + // Obviously that will fail because such key is not present + // in the map. + @KeyFor("config") String key = "port"; + System.out.println( config.get(key) ); + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/MonotonicNotNullExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/MonotonicNotNullExample.java new file mode 100644 index 0000000000..c4b9c6afe1 --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/MonotonicNotNullExample.java @@ -0,0 +1,28 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; + +import java.util.Date; + +public class MonotonicNotNullExample { + + // The idea is we need this field to be to lazily initialized, + // so it starts as null but once it becomes not null + // it cannot return null. + // In these cases, we can use @MonotonicNonNull + @MonotonicNonNull private Date firstCall; + + public Date getFirstCall() { + if (firstCall == null) { + firstCall = new Date(); + } + return firstCall; + } + + public void reset() { + // This is reported as error because + // we wrongly set the field back to null. + firstCall = null; + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/NonNullExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/NonNullExample.java new file mode 100644 index 0000000000..c929eea23f --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/NonNullExample.java @@ -0,0 +1,27 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +public class NonNullExample { + + // This method's parameter is annotated in order + // to tell the pluggable type system that it can never be null. + private static int countArgs(@NonNull String[] args) { + return args.length; + } + + // This method's parameter is annotated in order + // to tell the pluggable type system that it may be null. + public static void main(@Nullable String[] args) { + + // Here lies a potential error, + // because we pass a potential null reference to a method + // that does not accept nulls. + // The Checker Framework will spot this problem at compile time + // instead of runtime. + System.out.println(countArgs(args)); + + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/RegexExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/RegexExample.java new file mode 100644 index 0000000000..9503865214 --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/RegexExample.java @@ -0,0 +1,18 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.regex.qual.Regex; + +public class RegexExample { + + // For some reason we want to be sure that this regex + // contains at least one capturing group. + // However, we do an error and we forgot to define such + // capturing group in it. + @Regex(1) private static String findNumbers = "\\d*"; + + public static void main(String[] args) { + String message = "My phone number is +3911223344."; + message.matches(findNumbers); + } + +} diff --git a/core-groovy/README.md b/core-groovy/README.md new file mode 100644 index 0000000000..5954a8ab7e --- /dev/null +++ b/core-groovy/README.md @@ -0,0 +1,4 @@ +# Groovy + +## Relevant articles: + diff --git a/core-groovy/pom.xml b/core-groovy/pom.xml new file mode 100644 index 0000000000..965670e78e --- /dev/null +++ b/core-groovy/pom.xml @@ -0,0 +1,118 @@ + + + 4.0.0 + + core-groovy + 1.0-SNAPSHOT + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + central + http://jcenter.bintray.com + + + + + + org.codehaus.groovy + groovy + 2.4.13 + + + org.codehaus.groovy + groovy-sql + 2.4.13 + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.hsqldb + hsqldb + 2.4.0 + test + + + + + + + org.codehaus.gmavenplus + gmavenplus-plugin + 1.6 + + + + addSources + addTestSources + compile + compileTests + + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + maven-failsafe-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + + + + + + UTF-8 + 1.1.2 + 1.1.2 + 1.1.2 + 1.1.2 + 0.15 + 1.5.0 + + 5.0.0 + 1.0.0 + 4.12.0 + 4.12 + + + diff --git a/core-groovy/src/test/groovy/com/baeldung/groovy/sql/SqlTest.groovy b/core-groovy/src/test/groovy/com/baeldung/groovy/sql/SqlTest.groovy new file mode 100644 index 0000000000..ac96a55773 --- /dev/null +++ b/core-groovy/src/test/groovy/com/baeldung/groovy/sql/SqlTest.groovy @@ -0,0 +1,229 @@ +package com.baeldung.groovy.sql + +import groovy.sql.GroovyResultSet +import groovy.sql.GroovyRowResult +import groovy.sql.Sql +import groovy.transform.CompileStatic +import static org.junit.Assert.* +import org.junit.Test + +class SqlTest { + + final Map dbConnParams = [url: 'jdbc:hsqldb:mem:testDB', user: 'sa', password: '', driver: 'org.hsqldb.jdbc.JDBCDriver'] + + @Test + void whenNewSqlInstance_thenDbIsAccessed() { + def sql = Sql.newInstance(dbConnParams) + sql.close() + sql.close() + } + + @Test + void whenTableDoesNotExist_thenSelectFails() { + try { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.eachRow('select * from PROJECT') {} + } + + fail("An exception should have been thrown") + } catch (ignored) { + //Ok + } + } + + @Test + void whenTableCreated_thenSelectIsPossible() { + Sql.withInstance(dbConnParams) { Sql sql -> + def result = sql.execute 'create table PROJECT_1 (id integer not null, name varchar(50), url varchar(100))' + + assertEquals(0, sql.updateCount) + assertFalse(result) + + result = sql.execute('select * from PROJECT_1') + + assertTrue(result) + } + } + + @Test + void whenIdentityColumn_thenInsertReturnsNewId() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + def ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + + assertEquals(0, ids[0][0]) + + ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + + assertEquals(1, ids[0][0]) + } + } + + @Test + void whenUpdate_thenNumberOfAffectedRows() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + def count = sql.executeUpdate("UPDATE PROJECT_3 SET URL = 'https://' + URL") + + assertEquals(2, count) + } + } + + @Test + void whenEachRow_thenResultSetHasProperties() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + + sql.eachRow("SELECT * FROM PROJECT_4") { GroovyResultSet rs -> + assertNotNull(rs.name) + assertNotNull(rs.url) + assertNotNull(rs[0]) + assertNotNull(rs[1]) + assertNotNull(rs[2]) + assertEquals(rs.name, rs['name']) + assertEquals(rs.url, rs['url']) + } + } + } + + @Test + void whenPagination_thenSubsetIsReturned() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + def rows = sql.rows('SELECT * FROM PROJECT_5 ORDER BY NAME', 1, 1) + + assertEquals(1, rows.size()) + assertEquals('REST with Spring', rows[0].name) + } + } + + @Test + void whenParameters_thenReplacement() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.execute('INSERT INTO PROJECT_6 (NAME, URL) VALUES (?, ?)', 'tutorials', 'github.com/eugenp/tutorials') + sql.execute("INSERT INTO PROJECT_6 (NAME, URL) VALUES (:name, :url)", [name: 'REST with Spring', url: 'github.com/eugenp/REST-With-Spring']) + + def rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'tutorials'") + + assertEquals(1, rows.size()) + + rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'REST with Spring'") + + assertEquals(1, rows.size()) + } + } + + @Test + void whenParametersInGString_thenReplacement() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${'tutorials'}, ${'github.com/eugenp/tutorials'})" + def name = 'REST with Spring' + def url = 'github.com/eugenp/REST-With-Spring' + sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${name}, ${url})" + + def rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'tutorials'") + + assertEquals(1, rows.size()) + + rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'REST with Spring'") + + assertEquals(1, rows.size()) + } + } + + @Test + void whenTransactionRollback_thenNoDataInserted() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.withTransaction { + sql.execute 'create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + sql.rollback() + + def rows = sql.rows("SELECT * FROM PROJECT_8") + + assertEquals(0, rows.size()) + } + } + } + + @Test + void whenTransactionRollbackThenCommit_thenOnlyLastInserted() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.withTransaction { + sql.execute 'create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.rollback() + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + sql.rollback() + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + } + + def rows = sql.rows("SELECT * FROM PROJECT_9") + + assertEquals(2, rows.size()) + } + } + + @Test + void whenException_thenTransactionIsRolledBack() { + Sql.withInstance(dbConnParams) { Sql sql -> + try { + sql.withTransaction { + sql.execute 'create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_10 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + throw new Exception('rollback') + } + } catch (ignored) {} + + def rows = sql.rows("SELECT * FROM PROJECT_10") + + assertEquals(0, rows.size()) + } + } + + @Test + void givenCachedConnection_whenException_thenDataIsPersisted() { + Sql.withInstance(dbConnParams) { Sql sql -> + try { + sql.cacheConnection { + sql.execute 'create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_11 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + throw new Exception('This does not rollback') + } + } catch (ignored) {} + + def rows = sql.rows("SELECT * FROM PROJECT_11") + + assertEquals(1, rows.size()) + } + } + + /*@Test + void whenModifyResultSet_thenDataIsChanged() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + + sql.eachRow("SELECT * FROM PROJECT_5 FOR UPDATE ") { GroovyResultSet rs -> + rs['name'] = "The great ${rs.name}!" as String + rs.updateRow() + } + + sql.eachRow("SELECT * FROM PROJECT_5") { GroovyResultSet rs -> + assertTrue(rs.name.startsWith('The great ')) + } + } + }*/ + +} diff --git a/core-java-8/README.md b/core-java-8/README.md index 9260e3d447..1b5208961d 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -40,3 +40,4 @@ - [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency) - [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams) - [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator) +- [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection) diff --git a/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java index 4823c7178a..d013907c9a 100644 --- a/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java @@ -22,7 +22,7 @@ public class ShufflingCollectionsUnitTest { } @Test - public void whenShufflingMapKeys_thenValuesAreShuffled() { + public void whenShufflingMapEntries_thenValuesAreShuffled() { Map studentsById = new HashMap<>(); studentsById.put(1, "Foo"); studentsById.put(2, "Bar"); @@ -32,12 +32,12 @@ public class ShufflingCollectionsUnitTest { System.out.println("Students before shuffling:"); System.out.println(studentsById.values()); - List shuffledStudentIds = new ArrayList<>(studentsById.keySet()); - Collections.shuffle(shuffledStudentIds); + List> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet()); + Collections.shuffle(shuffledStudentEntries); - List shuffledStudents = shuffledStudentIds.stream() - .map(id -> studentsById.get(id)) - .collect(Collectors.toList()); + List shuffledStudents = shuffledStudentEntries.stream() + .map(Map.Entry::getValue) + .collect(Collectors.toList()); System.out.println("Students after shuffling"); System.out.println(shuffledStudents); diff --git a/core-java/src/main/java/com/baeldung/casting/Animal.java b/core-java/src/main/java/com/baeldung/casting/Animal.java new file mode 100644 index 0000000000..9f31c1dda3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Animal.java @@ -0,0 +1,13 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Animal { + private static final Logger LOGGER = LoggerFactory.getLogger(Animal.class); + + public void eat() { + LOGGER.info("animal is eating"); + } + +} diff --git a/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java b/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java new file mode 100644 index 0000000000..89b972e5c2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java @@ -0,0 +1,23 @@ +package com.baeldung.casting; + +import java.util.List; + +public class AnimalFeeder { + + public void feed(List animals) { + animals.forEach(animal -> { + animal.eat(); + if (animal instanceof Cat) { + ((Cat) animal).meow(); + } + }); + } + + public void uncheckedFeed(List animals) { + animals.forEach(animal -> { + animal.eat(); + ((Cat) animal).meow(); + }); + } + +} diff --git a/core-java/src/main/java/com/baeldung/casting/Cat.java b/core-java/src/main/java/com/baeldung/casting/Cat.java new file mode 100644 index 0000000000..aa9a9a881a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Cat.java @@ -0,0 +1,16 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Cat extends Animal implements Mew { + private static final Logger LOGGER = LoggerFactory.getLogger(Cat.class); + + public void eat() { + LOGGER.info("cat is eating"); + } + + public void meow() { + LOGGER.info("meow"); + } +} diff --git a/core-java/src/main/java/com/baeldung/casting/Dog.java b/core-java/src/main/java/com/baeldung/casting/Dog.java new file mode 100644 index 0000000000..763c6b4785 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Dog.java @@ -0,0 +1,12 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Dog extends Animal { + private static final Logger LOGGER = LoggerFactory.getLogger(Dog.class); + + public void eat() { + LOGGER.info("dog is eating"); + } +} diff --git a/core-java/src/main/java/com/baeldung/casting/Mew.java b/core-java/src/main/java/com/baeldung/casting/Mew.java new file mode 100644 index 0000000000..f3c7324551 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Mew.java @@ -0,0 +1,5 @@ +package com.baeldung.casting; + +public interface Mew { + public void meow(); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/AuthenticationProcessor.java b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/AuthenticationProcessor.java new file mode 100644 index 0000000000..b86a572393 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/AuthenticationProcessor.java @@ -0,0 +1,13 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +public abstract class AuthenticationProcessor { + + // next element in chain or responsibility + public AuthenticationProcessor nextProcessor; + + public AuthenticationProcessor(AuthenticationProcessor nextProcessor) { + this.nextProcessor = nextProcessor; + } + + public abstract boolean isAuthorized(AuthenticationProvider authProvider); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/AuthenticationProvider.java b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/AuthenticationProvider.java new file mode 100644 index 0000000000..552a7ff6d9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/AuthenticationProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +public interface AuthenticationProvider { + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/OAuthAuthenticationProcessor.java b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/OAuthAuthenticationProcessor.java new file mode 100644 index 0000000000..2e2e51fed2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/OAuthAuthenticationProcessor.java @@ -0,0 +1,21 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +public class OAuthAuthenticationProcessor extends AuthenticationProcessor { + + public OAuthAuthenticationProcessor(AuthenticationProcessor nextProcessor) { + super(nextProcessor); + } + + @Override + public boolean isAuthorized(AuthenticationProvider authProvider) { + + if (authProvider instanceof OAuthTokenProvider) { + return Boolean.TRUE; + } else if (nextProcessor != null) { + return nextProcessor.isAuthorized(authProvider); + } else { + return Boolean.FALSE; + } + } + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/OAuthTokenProvider.java b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/OAuthTokenProvider.java new file mode 100644 index 0000000000..d4e516053b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/OAuthTokenProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +public class OAuthTokenProvider implements AuthenticationProvider { + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/SamlAuthenticationProvider.java b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/SamlAuthenticationProvider.java new file mode 100644 index 0000000000..533b2b4a2d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/SamlAuthenticationProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +public class SamlAuthenticationProvider implements AuthenticationProvider { + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/UsernamePasswordAuthenticationProcessor.java b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/UsernamePasswordAuthenticationProcessor.java new file mode 100644 index 0000000000..df600c35db --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/UsernamePasswordAuthenticationProcessor.java @@ -0,0 +1,20 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +public class UsernamePasswordAuthenticationProcessor extends AuthenticationProcessor { + + public UsernamePasswordAuthenticationProcessor(AuthenticationProcessor nextProcessor) { + super(nextProcessor); + } + + @Override + public boolean isAuthorized(AuthenticationProvider authProvider) { + if (authProvider instanceof UsernamePasswordProvider) { + return Boolean.TRUE; + } else if (nextProcessor != null) { + return nextProcessor.isAuthorized(authProvider); + } else { + return Boolean.FALSE; + } + } + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/UsernamePasswordProvider.java b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/UsernamePasswordProvider.java new file mode 100644 index 0000000000..9fbfa7554d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/chainofresponsibility/UsernamePasswordProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +public class UsernamePasswordProvider implements AuthenticationProvider { + +} diff --git a/core-java/src/main/java/com/baeldung/java/list/CustomList.java b/core-java/src/main/java/com/baeldung/java/list/CustomList.java new file mode 100644 index 0000000000..bc1321b1a3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/list/CustomList.java @@ -0,0 +1,229 @@ +package com.baeldung.java.list; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public class CustomList implements List { + private Object[] internal = {}; + + @Override + public void add(int index, E element) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(int index, Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public E remove(int index) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object object) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public int size() { + return internal.length; + } + + @Override + public boolean isEmpty() { + return internal.length == 0; + } + + @Override + public boolean add(E element) { + // the first cycle + // internal = new Object[1]; + // internal[0] = element; + // return true; + + Object[] temp = new Object[internal.length + 1]; + System.arraycopy(internal, 0, temp, 0, internal.length); + temp[internal.length] = element; + internal = temp; + return true; + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + return (E) internal[index]; + } + + @Override + public boolean contains(Object object) { + // return false + + for (Object element : internal) { + if (object.equals(element)) { + return true; + } + } + return false; + } + + @Override + public boolean containsAll(Collection collection) { + // the first cycle + // for (Object element : collection) { + // if (element.equals(internal[0])) { + // return true; + // } + // } + // return false; + + for (Object element : collection) + if (!contains(element)) { + return false; + } + return true; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + E oldElement = (E) internal[index]; + internal[index] = element; + return oldElement; + } + + @Override + public void clear() { + internal = new Object[0]; + } + + @Override + public int indexOf(Object object) { + // the first cycle + // if (object.equals(internal[0])) { + // return 0; + // } + // return -1; + + for (int i = 0; i < internal.length; i++) { + if (object.equals(internal[i])) { + return i; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object object) { + // the first cycle + // if (object.equals(internal[0])) { + // return 0; + // } + // return -1; + + for (int i = internal.length - 1; i >= 0; i--) { + if (object.equals(internal[i])) { + return i; + } + } + return -1; + } + + @SuppressWarnings("unchecked") + @Override + public List subList(int fromIndex, int toIndex) { + // the first cycle + // return (List) Arrays.asList(internal); + + Object[] temp = new Object[toIndex - fromIndex]; + System.arraycopy(internal, fromIndex, temp, 0, temp.length); + return (List) Arrays.asList(temp); + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(internal, internal.length); + } + + @SuppressWarnings("unchecked") + @Override + public T[] toArray(T[] array) { + // the first cycle + // array[0] = (T) internal[0]; + // return array; + + // the second cycle + // if (array.length < internal.length) { + // return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); + // } + // return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); + + if (array.length < internal.length) { + return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); + } + + System.arraycopy(internal, 0, array, 0, internal.length); + if (array.length > internal.length) { + array[internal.length] = null; + } + return array; + } + + @Override + public Iterator iterator() { + return new CustomIterator(); + } + + @Override + public ListIterator listIterator() { + return null; + } + + @Override + public ListIterator listIterator(int index) { + // ignored for brevity + return null; + } + + private class CustomIterator implements Iterator { + int index; + + @Override + public boolean hasNext() { + // the first cycle + // return true; + + return index != internal.length; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + // the first cycle + // return (E) CustomList.this.internal[0]; + + E element = (E) CustomList.this.internal[index]; + index++; + return element; + } + } +} diff --git a/core-java/src/test/java/com/baeldung/casting/CastingTest.java b/core-java/src/test/java/com/baeldung/casting/CastingTest.java new file mode 100644 index 0000000000..0ca1ce88dc --- /dev/null +++ b/core-java/src/test/java/com/baeldung/casting/CastingTest.java @@ -0,0 +1,58 @@ +package com.baeldung.casting; + +import org.junit.Test; +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; + +public class CastingTest { + + @Test + public void whenPrimitiveConverted_thenValueChanged() { + double myDouble = 1.1; + int myInt = (int) myDouble; + assertNotEquals(myDouble, myInt); + } + + @Test + public void whenUpcast_thenInstanceUnchanged() { + Cat cat = new Cat(); + Animal animal = cat; + animal = (Animal) cat; + assertTrue(animal instanceof Cat); + } + + @Test + public void whenUpcastToObject_thenInstanceUnchanged() { + Object object = new Animal(); + assertTrue(object instanceof Animal); + } + + @Test + public void whenUpcastToInterface_thenInstanceUnchanged() { + Mew mew = new Cat(); + assertTrue(mew instanceof Cat); + } + + @Test + public void whenUpcastToAnimal_thenOverridenMethodsCalled() { + List animals = new ArrayList<>(); + animals.add(new Cat()); + animals.add(new Dog()); + new AnimalFeeder().feed(animals); + } + + @Test + public void whenDowncastToCat_thenMeowIsCalled() { + Animal animal = new Cat(); + ((Cat) animal).meow(); + } + + @Test(expected = ClassCastException.class) + public void whenDownCastWithoutCheck_thenExceptionThrown() { + List animals = new ArrayList<>(); + animals.add(new Cat()); + animals.add(new Dog()); + new AnimalFeeder().uncheckedFeed(animals); + } +} diff --git a/core-java/src/test/java/com/baeldung/designpatterns/chainofresponsibility/ChainOfResponsibilityTest.java b/core-java/src/test/java/com/baeldung/designpatterns/chainofresponsibility/ChainOfResponsibilityTest.java new file mode 100644 index 0000000000..a28577efb1 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/designpatterns/chainofresponsibility/ChainOfResponsibilityTest.java @@ -0,0 +1,37 @@ +package com.baeldung.designpatterns.chainofresponsibility; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ChainOfResponsibilityTest { + + private static AuthenticationProcessor getChainOfAuthProcessor() { + + AuthenticationProcessor oAuthProcessor = new OAuthAuthenticationProcessor(null); + AuthenticationProcessor unamePasswordProcessor = new UsernamePasswordAuthenticationProcessor(oAuthProcessor); + return unamePasswordProcessor; + } + + @Test + public void givenOAuthProvider_whenCheckingAuthorized_thenSuccess() { + AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor(); + boolean isAuthorized = authProcessorChain.isAuthorized(new OAuthTokenProvider()); + assertTrue(isAuthorized); + } + + @Test + public void givenUsernamePasswordProvider_whenCheckingAuthorized_thenSuccess() { + AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor(); + boolean isAuthorized = authProcessorChain.isAuthorized(new UsernamePasswordProvider()); + assertTrue(isAuthorized); + } + + @Test + public void givenSamlAuthProvider_whenCheckingAuthorized_thenFailure() { + AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor(); + boolean isAuthorized = authProcessorChain.isAuthorized(new SamlAuthenticationProvider()); + assertTrue(!isAuthorized); + } + +} diff --git a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java new file mode 100644 index 0000000000..3ee3195e80 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java @@ -0,0 +1,282 @@ +package com.baeldung.java.list; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.junit.Test; + +public class CustomListUnitTest { + @Test(expected = UnsupportedOperationException.class) + public void whenAddToSpecifiedIndex_thenExceptionIsThrown() { + new CustomList<>().add(0, null); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenAddAllToTheEnd_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + List list = new CustomList<>(); + list.addAll(collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenAddAllToSpecifiedIndex_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + List list = new CustomList<>(); + list.addAll(0, collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRemoveAtSpecifiedIndex_thenExceptionIsThrown() { + List list = new CustomList<>(); + list.add("baeldung"); + list.remove(0); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRemoveSpecifiedElement_thenExceptionIsThrown() { + List list = new CustomList<>(); + list.add("baeldung"); + list.remove("baeldung"); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRemoveAll_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.removeAll(collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRetainAll_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + list.retainAll(collection); + } + + @Test + public void givenEmptyList_whenSize_thenZeroIsReturned() { + List list = new CustomList<>(); + + assertEquals(0, list.size()); + } + + @Test + public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() { + List list = new CustomList<>(); + + assertTrue(list.isEmpty()); + } + + @Test + public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() { + List list = new CustomList<>(); + boolean succeeded = list.add("baeldung"); + Object element = list.get(0); + + assertTrue(succeeded); + assertEquals("baeldung", element); + } + + @Test + public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() { + List list = new CustomList<>(); + boolean succeeded1 = list.add("baeldung"); + boolean succeeded2 = list.add(".com"); + Object element1 = list.get(0); + Object element2 = list.get(1); + + assertTrue(succeeded1); + assertTrue(succeeded2); + assertEquals("baeldung", element1); + assertEquals(".com", element2); + } + + @Test + public void givenEmptyList_whenContains_thenFalseIsReturned() { + List list = new CustomList<>(); + + assertFalse(list.contains(null)); + } + + @Test + public void givenListWithAnElement_whenContains_thenTrueIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.contains("baeldung")); + } + + @Test + public void givenListWithAnElement_whenContainsAll_thenTrueIsReturned() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.containsAll(collection)); + } + + @Test + public void givenList_whenContainsAll_thenEitherTrueOrFalseIsReturned() { + Collection collection1 = new ArrayList<>(); + collection1.add("baeldung"); + collection1.add(".com"); + Collection collection2 = new ArrayList<>(); + collection2.add("baeldung"); + + List list = new CustomList<>(); + list.add("baeldung"); + + assertFalse(list.containsAll(collection1)); + assertTrue(list.containsAll(collection2)); + } + + @Test + public void givenList_whenSet_thenOldElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + Object element = list.set(0, null); + + assertEquals("baeldung", element); + assertNull(list.get(0)); + } + + @Test + public void givenList_whenClear_thenAllElementsAreRemoved() { + List list = new CustomList<>(); + list.add("baeldung"); + list.clear(); + + assertTrue(list.isEmpty()); + } + + @Test + public void givenList_whenIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.indexOf("baeldung")); + } + + @Test + public void givenList_whenIndexOf_thenPositiveIndexOrMinusOneIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add(".com"); + list.add(".com"); + + assertEquals(1, list.indexOf(".com")); + assertEquals(-1, list.indexOf("com")); + } + + @Test + public void whenLastIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.lastIndexOf("baeldung")); + } + + @Test + public void whenLastIndexOf_thenPositiveIndexOrMinusOneIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add("baeldung"); + list.add(".com"); + + assertEquals(1, list.lastIndexOf("baeldung")); + assertEquals(-1, list.indexOf("com")); + } + + @Test + public void whenSubListZeroToOne_thenListContainingFirstElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + List subList = list.subList(0, 1); + + assertEquals("baeldung", subList.get(0)); + } + + @Test + public void whenSubListOneToTwo_thenListContainingSecondElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add("."); + list.add("com"); + List subList = list.subList(1, 2); + + assertEquals(1, subList.size()); + assertEquals(".", subList.get(0)); + } + + @Test + public void givenListWithElements_whenToArray_thenArrayContainsThose() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add(".com"); + Object[] array = list.toArray(); + + assertArrayEquals(new Object[] { "baeldung", ".com" }, array); + } + + @Test + public void givenListWithAnElement_whenToArray_thenInputArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[1]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung" }, input); + } + + @Test + public void whenToArrayIsCalledWithEmptyInputArray_thenNewArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = {}; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung" }, output); + } + + @Test + public void whenToArrayIsCalledWithLargerInput_thenOutputHasTrailingNull() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[2]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung", null }, output); + } + + @Test + public void givenListWithOneElement_whenIterator_thenThisElementIsNext() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + assertTrue(iterator.hasNext()); + assertEquals("baeldung", iterator.next()); + } + + @Test + public void whenIteratorNextIsCalledTwice_thenTheSecondReturnsFalse() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + iterator.next(); + assertFalse(iterator.hasNext()); + } +} diff --git a/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java b/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java new file mode 100644 index 0000000000..5869676004 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java @@ -0,0 +1,154 @@ +package com.baeldung.string; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StringComparisonTest { + + @Test + public void whenUsingComparisonOperator_ThenComparingStrings(){ + + String string1 = "using comparison operator"; + String string2 = "using comparison operator"; + String string3 = new String("using comparison operator"); + + assertThat(string1 == string2).isTrue(); + assertThat(string1 == string3).isFalse(); + } + + @Test + public void whenUsingEqualsMethod_ThenComparingStrings(){ + + String string1 = "using equals method"; + String string2 = "using equals method"; + + String string3 = "using EQUALS method"; + String string4 = new String("using equals method"); + + assertThat(string1.equals(string2)).isTrue(); + assertThat(string1.equals(string4)).isTrue(); + + assertThat(string1.equals(null)).isFalse(); + assertThat(string1.equals(string3)).isFalse(); + } + + @Test + public void whenUsingEqualsIgnoreCase_ThenComparingStrings(){ + + String string1 = "using equals ignore case"; + String string2 = "USING EQUALS IGNORE CASE"; + + assertThat(string1.equalsIgnoreCase(string2)).isTrue(); + } + + @Test + public void whenUsingCompareTo_ThenComparingStrings(){ + + String author = "author"; + String book = "book"; + String duplicateBook = "book"; + + assertThat(author.compareTo(book)).isEqualTo(-1); + assertThat(book.compareTo(author)).isEqualTo(1); + assertThat(duplicateBook.compareTo(book)).isEqualTo(0); + } + + @Test + public void whenUsingCompareToIgnoreCase_ThenComparingStrings(){ + + String author = "Author"; + String book = "book"; + String duplicateBook = "BOOK"; + + assertThat(author.compareToIgnoreCase(book)).isEqualTo(-1); + assertThat(book.compareToIgnoreCase(author)).isEqualTo(1); + assertThat(duplicateBook.compareToIgnoreCase(book)).isEqualTo(0); + } + + @Test + public void whenUsingObjectsEqualsMethod_ThenComparingStrings(){ + + String string1 = "using objects equals"; + String string2 = "using objects equals"; + String string3 = new String("using objects equals"); + + assertThat(Objects.equals(string1, string2)).isTrue(); + assertThat(Objects.equals(string1, string3)).isTrue(); + + assertThat(Objects.equals(null, null)).isTrue(); + assertThat(Objects.equals(null, string1)).isFalse(); + } + + @Test + public void whenUsingEqualsOfApacheCommons_ThenComparingStrings(){ + + assertThat(StringUtils.equals(null, null)).isTrue(); + assertThat(StringUtils.equals(null, "equals method")).isFalse(); + assertThat(StringUtils.equals("equals method", "equals method")).isTrue(); + assertThat(StringUtils.equals("equals method", "EQUALS METHOD")).isFalse(); + } + + @Test + public void whenUsingEqualsIgnoreCaseOfApacheCommons_ThenComparingStrings(){ + + assertThat(StringUtils.equalsIgnoreCase(null, null)).isTrue(); + assertThat(StringUtils.equalsIgnoreCase(null, "equals method")).isFalse(); + assertThat(StringUtils.equalsIgnoreCase("equals method", "equals method")).isTrue(); + assertThat(StringUtils.equalsIgnoreCase("equals method", "EQUALS METHOD")).isTrue(); + } + + @Test + public void whenUsingEqualsAnyOf_ThenComparingStrings(){ + + assertThat(StringUtils.equalsAny(null, null, null)).isTrue(); + assertThat(StringUtils.equalsAny("equals any", "equals any", "any")).isTrue(); + assertThat(StringUtils.equalsAny("equals any", null, "equals any")).isTrue(); + assertThat(StringUtils.equalsAny(null, "equals", "any")).isFalse(); + assertThat(StringUtils.equalsAny("equals any", "EQUALS ANY", "ANY")).isFalse(); + } + + @Test + public void whenUsingEqualsAnyIgnoreCase_ThenComparingStrings(){ + + assertThat(StringUtils.equalsAnyIgnoreCase(null, null, null)).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase("equals any", "equals any", "any")).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase("equals any", null, "equals any")).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase(null, "equals", "any")).isFalse(); + assertThat(StringUtils.equalsAnyIgnoreCase( + "equals any ignore case", "EQUALS ANY IGNORE CASE", "any")).isTrue(); + } + + @Test + public void whenUsingCompare_thenComparingStringsWithNulls(){ + + assertThat(StringUtils.compare(null, null)).isEqualTo(0); + assertThat(StringUtils.compare(null, "abc")).isEqualTo(-1); + + assertThat(StringUtils.compare("abc", "bbc")).isEqualTo(-1); + assertThat(StringUtils.compare("bbc", "abc")).isEqualTo(1); + assertThat(StringUtils.compare("abc", "abc")).isEqualTo(0); + } + + @Test + public void whenUsingCompareIgnoreCase_ThenComparingStringsWithNulls(){ + + assertThat(StringUtils.compareIgnoreCase(null, null)).isEqualTo(0); + assertThat(StringUtils.compareIgnoreCase(null, "abc")).isEqualTo(-1); + + assertThat(StringUtils.compareIgnoreCase("Abc", "bbc")).isEqualTo(-1); + assertThat(StringUtils.compareIgnoreCase("bbc", "ABC")).isEqualTo(1); + assertThat(StringUtils.compareIgnoreCase("abc", "ABC")).isEqualTo(0); + } + + @Test + public void whenUsingCompareWithNullIsLessOption_ThenComparingStrings(){ + + assertThat(StringUtils.compare(null, "abc", true)).isEqualTo(-1); + assertThat(StringUtils.compare(null, "abc", false)).isEqualTo(1); + } + +} diff --git a/core-kotlin/README.md b/core-kotlin/README.md index 5908d480b7..1c64817815 100644 --- a/core-kotlin/README.md +++ b/core-kotlin/README.md @@ -17,4 +17,4 @@ - [Sealed Classes in Kotlin](http://www.baeldung.com/kotlin-sealed-classes) - [JUnit 5 for Kotlin Developers](http://www.baeldung.com/junit-5-kotlin) - [Extension Methods in Kotlin](http://www.baeldung.com/kotlin-extension-methods) - +- [Infix Functions in Kotlin](http://www.baeldung.com/kotlin-infix-functions) diff --git a/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java index 3c893eab1a..b1924198a4 100644 --- a/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java @@ -1,6 +1,6 @@ package com.baeldung.controller; -import java.io.IOException; +import com.baeldung.service.StudentService; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -8,23 +8,19 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; -import com.baeldung.service.StudentService; - -/** - * - * @author haseeb - * - */ @WebServlet(name = "StudentServlet", urlPatterns = "/student-record") public class StudentServlet extends HttpServlet { - protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - StudentService studentService = new StudentService(); + private final StudentService studentService = new StudentService(); + + private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String studentID = request.getParameter("id"); if (studentID != null) { int id = Integer.parseInt(studentID); - request.setAttribute("studentRecord", studentService.getStudent(id)); + studentService.getStudent(id) + .ifPresent(s -> request.setAttribute("studentRecord", s)); } RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp"); diff --git a/javax-servlets/src/main/java/com/baeldung/model/Student.java b/javax-servlets/src/main/java/com/baeldung/model/Student.java index 55afe6bc2a..ce8a27375a 100644 --- a/javax-servlets/src/main/java/com/baeldung/model/Student.java +++ b/javax-servlets/src/main/java/com/baeldung/model/Student.java @@ -1,10 +1,5 @@ package com.baeldung.model; -/** - * - * @author haseeb - * - */ public class Student { private int id; @@ -18,44 +13,26 @@ public class Student { this.lastName = lastName; } - /** - * @return the id - */ public int getId() { return id; } - /** - * @param id the id to set - */ public void setId(int id) { this.id = id; } - /** - * @return the firstName - */ public String getFirstName() { return firstName; } - /** - * @param firstName the firstName to set - */ public void setFirstName(String firstName) { this.firstName = firstName; } - /** - * @return the lastName - */ public String getLastName() { return lastName; } - /** - * @param lastName the lastName to set - */ public void setLastName(String lastName) { this.lastName = lastName; } diff --git a/javax-servlets/src/main/java/com/baeldung/service/StudentService.java b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java index 1666850d50..525d47683f 100644 --- a/javax-servlets/src/main/java/com/baeldung/service/StudentService.java +++ b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java @@ -2,33 +2,20 @@ package com.baeldung.service; import com.baeldung.model.Student; -/** - * - * @author haseeb - * - */ +import java.util.Optional; + public class StudentService { - /** - * - * @param id - * @return - */ - public Student getStudent(int id) { - - Student student = null; - + public Optional getStudent(int id) { switch (id) { - case 1: - student = new Student(1, "John", "Doe"); - break; - case 2: - student = new Student(2, "Jane", "Goodall"); - break; - case 3: - student = new Student(3, "Max", "Born"); - break; + case 1: + return Optional.of(new Student(1, "John", "Doe")); + case 2: + return Optional.of(new Student(2, "Jane", "Goodall")); + case 3: + return Optional.of(new Student(3, "Max", "Born")); + default: + return Optional.empty(); } - return student; } } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index 04c5fec42d..c78129a9cf 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -1,7 +1,6 @@ package com.baeldung.servlets; import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -13,7 +12,7 @@ public class FormServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { String height = request.getParameter("height"); String weight = request.getParameter("weight"); @@ -28,20 +27,17 @@ public class FormServlet extends HttpServlet { RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); dispatcher.forward(request, response); } catch (Exception e) { - response.sendRedirect("index.jsp"); } } @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + protected void doGet(HttpServletRequest request, HttpServletResponse response) { // do something else here } private Double calculateBMI(Double weight, Double height) { - return weight / (height * height); } } \ No newline at end of file diff --git a/jsonld/.gitignore b/jsonld/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/jsonld/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/jsonld/.mvn/wrapper/maven-wrapper.jar b/jsonld/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 0000000000..5fd4d5023f Binary files /dev/null and b/jsonld/.mvn/wrapper/maven-wrapper.jar differ diff --git a/jsonld/.mvn/wrapper/maven-wrapper.properties b/jsonld/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000000..c954cec91c --- /dev/null +++ b/jsonld/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1 @@ +distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip diff --git a/jsonld/README.md b/jsonld/README.md new file mode 100644 index 0000000000..51ca961bea --- /dev/null +++ b/jsonld/README.md @@ -0,0 +1,22 @@ +JSON-LD +======= + +Hypermedia serialization with JSON-LD. + +### Requirements + +- Maven +- JDK 8 +- JSON-LD + +### Running +To build and start the server simply type + +```bash +$ mvn clean install +$ mvn spring-boot:run +``` + +Now with default configurations it will be available at: [http://localhost:8080](http://localhost:8080) + +Enjoy it :) \ No newline at end of file diff --git a/jsonld/mvnw b/jsonld/mvnw new file mode 100755 index 0000000000..a1ba1bf554 --- /dev/null +++ b/jsonld/mvnw @@ -0,0 +1,233 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # + # Look for the Apple JDKs first to preserve the existing behaviour, and then look + # for the new JDKs provided by Oracle. + # + if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then + # + # Apple JDKs + # + export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home + fi + + if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then + # + # Apple JDKs + # + export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home + fi + + if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then + # + # Oracle JDKs + # + export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home + fi + + if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then + # + # Apple JDKs + # + export JAVA_HOME=`/usr/libexec/java_home` + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Migwn, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` +fi + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + local basedir=$(pwd) + local wdir=$(pwd) + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + wdir=$(cd "$wdir/.."; pwd) + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} "$@" diff --git a/jsonld/mvnw.cmd b/jsonld/mvnw.cmd new file mode 100644 index 0000000000..2b934e89dd --- /dev/null +++ b/jsonld/mvnw.cmd @@ -0,0 +1,145 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +set MAVEN_CMD_LINE_ARGS=%* + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" + +set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar"" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS% +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% \ No newline at end of file diff --git a/jsonld/pom.xml b/jsonld/pom.xml new file mode 100644 index 0000000000..1574878667 --- /dev/null +++ b/jsonld/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + jsonld + 0.0.1-SNAPSHOT + jar + + jsonld + Hypermedia serialization with JSON-LD + + + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-5 + + + + UTF-8 + UTF-8 + 1.8 + 0.11.1 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.github.jsonld-java + jsonld-java + ${jsonld.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/jsonld/src/main/java/com/baeldung/JsonLdApplication.java b/jsonld/src/main/java/com/baeldung/JsonLdApplication.java new file mode 100644 index 0000000000..0b8f338127 --- /dev/null +++ b/jsonld/src/main/java/com/baeldung/JsonLdApplication.java @@ -0,0 +1,11 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class JsonLdApplication { + public static void main(String[] args) { + SpringApplication.run(JsonLdApplication.class, args); + } +} diff --git a/jsonld/src/main/resources/application.properties b/jsonld/src/main/resources/application.properties new file mode 100644 index 0000000000..b6bfd8f6f3 --- /dev/null +++ b/jsonld/src/main/resources/application.properties @@ -0,0 +1,14 @@ +# the db host +spring.data.mongodb.host=localhost +# the connection port (defaults to 27107) +spring.data.mongodb.port=27017 +# The database's name +spring.data.mongodb.database=Jenkins-Pipeline + +# Or this +# spring.data.mongodb.uri=mongodb://localhost/Jenkins-Pipeline + +# spring.data.mongodb.username= +# spring.data.mongodb.password= + +spring.data.mongodb.repositories.enabled=true \ No newline at end of file diff --git a/jsonld/src/test/java/com/baeldung/JsonLdSerializatorTest.java b/jsonld/src/test/java/com/baeldung/JsonLdSerializatorTest.java new file mode 100644 index 0000000000..762a4254dc --- /dev/null +++ b/jsonld/src/test/java/com/baeldung/JsonLdSerializatorTest.java @@ -0,0 +1,33 @@ +package com.baeldung; + +import com.github.jsonldjava.core.JsonLdError; +import com.github.jsonldjava.core.JsonLdOptions; +import com.github.jsonldjava.core.JsonLdProcessor; +import com.github.jsonldjava.utils.JsonUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertNotEquals; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class JsonLdSerializatorTest { + + @Test + public void whenInserting_andCount_thenWeDontGetZero() throws JsonLdError { + String inputStream = "{name:}"; + Object jsonObject = JsonUtils.fromInputStream(inputStream); + + Map context = new HashMap(); + JsonLdOptions options = new JsonLdOptions(); + Object compact = JsonLdProcessor.compact(jsonObject, context, options); + + assertNotEquals(0, 0); + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java index bf214458f3..58929c0111 100644 --- a/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java +++ b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java @@ -20,8 +20,7 @@ public class CacheConfiguration { public static final String TRANSACTIONAL_CACHE = "transactional-cache"; public DefaultCacheManager cacheManager() { - DefaultCacheManager cacheManager = new DefaultCacheManager(); - return cacheManager; + return new DefaultCacheManager(); } public Cache transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) { diff --git a/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java index 2f6536ad87..942a2fb62d 100644 --- a/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java +++ b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java @@ -40,9 +40,7 @@ public class CacheListener { @CacheEntriesEvicted public void entriesEvicted(CacheEntriesEvictedEvent event) { final StringBuilder builder = new StringBuilder(); - event.getEntries().entrySet().forEach((e) -> - builder.append(e.getKey() + ", ") - ); + event.getEntries().forEach((key, value) -> builder.append(key).append(", ")); System.out.println("Evicting following entries from cache: " + builder.toString()); } diff --git a/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java index 0d1ffb4168..3ecefcc21a 100644 --- a/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java +++ b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java @@ -31,12 +31,7 @@ public class HelloWorldService { public String findSimpleHelloWorld() { String cacheKey = "simple-hello"; - String helloWorld = simpleHelloWorldCache.get(cacheKey); - if (helloWorld == null) { - helloWorld = repository.getHelloWorld(); - simpleHelloWorldCache.put(cacheKey, helloWorld); - } - return helloWorld; + return simpleHelloWorldCache.computeIfAbsent(cacheKey, k -> repository.getHelloWorld()); } public String findExpiringHelloWorld() { @@ -79,12 +74,7 @@ public class HelloWorldService { } public String findPassivatingHelloWorld(String key) { - String value = passivatingHelloWorldCache.get(key); - if(value == null) { - value = repository.getHelloWorld(); - passivatingHelloWorldCache.put(key, value); - } - return value; + return passivatingHelloWorldCache.computeIfAbsent(key, k -> repository.getHelloWorld()); } } diff --git a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java index 906a887e1a..c9ebe77679 100644 --- a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java +++ b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java @@ -9,7 +9,7 @@ import org.infinispan.manager.DefaultCacheManager; import org.junit.After; import org.junit.Before; -import java.util.concurrent.Callable; +import java.util.function.Supplier; public class ConfigurationTest { @@ -47,7 +47,6 @@ public class ConfigurationTest { passivatingHelloWorldCache); this.transactionalService = new TransactionalService(transactionalCache); - } @After @@ -55,15 +54,9 @@ public class ConfigurationTest { cacheManager.stop(); } - protected long timeThis(Callable callable) { - try { - long milis = System.currentTimeMillis(); - callable.call(); - return System.currentTimeMillis() - milis; - } catch (Exception e) { - e.printStackTrace(); - } - return 0l; + protected long timeThis(Supplier supplier) { + long millis = System.currentTimeMillis(); + supplier.get(); + return System.currentTimeMillis() - millis; } - } diff --git a/microprofile/pom.xml b/microprofile/pom.xml new file mode 100644 index 0000000000..ce8a2d13ca --- /dev/null +++ b/microprofile/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + com.baeldung + microprofile + 1.0-SNAPSHOT + war + + + UTF-8 + UTF-8 + 1.8 + 1.8 + library + ${project.build.directory}/${app.name}-service.jar + runnable + + + + + org.eclipse.microprofile + microprofile + 1.2 + provided + pom + + + + + + + maven-war-plugin + + false + pom.xml + + + + net.wasdev.wlp.maven.plugins + liberty-maven-plugin + 2.1.2 + + + io.openliberty + openliberty-runtime + 17.0.0.4 + zip + + ${basedir}/src/main/liberty/config/server.xml + ${package.file} + ${packaging.type} + false + project + + / + ${project.artifactId}-${project.version}.war + 9080 + 9443 + + + + + install-server + prepare-package + + install-server + create-server + install-feature + + + + package-server-with-apps + package + + install-apps + package-server + + + + + + + + diff --git a/microprofile/src/main/java/com/baeldung/microprofile/LibraryApplication.java b/microprofile/src/main/java/com/baeldung/microprofile/LibraryApplication.java new file mode 100644 index 0000000000..f5eccf969e --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/LibraryApplication.java @@ -0,0 +1,8 @@ +package com.baeldung.microprofile; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/library") +public class LibraryApplication extends Application { +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/model/Book.java b/microprofile/src/main/java/com/baeldung/microprofile/model/Book.java new file mode 100644 index 0000000000..44b7f5428d --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/model/Book.java @@ -0,0 +1,50 @@ +package com.baeldung.microprofile.model; + +public class Book { + + private String id; + private String isbn; + private String name; + private String author; + private Integer pages; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public Integer getPages() { + return pages; + } + + public void setPages(Integer pages) { + this.pages = pages; + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java new file mode 100644 index 0000000000..f7d0bfc5f7 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java @@ -0,0 +1,42 @@ +package com.baeldung.microprofile.providers; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.util.BookMapper; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonWriter; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.List; + +@Provider +@Produces(MediaType.APPLICATION_JSON) +public class BookListMessageBodyWriter implements MessageBodyWriter> { + + @Override + public boolean isWriteable(Class clazz, Type genericType, Annotation[] annotations, MediaType mediaType) { + return true; + } + + @Override + public long getSize(List books, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + @Override + public void writeTo(List books, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + JsonWriter jsonWriter = Json.createWriter(entityStream); + JsonArray jsonArray = BookMapper.map(books); + jsonWriter.writeArray(jsonArray); + jsonWriter.close(); + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java new file mode 100644 index 0000000000..26ce4c1b64 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java @@ -0,0 +1,30 @@ +package com.baeldung.microprofile.providers; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.util.BookMapper; + +import javax.ws.rs.Consumes; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.Provider; +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +@Provider +@Consumes(MediaType.APPLICATION_JSON) +public class BookMessageBodyReader implements MessageBodyReader { + + @Override + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return type.equals(Book.class); + } + + @Override + public Book readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { + return BookMapper.map(entityStream); + } +} \ No newline at end of file diff --git a/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java new file mode 100644 index 0000000000..9bc6e89958 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java @@ -0,0 +1,57 @@ +package com.baeldung.microprofile.providers; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.util.BookMapper; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonWriter; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +@Provider +@Produces(MediaType.APPLICATION_JSON) +public class BookMessageBodyWriter implements MessageBodyWriter { + @Override + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return type.equals(Book.class); + } + + /* + Deprecated in JAX RS 2.0 + */ + @Override + public long getSize(Book book, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + /** + * Marsahl Book to OutputStream + * + * @param book + * @param type + * @param genericType + * @param annotations + * @param mediaType + * @param httpHeaders + * @param entityStream + * @throws IOException + * @throws WebApplicationException + */ + @Override + public void writeTo(Book book, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + JsonWriter jsonWriter = Json.createWriter(entityStream); + JsonObject jsonObject = BookMapper.map(book); + jsonWriter.writeObject(jsonObject); + jsonWriter.close(); + } + +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/repo/BookManager.java b/microprofile/src/main/java/com/baeldung/microprofile/repo/BookManager.java new file mode 100644 index 0000000000..924cf0ce71 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/repo/BookManager.java @@ -0,0 +1,53 @@ +package com.baeldung.microprofile.repo; + +import com.baeldung.microprofile.model.Book; + +import javax.enterprise.context.ApplicationScoped; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; + +@ApplicationScoped +public class BookManager { + + private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM"); + private AtomicInteger bookIdGenerator = new AtomicInteger(0); + + private ConcurrentMap inMemoryStore = new ConcurrentHashMap<>(); + + public BookManager() { + Book book = new Book(); + book.setId(getNextId()); + book.setName("Building Microservice With Eclipse MicroProfile"); + book.setIsbn("1"); + book.setAuthor("baeldung"); + book.setPages(420); + inMemoryStore.put(book.getId(), book); + } + + private String getNextId() { + String date = LocalDate.now().format(formatter); + return String.format("%04d-%s", bookIdGenerator.incrementAndGet(), date); + } + + public String add(Book book) { + String id = getNextId(); + book.setId(id); + inMemoryStore.put(id, book); + return id; + } + + public Book get(String id) { + return inMemoryStore.get(id); + } + + public List getAll() { + List books = new ArrayList<>(); + books.addAll(inMemoryStore.values()); + return books; + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/util/BookMapper.java b/microprofile/src/main/java/com/baeldung/microprofile/util/BookMapper.java new file mode 100644 index 0000000000..861b172299 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/util/BookMapper.java @@ -0,0 +1,72 @@ +package com.baeldung.microprofile.util; + +import com.baeldung.microprofile.model.Book; + +import javax.json.*; +import java.io.InputStream; +import java.util.List; + +public class BookMapper { + + public static JsonObject map(Book book) { + JsonObjectBuilder builder = Json.createObjectBuilder(); + addValue(builder, "id", book.getId()); + addValue(builder, "isbn", book.getIsbn()); + addValue(builder, "name", book.getName()); + addValue(builder, "author", book.getAuthor()); + addValue(builder, "pages", book.getPages()); + return builder.build(); + } + + private static void addValue(JsonObjectBuilder builder, String key, Object value) { + if (value != null) { + builder.add(key, value.toString()); + } else { + builder.addNull(key); + } + } + + public static JsonArray map(List books) { + final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); + books.forEach(book -> { + JsonObject jsonObject = map(book); + arrayBuilder.add(jsonObject); + }); + return arrayBuilder.build(); + } + + public static Book map(InputStream is) { + try(JsonReader jsonReader = Json.createReader(is)) { + JsonObject jsonObject = jsonReader.readObject(); + Book book = new Book(); + book.setId(getStringFromJson("id", jsonObject)); + book.setIsbn(getStringFromJson("isbn", jsonObject)); + book.setName(getStringFromJson("name", jsonObject)); + book.setAuthor(getStringFromJson("author", jsonObject)); + book.setPages(getIntFromJson("pages",jsonObject)); + return book; + } + } + + private static String getStringFromJson(String key, JsonObject json) { + String returnedString = null; + if (json.containsKey(key)) { + JsonString value = json.getJsonString(key); + if (value != null) { + returnedString = value.getString(); + } + } + return returnedString; + } + + private static Integer getIntFromJson(String key, JsonObject json) { + Integer returnedValue = null; + if (json.containsKey(key)) { + JsonNumber value = json.getJsonNumber(key); + if (value != null) { + returnedValue = value.intValue(); + } + } + return returnedValue; + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java b/microprofile/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java new file mode 100644 index 0000000000..13143a5644 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java @@ -0,0 +1,42 @@ +package com.baeldung.microprofile.web; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.repo.BookManager; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; + +@Path("books") +@RequestScoped +public class BookEndpoint { + + @Inject + private BookManager bookManager; + + @GET + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response getBook(@PathParam("id") String id) { + Book book = bookManager.get(id); + return Response.ok(book).build(); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getAllBooks() { + return Response.ok(bookManager.getAll()).build(); + } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + public Response add(Book book) { + String bookId = bookManager.add(book); + return Response.created( + UriBuilder.fromResource(this.getClass()).path(bookId).build()) + .build(); + } +} diff --git a/microprofile/src/main/liberty/config/server.xml b/microprofile/src/main/liberty/config/server.xml new file mode 100644 index 0000000000..2b855bee05 --- /dev/null +++ b/microprofile/src/main/liberty/config/server.xml @@ -0,0 +1,11 @@ + + + jaxrs-2.0 + cdi-1.2 + jsonp-1.0 + + + + + diff --git a/out/production/generated-sources8/src.main.resources.reladomo.ReladomoClassList.xml.log b/out/production/generated-sources8/src.main.resources.reladomo.ReladomoClassList.xml.log new file mode 100644 index 0000000000..0bd4e29a45 --- /dev/null +++ b/out/production/generated-sources8/src.main.resources.reladomo.ReladomoClassList.xml.log @@ -0,0 +1,2 @@ +f5a6ba3b942a82fcbfb72e61502d5c30 +9201deea diff --git a/out/production/introduction/views/index.scala.html b/out/production/introduction/views/index.scala.html new file mode 100644 index 0000000000..4539f5a10b --- /dev/null +++ b/out/production/introduction/views/index.scala.html @@ -0,0 +1,20 @@ +@* + * This template takes a single argument, a String containing a + * message to display. + *@ +@(message: String) + +@* + * Call the `main` template with two arguments. The first + * argument is a `String` with the title of the page, the second + * argument is an `Html` object containing the body of the page. + *@ +@main("Welcome to Play") { + + @* + * Get an `Html` object by calling the built-in Play welcome + * template and passing a `String` message. + *@ + @play20.welcome(message, style = "Java") + +} diff --git a/out/production/introduction/views/main.scala.html b/out/production/introduction/views/main.scala.html new file mode 100644 index 0000000000..9414f4be6e --- /dev/null +++ b/out/production/introduction/views/main.scala.html @@ -0,0 +1,23 @@ +@* + * This template is called from the `index` template. This template + * handles the rendering of the page header and body tags. It takes + * two arguments, a `String` for the title of the page and an `Html` + * object to insert into the body of the page. + *@ +@(title: String)(content: Html) + + + + + @* Here's where we render the page title `String`. *@ + @title + + + + + + @* And here's where we render the `Html` object containing + * the page content. *@ + @content + + diff --git a/out/production/main122/.gitignore b/out/production/main122/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/out/production/main122/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/out/production/main151/com/baeldung/.gitignore b/out/production/main151/com/baeldung/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/out/production/main151/com/baeldung/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/out/production/main151/com/baeldung/README.md b/out/production/main151/com/baeldung/README.md new file mode 100644 index 0000000000..51809b2882 --- /dev/null +++ b/out/production/main151/com/baeldung/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [SHA-256 Hashing in Java](http://www.baeldung.com/sha-256-hashing-java) diff --git a/out/production/main155/com/baeldung/git/README.md b/out/production/main155/com/baeldung/git/README.md new file mode 100644 index 0000000000..7e6a597c28 --- /dev/null +++ b/out/production/main155/com/baeldung/git/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Injecting Git Information Into Spring](http://www.baeldung.com/spring-git-information) diff --git a/out/production/main173/log4j.properties b/out/production/main173/log4j.properties new file mode 100644 index 0000000000..5fe42d854c --- /dev/null +++ b/out/production/main173/log4j.properties @@ -0,0 +1,9 @@ +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=DEBUG, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/README.txt b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/README.txt new file mode 100644 index 0000000000..bffe24e485 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/README.txt @@ -0,0 +1,76 @@ +About the application +--------------------- +This application demonstrates the usage of JavaEE Web Annotations. + + +Contents of the application +--------------------------- +1. AccountServlet.java - Demonstrates the @WebServlet and @ServletSecurity annotation. + +NOTES: @WebServlet annotation designates the AccountServlet class as a Servlet component. + The usage of its parameters 'urlPatterns' & 'initParams' can be observed. + An initialization parameter 'type' is being set to denote the type of the bank account. + + @ServletSecurity annotation imposes security constraints on the AccountServlet based on + the tomcat-users.xml. +   + This code assumes that your tomcat-users.xml looks as follows: + + + + + + + +   +N.B : To see @ServletSecurity annotation in action, please uncomment the annotation code + for @ServletSecurity. + + +2. BankAppServletContextListener.java - Demonstrates the @WebListener annotation for denoting a class as a ServletContextListener. + +NOTES: Sets a Servlet context attribute ATTR_DEFAULT_LANGUAGE to 'english' on web application start up, + which can then be used throughout the application. + + +3. LogInFilter.java - Demonstrates the @WebFilter annotation. + +NOTES: @WebFilter annotation designates the LogInFilter class as a Filter component. + It filters all requests to the bank account servlet and redirects them to + the login page. + +N.B : To see @WebFilter annotation in action, please uncomment the annotation code for @WebFilter. + + +4. UploadCustomerDocumentsServlet.java - Demonstrates the @MultipartConfig annotation. + +NOTES: @MultipartConfig anotation designates the UploadCustomerDocumentsServlet Servlet component, + to handle multipart/form-data requests. + To see it in action, deploy the web application an access the url: http://:/JavaEEAnnotationsSample/upload.jsp + Once you upload a file from here, it will get uploaded to D:/custDocs (assuming such a folder exists). + + +5. index.jsp - This is the welcome page. + +NOTES: You can enter a deposit amount here and click on the 'Deposit' button to see the AccountServlet in action. + +6. login.jsp - All requests to the AccountServlet are redirected to this page, if the LogInFilter is imposed. + +7. upload.jsp - Demonstrates the usage of handling multipart/form-data requests by the UploadCustomerDocumentsServlet. + + +Building and Running the application +------------------------------------ +To build the application: + +1. Open the project in eclipse +2. Right click on it in eclispe and choose Run As > Maven build +3. Give 'clean install' under Goals +4. This should build the WAR file of the application + +To run the application: + +1. Right click on the project +2. Run as > Run on Server +3. This will start you Tomcat server and deploy the application (Provided that you have configured Tomcat in your eclipse) +4. You should now be able to access the url : http://:/JavaEEAnnotationsSample diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/pom.xml b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/pom.xml new file mode 100644 index 0000000000..de69efa43a --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + com.baeldung.javaeeannotations + JavaEEAnnotationsSample + 0.0.1-SNAPSHOT + war + JavaEEAnnotationsSample + JavaEEAnnotationsSample + + + + + javax.annotation + javax.annotation-api + 1.3 + + + + javax.servlet + javax.servlet-api + 3.1.0 + + + + javax.servlet.jsp + jsp-api + 2.1 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + src/main/webapp + SpringFieldConstructorInjection + false + + + + + JavaEEAnnotationsSample + + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/WEB-INF/web.xml b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..a92885ec11 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,10 @@ + + + + BASIC + default + + diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/index.jsp b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/index.jsp new file mode 100644 index 0000000000..c49dec859e --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/index.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +My Account + + +
+ Amount: +    + +
+ + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/login.jsp b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/login.jsp new file mode 100644 index 0000000000..6892cb0420 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/login.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Login + + +Login Here... + + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/upload.jsp b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/upload.jsp new file mode 100644 index 0000000000..3601322ef0 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/upload.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Insert title here + + +
+ +
+ +
+ + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/jaxws/wsdl/employeeservicetopdown.wsdl b/out/production/main180/com/baeldung/jaxws/wsdl/employeeservicetopdown.wsdl new file mode 100644 index 0000000000..426717f90e --- /dev/null +++ b/out/production/main180/com/baeldung/jaxws/wsdl/employeeservicetopdown.wsdl @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/main195/com/baeldung/java/nio/selector/README.md b/out/production/main195/com/baeldung/java/nio/selector/README.md new file mode 100644 index 0000000000..b28aae1397 --- /dev/null +++ b/out/production/main195/com/baeldung/java/nio/selector/README.md @@ -0,0 +1,2 @@ +###Relevant Articles: +- [Introduction to the Java NIO Selector](http://www.baeldung.com/java-nio-selector) diff --git a/out/production/main216/com/baeldung/googlehttpclientguide/logging.properties b/out/production/main216/com/baeldung/googlehttpclientguide/logging.properties new file mode 100644 index 0000000000..02489378df --- /dev/null +++ b/out/production/main216/com/baeldung/googlehttpclientguide/logging.properties @@ -0,0 +1,10 @@ +# Properties file which configures the operation of the JDK logging facility. +# The system will look for this config file to be specified as a system property: +# -Djava.util.logging.config.file=${project_loc:dailymotion-simple-cmdline-sample}/logging.properties + +# Set up the console handler (uncomment "level" to show more fine-grained messages) +handlers = java.util.logging.ConsoleHandler +java.util.logging.ConsoleHandler.level = ALL + +# Set up logging of HTTP requests and responses (uncomment "level" to show) +com.google.api.client.http.level = ALL diff --git a/out/production/main231/com/baeldung/wicket/examples/HelloWorld.html b/out/production/main231/com/baeldung/wicket/examples/HelloWorld.html new file mode 100644 index 0000000000..497e98e01a --- /dev/null +++ b/out/production/main231/com/baeldung/wicket/examples/HelloWorld.html @@ -0,0 +1,52 @@ + + + + +Wicket Intro Examples + + + +
+
+
+

Wicket Introduction Examples:

+ + Hello World! +
+
+ Cafes +
+
+
+
+ + diff --git a/out/production/main231/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html b/out/production/main231/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html new file mode 100644 index 0000000000..c5ada2323d --- /dev/null +++ b/out/production/main231/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html @@ -0,0 +1,15 @@ + + + + +Cafes + + +
+ +

+ Address: address +

+
+ + diff --git a/out/production/main231/com/baeldung/wicket/examples/helloworld/HelloWorld.html b/out/production/main231/com/baeldung/wicket/examples/helloworld/HelloWorld.html new file mode 100644 index 0000000000..c56d07fc10 --- /dev/null +++ b/out/production/main231/com/baeldung/wicket/examples/helloworld/HelloWorld.html @@ -0,0 +1,5 @@ + + + + + diff --git a/out/production/main234/com/baeldung/activiti/security.rar b/out/production/main234/com/baeldung/activiti/security.rar new file mode 100644 index 0000000000..38c4946168 Binary files /dev/null and b/out/production/main234/com/baeldung/activiti/security.rar differ diff --git a/out/production/main237/com/baeldung/datetime/README.md b/out/production/main237/com/baeldung/datetime/README.md new file mode 100644 index 0000000000..1e4adbb612 --- /dev/null +++ b/out/production/main237/com/baeldung/datetime/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro) diff --git a/out/production/main291/xml-bean-config.xml b/out/production/main291/xml-bean-config.xml new file mode 100644 index 0000000000..3b880bbd70 --- /dev/null +++ b/out/production/main291/xml-bean-config.xml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/out/production/main30/com/baeldung/factorybean/README.md b/out/production/main30/com/baeldung/factorybean/README.md new file mode 100644 index 0000000000..13f9f379e0 --- /dev/null +++ b/out/production/main30/com/baeldung/factorybean/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean) diff --git a/out/production/main330/com/baeldung/.gitignore b/out/production/main330/com/baeldung/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/out/production/main330/com/baeldung/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/out/production/main330/com/baeldung/README.md b/out/production/main330/com/baeldung/README.md new file mode 100644 index 0000000000..51809b2882 --- /dev/null +++ b/out/production/main330/com/baeldung/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [SHA-256 Hashing in Java](http://www.baeldung.com/sha-256-hashing-java) diff --git a/out/production/main330/com/baeldung/enums/README.md b/out/production/main330/com/baeldung/enums/README.md new file mode 100644 index 0000000000..6ccfa725f5 --- /dev/null +++ b/out/production/main330/com/baeldung/enums/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [A Guide to Java Enums](http://www.baeldung.com/a-guide-to-java-enums) diff --git a/out/production/main330/com/baeldung/networking/README.md b/out/production/main330/com/baeldung/networking/README.md new file mode 100644 index 0000000000..b9e827f085 --- /dev/null +++ b/out/production/main330/com/baeldung/networking/README.md @@ -0,0 +1,5 @@ +### Relevant Articles: +- [A Guide To UDP In Java](http://www.baeldung.com/udp-in-java) +- [A Guide To HTTP Cookies In Java](http://www.baeldung.com/cookies-java) +- [A Guide to the Java URL](http://www.baeldung.com/java-url) +- [Working with Network Interfaces in Java](http://www.baeldung.com/java-network-interfaces) diff --git a/out/production/main330/com/baeldung/printscreen/README.md b/out/production/main330/com/baeldung/printscreen/README.md new file mode 100644 index 0000000000..7b3b40c102 --- /dev/null +++ b/out/production/main330/com/baeldung/printscreen/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) diff --git a/out/production/main330/log4j.properties b/out/production/main330/log4j.properties new file mode 100644 index 0000000000..5fe42d854c --- /dev/null +++ b/out/production/main330/log4j.properties @@ -0,0 +1,9 @@ +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=DEBUG, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n diff --git a/out/production/main351/com/baeldung/produceimage/README.md b/out/production/main351/com/baeldung/produceimage/README.md new file mode 100644 index 0000000000..acd546598d --- /dev/null +++ b/out/production/main351/com/baeldung/produceimage/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Returning an Image or a File with Spring](http://www.baeldung.com/spring-controller-return-image-file) diff --git a/out/production/main96/com/baeldung/git/README.md b/out/production/main96/com/baeldung/git/README.md new file mode 100644 index 0000000000..7e6a597c28 --- /dev/null +++ b/out/production/main96/com/baeldung/git/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Injecting Git Information Into Spring](http://www.baeldung.com/spring-git-information) diff --git a/out/production/routing-in-play/views/index.scala.html b/out/production/routing-in-play/views/index.scala.html new file mode 100644 index 0000000000..4539f5a10b --- /dev/null +++ b/out/production/routing-in-play/views/index.scala.html @@ -0,0 +1,20 @@ +@* + * This template takes a single argument, a String containing a + * message to display. + *@ +@(message: String) + +@* + * Call the `main` template with two arguments. The first + * argument is a `String` with the title of the page, the second + * argument is an `Html` object containing the body of the page. + *@ +@main("Welcome to Play") { + + @* + * Get an `Html` object by calling the built-in Play welcome + * template and passing a `String` message. + *@ + @play20.welcome(message, style = "Java") + +} diff --git a/out/production/routing-in-play/views/main.scala.html b/out/production/routing-in-play/views/main.scala.html new file mode 100644 index 0000000000..9414f4be6e --- /dev/null +++ b/out/production/routing-in-play/views/main.scala.html @@ -0,0 +1,23 @@ +@* + * This template is called from the `index` template. This template + * handles the rendering of the page header and body tags. It takes + * two arguments, a `String` for the title of the page and an `Html` + * object to insert into the body of the page. + *@ +@(title: String)(content: Html) + + + + + @* Here's where we render the page title `String`. *@ + @title + + + + + + @* And here's where we render the `Html` object containing + * the page content. *@ + @content + + diff --git a/out/test/test105/com/baeldung/cglib/proxy/README.md b/out/test/test105/com/baeldung/cglib/proxy/README.md new file mode 100644 index 0000000000..abeabc6162 --- /dev/null +++ b/out/test/test105/com/baeldung/cglib/proxy/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Introduction to cglib](http://www.baeldung.com/cglib) diff --git a/out/test/test143/com/baeldung/java9/README.MD b/out/test/test143/com/baeldung/java9/README.MD new file mode 100644 index 0000000000..2f44a2336b --- /dev/null +++ b/out/test/test143/com/baeldung/java9/README.MD @@ -0,0 +1,2 @@ +### Relevant Artiles: +- [Filtering a Stream of Optionals in Java](http://www.baeldung.com/java-filter-stream-of-optional) diff --git a/out/test/test174/org/baeldung/hamcrest/README.md b/out/test/test174/org/baeldung/hamcrest/README.md new file mode 100644 index 0000000000..7266ecda3a --- /dev/null +++ b/out/test/test174/org/baeldung/hamcrest/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Testing with Hamcrest](http://www.baeldung.com/java-junit-hamcrest-guide) diff --git a/out/test/test191/com/baeldung/web/controller/README.md b/out/test/test191/com/baeldung/web/controller/README.md new file mode 100644 index 0000000000..9923962dde --- /dev/null +++ b/out/test/test191/com/baeldung/web/controller/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [WebAppConfiguration in Spring Tests](http://www.baeldung.com/spring-webappconfiguration) diff --git a/out/test/test197/com/baeldung/java/nio2/README.md b/out/test/test197/com/baeldung/java/nio2/README.md new file mode 100644 index 0000000000..569be82d27 --- /dev/null +++ b/out/test/test197/com/baeldung/java/nio2/README.md @@ -0,0 +1,11 @@ +### Relevant Articles: +- [Introduction to the Java NIO2 File API](http://www.baeldung.com/java-nio-2-file-api) +- [Java NIO2 Path API](http://www.baeldung.com/java-nio-2-path) +- [A Guide To NIO2 Asynchronous File Channel](http://www.baeldung.com/java-nio2-async-file-channel) +- [Guide to Selenium with JUnit / TestNG](http://www.baeldung.com/java-selenium-with-junit-and-testng) +- [A Guide to NIO2 Asynchronous Socket Channel](http://www.baeldung.com/java-nio2-async-socket-channel) +- [A Guide To NIO2 FileVisitor](http://www.baeldung.com/java-nio2-file-visitor) +- [A Guide To NIO2 File Attribute APIs](http://www.baeldung.com/java-nio2-file-attribute) +- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean) +- [A Guide to WatchService in Java NIO2](http://www.baeldung.com/java-nio2-watchservice) +- [Guide to Java NIO2 Asynchronous Channel APIs](http://www.baeldung.com/java-nio-2-async-channels) diff --git a/out/test/test237/META-INF/persistence.xml b/out/test/test237/META-INF/persistence.xml new file mode 100644 index 0000000000..922aedbc39 --- /dev/null +++ b/out/test/test237/META-INF/persistence.xml @@ -0,0 +1,20 @@ + + + + org.baeldung.persistence.model.Foo + org.baeldung.persistence.model.Bar + + + + + + + + + + + + + diff --git a/out/test/test95/com/baeldung/hexToAscii/README.md b/out/test/test95/com/baeldung/hexToAscii/README.md new file mode 100644 index 0000000000..c6d5826333 --- /dev/null +++ b/out/test/test95/com/baeldung/hexToAscii/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Convert Hex to ASCII in Java](http://www.baeldung.com/java-convert-hex-to-ascii) diff --git a/out/test/test95/com/baeldung/java/conversion/README.md b/out/test/test95/com/baeldung/java/conversion/README.md new file mode 100644 index 0000000000..7c81180249 --- /dev/null +++ b/out/test/test95/com/baeldung/java/conversion/README.md @@ -0,0 +1,2 @@ +Relevant Articles: +- [Java String Conversions](http://www.baeldung.com/java-string-conversions) diff --git a/out/test/test95/org/baeldung/java/collections/README.md b/out/test/test95/org/baeldung/java/collections/README.md new file mode 100644 index 0000000000..317d81fae7 --- /dev/null +++ b/out/test/test95/org/baeldung/java/collections/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: +- [Join and Split Arrays and Collections in Java](http://www.baeldung.com/java-join-and-split) +- [Introduction to Java Servlets](http://www.baeldung.com/intro-to-servlets) diff --git a/out/test/test95/org/baeldung/java/lists/README.md b/out/test/test95/org/baeldung/java/lists/README.md new file mode 100644 index 0000000000..2a1e8aeeaa --- /dev/null +++ b/out/test/test95/org/baeldung/java/lists/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Check If Two Lists are Equal in Java](http://www.baeldung.com/java-test-a-list-for-ordinality-and-equality) diff --git a/out/test/test98/com/baeldung/applicationcontext/README.md b/out/test/test98/com/baeldung/applicationcontext/README.md new file mode 100644 index 0000000000..211007e0cf --- /dev/null +++ b/out/test/test98/com/baeldung/applicationcontext/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: +- [Introduction to Java Servlets](http://www.baeldung.com/intro-to-servlets) +- [Intro to the Spring ClassPathXmlApplicationContext](http://www.baeldung.com/spring-classpathxmlapplicationcontext) diff --git a/out/test/test98/com/baeldung/beanfactory/README.md b/out/test/test98/com/baeldung/beanfactory/README.md new file mode 100644 index 0000000000..cff20a184b --- /dev/null +++ b/out/test/test98/com/baeldung/beanfactory/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Guide to the Spring BeanFactory](http://www.baeldung.com/spring-beanfactory) diff --git a/persistence-modules/spring-data-dynamodb/pom.xml b/persistence-modules/spring-data-dynamodb/pom.xml index 0b78aac10e..c3ea9abf08 100644 --- a/persistence-modules/spring-data-dynamodb/pom.xml +++ b/persistence-modules/spring-data-dynamodb/pom.xml @@ -26,7 +26,7 @@ 1.0.392 1.11.106 1.11.86 - https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/release + https://s3-us-west-2.amazonaws.com/dynamodb-local/release diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java index 8052aba3df..6cbd5b0a5a 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java @@ -8,7 +8,7 @@ import com.amazonaws.services.dynamodbv2.model.ResourceInUseException; import com.baeldung.Application; import com.baeldung.spring.data.dynamodb.model.ProductInfo; import com.baeldung.spring.data.dynamodb.repositories.ProductInfoRepository; -import com.baeldung.spring.data.dynamodb.repository.rule.LocalDynamoDBCreationRule; +import com.baeldung.spring.data.dynamodb.repository.rule.LocalDbCreationRule; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; @@ -35,7 +35,7 @@ import static org.junit.Assert.assertThat; public class ProductInfoRepositoryIntegrationTest { @ClassRule - public static LocalDynamoDBCreationRule dynamoDB = new LocalDynamoDBCreationRule(); + public static LocalDbCreationRule dynamoDB = new LocalDbCreationRule(); private DynamoDBMapper dynamoDBMapper; diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDbCreationRule.java similarity index 83% rename from persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java rename to persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDbCreationRule.java index 62334b6d00..555d558b06 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDbCreationRule.java @@ -6,11 +6,11 @@ import org.junit.rules.ExternalResource; import java.util.Optional; -public class LocalDynamoDBCreationRule extends ExternalResource { +public class LocalDbCreationRule extends ExternalResource { protected DynamoDBProxyServer server; - public LocalDynamoDBCreationRule() { + public LocalDbCreationRule() { System.setProperty("sqlite4java.library.path", "native-libs"); } @@ -23,7 +23,7 @@ public class LocalDynamoDBCreationRule extends ExternalResource { @Override protected void after() { - Optional.ofNullable(server).ifPresent(this::stopUnchecked); + this.stopUnchecked(server); } protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) { diff --git a/pom.xml b/pom.xml index fc0c8f8ba7..d29f441a7e 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,7 @@ core-java core-java-io core-java-8 + core-groovy diff --git a/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java index 9d5d094875..81dfda3bb3 100644 --- a/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java +++ b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java @@ -17,7 +17,7 @@ public class CombiningPublishersTest { @Test - public void testMerge() { + public void givenFluxes_whenMergeIsInvoked_thenMerge() { Flux fluxOfIntegers = Flux.merge( evenNumbers, oddNumbers); @@ -33,7 +33,7 @@ public class CombiningPublishersTest { } @Test - public void testMergeWithDelayedElements() { + public void givenFluxes_whenMergeWithDelayedElementsIsInvoked_thenMergeWithDelayedElements() { Flux fluxOfIntegers = Flux.merge( evenNumbers.delayElements(Duration.ofMillis(500L)), oddNumbers.delayElements(Duration.ofMillis(300L))); @@ -49,7 +49,7 @@ public class CombiningPublishersTest { } @Test - public void testConcat() { + public void givenFluxes_whenConcatIsInvoked_thenConcat() { Flux fluxOfIntegers = Flux.concat( evenNumbers.delayElements(Duration.ofMillis(500L)), oddNumbers.delayElements(Duration.ofMillis(300L))); @@ -65,7 +65,7 @@ public class CombiningPublishersTest { } @Test - public void testConcatWith() { + public void givenFluxes_whenConcatWithIsInvoked_thenConcatWith() { Flux fluxOfIntegers = evenNumbers .concatWith(oddNumbers); @@ -80,7 +80,7 @@ public class CombiningPublishersTest { } @Test - public void testCombineLatest() { + public void givenFluxes_whenCombineLatestIsInvoked_thenCombineLatest() { Flux fluxOfIntegers = Flux.combineLatest( evenNumbers, oddNumbers, @@ -96,7 +96,7 @@ public class CombiningPublishersTest { } @Test - public void testCombineLatest1() { + public void givenFluxes_whenCombineLatestIsInvoked_thenCombineLatest1() { StepVerifier.create(Flux.combineLatest(obj -> (int) obj[1], evenNumbers, oddNumbers)) .expectNext(1) .expectNext(3) @@ -105,7 +105,7 @@ public class CombiningPublishersTest { } @Test - public void testMergeSequential() { + public void givenFluxes_whenMergeSequentialIsInvoked_thenMergeSequential() { Flux fluxOfIntegers = Flux.mergeSequential( evenNumbers, oddNumbers); @@ -122,7 +122,7 @@ public class CombiningPublishersTest { @Test - public void testMergeDelayError() { + public void givenFluxes_whenMergeDelayErrorIsInvoked_thenMergeDelayError() { Flux fluxOfIntegers = Flux.mergeDelayError(1, evenNumbers.delayElements(Duration.ofMillis(500L)), oddNumbers.delayElements(Duration.ofMillis(300L))); @@ -138,7 +138,7 @@ public class CombiningPublishersTest { } @Test - public void testMergeWith() { + public void givenFluxes_whenMergeWithIsInvoked_thenMergeWith() { Flux fluxOfIntegers = evenNumbers.mergeWith(oddNumbers); StepVerifier.create(fluxOfIntegers) @@ -152,7 +152,7 @@ public class CombiningPublishersTest { } @Test - public void testZip() { + public void givenFluxes_whenZipIsInvoked_thenZip() { Flux fluxOfIntegers = Flux.zip( evenNumbers, oddNumbers, @@ -166,7 +166,7 @@ public class CombiningPublishersTest { } @Test - public void testZipWith() { + public void givenFluxes_whenZipWithIsInvoked_thenZipWith() { Flux fluxOfIntegers = evenNumbers .zipWith(oddNumbers, (a, b) -> a * b); @@ -177,6 +177,4 @@ public class CombiningPublishersTest { .expectComplete() .verify(); } - - } diff --git a/rxjava/pom.xml b/rxjava/pom.xml index 0f950914ff..a6c5e9d2fb 100644 --- a/rxjava/pom.xml +++ b/rxjava/pom.xml @@ -31,6 +31,13 @@ 1.0.0 + + junit + junit + 4.12 + test + + com.jayway.awaitility awaitility diff --git a/rxjava/src/test/java/com/baeldung/rxjava/FlowableTest.java b/rxjava/src/test/java/com/baeldung/rxjava/FlowableTest.java new file mode 100644 index 0000000000..b9d1d64c24 --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/FlowableTest.java @@ -0,0 +1,93 @@ +package com.baeldung.rxjava; + +import io.reactivex.BackpressureStrategy; +import io.reactivex.Flowable; +import io.reactivex.FlowableOnSubscribe; +import io.reactivex.Observable; +import io.reactivex.exceptions.MissingBackpressureException; +import io.reactivex.schedulers.Schedulers; +import io.reactivex.subscribers.TestSubscriber; + +import org.junit.Test; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class FlowableTest { + + @Test public void whenFlowableIsCreated_thenItIsProperlyInitialized() { + Flowable integerFlowable = Flowable.just(1, 2, 3, 4); + assertNotNull(integerFlowable); + } + + @Test public void whenFlowableIsCreatedFromObservable_thenItIsProperlyInitialized() throws InterruptedException { + Observable integerObservable = Observable.just(1, 2, 3); + Flowable integerFlowable = integerObservable.toFlowable(BackpressureStrategy.BUFFER); + assertNotNull(integerFlowable); + + } + + @Test public void whenFlowableIsCreatedFromFlowableOnSubscribe_thenItIsProperlyInitialized() throws InterruptedException { + FlowableOnSubscribe flowableOnSubscribe = flowableEmitter -> flowableEmitter.onNext(1); + Flowable integerFlowable = Flowable.create(flowableOnSubscribe, BackpressureStrategy.BUFFER); + assertNotNull(integerFlowable); + } + + @Test public void thenAllValuesAreBufferedAndReceived() { + List testList = IntStream.range(0, 100000).boxed().collect(Collectors.toList()); + Observable observable = Observable.fromIterable(testList); + TestSubscriber testSubscriber = observable.toFlowable(BackpressureStrategy.BUFFER).observeOn(Schedulers.computation()).test(); + + testSubscriber.awaitTerminalEvent(); + + List receivedInts = testSubscriber.getEvents().get(0).stream().mapToInt(object -> (int) object).boxed().collect(Collectors.toList()); + + assertEquals(testList, receivedInts); + } + + @Test public void whenDropStrategyUsed_thenOnBackpressureDropped() { + List testList = IntStream.range(0, 100000).boxed().collect(Collectors.toList()); + + Observable observable = Observable.fromIterable(testList); + TestSubscriber testSubscriber = observable.toFlowable(BackpressureStrategy.DROP).observeOn(Schedulers.computation()).test(); + testSubscriber.awaitTerminalEvent(); + List receivedInts = testSubscriber.getEvents().get(0).stream().mapToInt(object -> (int) object).boxed().collect(Collectors.toList()); + + assertThat(receivedInts.size() < testList.size()); + assertThat(!receivedInts.contains(100000)); + } + + @Test public void whenMissingStrategyUsed_thenException() { + Observable observable = Observable.range(1, 100000); + TestSubscriber subscriber = observable.toFlowable(BackpressureStrategy.MISSING).observeOn(Schedulers.computation()).test(); + + subscriber.awaitTerminalEvent(); + subscriber.assertError(MissingBackpressureException.class); + } + + @Test public void whenErrorStrategyUsed_thenExceptionIsThrown() { + Observable observable = Observable.range(1, 100000); + TestSubscriber subscriber = observable.toFlowable(BackpressureStrategy.ERROR).observeOn(Schedulers.computation()).test(); + + subscriber.awaitTerminalEvent(); + subscriber.assertError(MissingBackpressureException.class); + } + + @Test public void whenLatestStrategyUsed_thenTheLastElementReceived() { + List testList = IntStream.range(0, 100000).boxed().collect(Collectors.toList()); + Observable observable = Observable.fromIterable(testList); + TestSubscriber testSubscriber = observable.toFlowable(BackpressureStrategy.LATEST).observeOn(Schedulers.computation()).test(); + + testSubscriber.awaitTerminalEvent(); + List receivedInts = testSubscriber.getEvents().get(0).stream().mapToInt(object -> (int) object).boxed().collect(Collectors.toList()); + + assertThat(receivedInts.size() < testList.size()); + assertThat(receivedInts.contains(100000)); + } + +} \ No newline at end of file diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/ChunksConfig.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/ChunksConfig.java new file mode 100644 index 0000000000..601ffb7f27 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/ChunksConfig.java @@ -0,0 +1,90 @@ +package org.baeldung.taskletsvschunks.config; + +import org.baeldung.taskletsvschunks.chunks.LineProcessor; +import org.baeldung.taskletsvschunks.chunks.LineReader; +import org.baeldung.taskletsvschunks.chunks.LinesWriter; +import org.baeldung.taskletsvschunks.model.Line; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +@EnableBatchProcessing +public class ChunksConfig { + + @Autowired private JobBuilderFactory jobs; + + @Autowired private StepBuilderFactory steps; + + @Bean + public JobLauncherTestUtils jobLauncherTestUtils() { + return new JobLauncherTestUtils(); + } + + @Bean + public JobRepository jobRepository() throws Exception { + MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(); + factory.setTransactionManager(transactionManager()); + return (JobRepository) factory.getObject(); + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new ResourcelessTransactionManager(); + } + + @Bean + public JobLauncher jobLauncher() throws Exception { + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + jobLauncher.setJobRepository(jobRepository()); + return jobLauncher; + } + + @Bean + public ItemReader itemReader() { + return new LineReader(); + } + + @Bean + public ItemProcessor itemProcessor() { + return new LineProcessor(); + } + + @Bean + public ItemWriter itemWriter() { + return new LinesWriter(); + } + + @Bean + protected Step processLines(ItemReader reader, ItemProcessor processor, ItemWriter writer) { + return steps.get("processLines"). chunk(2) + .reader(reader) + .processor(processor) + .writer(writer) + .build(); + } + + @Bean + public Job job() { + return jobs + .get("chunksJob") + .start(processLines(itemReader(), itemProcessor(), itemWriter())) + .build(); + } + +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/TaskletsConfig.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/TaskletsConfig.java new file mode 100644 index 0000000000..b9d06d2639 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/TaskletsConfig.java @@ -0,0 +1,103 @@ +package org.baeldung.taskletsvschunks.config; + +import org.baeldung.taskletsvschunks.tasklets.LinesProcessor; +import org.baeldung.taskletsvschunks.tasklets.LinesReader; +import org.baeldung.taskletsvschunks.tasklets.LinesWriter; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +@EnableBatchProcessing +public class TaskletsConfig { + + @Autowired private JobBuilderFactory jobs; + + @Autowired private StepBuilderFactory steps; + + @Bean + public JobLauncherTestUtils jobLauncherTestUtils() { + return new JobLauncherTestUtils(); + } + + @Bean + public JobRepository jobRepository() throws Exception { + MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(); + factory.setTransactionManager(transactionManager()); + return (JobRepository) factory.getObject(); + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new ResourcelessTransactionManager(); + } + + @Bean + public JobLauncher jobLauncher() throws Exception { + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + jobLauncher.setJobRepository(jobRepository()); + return jobLauncher; + } + + @Bean + public LinesReader linesReader() { + return new LinesReader(); + } + + @Bean + public LinesProcessor linesProcessor() { + return new LinesProcessor(); + } + + @Bean + public LinesWriter linesWriter() { + return new LinesWriter(); + } + + @Bean + protected Step readLines() { + return steps + .get("readLines") + .tasklet(linesReader()) + .build(); + } + + @Bean + protected Step processLines() { + return steps + .get("processLines") + .tasklet(linesProcessor()) + .build(); + } + + @Bean + protected Step writeLines() { + return steps + .get("writeLines") + .tasklet(linesWriter()) + .build(); + } + + @Bean + public Job job() { + return jobs + .get("taskletsJob") + .start(readLines()) + .next(processLines()) + .next(writeLines()) + .build(); + } + +} diff --git a/spring-batch/src/main/resources/taskletsvschunks/chunks.xml b/spring-batch/src/main/resources/taskletsvschunks/chunks.xml deleted file mode 100644 index f4b77ac10c..0000000000 --- a/spring-batch/src/main/resources/taskletsvschunks/chunks.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml b/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml deleted file mode 100644 index 34ce2944bc..0000000000 --- a/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java index 34b6315dc4..2a71970637 100644 --- a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java @@ -1,5 +1,6 @@ package org.baeldung.taskletsvschunks.chunks; +import org.baeldung.taskletsvschunks.config.ChunksConfig; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -11,7 +12,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = "classpath:/taskletsvschunks/chunks.xml") +@ContextConfiguration(classes = ChunksConfig.class) public class ChunksTest { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java index 53731feed4..20379b58fe 100644 --- a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java @@ -1,5 +1,6 @@ package org.baeldung.taskletsvschunks.tasklets; +import org.baeldung.taskletsvschunks.config.TaskletsConfig; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -11,7 +12,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = "classpath:/taskletsvschunks/tasklets.xml") +@ContextConfiguration(classes = TaskletsConfig.class) public class TaskletsTest { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore b/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore new file mode 100644 index 0000000000..f675857856 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore @@ -0,0 +1,2 @@ +/target/ +/.settings/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml new file mode 100644 index 0000000000..4805f5296c --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml @@ -0,0 +1,81 @@ + + 4.0.0 + org.baeldung.cloud + springcloudtask + 0.0.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + com.baeldung.TaskDemo + UTF-8 + UTF-8 + 1.8 + 1.2.2.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.springframework.cloud + spring-cloud-starter-task + + + + org.springframework.cloud + spring-cloud-task-core + + + + org.springframework.boot + spring-boot-starter-batch + + + + org.springframework.cloud + spring-cloud-task-batch + + + + + + + org.springframework.cloud + spring-cloud-task-dependencies + ${spring-cloud-task.version} + pom + import + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java new file mode 100644 index 0000000000..dc2ba53138 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java @@ -0,0 +1,14 @@ +package com.baeldung.task; + +import javax.sql.DataSource; + +import org.springframework.cloud.task.configuration.DefaultTaskConfigurer; + +public class HelloWorldTaskConfigurer + extends + DefaultTaskConfigurer { + + public HelloWorldTaskConfigurer(DataSource dataSource) { + super(dataSource); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java new file mode 100644 index 0000000000..6c215ad53d --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java @@ -0,0 +1,105 @@ +package com.baeldung.task; + +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class JobConfiguration { + + private final static Logger LOGGER = Logger + .getLogger(JobConfiguration.class.getName()); + + @Autowired + private JobBuilderFactory jobBuilderFactory; + + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Step step1() { + return this.stepBuilderFactory.get("job1step1") + .tasklet(new Tasklet() { + @Override + public RepeatStatus execute( + StepContribution contribution, + ChunkContext chunkContext) + throws Exception { + LOGGER.info("Tasklet has run"); + return RepeatStatus.FINISHED; + } + }).build(); + } + + @Bean + public Step step2() { + return this.stepBuilderFactory + .get("job1step2") + . chunk(3) + .reader( + new ListItemReader<>(Arrays.asList("7", + "2", "3", "10", "5", "6"))) + .processor( + new ItemProcessor() { + @Override + public String process(String item) + throws Exception { + LOGGER.info("Processing of chunks"); + return String.valueOf(Integer + .parseInt(item) * -1); + } + }) + .writer(new ItemWriter() { + @Override + public void write( + List items) + throws Exception { + for (String item : items) { + LOGGER.info(">> " + item); + } + } + }).build(); + } + + @Bean + public Job job1() { + return this.jobBuilderFactory.get("job1") + .start(step1()) + .next(step2()) + .build(); + } + + @Bean + public Job job2() { + return jobBuilderFactory.get("job2") + .start(stepBuilderFactory.get("job2step1") + .tasklet(new Tasklet() { + @Override + public RepeatStatus execute( + StepContribution contribution, + ChunkContext chunkContext) + throws Exception { + LOGGER + .info("This job is from Baeldung"); + return RepeatStatus.FINISHED; + } + }) + .build()) + .build(); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java new file mode 100644 index 0000000000..be2a173589 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java @@ -0,0 +1,56 @@ +package com.baeldung.task; + +import java.util.logging.Logger; + +import javax.sql.DataSource; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.task.configuration.EnableTask; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +@SpringBootApplication +@EnableTask +@EnableBatchProcessing +public class TaskDemo { + + private final static Logger LOGGER = Logger + .getLogger(TaskDemo.class.getName()); + + @Autowired + private DataSource dataSource; + + @Bean + public HelloWorldTaskConfigurer getTaskConfigurer() + { + return new HelloWorldTaskConfigurer(dataSource); + } + + @Bean + public TaskListener taskListener() { + return new TaskListener(); + } + + public static void main(String[] args) { + SpringApplication.run(TaskDemo.class, args); + } + + @Component + public static class HelloWorldApplicationRunner + implements + ApplicationRunner { + + @Override + public void run(ApplicationArguments arg0) + throws Exception { + // TODO Auto-generated method stub + LOGGER + .info("Hello World from Spring Cloud Task!"); + } + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java new file mode 100644 index 0000000000..8250153b66 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java @@ -0,0 +1,31 @@ +package com.baeldung.task; + +import java.util.logging.Logger; + +import org.springframework.cloud.task.listener.TaskExecutionListener; +import org.springframework.cloud.task.repository.TaskExecution; + +public class TaskListener implements TaskExecutionListener { + + private final static Logger LOGGER = Logger + .getLogger(TaskListener.class.getName()); + + @Override + public void onTaskEnd(TaskExecution arg0) { + // TODO Auto-generated method stub + LOGGER.info("End of Task"); + } + + @Override + public void onTaskFailed(TaskExecution arg0, + Throwable arg1) { + // TODO Auto-generated method stub + + } + + @Override + public void onTaskStartup(TaskExecution arg0) { + // TODO Auto-generated method stub + LOGGER.info("Task Startup"); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml new file mode 100644 index 0000000000..1187c12fe7 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml @@ -0,0 +1,26 @@ +logging: + level: + org: + springframework: + cloud: + task=DEBUG + +spring: + application: + name=helloWorld + datasource: + url: jdbc:mysql://localhost:3306/springcloud?useSSL=false + username: root + password: + jpa: + hibernate: + ddl-auto: create-drop + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL5Dialect + batch: + initialize-schema: always +maven: + remoteRepositories: + springRepo: + url: https://repo.spring.io/libs-snapshot \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore b/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml new file mode 100644 index 0000000000..b717fffc7c --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + com.baeldung + SpringCloudTaskSink + 0.0.1-SNAPSHOT + jar + + SpringCloudTaskSink + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + 1.2.2.RELEASE + Edgware.SR2 + + + + + org.springframework.cloud + spring-cloud-starter-stream-rabbit + + + org.springframework.cloud + spring-cloud-starter-task + + + org.springframework.cloud + spring-cloud-stream-test-support + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-deployer-local + 1.3.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-task-dependencies + ${spring-cloud-task.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java new file mode 100644 index 0000000000..62085e5fa5 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.SpringCloudTaskFinal; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.task.launcher.annotation.EnableTaskLauncher; + +@SpringBootApplication +@EnableTaskLauncher +public class SpringCloudTaskSinkApplication { + + public static void main(String[] args) { + SpringApplication.run( + SpringCloudTaskSinkApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java new file mode 100644 index 0000000000..638de58ecb --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java @@ -0,0 +1,18 @@ +package com.baeldung.SpringCloudTaskFinal; + +import static org.mockito.Mockito.mock; + +import org.springframework.cloud.deployer.spi.task.TaskLauncher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +public class TaskSinkConfiguration { + + @Bean + public TaskLauncher taskLauncher() { + return mock(TaskLauncher.class); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties new file mode 100644 index 0000000000..1660dc8516 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties @@ -0,0 +1 @@ +maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java new file mode 100644 index 0000000000..1f47eada14 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java @@ -0,0 +1,72 @@ +package com.baeldung.SpringCloudTaskFinal; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest; +import org.springframework.cloud.deployer.spi.task.TaskLauncher; +import org.springframework.cloud.stream.messaging.Sink; +import org.springframework.cloud.task.launcher.TaskLaunchRequest; +import org.springframework.context.ApplicationContext; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; + +@RunWith(SpringRunner.class) +@SpringBootTest( + classes = SpringCloudTaskSinkApplication.class) +public class SpringCloudTaskSinkApplicationTests { + + @Autowired + ApplicationContext context; + + @Autowired + private Sink sink; + + @Test + public void testTaskLaunch() throws IOException { + + TaskLauncher taskLauncher = + context.getBean(TaskLauncher.class); + + Map prop = new HashMap(); + prop.put("server.port", "0"); + TaskLaunchRequest request = new TaskLaunchRequest( + "maven://org.springframework.cloud.task.app:" + + "timestamp-task:jar:1.0.1.RELEASE", null, + prop, + null, null); + GenericMessage message = new GenericMessage( + request); + this.sink.input().send(message); + + ArgumentCaptor deploymentRequest = ArgumentCaptor + .forClass(AppDeploymentRequest.class); + + verify(taskLauncher).launch( + deploymentRequest.capture()); + + AppDeploymentRequest actualRequest = deploymentRequest + .getValue(); + + // Verifying the co-ordinate of launched Task here. + assertTrue(actualRequest.getCommandlineArguments() + .isEmpty()); + assertEquals("0", actualRequest.getDefinition() + .getProperties().get("server.port")); + assertTrue(actualRequest + .getResource() + .toString() + .contains( + "org.springframework.cloud.task.app:timestamp-task:jar:1.0.1.RELEASE")); + } +} \ No newline at end of file diff --git a/spring-mvc-kotlin/pom.xml b/spring-mvc-kotlin/pom.xml index 2d1dac5e29..28a5681c03 100644 --- a/spring-mvc-kotlin/pom.xml +++ b/spring-mvc-kotlin/pom.xml @@ -17,38 +17,64 @@ war + + UTF-8 + 5.2.13.Final + 1.2.21 + 4.3.10.RELEASE + 3.0.7.RELEASE + 1.4.196 + + org.jetbrains.kotlin - kotlin-stdlib-jre8 - 1.1.4 + kotlin-stdlib-jdk8 + ${kotlin.version} org.springframework spring-web - 4.3.10.RELEASE + ${spring.version} org.springframework spring-webmvc - 4.3.10.RELEASE + ${spring.version} org.thymeleaf thymeleaf - 3.0.7.RELEASE + ${thymeleaf.version} org.thymeleaf thymeleaf-spring4 - 3.0.7.RELEASE + ${thymeleaf.version} + + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.hibernate + hibernate-testing + ${hibernate.version} + test + + + com.h2database + h2 + ${h2.version} + test org.springframework spring-test - 4.3.10.RELEASE + ${spring.version} test @@ -60,10 +86,11 @@ kotlin-maven-plugin org.jetbrains.kotlin - 1.1.4 + ${kotlin.version} spring + jpa 1.8 @@ -87,7 +114,12 @@ org.jetbrains.kotlin kotlin-maven-allopen - 1.1.4-3 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt new file mode 100644 index 0000000000..801bd1b0a5 --- /dev/null +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt @@ -0,0 +1,15 @@ +package com.baeldung.jpa + +import javax.persistence.Entity +import javax.persistence.GeneratedValue +import javax.persistence.GenerationType +import javax.persistence.Id +import javax.persistence.Table + +@Entity +@Table(name = "person") +data class Person( + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + val id: Int, + val name: String) \ No newline at end of file diff --git a/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt new file mode 100644 index 0000000000..8f54373406 --- /dev/null +++ b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt @@ -0,0 +1,44 @@ +package com.baeldung.kotlin.jpa + +import com.baeldung.jpa.Person +import org.hibernate.cfg.Configuration +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase +import org.hibernate.testing.transaction.TransactionUtil.doInHibernate +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.IOException +import java.util.* + + +class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() { + + private val properties: Properties + @Throws(IOException::class) + get() { + val properties = Properties() + properties.load(javaClass.classLoader.getResourceAsStream("hibernate.properties")) + return properties + } + + override fun getAnnotatedClasses(): Array> { + return arrayOf(Person::class.java) + } + + override fun configure(configuration: Configuration) { + super.configure(configuration) + configuration.properties = properties + } + + @Test + fun givenPerson_whenSaved_thenFound() { + val personToSave = Person(0, "John") + doInHibernate(({ this.sessionFactory() }), { session -> + session.persist(personToSave) + assertTrue(session.contains(personToSave)) + }) + doInHibernate(({ this.sessionFactory() }), { session -> + val personFound = session.find(Person::class.java, personToSave.id) + assertTrue(personToSave == personFound) + }) + } +} \ No newline at end of file diff --git a/spring-mvc-kotlin/src/test/resources/hibernate.properties b/spring-mvc-kotlin/src/test/resources/hibernate.properties new file mode 100644 index 0000000000..7b8764637b --- /dev/null +++ b/spring-mvc-kotlin/src/test/resources/hibernate.properties @@ -0,0 +1,9 @@ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1 +hibernate.connection.username=sa +hibernate.connection.autocommit=true +jdbc.password= + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/spring-mvc-simple/pom.xml b/spring-mvc-simple/pom.xml index 595e58f5f3..c0e4b63897 100644 --- a/spring-mvc-simple/pom.xml +++ b/spring-mvc-simple/pom.xml @@ -31,9 +31,17 @@ 5.0.2 1.0.2 1.9.0 + 2.9.4 + 1.4.9 + 5.1.0 + + org.springframework + spring-oxm + 5.0.2.RELEASE + javax.servlet javax.servlet-api @@ -121,6 +129,28 @@ rome ${rome.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + com.thoughtworks.xstream + xstream + ${xstream.version} + + + com.github.scribejava + scribejava-apis + ${scribejava.version} + diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java index 69c45d90b3..3275d919ea 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java @@ -1,26 +1,34 @@ package com.baeldung.spring.configuration; import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.http.converter.feed.RssChannelHttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; +import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.accept.ContentNegotiationManager; -import java.util.List; import java.util.ArrayList; +import java.util.List; @Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" }) -class ApplicationConfiguration implements WebMvcConfigurer { +class ApplicationConfiguration extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { @@ -49,4 +57,20 @@ class ApplicationConfiguration implements WebMvcConfigurer { multipartResolver.setMaxUploadSize(5242880); return multipartResolver; } + + @Override + public void configureMessageConverters(List> converters) { + Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml(); + builder.indentOutput(true); + + XmlMapper xmlMapper = builder.createXmlMapper(true).build(); + xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); + + converters.add(new StringHttpMessageConverter()); + converters.add(new RssChannelHttpMessageConverter()); + converters.add(new MappingJackson2HttpMessageConverter()); + converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper)); + + super.configureMessageConverters(converters); + } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java new file mode 100644 index 0000000000..71b225bf3f --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.controller.rss; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +@JacksonXmlRootElement(localName="articles") +public class ArticleFeed extends RssData implements Serializable { + + @JacksonXmlProperty(localName = "item") + @JacksonXmlElementWrapper(useWrapping = false) + private List items = new ArrayList(); + + public void addItem(ArticleItem articleItem) { + this.items.add(articleItem); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java new file mode 100644 index 0000000000..6c91819676 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java @@ -0,0 +1,22 @@ +package com.baeldung.spring.controller.rss; + +import java.io.Serializable; + +public class ArticleItem extends RssData implements Serializable { + private String author; + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public String toString() { + return "ArticleItem{" + + "author='" + author + '\'' + + '}'; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java index 8f23076e8e..b0cce99d33 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java @@ -1,59 +1,46 @@ package com.baeldung.spring.controller.rss; -import com.rometools.rome.feed.synd.*; -import com.rometools.rome.io.FeedException; -import com.rometools.rome.io.SyndFeedOutput; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; -import java.util.ArrayList; import java.util.Date; -import java.util.List; @Controller public class ArticleRssController { - @GetMapping(value = "/rssMvc") + @GetMapping(value = "/rss1") public String articleMvcFeed() { return "articleFeedView"; } - @GetMapping(value = "/rssRest", produces = "application/rss+xml") + @GetMapping(value = "/rss2") @ResponseBody - public String articleRestFeed() throws FeedException { - SyndFeed feed = new SyndFeedImpl(); - feed.setFeedType("rss_2.0"); + public ArticleFeed articleRestFeed2() { + ArticleFeed feed = new ArticleFeed(); feed.setLink("http://localhost:8080/spring-mvc-simple/rss"); feed.setTitle("Article Feed"); feed.setDescription("Article Feed Description"); feed.setPublishedDate(new Date()); - List list = new ArrayList(); - - SyndEntry item1 = new SyndEntryImpl(); + ArticleItem item1 = new ArticleItem(); item1.setLink("http://www.baeldung.com/netty-exception-handling"); item1.setTitle("Exceptions in Netty"); - SyndContent description1 = new SyndContentImpl(); - description1.setValue("In this quick article, we’ll be looking at exception handling in Netty."); - item1.setDescription(description1); + item1.setDescription("In this quick article, we’ll be looking at exception handling in Netty."); item1.setPublishedDate(new Date()); item1.setAuthor("Carlos"); - SyndEntry item2 = new SyndEntryImpl(); + ArticleItem item2 = new ArticleItem(); item2.setLink("http://www.baeldung.com/cockroachdb-java"); item2.setTitle("Guide to CockroachDB in Java"); - SyndContent description2 = new SyndContentImpl(); - description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java."); - item2.setDescription(description2); + item2.setDescription("This tutorial is an introductory guide to using CockroachDB with Java."); item2.setPublishedDate(new Date()); item2.setAuthor("Baeldung"); - list.add(item1); - list.add(item2); - feed.setEntries(list); + feed.addItem(item1); + feed.addItem(item2); - return new SyndFeedOutput().outputString(feed); + return feed; } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java new file mode 100644 index 0000000000..258712eb2d --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java @@ -0,0 +1,56 @@ +package com.baeldung.spring.controller.rss; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class RssData implements Serializable { + private String link; + private String title; + private String description; + private String publishedDate; + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getPublishedDate() { + return publishedDate; + } + + public void setPublishedDate(Date publishedDate) { + DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); + this.publishedDate = df.format(publishedDate); + } + + @Override + public String toString() { + return "RssData{" + + "link='" + link + '\'' + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + ", publishedDate=" + publishedDate + + '}'; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/ScribeController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/ScribeController.java new file mode 100644 index 0000000000..c5c97ff009 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/ScribeController.java @@ -0,0 +1,55 @@ +package com.baeldung.spring.controller.scribe; + +import com.github.scribejava.apis.TwitterApi; +import com.github.scribejava.core.builder.ServiceBuilder; +import com.github.scribejava.core.model.*; +import com.github.scribejava.core.oauth.OAuth10aService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.view.RedirectView; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +@Controller +@RequestMapping("twitter") +public class ScribeController { + + private OAuth10aService createTwitterService() { + return new ServiceBuilder("PSRszoHhRDVhyo2RIkThEbWko") + .apiSecret("prpJbz03DcGRN46sb4ucdSYtVxG8unUKhcnu3an5ItXbEOuenL") + .callback("http://localhost:8080/spring-mvc-simple/twitter/callback") + .build(TwitterApi.instance()); + } + + @GetMapping(value = "/authorization") + public RedirectView authorization(HttpServletRequest servletReq) throws InterruptedException, ExecutionException, IOException { + OAuth10aService twitterService = createTwitterService(); + + OAuth1RequestToken requestToken = twitterService.getRequestToken(); + String authorizationUrl = twitterService.getAuthorizationUrl(requestToken); + servletReq.getSession().setAttribute("requestToken", requestToken); + + RedirectView redirectView = new RedirectView(); + redirectView.setUrl(authorizationUrl); + return redirectView; + } + + @GetMapping(value = "/callback", produces = "text/plain") + @ResponseBody + public String callback(HttpServletRequest servletReq, @RequestParam("oauth_verifier") String oauthV) throws InterruptedException, ExecutionException, IOException { + OAuth10aService twitterService = createTwitterService(); + OAuth1RequestToken requestToken = (OAuth1RequestToken) servletReq.getSession().getAttribute("requestToken"); + OAuth1AccessToken accessToken = twitterService.getAccessToken(requestToken, oauthV); + + OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json"); + twitterService.signRequest(accessToken, request); + Response response = twitterService.execute(request); + + return response.getBody(); + } +} diff --git a/spring-security-openid/pom.xml b/spring-security-openid/pom.xml index 4c8112a163..aeabb161c9 100644 --- a/spring-security-openid/pom.xml +++ b/spring-security-openid/pom.xml @@ -46,6 +46,11 @@ 1.0.9.RELEASE + + com.auth0 + jwks-rsa + 0.3.0 + diff --git a/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java b/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java index c0970ab3cf..f12169cb27 100644 --- a/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java +++ b/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java @@ -1,12 +1,16 @@ package org.baeldung.security; import java.io.IOException; +import java.net.URL; +import java.security.interfaces.RSAPublicKey; +import java.util.Date; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -14,16 +18,28 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.jwt.Jwt; import org.springframework.security.jwt.JwtHelper; +import org.springframework.security.jwt.crypto.sign.RsaVerifier; import org.springframework.security.oauth2.client.OAuth2RestOperations; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.common.OAuth2AccessToken; -import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; +import com.auth0.jwk.Jwk; +import com.auth0.jwk.JwkProvider; +import com.auth0.jwk.UrlJwkProvider; import com.fasterxml.jackson.databind.ObjectMapper; public class OpenIdConnectFilter extends AbstractAuthenticationProcessingFilter { + @Value("${google.clientId}") + private String clientId; + + @Value("${google.issuer}") + private String issuer; + + @Value("${google.jwkUrl}") + private String jwkUrl; + public OAuth2RestOperations restTemplate; public OpenIdConnectFilter(String defaultFilterProcessesUrl) { @@ -42,19 +58,35 @@ public class OpenIdConnectFilter extends AbstractAuthenticationProcessingFilter } try { final String idToken = accessToken.getAdditionalInformation().get("id_token").toString(); - final Jwt tokenDecoded = JwtHelper.decode(idToken); - System.out.println("===== : " + tokenDecoded.getClaims()); - + String kid = JwtHelper.headers(idToken) + .get("kid"); + final Jwt tokenDecoded = JwtHelper.decodeAndVerify(idToken, verifier(kid)); final Map authInfo = new ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class); - + verifyClaims(authInfo); final OpenIdConnectUserDetails user = new OpenIdConnectUserDetails(authInfo, accessToken); return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities()); - } catch (final InvalidTokenException e) { + } catch (final Exception e) { throw new BadCredentialsException("Could not obtain user details from token", e); } } + public void verifyClaims(Map claims) { + int exp = (int) claims.get("exp"); + Date expireDate = new Date(exp * 1000L); + Date now = new Date(); + if (expireDate.before(now) || !claims.get("iss").equals(issuer) || !claims.get("aud").equals(clientId)) { + throw new RuntimeException("Invalid claims"); + } + } + + + private RsaVerifier verifier(String kid) throws Exception { + JwkProvider provider = new UrlJwkProvider(new URL(jwkUrl)); + Jwk jwk = provider.get(kid); + return new RsaVerifier((RSAPublicKey) jwk.getPublicKey()); + } + public void setRestTemplate(OAuth2RestTemplate restTemplate2) { restTemplate = restTemplate2; diff --git a/spring-security-openid/src/main/resources/application.properties.sample.properties b/spring-security-openid/src/main/resources/application.properties.sample.properties index 3b4f7716f0..49022bf280 100644 --- a/spring-security-openid/src/main/resources/application.properties.sample.properties +++ b/spring-security-openid/src/main/resources/application.properties.sample.properties @@ -3,4 +3,6 @@ google.clientId=TODO google.clientSecret=TODO google.accessTokenUri=https://www.googleapis.com/oauth2/v3/token google.userAuthorizationUri=https://accounts.google.com/o/oauth2/auth -google.redirectUri=http://localhost:8081/google-login \ No newline at end of file +google.redirectUri=http://localhost:8081/google-login +google.issuer=accounts.google.com +google.jwkUrl=https://www.googleapis.com/oauth2/v2/certs \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/BuildConfig.java b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/BuildConfig.java new file mode 100644 index 0000000000..85233bc278 --- /dev/null +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/BuildConfig.java @@ -0,0 +1,8 @@ +/*___Generated_by_IDEA___*/ + +package com.baeldung.petstore.client.invoker; + +/* This stub is only used by the IDE. It is NOT the BuildConfig class actually packed into the APK */ +public final class BuildConfig { + public final static boolean DEBUG = Boolean.parseBoolean(null); +} \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/Manifest.java b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/Manifest.java new file mode 100644 index 0000000000..06c202e733 --- /dev/null +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/Manifest.java @@ -0,0 +1,7 @@ +/*___Generated_by_IDEA___*/ + +package com.baeldung.petstore.client.invoker; + +/* This stub is only used by the IDE. It is NOT the Manifest class actually packed into the APK */ +public final class Manifest { +} \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/R.java b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/R.java new file mode 100644 index 0000000000..41e2b545fb --- /dev/null +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/R.java @@ -0,0 +1,7 @@ +/*___Generated_by_IDEA___*/ + +package com.baeldung.petstore.client.invoker; + +/* This stub is only used by the IDE. It is NOT the R class actually packed into the APK */ +public final class R { +} \ No newline at end of file diff --git a/testing-modules/junit-5/pom.xml b/testing-modules/junit-5/pom.xml index 585232b3fe..28a29d8545 100644 --- a/testing-modules/junit-5/pom.xml +++ b/testing-modules/junit-5/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.8 - 5.0.2 + 5.1.0 1.0.1 4.12.1 2.8.2