Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
dbc0a2d957
|
@ -3,3 +3,4 @@
|
||||||
- [NoSuchMethodError in Java](https://www.baeldung.com/java-nosuchmethod-error)
|
- [NoSuchMethodError in Java](https://www.baeldung.com/java-nosuchmethod-error)
|
||||||
- [IllegalArgumentException or NullPointerException for a Null Parameter?](https://www.baeldung.com/java-illegalargumentexception-or-nullpointerexception)
|
- [IllegalArgumentException or NullPointerException for a Null Parameter?](https://www.baeldung.com/java-illegalargumentexception-or-nullpointerexception)
|
||||||
- [IllegalMonitorStateException in Java](https://www.baeldung.com/java-illegalmonitorstateexception)
|
- [IllegalMonitorStateException in Java](https://www.baeldung.com/java-illegalmonitorstateexception)
|
||||||
|
- [AbstractMethodError in Java](https://www.baeldung.com/java-abstractmethoderror)
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.transientkw;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Book implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -2936687026040726549L;
|
||||||
|
|
||||||
|
private String bookName;
|
||||||
|
private transient String description;
|
||||||
|
private transient int copies;
|
||||||
|
private final transient String bookCategory = "Fiction";
|
||||||
|
|
||||||
|
public String getBookName() {
|
||||||
|
return bookName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookName(String bookName) {
|
||||||
|
this.bookName = bookName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCopies() {
|
||||||
|
return copies;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCopies(int copies) {
|
||||||
|
this.copies = copies;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBookCategory() {
|
||||||
|
return bookCategory;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.transientkw;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
|
public class BookSerDe {
|
||||||
|
static String fileName = "book.ser";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to serialize Book objects to the file
|
||||||
|
* @throws FileNotFoundException
|
||||||
|
*/
|
||||||
|
public static void serialize(Book book) throws Exception {
|
||||||
|
FileOutputStream file = new FileOutputStream(fileName);
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(file);
|
||||||
|
|
||||||
|
out.writeObject(book);
|
||||||
|
|
||||||
|
out.close();
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to deserialize the person object
|
||||||
|
* @return book
|
||||||
|
* @throws IOException, ClassNotFoundException
|
||||||
|
*/
|
||||||
|
public static Book deserialize() throws Exception {
|
||||||
|
FileInputStream file = new FileInputStream(fileName);
|
||||||
|
ObjectInputStream in = new ObjectInputStream(file);
|
||||||
|
|
||||||
|
Book book = (Book) in.readObject();
|
||||||
|
|
||||||
|
in.close();
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.transientkw;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class TransientUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenTransient_whenSerDe_thenVerifyValues() throws Exception {
|
||||||
|
Book book = new Book();
|
||||||
|
book.setBookName("Java Reference");
|
||||||
|
book.setDescription("will not be saved");
|
||||||
|
book.setCopies(25);
|
||||||
|
|
||||||
|
BookSerDe.serialize(book);
|
||||||
|
Book book2 = BookSerDe.deserialize();
|
||||||
|
|
||||||
|
assertEquals("Java Reference", book2.getBookName());
|
||||||
|
assertNull(book2.getDescription());
|
||||||
|
assertEquals(0, book2.getCopies());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenFinalTransient_whenSerDe_thenValuePersisted() throws Exception {
|
||||||
|
Book book = new Book();
|
||||||
|
|
||||||
|
BookSerDe.serialize(book);
|
||||||
|
Book book2 = BookSerDe.deserialize();
|
||||||
|
|
||||||
|
assertEquals("Fiction", book2.getBookCategory());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.tostring;
|
||||||
|
|
||||||
|
public class StringCastUtils {
|
||||||
|
public static String castToString(Object object) {
|
||||||
|
if (object instanceof String) {
|
||||||
|
return (String) object;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getStringRepresentation(Object object) {
|
||||||
|
if (object != null) {
|
||||||
|
return object.toString();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,104 @@
|
||||||
|
package com.baeldung.tostring;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
public class ToStringUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCastToObjectAndString_thenSameAndNoException() {
|
||||||
|
String input = "baeldung";
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
String str = (String) obj;
|
||||||
|
|
||||||
|
assertEquals(obj, str);
|
||||||
|
assertEquals(str, input);
|
||||||
|
assertSame(input, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenIntegerObject_whenCastToObjectAndString_thenCastClassException() {
|
||||||
|
Integer input = 1234;
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
String str = (String) obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNullInteger_whenCastToObjectAndString_thenSameAndNoException() {
|
||||||
|
Integer input = null;
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
String str = (String) obj;
|
||||||
|
|
||||||
|
assertEquals(obj, str);
|
||||||
|
assertEquals(str, input);
|
||||||
|
assertSame(input, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void givenNullInteger_whenToString_thenNullPointerException() {
|
||||||
|
Integer input = null;
|
||||||
|
|
||||||
|
String str = input.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInteger_whenCastToObject_thenToStringEquals() {
|
||||||
|
Integer input = 1234;
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
|
||||||
|
assertEquals("1234", input.toString());
|
||||||
|
assertEquals("1234", obj.toString());
|
||||||
|
assertEquals(input.toString(), obj.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenToString_thenSame() {
|
||||||
|
String str = "baeldung";
|
||||||
|
|
||||||
|
assertEquals("baeldung", str.toString());
|
||||||
|
assertSame(str, str.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCastToObject_thenCastToStringReturnsSame() {
|
||||||
|
String input = "baeldung";
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
|
||||||
|
assertSame(input, StringCastUtils.castToString(obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInteger_whenCastToObject_thenCastToStringReturnsNull() {
|
||||||
|
Integer input = 1234;
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
|
||||||
|
assertEquals(null, StringCastUtils.castToString(obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenIntegerNull_whenCastToObject_thenCastToStringReturnsNull() {
|
||||||
|
Integer input = null;
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
|
||||||
|
assertEquals(null, StringCastUtils.castToString(obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenIntegerNotNull_whenCastToObject_thenGetToStringReturnsString() {
|
||||||
|
Integer input = 1234;
|
||||||
|
|
||||||
|
Object obj = input;
|
||||||
|
|
||||||
|
assertEquals("1234", StringCastUtils.getStringRepresentation(obj));
|
||||||
|
assertNotSame("1234", StringCastUtils.getStringRepresentation(obj));
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,4 +22,57 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib-jdk7</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib-common</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-reflect</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test-junit</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test-common</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test-annotations-common</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<kotlin.version>1.4.10</kotlin.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.kotlin.delegates
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.properties.PropertyDelegateProvider
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class DelegateProvider<in T, D>(private val field: String, private val id: Int)
|
||||||
|
: PropertyDelegateProvider<T, DatabaseDelegate<T, D>> {
|
||||||
|
override operator fun provideDelegate(thisRef: T, prop: KProperty<*>): DatabaseDelegate<T, D> {
|
||||||
|
println("Providing delegate for field $field and ID $id")
|
||||||
|
prop.returnType.isMarkedNullable
|
||||||
|
return DatabaseDelegate(field, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DelegateProvidingUser(val id: Int) {
|
||||||
|
var name: String by DelegateProvider("name", id)
|
||||||
|
var age: Int by DelegateProvider("age", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
class DelegateProviderTest {
|
||||||
|
@Test
|
||||||
|
fun testGetKnownFields() {
|
||||||
|
val user = DelegateProvidingUser(1)
|
||||||
|
assertEquals("George", user.name)
|
||||||
|
assertEquals(4, user.age)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSetKnownFields() {
|
||||||
|
val user = DelegateProvidingUser(2)
|
||||||
|
user.age = 3
|
||||||
|
assertEquals(3, user.age)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NoRecordFoundException::class)
|
||||||
|
fun testGetKnownField() {
|
||||||
|
val user = DelegateProvidingUser(3)
|
||||||
|
user.name
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,3 +8,4 @@ This module contains articles about Gradle
|
||||||
- [Creating a Fat Jar in Gradle](https://www.baeldung.com/gradle-fat-jar)
|
- [Creating a Fat Jar in Gradle](https://www.baeldung.com/gradle-fat-jar)
|
||||||
- [A Custom Task in Gradle](https://www.baeldung.com/gradle-custom-task)
|
- [A Custom Task in Gradle](https://www.baeldung.com/gradle-custom-task)
|
||||||
- [Using JUnit 5 with Gradle](https://www.baeldung.com/junit-5-gradle)
|
- [Using JUnit 5 with Gradle](https://www.baeldung.com/junit-5-gradle)
|
||||||
|
- [Dependency Management in Gradle](https://www.baeldung.com/gradle-dependency-management)
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.jvmbitversion;
|
||||||
|
|
||||||
|
import com.sun.jna.Platform;
|
||||||
|
|
||||||
|
public class JVMBitVersion {
|
||||||
|
|
||||||
|
public String getUsingSystemClass() {
|
||||||
|
return System.getProperty("sun.arch.data.model") + "-bit";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsingNativeClass() {
|
||||||
|
if (com.sun.jna.Native.POINTER_SIZE == 8) {
|
||||||
|
return "64-bit";
|
||||||
|
} else if (com.sun.jna.Native.POINTER_SIZE == 4) {
|
||||||
|
return "32-bit";
|
||||||
|
} else
|
||||||
|
return "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getUsingPlatformClass() {
|
||||||
|
return (Platform.is64Bit());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.baeldung.jvmbitversion;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.jvmbitversion.JVMBitVersion;
|
||||||
|
import com.sun.jna.Platform;
|
||||||
|
|
||||||
|
public class JVMBitVersionUnitTest {
|
||||||
|
|
||||||
|
private JVMBitVersion jvmVersion;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
jvmVersion = new JVMBitVersion();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingSystemClass_thenOutputIsAsExpected() {
|
||||||
|
if (System.getProperty("sun.arch.data.model") == "64") {
|
||||||
|
assertEquals("64-bit", jvmVersion.getUsingSystemClass());
|
||||||
|
} else if (System.getProperty("sun.arch.data.model") == "32") {
|
||||||
|
assertEquals("32-bit", jvmVersion.getUsingSystemClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingNativeClass_thenResultIsAsExpected() {
|
||||||
|
if (com.sun.jna.Native.POINTER_SIZE == 8) {
|
||||||
|
assertEquals("64-bit", jvmVersion.getUsingNativeClass());
|
||||||
|
} else if (com.sun.jna.Native.POINTER_SIZE == 4) {
|
||||||
|
assertEquals("32-bit", jvmVersion.getUsingNativeClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingPlatformClass_thenResultIsAsExpected() {
|
||||||
|
if (Platform.is64Bit() == Boolean.TRUE) {
|
||||||
|
assertEquals(Boolean.TRUE, jvmVersion.getUsingPlatformClass());
|
||||||
|
} else if (com.sun.jna.Native.POINTER_SIZE == 4) {
|
||||||
|
assertEquals(Boolean.FALSE, jvmVersion.getUsingPlatformClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [Probability in Java](https://www.baeldung.com/java-probability)
|
- [Probability in Java](https://www.baeldung.com/java-probability)
|
||||||
|
- [Understanding the & 0xff Value in Java](https://www.baeldung.com/java-and-0xff)
|
||||||
|
|
|
@ -17,4 +17,5 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
|
||||||
- [Introduction to Protonpack](https://www.baeldung.com/java-protonpack)
|
- [Introduction to Protonpack](https://www.baeldung.com/java-protonpack)
|
||||||
- [Java-R Integration](https://www.baeldung.com/java-r-integration)
|
- [Java-R Integration](https://www.baeldung.com/java-r-integration)
|
||||||
- [Using libphonenumber to Validate Phone Numbers](https://www.baeldung.com/java-libphonenumber)
|
- [Using libphonenumber to Validate Phone Numbers](https://www.baeldung.com/java-libphonenumber)
|
||||||
|
- [Apache Commons Collections vs Google Guava](https://www.baeldung.com/apache-commons-collections-vs-guava)
|
||||||
- More articles [[<-- prev]](/libraries-5)
|
- More articles [[<-- prev]](/libraries-5)
|
||||||
|
|
|
@ -76,6 +76,11 @@
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-collections4</artifactId>
|
||||||
|
<version>${commons-collections4.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-net</groupId>
|
<groupId>commons-net</groupId>
|
||||||
<artifactId>commons-net</artifactId>
|
<artifactId>commons-net</artifactId>
|
||||||
|
@ -156,6 +161,7 @@
|
||||||
<renjin.version>RELEASE</renjin.version>
|
<renjin.version>RELEASE</renjin.version>
|
||||||
<rcaller.version>3.0</rcaller.version>
|
<rcaller.version>3.0</rcaller.version>
|
||||||
<rserve.version>1.8.1</rserve.version>
|
<rserve.version>1.8.1</rserve.version>
|
||||||
|
<commons-collections4.version>4.4</commons-collections4.version>
|
||||||
<libphonenumber.version>8.12.9</libphonenumber.version>
|
<libphonenumber.version>8.12.9</libphonenumber.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -6,5 +6,6 @@ This module contains articles about HTTP libraries.
|
||||||
|
|
||||||
- [Jetty ReactiveStreams HTTP Client](https://www.baeldung.com/jetty-reactivestreams-http-client)
|
- [Jetty ReactiveStreams HTTP Client](https://www.baeldung.com/jetty-reactivestreams-http-client)
|
||||||
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
|
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
|
||||||
|
- [Retrofit 2 – Dynamic URL](https://www.baeldung.com/retrofit-dynamic-url)
|
||||||
- More articles [[<-- prev]](/libraries-http)
|
- More articles [[<-- prev]](/libraries-http)
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,5 @@
|
||||||
- [List of In-Memory Databases](https://www.baeldung.com/java-in-memory-databases)
|
- [List of In-Memory Databases](https://www.baeldung.com/java-in-memory-databases)
|
||||||
- [Oracle Connection Pooling With Spring](https://www.baeldung.com/spring-oracle-connection-pooling)
|
- [Oracle Connection Pooling With Spring](https://www.baeldung.com/spring-oracle-connection-pooling)
|
||||||
- [Object States in Hibernate’s Session](https://www.baeldung.com/hibernate-session-object-states)
|
- [Object States in Hibernate’s Session](https://www.baeldung.com/hibernate-session-object-states)
|
||||||
|
- [Storing Files Indexed by a Database](https://www.baeldung.com/java-db-storing-files)
|
||||||
- More articles: [[<-- prev]](../spring-boot-persistence)
|
- More articles: [[<-- prev]](../spring-boot-persistence)
|
||||||
|
|
|
@ -22,6 +22,10 @@ class Image {
|
||||||
public Image() {
|
public Image() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Image(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Image(String name, String location) {
|
public Image(String name, String location) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.location = location;
|
this.location = location;
|
||||||
|
|
|
@ -35,8 +35,12 @@ class FileSystemImageIntegrationTest {
|
||||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||||
InputStream image = classLoader.getResourceAsStream("baeldung.jpeg");
|
InputStream image = classLoader.getResourceAsStream("baeldung.jpeg");
|
||||||
|
|
||||||
|
MockMultipartFile jpegImage = new MockMultipartFile("image", "baeldung", MediaType.TEXT_PLAIN_VALUE, image);
|
||||||
MockMultipartHttpServletRequestBuilder multipartRequest = MockMvcRequestBuilders.multipart("/file-system/image")
|
MockMultipartHttpServletRequestBuilder multipartRequest = MockMvcRequestBuilders.multipart("/file-system/image")
|
||||||
.file(new MockMultipartFile("image", "baeldung", MediaType.TEXT_PLAIN_VALUE, image));
|
.file(jpegImage);
|
||||||
|
|
||||||
|
given(fileLocationService.save(jpegImage.getBytes(), "baeldung"))
|
||||||
|
.willReturn(1L);
|
||||||
|
|
||||||
MvcResult result = mockMvc.perform(multipartRequest)
|
MvcResult result = mockMvc.perform(multipartRequest)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.db.indexing;
|
package com.baeldung.db.indexing;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@ -34,13 +35,10 @@ class ImageIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenBaeldungJpegImage_whenUploadIt_thenReturnItsId() throws Exception {
|
void givenBaeldungJpegImage_whenUploadIt_thenReturnItsId() throws Exception {
|
||||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
given(imageRepository.save(any()))
|
||||||
InputStream image = classLoader.getResourceAsStream("baeldung.jpeg");
|
.willReturn(new Image(1L));
|
||||||
|
|
||||||
MockMultipartHttpServletRequestBuilder multipartRequest = MockMvcRequestBuilders.multipart("/image")
|
MvcResult result = mockMvc.perform(createUploadImageRequest())
|
||||||
.file(new MockMultipartFile("image", "baeldung", MediaType.TEXT_PLAIN_VALUE, image));
|
|
||||||
|
|
||||||
MvcResult result = mockMvc.perform(multipartRequest)
|
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andReturn();
|
.andReturn();
|
||||||
|
|
||||||
|
@ -60,6 +58,14 @@ class ImageIntegrationTest {
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MockMultipartHttpServletRequestBuilder createUploadImageRequest() throws IOException {
|
||||||
|
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||||
|
InputStream image = classLoader.getResourceAsStream("baeldung.jpeg");
|
||||||
|
|
||||||
|
return MockMvcRequestBuilders.multipart("/image")
|
||||||
|
.file(new MockMultipartFile("multipartImage", "baeldung", MediaType.TEXT_PLAIN_VALUE, image));
|
||||||
|
}
|
||||||
|
|
||||||
private Image baeldungImage() throws IOException {
|
private Image baeldungImage() throws IOException {
|
||||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
package com.baeldung.properties;
|
package com.baeldung.properties;
|
||||||
|
|
||||||
import com.baeldung.buildproperties.Application;
|
import com.baeldung.configurationproperties.ConfigProperties;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
import com.baeldung.configurationproperties.ConfigProperties;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@ComponentScan(basePackageClasses = {ConfigProperties.class, AdditionalProperties.class})
|
@ComponentScan(basePackageClasses = {ConfigProperties.class, AdditionalProperties.class})
|
||||||
public class ConfigPropertiesDemoApplication {
|
public class ConfigPropertiesDemoApplication {
|
||||||
|
|
|
@ -4,12 +4,10 @@ import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.configurationproperties.ConfigProperties;
|
|
||||||
import com.baeldung.properties.AdditionalProperties;
|
import com.baeldung.properties.AdditionalProperties;
|
||||||
import com.baeldung.properties.ConfigPropertiesDemoApplication;
|
import com.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||||
|
|
||||||
|
@ -19,34 +17,34 @@ import com.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||||
public class ConfigPropertiesIntegrationTest {
|
public class ConfigPropertiesIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ConfigProperties properties;
|
private ConfigProperties configProperties;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AdditionalProperties additionalProperties;
|
private AdditionalProperties additionalProperties;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
|
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
|
||||||
Assert.assertTrue("From address is read as null!", properties.getFrom() != null);
|
Assert.assertTrue("From address is read as null!", configProperties.getFrom() != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
|
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
|
||||||
Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2);
|
Assert.assertTrue("Couldn't bind list property!", configProperties.getDefaultRecipients().size() == 2);
|
||||||
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2);
|
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", configProperties.getDefaultRecipients().size() == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
|
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
|
||||||
Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null);
|
Assert.assertTrue("Couldn't bind map property!", configProperties.getAdditionalHeaders() != null);
|
||||||
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3);
|
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", configProperties.getAdditionalHeaders().size() == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
|
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
|
||||||
Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null);
|
Assert.assertTrue("Couldn't bind map property!", configProperties.getCredentials() != null);
|
||||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1"));
|
Assert.assertTrue("Incorrectly bound object property!", configProperties.getCredentials().getAuthMethod().equals("SHA1"));
|
||||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john"));
|
Assert.assertTrue("Incorrectly bound object property!", configProperties.getCredentials().getUsername().equals("john"));
|
||||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password"));
|
Assert.assertTrue("Incorrectly bound object property!", configProperties.getCredentials().getPassword().equals("password"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
## Relevant Articles:
|
## Relevant Articles:
|
||||||
|
|
||||||
|
- [Set JWT with Spring Boot and Swagger UI](https://www.baeldung.com/spring-boot-swagger-jwt)
|
||||||
|
|
|
@ -26,7 +26,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||||
@AutoConfigureTestDatabase
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
public class GreetingControllerUnitTest {
|
public class GreetingControllerManualTest {
|
||||||
|
|
||||||
private static final String SIMPLE_GREETING = "/greeting/simple";
|
private static final String SIMPLE_GREETING = "/greeting/simple";
|
||||||
private static final String ADVANCED_GREETING = "/greeting/advanced";
|
private static final String ADVANCED_GREETING = "/greeting/advanced";
|
Loading…
Reference in New Issue