diff --git a/asm/pom.xml b/asm/pom.xml new file mode 100644 index 0000000000..407ceab458 --- /dev/null +++ b/asm/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + com.baeldung.examples + asm + 1.0 + jar + + + org.ow2.asm + asm + 5.2 + + + org.ow2.asm + asm-util + 5.2 + + + + UTF-8 + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + + com.baeldung.examples.asm.instrumentation.Premain + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.9 + + -javaagent:"C:\asm-1.0.jar" + + + + + \ No newline at end of file diff --git a/asm/src/main/java/com/baeldung/examples/asm/CustomClassWriter.java b/asm/src/main/java/com/baeldung/examples/asm/CustomClassWriter.java new file mode 100644 index 0000000000..d41a1a16a3 --- /dev/null +++ b/asm/src/main/java/com/baeldung/examples/asm/CustomClassWriter.java @@ -0,0 +1,160 @@ +package com.baeldung.examples.asm; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodVisitor; +import static org.objectweb.asm.Opcodes.ACC_PUBLIC; +import static org.objectweb.asm.Opcodes.ACC_STATIC; +import static org.objectweb.asm.Opcodes.ASM4; +import static org.objectweb.asm.Opcodes.V1_5; +import org.objectweb.asm.Type; +import org.objectweb.asm.util.TraceClassVisitor; + +/** + * + * @author baeldung + * @param + */ +public class CustomClassWriter { + + ClassReader reader; + ClassWriter writer; + AddFieldAdapter addFieldAdapter; + AddInterfaceAdapter addInterfaceAdapter; + PublicizeMethodAdapter pubMethAdapter; + final static String CLASSNAME = "java.lang.Integer"; + final static String CLONEABLE = "java/lang/Cloneable"; + + public CustomClassWriter() { + + try { + reader = new ClassReader(CLASSNAME); + writer = new ClassWriter(reader, 0); + + } catch (IOException ex) { + Logger.getLogger(CustomClassWriter.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public CustomClassWriter(byte[] contents) { + reader = new ClassReader(contents); + writer = new ClassWriter(reader, 0); + } + + public static void main(String[] args) { + CustomClassWriter ccw = new CustomClassWriter(); + ccw.publicizeMethod(); + } + + public byte[] addField() { + addFieldAdapter = new AddFieldAdapter("aNewBooleanField", org.objectweb.asm.Opcodes.ACC_PUBLIC, writer); + reader.accept(addFieldAdapter, 0); + return writer.toByteArray(); + } + + public byte[] publicizeMethod() { + pubMethAdapter = new PublicizeMethodAdapter(writer); + reader.accept(pubMethAdapter, 0); + return writer.toByteArray(); + } + + public byte[] addInterface() { + addInterfaceAdapter = new AddInterfaceAdapter(writer); + reader.accept(addInterfaceAdapter, 0); + return writer.toByteArray(); + } + + public class AddInterfaceAdapter extends ClassVisitor { + + public AddInterfaceAdapter(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public void visit(int version, int access, String name, + String signature, String superName, String[] interfaces) { + String[] holding = new String[interfaces.length + 1]; + holding[holding.length - 1] = CLONEABLE; + System.arraycopy(interfaces, 0, holding, 0, interfaces.length); + + cv.visit(V1_5, access, name, signature, superName, holding); + } + + } + + public class PublicizeMethodAdapter extends ClassVisitor { + + final Logger logger = Logger.getLogger("PublicizeMethodAdapter"); + TraceClassVisitor tracer; + PrintWriter pw = new PrintWriter(System.out); + + public PublicizeMethodAdapter(ClassVisitor cv) { + super(ASM4, cv); + this.cv = cv; + tracer = new TraceClassVisitor(cv, pw); + } + + @Override + public MethodVisitor visitMethod(int access, + String name, + String desc, + String signature, + String[] exceptions) { + + if (name.equals("toUnsignedString0")) { + logger.info("Visiting unsigned method"); + return tracer.visitMethod(ACC_PUBLIC + ACC_STATIC, name, desc, signature, exceptions); + } + return tracer.visitMethod(access, name, desc, signature, exceptions); + + } + + public void visitEnd() { + tracer.visitEnd(); + System.out.println(tracer.p.getText()); + } + + } + + public class AddFieldAdapter extends ClassVisitor { + + String fieldName; + int access; + boolean isFieldPresent; + + public AddFieldAdapter(String fieldName, int access, ClassVisitor cv) { + super(ASM4, cv); + this.cv = cv; + this.access = access; + this.fieldName = fieldName; + } + + @Override + public FieldVisitor visitField(int access, String name, String desc, + String signature, Object value) { + if (name.equals(fieldName)) { + isFieldPresent = true; + } + return cv.visitField(access, name, desc, signature, value); + } + + @Override + public void visitEnd() { + if (!isFieldPresent) { + FieldVisitor fv = cv.visitField(access, fieldName, Type.BOOLEAN_TYPE.toString(), null, null); + if (fv != null) { + fv.visitEnd(); + } + } + cv.visitEnd(); + } + + } + +} diff --git a/asm/src/main/java/com/baeldung/examples/asm/instrumentation/Premain.java b/asm/src/main/java/com/baeldung/examples/asm/instrumentation/Premain.java new file mode 100644 index 0000000000..a3e69b6785 --- /dev/null +++ b/asm/src/main/java/com/baeldung/examples/asm/instrumentation/Premain.java @@ -0,0 +1,32 @@ +package com.baeldung.examples.asm.instrumentation; + +import com.baeldung.examples.asm.CustomClassWriter; +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.lang.instrument.Instrumentation; +import java.security.ProtectionDomain; + +/** + * + * @author baeldung + */ +public class Premain { + + public static void premain(String agentArgs, Instrumentation inst) { + inst.addTransformer(new ClassFileTransformer() { + + @Override + public byte[] transform(ClassLoader l, String name, Class c, + ProtectionDomain d, byte[] b) + throws IllegalClassFormatException { + + if (name.equals("java/lang/Integer")) { + CustomClassWriter cr = new CustomClassWriter(b); + return cr.addField(); + } + return b; + } + }); + } + +} diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 856a37ded0..e795d1e042 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -5,6 +5,7 @@ core-kotlin 1.0-SNAPSHOT + jar com.baeldung @@ -20,6 +21,24 @@ + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + junit + junit + ${junit4.version} + test + org.jetbrains.kotlin kotlin-stdlib @@ -116,16 +135,51 @@ + + 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 - \ No newline at end of file + diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt new file mode 100644 index 0000000000..1b61c05887 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt @@ -0,0 +1,17 @@ +package com.baeldung.kotlin.junit5 + +class Calculator { + fun add(a: Int, b: Int) = a + b + + fun divide(a: Int, b: Int) = if (b == 0) { + throw DivideByZeroException(a) + } else { + a / b + } + + fun square(a: Int) = a * a + + fun squareRoot(a: Int) = Math.sqrt(a.toDouble()) + + fun log(base: Int, value: Int) = Math.log(value.toDouble()) / Math.log(base.toDouble()) +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt new file mode 100644 index 0000000000..dd35805044 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt @@ -0,0 +1,82 @@ +package com.baeldung.kotlin.junit5 + +import org.junit.jupiter.api.* +import org.junit.jupiter.api.function.Executable + +class CalculatorTest5 { + private val calculator = Calculator() + + @Test + fun testAddition() { + Assertions.assertEquals(4, calculator.add(1, 3)) + } + + @Test + fun testDivideByZero() { + val exception = Assertions.assertThrows(DivideByZeroException::class.java) { + calculator.divide(5, 0) + } + + Assertions.assertEquals(5, exception.numerator) + } + + @Test + fun testSquares() { + Assertions.assertAll( + Executable { Assertions.assertEquals(1, calculator.square(1)) }, + Executable { Assertions.assertEquals(4, calculator.square(2)) }, + Executable { Assertions.assertEquals(9, calculator.square(3)) } + ) + } + + @TestFactory + fun testSquaresFactory() = listOf( + DynamicTest.dynamicTest("1 squared") { Assertions.assertEquals(1,calculator.square(1))}, + DynamicTest.dynamicTest("2 squared") { Assertions.assertEquals(4,calculator.square(2))}, + DynamicTest.dynamicTest("3 squared") { Assertions.assertEquals(9,calculator.square(3))} + ) + + @TestFactory + fun testSquaresFactory2() = listOf( + 1 to 1, + 2 to 4, + 3 to 9, + 4 to 16, + 5 to 25) + .map { (input, expected) -> + DynamicTest.dynamicTest("$input squared") { + Assertions.assertEquals(expected, calculator.square(input)) + } + } + + private val squaresTestData = listOf( + 1 to 1, + 2 to 4, + 3 to 9, + 4 to 16, + 5 to 25) + + @TestFactory + fun testSquaresFactory3() = squaresTestData + .map { (input, expected) -> + DynamicTest.dynamicTest("$input squared") { + Assertions.assertEquals(expected, calculator.square(input)) + } + } + @TestFactory + fun testSquareRootsFactory3() = squaresTestData + .map { (expected, input) -> + DynamicTest.dynamicTest("Square root of $input") { + Assertions.assertEquals(expected.toDouble(), calculator.squareRoot(input)) + } + } + + @Tags( + Tag("slow"), + Tag("logarithms") + ) + @Test + fun testLogarithms() { + Assertions.assertEquals(3.0, calculator.log(2, 8)) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt new file mode 100644 index 0000000000..60bc4e2944 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.junit5 + +class DivideByZeroException(val numerator: Int) : Exception() diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt new file mode 100644 index 0000000000..c04ab568f7 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt @@ -0,0 +1,21 @@ +package com.baeldung.kotlin.junit5 + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test + +class SimpleTest5 { + @Test + fun testEmpty() { + val list = listOf() + Assertions.assertTrue(list::isEmpty) + } + + @Test + @Disabled + fun testMessage() { + Assertions.assertEquals(3, 4) { + "Three does not equal four" + } + } +} diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml index cae8a725a6..90b1f6bb1d 100644 --- a/libraries-data/pom.xml +++ b/libraries-data/pom.xml @@ -36,6 +36,11 @@ reladomo-test-util ${reladomo.version} + + com.j256.ormlite + ormlite-jdbc + ${ormlite.version} + @@ -144,5 +149,6 @@ 16.5.1 4.12 3.6.2 + 5.0 \ No newline at end of file diff --git a/libraries-data/src/main/java/com/baeldung/ormlite/Address.java b/libraries-data/src/main/java/com/baeldung/ormlite/Address.java new file mode 100644 index 0000000000..747b0b0b12 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ormlite/Address.java @@ -0,0 +1,37 @@ +package com.baeldung.ormlite; + +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +@DatabaseTable(tableName = "addresses") +public class Address { + @DatabaseField(generatedId = true) + private long addressId; + + @DatabaseField(canBeNull = false) + private String addressLine; + + public Address() { + } + + public Address(String addressLine) { + this.addressLine = addressLine; + } + + public long getAddressId() { + return addressId; + } + + public void setAddressId(long addressId) { + this.addressId = addressId; + } + + public String getAddressLine() { + return addressLine; + } + + public void setAddressLine(String addressLine) { + this.addressLine = addressLine; + } + +} diff --git a/libraries-data/src/main/java/com/baeldung/ormlite/Book.java b/libraries-data/src/main/java/com/baeldung/ormlite/Book.java new file mode 100644 index 0000000000..ed7b813b8d --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ormlite/Book.java @@ -0,0 +1,49 @@ +package com.baeldung.ormlite; + +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +@DatabaseTable +public class Book { + + @DatabaseField(generatedId = true) + private long bookId; + + @DatabaseField + private String title; + + @DatabaseField(foreign = true, foreignAutoRefresh = true, foreignAutoCreate = true) + private Library library; + + public Book() { + } + + public Book(String title) { + this.title = title; + } + + public long getBookId() { + return bookId; + } + + public void setBookId(long bookId) { + this.bookId = bookId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Library getLibrary() { + return library; + } + + public void setLibrary(Library library) { + this.library = library; + } + +} diff --git a/libraries-data/src/main/java/com/baeldung/ormlite/Library.java b/libraries-data/src/main/java/com/baeldung/ormlite/Library.java new file mode 100644 index 0000000000..994b4c6575 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ormlite/Library.java @@ -0,0 +1,58 @@ +package com.baeldung.ormlite; + +import com.j256.ormlite.dao.ForeignCollection; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.field.ForeignCollectionField; +import com.j256.ormlite.table.DatabaseTable; + +@DatabaseTable(tableName = "libraries", daoClass = LibraryDaoImpl.class) +public class Library { + + @DatabaseField(generatedId = true) + private long libraryId; + + @DatabaseField(canBeNull = false) + private String name; + + @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true) + private Address address; + + @ForeignCollectionField(eager = false) + private ForeignCollection books; + + public Library() { + } + + public long getLibraryId() { + return libraryId; + } + + public void setLibraryId(long libraryId) { + this.libraryId = libraryId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public ForeignCollection getBooks() { + return books; + } + + public void setBooks(ForeignCollection books) { + this.books = books; + } + +} diff --git a/libraries-data/src/main/java/com/baeldung/ormlite/LibraryDao.java b/libraries-data/src/main/java/com/baeldung/ormlite/LibraryDao.java new file mode 100644 index 0000000000..fd8f5f40d6 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ormlite/LibraryDao.java @@ -0,0 +1,10 @@ +package com.baeldung.ormlite; + +import java.sql.SQLException; +import java.util.List; + +import com.j256.ormlite.dao.Dao; + +public interface LibraryDao extends Dao { + public List findByName(String name) throws SQLException; +} diff --git a/libraries-data/src/main/java/com/baeldung/ormlite/LibraryDaoImpl.java b/libraries-data/src/main/java/com/baeldung/ormlite/LibraryDaoImpl.java new file mode 100644 index 0000000000..af313101e2 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ormlite/LibraryDaoImpl.java @@ -0,0 +1,21 @@ +package com.baeldung.ormlite; + +import java.sql.SQLException; +import java.util.List; + +import com.j256.ormlite.dao.BaseDaoImpl; +import com.j256.ormlite.support.ConnectionSource; + +public class LibraryDaoImpl extends BaseDaoImpl implements LibraryDao { + + public LibraryDaoImpl(ConnectionSource connectionSource) throws SQLException { + super(connectionSource, Library.class); + } + + @Override + public List findByName(String name) throws SQLException { + return super.queryForEq("name", name); + + } + +} diff --git a/libraries-data/src/test/java/com/baeldung/ormlite/ORMLiteTest.java b/libraries-data/src/test/java/com/baeldung/ormlite/ORMLiteTest.java new file mode 100644 index 0000000000..26eb481286 --- /dev/null +++ b/libraries-data/src/test/java/com/baeldung/ormlite/ORMLiteTest.java @@ -0,0 +1,168 @@ +package com.baeldung.ormlite; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import com.j256.ormlite.dao.CloseableWrappedIterable; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.dao.DaoManager; +import com.j256.ormlite.jdbc.JdbcPooledConnectionSource; +import com.j256.ormlite.table.TableUtils; + +public class ORMLiteTest { + private static JdbcPooledConnectionSource connectionSource; + + private static Dao libraryDao; + private static Dao bookDao; + + @BeforeClass + public static void setup() throws SQLException { + connectionSource = new JdbcPooledConnectionSource("jdbc:h2:mem:myDb"); + TableUtils.createTableIfNotExists(connectionSource, Library.class); + TableUtils.createTableIfNotExists(connectionSource, Address.class); + TableUtils.createTableIfNotExists(connectionSource, Book.class); + + libraryDao = DaoManager.createDao(connectionSource, Library.class); + + bookDao = DaoManager.createDao(connectionSource, Book.class); + } + + @Test + public void givenDAO_whenCRUD_thenOk() throws SQLException { + Library library = new Library(); + library.setName("My Library"); + libraryDao.create(library); + + Library result = libraryDao.queryForId(library.getLibraryId()); + assertEquals("My Library", result.getName()); + + library.setName("My Other Library"); + libraryDao.update(library); + + libraryDao.delete(library); + + } + + @Test + public void whenLoopDao_thenOk() throws SQLException { + Library library1 = new Library(); + library1.setName("My Library"); + libraryDao.create(library1); + + Library library2 = new Library(); + library2.setName("My Other Library"); + libraryDao.create(library2); + + libraryDao.forEach(lib -> { + System.out.println(lib.getName()); + }); + + } + + @Test + public void givenIterator_whenLoop_thenOk() throws SQLException, IOException { + Library library1 = new Library(); + library1.setName("My Library"); + libraryDao.create(library1); + + Library library2 = new Library(); + library2.setName("My Other Library"); + libraryDao.create(library2); + + try (CloseableWrappedIterable wrappedIterable = libraryDao.getWrappedIterable()) { + wrappedIterable.forEach(lib -> { + System.out.println(lib.getName()); + }); + } + + } + + @Test + public void givenCustomDao_whenSave_thenOk() throws SQLException, IOException { + Library library = new Library(); + library.setName("My Library"); + + LibraryDao customLibraryDao = DaoManager.createDao(connectionSource, Library.class); + customLibraryDao.create(library); + assertEquals(1, customLibraryDao.findByName("My Library") + .size()); + } + + @Test + public void whenSaveForeignField_thenOk() throws SQLException, IOException { + Library library = new Library(); + library.setName("My Library"); + library.setAddress(new Address("Main Street nr 20")); + libraryDao.create(library); + + Dao addressDao = DaoManager.createDao(connectionSource, Address.class); + assertEquals(1, addressDao.queryForEq("addressLine", "Main Street nr 20") + .size()); + } + + @Test + public void whenSaveForeignCollection_thenOk() throws SQLException, IOException { + Library library = new Library(); + library.setName("My Library"); + libraryDao.create(library); + libraryDao.refresh(library); + library.getBooks() + .add(new Book("1984")); + + Book book = new Book("It"); + book.setLibrary(library); + bookDao.create(book); + + assertEquals(2, bookDao.queryForEq("library_id", library) + .size()); + } + + @Test + public void whenGetLibrariesWithMoreThanOneBook_thenOk() throws SQLException, IOException { + Library library = new Library(); + library.setName("My Library"); + libraryDao.create(library); + Library library2 = new Library(); + library2.setName("My Other Library"); + libraryDao.create(library2); + + libraryDao.refresh(library); + libraryDao.refresh(library2); + + library.getBooks() + .add(new Book("Book1")); + library2.getBooks() + .add(new Book("Book2")); + library2.getBooks() + .add(new Book("Book3")); + + List libraries = libraryDao.queryBuilder() + .where() + .in("libraryId", bookDao.queryBuilder() + .selectColumns("library_id") + .groupBy("library_id") + .having("count(*) > 1")) + .query(); + assertEquals(1, libraries.size()); + + } + + @After + public void clear() throws SQLException { + TableUtils.clearTable(connectionSource, Library.class); + TableUtils.clearTable(connectionSource, Book.class); + TableUtils.clearTable(connectionSource, Address.class); + } + + @AfterClass + public static void tearDown() throws SQLException, IOException { + connectionSource.close(); + } +} diff --git a/libraries/README.md b/libraries/README.md index 74766fb828..7214cd278c 100644 --- a/libraries/README.md +++ b/libraries/README.md @@ -41,6 +41,7 @@ - [Introduction to NoException](http://www.baeldung.com/no-exception) - [Introduction to FunctionalJava](http://www.baeldung.com/functional-java) - [Apache Commons IO](http://www.baeldung.com/apache-commons-io) +- [Introduction to Conflict-Free Replicated Data Types](http://www.baeldung.com/java-conflict-free-replicated-data-types) The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own. diff --git a/libraries/pom.xml b/libraries/pom.xml index c7c5da95a6..b519b9cd53 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -575,22 +575,6 @@ hazelcast ${hazelcast.version} - - com.atlassian.jira - jira-rest-java-client-core - 4.0.0 - - - com.atlassian.fugue - fugue - 2.6.1 - - - org.jgrapht jgrapht-core @@ -606,11 +590,16 @@ docx4j 3.3.5 - - javax.xml.bind - jaxb-api - 2.1 - + + javax.xml.bind + jaxb-api + 2.1 + + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + @@ -626,10 +615,6 @@ bintray http://dl.bintray.com/cuba-platform/main - - atlassian-public - https://packages.atlassian.com/maven/repository/public - 0.1.0 @@ -686,5 +671,6 @@ 1.0.3 1.0.0 3.8.4 + 2.5.5 diff --git a/libraries/src/main/java/com/baeldung/caffeine/DataObject.java b/libraries/src/main/java/com/baeldung/caffeine/DataObject.java new file mode 100644 index 0000000000..2a8b60b045 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/caffeine/DataObject.java @@ -0,0 +1,28 @@ +package com.baeldung.caffeine; + +final class DataObject { + private final String data; + + private static int objectCounter = 0; + + private DataObject(String data) { + this.data = data; + } + + public String getData() { + return data; + } + + @Override + public String toString() { + return "DataObject{" + + "data='" + data + '\'' + + '}'; + } + + public static DataObject get(String data) { + objectCounter++; + System.out.println(String.format("Initializing DataObject#%d with data '%s'", objectCounter, data)); + return new DataObject(data); + } +} diff --git a/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java b/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java new file mode 100644 index 0000000000..56dbda5974 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java @@ -0,0 +1,174 @@ +package com.baeldung.caffeine; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import javax.annotation.Nonnull; + +import org.junit.Test; + +import com.github.benmanes.caffeine.cache.*; + +public class CaffeineUnitTest { + + @Test + public void givenCache_whenPopulate_thenValueStored() { + Cache cache = Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.MINUTES) + .maximumSize(100) + .build(); + + String key = "A"; + DataObject dataObject = cache.getIfPresent(key); + + assertNull(dataObject); + + dataObject = cache.get(key, k -> DataObject.get("Data for A")); + + assertNotNull(dataObject); + assertEquals("Data for A", dataObject.getData()); + + cache.put(key, dataObject); + dataObject = cache.getIfPresent(key); + + assertNotNull(dataObject); + + cache.invalidate(key); + dataObject = cache.getIfPresent(key); + + assertNull(dataObject); + } + + @Test + public void givenLoadingCache_whenGet_thenValuePopulated() { + LoadingCache cache = Caffeine.newBuilder() + .maximumSize(100) + .expireAfterWrite(1, TimeUnit.MINUTES) + .build(k -> DataObject.get("Data for " + k)); + String key = "A"; + + DataObject dataObject = cache.get(key); + + assertNotNull(dataObject); + assertEquals("Data for " + key, dataObject.getData()); + + Map dataObjectMap = cache.getAll(Arrays.asList("A", "B", "C")); + + assertEquals(3, dataObjectMap.size()); + } + + @Test + public void givenAsyncLoadingCache_whenGet_thenValuePopulated() { + + AsyncLoadingCache cache = Caffeine.newBuilder() + .maximumSize(100) + .expireAfterWrite(1, TimeUnit.MINUTES) + .buildAsync(k -> DataObject.get("Data for " + k)); + String key = "A"; + + cache.get(key).thenAccept(dataObject -> { + assertNotNull(dataObject); + assertEquals("Data for " + key, dataObject.getData()); + }); + + cache.getAll(Arrays.asList("A", "B", "C")) + .thenAccept(dataObjectMap -> assertEquals(3, dataObjectMap.size())); + } + + @Test + public void givenLoadingCacheWithSmallSize_whenPut_thenSizeIsConstant() { + LoadingCache cache = Caffeine.newBuilder() + .maximumSize(1) + .refreshAfterWrite(10, TimeUnit.MINUTES) + .build(k -> DataObject.get("Data for " + k)); + + assertEquals(0, cache.estimatedSize()); + + cache.get("A"); + + assertEquals(1, cache.estimatedSize()); + + cache.get("B"); + cache.cleanUp(); + + assertEquals(1, cache.estimatedSize()); + } + + @Test + public void givenLoadingCacheWithWeigher_whenPut_thenSizeIsConstant() { + LoadingCache cache = Caffeine.newBuilder() + .maximumWeight(10) + .weigher((k,v) -> 5) + .build(k -> DataObject.get("Data for " + k)); + + assertEquals(0, cache.estimatedSize()); + + cache.get("A"); + + assertEquals(1, cache.estimatedSize()); + + cache.get("B"); + + assertEquals(2, cache.estimatedSize()); + + cache.get("C"); + cache.cleanUp(); + + assertEquals(2, cache.estimatedSize()); + } + + @Test + public void givenTimeEvictionCache_whenTimeLeft_thenValueEvicted() { + LoadingCache cache = Caffeine.newBuilder() + .expireAfterAccess(5, TimeUnit.MINUTES) + .build(k -> DataObject.get("Data for " + k)); + + cache = Caffeine.newBuilder() + .expireAfterWrite(10, TimeUnit.SECONDS) + .weakKeys() + .weakValues() + .build(k -> DataObject.get("Data for " + k)); + + cache = Caffeine.newBuilder() + .expireAfterWrite(10, TimeUnit.SECONDS) + .softValues() + .build(k -> DataObject.get("Data for " + k)); + + cache = Caffeine.newBuilder().expireAfter(new Expiry() { + @Override + public long expireAfterCreate(@Nonnull String key, @Nonnull DataObject value, long currentTime) { + return value.getData().length() * 1000; + } + + @Override + public long expireAfterUpdate(@Nonnull String key, @Nonnull DataObject value, long currentTime, long currentDuration) { + return currentDuration; + } + + @Override + public long expireAfterRead(@Nonnull String key, @Nonnull DataObject value, long currentTime, long currentDuration) { + return currentDuration; + } + }).build(k -> DataObject.get("Data for " + k)); + + cache = Caffeine.newBuilder() + .refreshAfterWrite(1, TimeUnit.MINUTES) + .build(k -> DataObject.get("Data for " + k)); + } + + @Test + public void givenCache_whenStatsEnabled_thenStatsRecorded() { + LoadingCache cache = Caffeine.newBuilder() + .maximumSize(100) + .recordStats() + .build(k -> DataObject.get("Data for " + k)); + cache.get("A"); + cache.get("A"); + + assertEquals(1, cache.stats().hitCount()); + assertEquals(1, cache.stats().missCount()); + } +} \ No newline at end of file diff --git a/mockito/README.md b/mockito/README.md index 6de2fb0c7a..2407a5c3c5 100644 --- a/mockito/README.md +++ b/mockito/README.md @@ -10,3 +10,4 @@ - [Mockito – @Mock, @Spy, @Captor and @InjectMocks](http://www.baeldung.com/mockito-annotations) - [Mockito’s Mock Methods](http://www.baeldung.com/mockito-mock-methods) - [Introduction to PowerMock](http://www.baeldung.com/intro-to-powermock) +- [Mocking Exception Throwing using Mockito](http://www.baeldung.com/mockito-exceptions) diff --git a/pom.xml b/pom.xml index c3915e4fce..f44cf9481e 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ + asm atomix apache-cayenne aws @@ -248,6 +249,7 @@ mockserver undertow vertx-and-rxjava + saas deeplearning4j diff --git a/rxjava/pom.xml b/rxjava/pom.xml index 783833243b..0f950914ff 100644 --- a/rxjava/pom.xml +++ b/rxjava/pom.xml @@ -25,6 +25,12 @@ 2.1.3 + + io.reactivex + rxjava-math + 1.0.0 + + com.jayway.awaitility awaitility diff --git a/rxjava/src/test/java/com/baeldung/rxjava/operators/RxAggregateOperatorsTest.java b/rxjava/src/test/java/com/baeldung/rxjava/operators/RxAggregateOperatorsTest.java new file mode 100644 index 0000000000..1af41f795f --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/operators/RxAggregateOperatorsTest.java @@ -0,0 +1,210 @@ +package com.baeldung.rxjava.operators; + +import org.junit.Test; +import rx.Observable; +import rx.observers.TestSubscriber; + +import java.util.*; + +public class RxAggregateOperatorsTest { + + @Test + public void givenTwoObservable_whenConcatenatingThem_thenSuccessfull() { + // given + List listOne = Arrays.asList(1, 2, 3, 4); + Observable observableOne = Observable.from(listOne); + + List listTwo = Arrays.asList(5, 6, 7, 8); + Observable observableTwo = Observable.from(listTwo); + + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable concatObservable = observableOne.concatWith(observableTwo); + + concatObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(8); + subscriber.assertValues(1, 2, 3, 4, 5, 6, 7, 8); + + } + + @Test + public void givenObservable_whenCounting_thenObtainingNumberOfElements() { + // given + List lettersList = Arrays.asList("A", "B", "C", "D", "E", "F", "G"); + + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable sourceObservable = Observable.from(lettersList) + .count(); + sourceObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(7); + } + + @Test + public void givenObservable_whenReducing_thenObtainingInvertedConcatenatedString() { + // given + List list = Arrays.asList("A", "B", "C", "D", "E", "F", "G"); + + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable reduceObservable = Observable.from(list) + .reduce((letter1, letter2) -> letter2 + letter1); + reduceObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue("GFEDCBA"); + } + + @Test + public void givenObservable_whenCollecting_thenObtainingASet() { + // given + List list = Arrays.asList("A", "B", "C", "B", "B", "A", "D"); + + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable reduceListObservable = Observable.from(list) + .collect(() -> new HashSet(), (set, item) -> set.add(item)); + reduceListObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValues(new HashSet(list)); + } + + @Test + public void givenObservable_whenUsingToList_thenObtainedAList() { + // given + Observable sourceObservable = Observable.range(1, 5); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable> listObservable = sourceObservable.toList(); + listObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(Arrays.asList(1, 2, 3, 4, 5)); + + } + + @Test + public void givenObservable_whenUsingToSortedList_thenObtainedASortedList() { + // given + Observable sourceObservable = Observable.range(10, 5); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable> listObservable = sourceObservable.toSortedList(); + listObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(Arrays.asList(10, 11, 12, 13, 14)); + } + + @Test + public void givenObservable_whenUsingToSortedListWithComparator_thenObtainedAnInverseSortedList() { + // given + Observable sourceObservable = Observable.range(10, 5); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable> listObservable = sourceObservable.toSortedList((int1, int2) -> int2 - int1); + listObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(Arrays.asList(14, 13, 12, 11, 10)); + + } + + @Test + public void givenObservable_whenUsingToMap_thenObtainedAMap() { + // given + Observable bookObservable = Observable.just(new Book("The North Water", 2016), new Book("Origin", 2017), new Book("Sleeping Beauties", 2017)); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable> mapObservable = bookObservable.toMap(Book::getTitle, Book::getYear, HashMap::new); + + mapObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(new HashMap() { + { + put("The North Water", 2016); + put("Origin", 2017); + put("Sleeping Beauties", 2017); + } + }); + } + + @Test + public void givenObservable_whenUsingToMultiMap_thenObtainedAMultiMap() { + // given + Observable bookObservable = Observable.just(new Book("The North Water", 2016), new Book("Origin", 2017), new Book("Sleeping Beauties", 2017)); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + Observable multiMapObservable = bookObservable.toMultimap(Book::getYear, Book::getTitle, () -> new HashMap(), (key) -> new ArrayList()); + + multiMapObservable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(new HashMap() { + { + put(2016, Arrays.asList("The North Water")); + put(2017, Arrays.asList("Origin", "Sleeping Beauties")); + } + }); + + } + + class Book { + private String title; + private Integer year; + + public Book(String title, Integer year) { + this.title = title; + this.year = year; + } + + public String getTitle() { + return title; + } + + public Integer getYear() { + return year; + } + } +} diff --git a/rxjava/src/test/java/com/baeldung/rxjava/operators/RxMathematicalOperatorsTest.java b/rxjava/src/test/java/com/baeldung/rxjava/operators/RxMathematicalOperatorsTest.java new file mode 100644 index 0000000000..cd212a2e18 --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/operators/RxMathematicalOperatorsTest.java @@ -0,0 +1,139 @@ +package com.baeldung.rxjava.operators; + +import org.junit.Test; +import rx.Observable; +import rx.observables.MathObservable; +import rx.observers.TestSubscriber; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class RxMathematicalOperatorsTest { + + @Test + public void givenRangeNumericObservable_whenCalculatingAverage_ThenSuccessfull() { + // given + Observable sourceObservable = Observable.range(1, 20); + + TestSubscriber subscriber = TestSubscriber.create(); + + // when + MathObservable.averageInteger(sourceObservable) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(10); + } + + @Test + public void givenRangeNumericObservable_whenCalculatingSum_ThenSuccessfull() { + // given + Observable sourceObservable = Observable.range(1, 20); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + MathObservable.sumInteger(sourceObservable) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(210); + + } + + @Test + public void givenRangeNumericObservable_whenCalculatingMax_ThenSuccessfullObtainingMaxValue() { + // given + Observable sourceObservable = Observable.range(1, 20); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + MathObservable.max(sourceObservable) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(20); + } + + @Test + public void givenRangeNumericObservable_whenCalculatingMin_ThenSuccessfullObtainingMinValue() { + // given + Observable sourceObservable = Observable.range(1, 20); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + MathObservable.min(sourceObservable) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(1); + } + + @Test + public void givenItemObservable_whenCalculatingMaxWithComparator_ThenSuccessfullObtainingMaxItem() { + // given + Item five = new Item(5); + List list = Arrays.asList(new Item(1), new Item(2), new Item(3), new Item(4), five); + Observable itemObservable = Observable.from(list); + + TestSubscriber subscriber = TestSubscriber.create(); + + // when + MathObservable.from(itemObservable) + .max(Comparator.comparing(Item::getId)) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(five); + + } + + @Test + public void givenItemObservable_whenCalculatingMinWithComparator_ThenSuccessfullObtainingMinItem() { + // given + Item one = new Item(1); + List list = Arrays.asList(one, new Item(2), new Item(3), new Item(4), new Item(5)); + TestSubscriber subscriber = TestSubscriber.create(); + Observable itemObservable = Observable.from(list); + + // when + MathObservable.from(itemObservable) + .min(Comparator.comparing(Item::getId)) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValue(one); + + } + + class Item { + private Integer id; + + public Item(Integer id) { + this.id = id; + } + + public Integer getId() { + return id; + } + + } +} diff --git a/saas/.gitignore b/saas/.gitignore new file mode 100644 index 0000000000..3de4cc647e --- /dev/null +++ b/saas/.gitignore @@ -0,0 +1,26 @@ +*.class + +0.* + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* +.resourceCache + +# Packaged files # +*.jar +*.war +*.ear + +# Files generated by integration tests +*.txt +backup-pom.xml +/bin/ +/temp + +#IntelliJ specific +.idea/ +*.iml \ No newline at end of file diff --git a/saas/pom.xml b/saas/pom.xml new file mode 100644 index 0000000000..7c8745910f --- /dev/null +++ b/saas/pom.xml @@ -0,0 +1,84 @@ + + 4.0.0 + com.baeldung + saas + 0.1.0-SNAPSHOT + jar + saas + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + com.atlassian.jira + jira-rest-java-client-core + 4.0.0 + + + com.atlassian.fugue + fugue + 2.6.1 + + + com.google.guava + guava + 19.0 + + + + + + saas + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + java + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + -Xmx300m + -XX:+UseParallelGC + -classpath + + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + + + + + + + + atlassian-public + https://packages.atlassian.com/maven/repository/public + + + + + 3.6.0 + + \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/jira/MyJiraClient.java b/saas/src/main/java/com/baeldung/saas/jira/MyJiraClient.java similarity index 99% rename from libraries/src/main/java/com/baeldung/jira/MyJiraClient.java rename to saas/src/main/java/com/baeldung/saas/jira/MyJiraClient.java index ea1d73f52a..ff1de5a6d0 100644 --- a/libraries/src/main/java/com/baeldung/jira/MyJiraClient.java +++ b/saas/src/main/java/com/baeldung/saas/jira/MyJiraClient.java @@ -1,4 +1,4 @@ -package com.baeldung.jira; +package com.baeldung.saas.jira; import com.atlassian.jira.rest.client.api.IssueRestClient; import com.atlassian.jira.rest.client.api.JiraRestClient; diff --git a/spring-all/pom.xml b/spring-all/pom.xml index 1ecb824c40..6615e1d6cd 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -195,7 +195,11 @@ spring-core ${org.springframework.version} - + + + org.springframework.boot + spring-boot-starter-thymeleaf + @@ -280,4 +284,4 @@ - \ No newline at end of file + diff --git a/spring-all/src/main/webapp/WEB-INF/view/viewPage.html b/spring-all/src/main/webapp/WEB-INF/view/viewPage.html new file mode 100644 index 0000000000..71f766407e --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/view/viewPage.html @@ -0,0 +1,9 @@ + + + + Title + + +
Web Application. Passed parameter : th:text="${message}"
+ + diff --git a/spring-all/src/main/webapp/WEB-INF/view/viewPage.jsp b/spring-all/src/main/webapp/WEB-INF/view/viewPage.jsp deleted file mode 100644 index ca638b33f5..0000000000 --- a/spring-all/src/main/webapp/WEB-INF/view/viewPage.jsp +++ /dev/null @@ -1,11 +0,0 @@ -<%@ page contentType="text/html;charset=UTF-8" language="java" %> - - - Title - - -
- Web Application. Passed parameter : ${message} -
- -