Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -4,5 +4,5 @@ This module contains articles about Java 11 core features
|
||||
|
||||
### Relevant articles
|
||||
- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional)
|
||||
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public abstract class Animal implements Eating {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Bird extends Animal {
|
||||
private boolean walks;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public interface Eating {
|
||||
String eats();
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Goat extends Animal implements Locomotion {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
@Greeter(greet="Good morning")
|
||||
public class Greetings {
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public interface Locomotion {
|
||||
String getLocomotion();
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Operations {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
+33
-40
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -32,23 +32,23 @@ public class ReflectionUnitTest {
|
||||
final Class<?> clazz = goat.getClass();
|
||||
|
||||
assertEquals("Goat", clazz.getSimpleName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> clazz = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> clazz = Class.forName("com.baeldung.reflection.Goat");
|
||||
|
||||
assertEquals("Goat", clazz.getSimpleName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenRecognisesModifiers_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final int goatMods = goatClass.getModifiers();
|
||||
final int animalMods = animalClass.getModifiers();
|
||||
|
||||
@@ -63,7 +63,7 @@ public class ReflectionUnitTest {
|
||||
final Class<?> goatClass = goat.getClass();
|
||||
final Package pkg = goatClass.getPackage();
|
||||
|
||||
assertEquals("com.baeldung.java.reflection", pkg.getName());
|
||||
assertEquals("com.baeldung.reflection", pkg.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -81,8 +81,8 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsImplementedInterfaces_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final Class<?>[] goatInterfaces = goatClass.getInterfaces();
|
||||
final Class<?>[] animalInterfaces = animalClass.getInterfaces();
|
||||
|
||||
@@ -94,16 +94,16 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsConstructor_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
|
||||
final Constructor<?>[] constructors = goatClass.getConstructors();
|
||||
|
||||
assertEquals(1, constructors.length);
|
||||
assertEquals("com.baeldung.java.reflection.Goat", constructors[0].getName());
|
||||
assertEquals("com.baeldung.reflection.Goat", constructors[0].getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsFields_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final Field[] fields = animalClass.getDeclaredFields();
|
||||
|
||||
final List<String> actualFields = getFieldNames(fields);
|
||||
@@ -114,7 +114,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsMethods_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final Method[] methods = animalClass.getDeclaredMethods();
|
||||
final List<String> actualMethods = getMethodNames(methods);
|
||||
|
||||
@@ -124,7 +124,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsAllConstructors_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Constructor<?>[] constructors = birdClass.getConstructors();
|
||||
|
||||
assertEquals(3, constructors.length);
|
||||
@@ -132,7 +132,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsEachConstructorByParamTypes_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
birdClass.getConstructor();
|
||||
birdClass.getConstructor(String.class);
|
||||
birdClass.getConstructor(String.class, boolean.class);
|
||||
@@ -140,7 +140,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
|
||||
final Constructor<?> cons1 = birdClass.getConstructor();
|
||||
final Constructor<?> cons2 = birdClass.getConstructor(String.class);
|
||||
@@ -159,7 +159,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsPublicFields_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field[] fields = birdClass.getFields();
|
||||
assertEquals(1, fields.length);
|
||||
assertEquals("CATEGORY", fields[0].getName());
|
||||
@@ -168,7 +168,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsPublicFieldByName_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field field = birdClass.getField("CATEGORY");
|
||||
assertEquals("CATEGORY", field.getName());
|
||||
|
||||
@@ -176,7 +176,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsDeclaredFields_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field[] fields = birdClass.getDeclaredFields();
|
||||
assertEquals(1, fields.length);
|
||||
assertEquals("walks", fields[0].getName());
|
||||
@@ -184,7 +184,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsFieldsByName_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field field = birdClass.getDeclaredField("walks");
|
||||
assertEquals("walks", field.getName());
|
||||
|
||||
@@ -192,14 +192,14 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClassField_whenGetsType_thenCorrect() throws Exception {
|
||||
final Field field = Class.forName("com.baeldung.java.reflection.Bird").getDeclaredField("walks");
|
||||
final Field field = Class.forName("com.baeldung.reflection.Bird").getDeclaredField("walks");
|
||||
final Class<?> fieldClass = field.getType();
|
||||
assertEquals("boolean", fieldClass.getSimpleName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||
final Field field = birdClass.getDeclaredField("walks");
|
||||
field.setAccessible(true);
|
||||
@@ -216,7 +216,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClassField_whenGetsAndSetsWithNull_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field field = birdClass.getField("CATEGORY");
|
||||
field.setAccessible(true);
|
||||
|
||||
@@ -225,7 +225,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsAllPublicMethods_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Method[] methods = birdClass.getMethods();
|
||||
final List<String> methodNames = getMethodNames(methods);
|
||||
|
||||
@@ -235,7 +235,7 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsOnlyDeclaredMethods_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final List<String> actualMethodNames = getMethodNames(birdClass.getDeclaredMethods());
|
||||
|
||||
final List<String> expectedMethodNames = Arrays.asList("setWalks", "walks", "getSound", "eats");
|
||||
@@ -248,24 +248,17 @@ public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMethodName_whenGetsMethod_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Method walksMethod = birdClass.getDeclaredMethod("walks");
|
||||
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||
|
||||
assertFalse(walksMethod.isAccessible());
|
||||
assertFalse(setWalksMethod.isAccessible());
|
||||
|
||||
walksMethod.setAccessible(true);
|
||||
setWalksMethod.setAccessible(true);
|
||||
|
||||
assertTrue(walksMethod.isAccessible());
|
||||
assertTrue(setWalksMethod.isAccessible());
|
||||
final Bird bird = new Bird();
|
||||
final Method walksMethod = bird.getClass().getDeclaredMethod("walks");
|
||||
final Method setWalksMethod = bird.getClass().getDeclaredMethod("setWalks", boolean.class);
|
||||
|
||||
assertTrue(walksMethod.canAccess(bird));
|
||||
assertTrue(setWalksMethod.canAccess(bird));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||
final Method walksMethod = birdClass.getDeclaredMethod("walks");
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.collections.removeallperformance;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import com.baeldung.collections.containsperformance.Employee;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 5)
|
||||
public class HashSetBenchmark {
|
||||
|
||||
@State(Scope.Thread)
|
||||
public static class MyState {
|
||||
private Set<Employee> employeeSet1 = new HashSet<>();
|
||||
private List<Employee> employeeList1 = new ArrayList<>();
|
||||
private Set<Employee> employeeSet2 = new HashSet<>();
|
||||
private List<Employee> employeeList2 = new ArrayList<>();
|
||||
|
||||
private long set1Size = 60000;
|
||||
private long list1Size = 50000;
|
||||
private long set2Size = 50000;
|
||||
private long list2Size = 60000;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setUp() {
|
||||
|
||||
for (long i = 0; i < set1Size; i++) {
|
||||
employeeSet1.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
for (long i = 0; i < list1Size; i++) {
|
||||
employeeList1.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
for (long i = 0; i < set2Size; i++) {
|
||||
employeeSet2.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
for (long i = 0; i < list2Size; i++) {
|
||||
employeeList2.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean given_SizeOfHashsetGreaterThanSizeOfCollection_When_RemoveAllFromHashSet_Then_GoodPerformance(MyState state) {
|
||||
return state.employeeSet1.removeAll(state.employeeList1);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean given_SizeOfHashsetSmallerThanSizeOfCollection_When_RemoveAllFromHashSet_Then_BadPerformance(MyState state) {
|
||||
return state.employeeSet2.removeAll(state.employeeList2);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Options options = new OptionsBuilder().include(HashSetBenchmark.class.getSimpleName())
|
||||
.threads(1)
|
||||
.forks(1)
|
||||
.shouldFailOnError(true)
|
||||
.shouldDoGC(true)
|
||||
.jvmArgs("-server")
|
||||
.build();
|
||||
new Runner(options).run();
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.collections.hashset;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HashSetUnitTest {
|
||||
|
||||
@Test
|
||||
void whenRemoveAllFromHashset_thenRemovesAllElementsFromHashsetThatArePresentInCollection() {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
Collection<Integer> collection = new ArrayList<>();
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
set.add(3);
|
||||
set.add(4);
|
||||
collection.add(1);
|
||||
collection.add(3);
|
||||
|
||||
set.removeAll(collection);
|
||||
|
||||
assertEquals(2, set.size());
|
||||
Integer[] actualElements = new Integer[set.size()];
|
||||
Integer[] expectedElements = new Integer[] { 2, 4 };
|
||||
assertArrayEquals(expectedElements, set.toArray(actualElements));
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.checkclassexistence;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CheckClassExistenceUnitTest {
|
||||
|
||||
public static class InitializingClass {
|
||||
static {
|
||||
if (true) { //enable throwing of an exception in a static initialization block
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = ClassNotFoundException.class) //thrown when class does not exist
|
||||
public void givenNonExistingClass_whenUsingForName_thenClassNotFound() throws ClassNotFoundException {
|
||||
Class.forName("class.that.does.not.exist");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingClass_whenUsingForName_thenNoException() throws ClassNotFoundException {
|
||||
Class.forName("java.lang.String");
|
||||
}
|
||||
|
||||
@Test(expected = ExceptionInInitializerError.class) //thrown when exception occurs inside of a static initialization block
|
||||
public void givenInitializingClass_whenUsingForName_thenInitializationError() throws ClassNotFoundException {
|
||||
Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitializingClass_whenUsingForNameWithoutInitialization_thenNoException() throws ClassNotFoundException {
|
||||
Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass", false, getClass().getClassLoader());
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,12 @@
|
||||
<artifactId>async-http-client</artifactId>
|
||||
<version>${async-http-client.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail</artifactId>
|
||||
<version>1.5.8</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.mail.mailwithattachment;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
import javax.mail.BodyPart;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
|
||||
public class MailWithAttachmentService {
|
||||
|
||||
private String username = "";
|
||||
private String password = "";
|
||||
private String host = "";
|
||||
private String port = "";
|
||||
|
||||
MailWithAttachmentService() {
|
||||
}
|
||||
|
||||
MailWithAttachmentService(String username, String password, String host, String port) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Session getSession() {
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.smtp.host", this.host);
|
||||
props.put("mail.smtp.port", this.port);
|
||||
|
||||
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
return session;
|
||||
}
|
||||
|
||||
public Message createMail(Session session) throws AddressException, MessagingException, IOException {
|
||||
Message message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress("[email protected]"));
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
|
||||
message.setSubject("Testing Subject");
|
||||
|
||||
BodyPart messageBodyPart = new MimeBodyPart();
|
||||
messageBodyPart.setText("This is message body");
|
||||
|
||||
Multipart multipart = new MimeMultipart();
|
||||
multipart.addBodyPart(messageBodyPart);
|
||||
|
||||
MimeBodyPart attachmentPart = new MimeBodyPart();
|
||||
MimeBodyPart attachmentPart2 = new MimeBodyPart();
|
||||
|
||||
attachmentPart.attachFile(new File("C:\\Document1.txt"));
|
||||
attachmentPart2.attachFile(new File("C:\\Document2.txt"));
|
||||
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
multipart.addBodyPart(attachmentPart2);
|
||||
|
||||
message.setContent(multipart);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public void sendMail(Session session) throws MessagingException, IOException {
|
||||
|
||||
Message message = createMail(session);
|
||||
Transport.send(message);
|
||||
}
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.mail.mailwithattachment;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import javax.annotation.Resource;
|
||||
import javax.mail.Session;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.mail.mailwithattachment.MailWithAttachmentService;
|
||||
import com.icegreen.greenmail.util.GreenMail;
|
||||
import com.icegreen.greenmail.util.ServerSetupTest;
|
||||
|
||||
public class MailWithAttachmentServiceLiveTest {
|
||||
|
||||
@Resource
|
||||
private MailWithAttachmentService emailService;
|
||||
private GreenMail greenMail;
|
||||
|
||||
@Before
|
||||
public void startMailServer() {
|
||||
emailService = new MailWithAttachmentService();
|
||||
greenMail = new GreenMail(ServerSetupTest.SMTP);
|
||||
greenMail.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopMailServer() {
|
||||
greenMail.stop();
|
||||
emailService = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canSendMail() {
|
||||
try {
|
||||
Session testSession = greenMail.getSmtp()
|
||||
.createSession();
|
||||
emailService.sendMail(testSession);
|
||||
assertEquals(1, greenMail.getReceivedMessages().length);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
- [Void Type in Java](https://www.baeldung.com/java-void-type)
|
||||
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
|
||||
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
|
||||
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
|
||||
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
|
||||
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
apply plugin: "java"
|
||||
apply plugin: "application"
|
||||
description = "Gradle Command Line Arguments examples"
|
||||
|
||||
ext.javaMainClass = "com.baeldung.cmd.MainClass"
|
||||
|
||||
application {
|
||||
mainClassName = javaMainClass
|
||||
}
|
||||
|
||||
task propertyTypes(){
|
||||
doLast{
|
||||
if (project.hasProperty("args")) {
|
||||
println "Our input argument with project property ["+project.getProperty("args")+"]"
|
||||
}
|
||||
println "Our input argument with system property ["+System.getProperty("args")+"]"
|
||||
}
|
||||
}
|
||||
|
||||
if (project.hasProperty("args")) {
|
||||
ext.cmdargs = project.getProperty("args")
|
||||
} else {
|
||||
ext.cmdargs = "ls"
|
||||
}
|
||||
|
||||
task cmdLineJavaExec(type: JavaExec) {
|
||||
group = "Execution"
|
||||
description = "Run the main class with JavaExecTask"
|
||||
classpath = sourceSets.main.runtimeClasspath
|
||||
main = javaMainClass
|
||||
args cmdargs.split()
|
||||
}
|
||||
|
||||
task cmdLineExec(type: Exec) {
|
||||
group = "Execution"
|
||||
description = "Run an external program with ExecTask"
|
||||
commandLine cmdargs.split()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.cmd;
|
||||
|
||||
public class MainClass {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Gradle command line arguments example");
|
||||
for (String arg : args) {
|
||||
System.out.println("Got argument [" + arg + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
rootProject.name='gradle-5-articles'
|
||||
include 'java-exec'
|
||||
include 'unused-dependencies'
|
||||
include 'source-sets'
|
||||
include 'source-sets'
|
||||
include 'cmd-line-args'
|
||||
@@ -15,6 +15,12 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.17.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
|
||||
+7
-7
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.arrayconversion;
|
||||
|
||||
import org.assertj.core.api.ListAssert;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -7,8 +8,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ArrayToListConversionUnitTest {
|
||||
|
||||
@@ -17,8 +17,8 @@ public class ArrayToListConversionUnitTest {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = Arrays.asList(stringArray);
|
||||
stringList.set(0, "E");
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D"));
|
||||
assertArrayEquals(stringArray, new String[] { "E", "B", "C", "D" });
|
||||
assertThat(stringList).containsExactly("E", "B", "C", "D");
|
||||
assertThat(stringArray).containsExactly("E", "B", "C", "D");
|
||||
stringList.add("F");
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ public class ArrayToListConversionUnitTest {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = new ArrayList<>(Arrays.asList(stringArray));
|
||||
stringList.set(0, "E");
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D"));
|
||||
assertArrayEquals(stringArray, new String[] { "A", "B", "C", "D" });
|
||||
assertThat(stringList).containsExactly("E", "B", "C", "D");
|
||||
assertThat(stringArray).containsExactly("A", "B", "C", "D");
|
||||
stringList.add("F");
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D", "F"));
|
||||
assertThat(stringList).containsExactly("E", "B", "C", "D", "F");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -79,6 +79,8 @@
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>logging-service</finalName>
|
||||
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -93,13 +95,21 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<finalName>logging-service</finalName>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<log4j2.version>2.7</log4j2.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
<jbosslogging.version>3.3.0.Final</jbosslogging.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user