Merge branch 'master' of github.com:eugenp/tutorials into master
This commit is contained in:
+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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,4 +9,5 @@ This module contains articles about core Java input and output (IO)
|
||||
- [Check If a File or Directory Exists in Java](https://www.baeldung.com/java-file-directory-exists)
|
||||
- [Copy a Directory in Java](https://www.baeldung.com/java-copy-directory)
|
||||
- [Java Files Open Options](https://www.baeldung.com/java-file-options)
|
||||
- [Creating Temporary Directories in Java](https://www.baeldung.com/java-temp-directories)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-2)
|
||||
|
||||
+3
-2
@@ -4,6 +4,7 @@ import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -20,8 +21,8 @@ public class DirectoryEmptinessUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException {
|
||||
Path aFile = Paths.get(getClass().getResource("/notDir.txt").getPath());
|
||||
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException, URISyntaxException {
|
||||
Path aFile = Paths.get(getClass().getResource("/notDir.txt").toURI());
|
||||
assertThat(isEmpty(aFile)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
+11
-4
@@ -4,6 +4,7 @@ import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -66,10 +67,16 @@ public class TemporaryDirectoriesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixWithFileAttrs_whenCreatePlainJava_thenAttributesAreSet() throws IOException {
|
||||
final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------"));
|
||||
boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
|
||||
|
||||
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix", attrs);
|
||||
assertThat(tmpdir.toFile().getPath()).startsWith("target");
|
||||
assertThat(tmpdir.toFile().canWrite()).isFalse();
|
||||
if(!isPosix){
|
||||
System.out.println("You must be under a Posix Compliant Filesystem to run this test.");
|
||||
} else {
|
||||
final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------"));
|
||||
|
||||
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix", attrs);
|
||||
assertThat(tmpdir.toFile().getPath()).startsWith("target");
|
||||
assertThat(tmpdir.toFile().canWrite()).isFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+4
-6
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>hazelcast</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
@@ -15,7 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Hazelcast Jet -->
|
||||
<dependency>
|
||||
<groupId>com.hazelcast.jet</groupId>
|
||||
<artifactId>hazelcast-jet</artifactId>
|
||||
@@ -34,8 +33,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- hazelcast jet -->
|
||||
<hazelcast.jet.version>0.6</hazelcast.jet.version>
|
||||
<hazelcast.jet.version>4.2</hazelcast.jet.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,24 +1,20 @@
|
||||
package com.baeldung.hazelcast.cluster;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.hazelcast.client.HazelcastClient;
|
||||
import com.hazelcast.client.config.ClientConfig;
|
||||
import com.hazelcast.config.GroupConfig;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.core.IMap;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class NativeClient {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
public static void main(String[] args) {
|
||||
ClientConfig config = new ClientConfig();
|
||||
GroupConfig groupConfig = config.getGroupConfig();
|
||||
groupConfig.setName("dev");
|
||||
groupConfig.setPassword("dev-pass");
|
||||
config.setClusterName("dev");
|
||||
HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);
|
||||
IMap<Long, String> map = hazelcastInstanceClient.getMap("data");
|
||||
for (Entry<Long, String> entry : map.entrySet()) {
|
||||
System.out.println(String.format("Key: %d, Value: %s", entry.getKey(), entry.getValue()));
|
||||
Map<Long, String> map = hazelcastInstanceClient.getMap("data");
|
||||
for (Map.Entry<Long, String> entry : map.entrySet()) {
|
||||
System.out.printf("Key: %d, Value: %s%n", entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
package com.baeldung.hazelcast.cluster;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.hazelcast.core.Hazelcast;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.core.IdGenerator;
|
||||
import com.hazelcast.flakeidgen.FlakeIdGenerator;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ServerNode {
|
||||
|
||||
public static void main(String[] args) {
|
||||
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
|
||||
Map<Long, String> map = hazelcastInstance.getMap("data");
|
||||
IdGenerator idGenerator = hazelcastInstance.getIdGenerator("newid");
|
||||
FlakeIdGenerator idGenerator = hazelcastInstance.getFlakeIdGenerator("newid");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
map.put(idGenerator.newId(), "message" + 1);
|
||||
map.put(idGenerator.newId(), "message" + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,31 @@
|
||||
package com.baeldung.hazelcast.jet;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.hazelcast.jet.Traversers.traverseArray;
|
||||
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
|
||||
import static com.hazelcast.jet.function.DistributedFunctions.wholeItem;
|
||||
|
||||
import com.hazelcast.jet.Jet;
|
||||
import com.hazelcast.jet.JetInstance;
|
||||
import com.hazelcast.jet.pipeline.Pipeline;
|
||||
import com.hazelcast.jet.pipeline.Sinks;
|
||||
import com.hazelcast.jet.pipeline.Sources;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.hazelcast.function.Functions.wholeItem;
|
||||
import static com.hazelcast.jet.Traversers.traverseArray;
|
||||
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
|
||||
|
||||
public class WordCounter {
|
||||
|
||||
private static final String LIST_NAME = "textList";
|
||||
|
||||
private static final String MAP_NAME = "countMap";
|
||||
|
||||
private Pipeline createPipeLine() {
|
||||
Pipeline p = Pipeline.create();
|
||||
p.drawFrom(Sources.<String> list(LIST_NAME))
|
||||
.flatMap(word -> traverseArray(word.toLowerCase()
|
||||
.split("\\W+")))
|
||||
p.readFrom(Sources.<String>list(LIST_NAME))
|
||||
.flatMap(word -> traverseArray(word.toLowerCase().split("\\W+")))
|
||||
.filter(word -> !word.isEmpty())
|
||||
.groupingKey(wholeItem())
|
||||
.aggregate(counting())
|
||||
.drainTo(Sinks.map(MAP_NAME));
|
||||
.writeTo(Sinks.map(MAP_NAME));
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -38,8 +36,7 @@ public class WordCounter {
|
||||
List<String> textList = jet.getList(LIST_NAME);
|
||||
textList.addAll(sentences);
|
||||
Pipeline p = createPipeLine();
|
||||
jet.newJob(p)
|
||||
.join();
|
||||
jet.newJob(p).join();
|
||||
Map<String, Long> counts = jet.getMap(MAP_NAME);
|
||||
count = counts.get(word);
|
||||
} finally {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.baeldung.hazelcast.jet;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class WordCounterUnitTest {
|
||||
|
||||
@@ -15,7 +15,7 @@ public class WordCounterUnitTest {
|
||||
sentences.add("The first second was alright, but the second second was tough.");
|
||||
WordCounter wordCounter = new WordCounter();
|
||||
long countSecond = wordCounter.countWord(sentences, "second");
|
||||
assertTrue(countSecond == 3);
|
||||
assertEquals(3, countSecond);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.entitymodule</groupId>
|
||||
<artifactId>entitymodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>entitymodule</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodule-maven-project</groupId>
|
||||
<artifactId>multimodule-maven-project</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + '}';
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
module com.baeldung.entity {
|
||||
exports com.baeldung.entity;
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.mainappmodule</groupId>
|
||||
<artifactId>mainappmodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>mainappmodule</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodule-maven-project</groupId>
|
||||
<artifactId>multimodule-maven-project</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.entitymodule</groupId>
|
||||
<artifactId>entitymodule</artifactId>
|
||||
<version>${entitymodule.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.daomodule</groupId>
|
||||
<artifactId>daomodule</artifactId>
|
||||
<version>${daomodule.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.userdaomodule</groupId>
|
||||
<artifactId>userdaomodule</artifactId>
|
||||
<version>${userdaomodule.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<entitymodule.version>1.0</entitymodule.version>
|
||||
<daomodule.version>1.0</daomodule.version>
|
||||
<userdaomodule.version>1.0</userdaomodule.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.mainapp;
|
||||
|
||||
import com.baeldung.dao.Dao;
|
||||
import com.baeldung.entity.User;
|
||||
import com.baeldung.userdao.UserDao;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<Integer, User> users = new HashMap<>();
|
||||
users.put(1, new User("Julie"));
|
||||
users.put(2, new User("David"));
|
||||
Dao userDao = new UserDao(users);
|
||||
userDao.findAll().forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
module com.baeldung.mainapp {
|
||||
requires com.baeldung.entity;
|
||||
requires com.baeldung.userdao;
|
||||
requires com.baeldung.dao;
|
||||
uses com.baeldung.dao.Dao;
|
||||
}
|
||||
@@ -14,6 +14,11 @@
|
||||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<!--<module>custom-rule</module>--> <!-- Fixing in JAVA-2819 -->
|
||||
<module>maven-enforcer</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@ package com.baeldung.multipledb.dao.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.Possession;
|
||||
|
||||
public interface PossessionRepository extends JpaRepository<PossessionMultipleDB, Long> {
|
||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@ package com.baeldung.multipledb.dao.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserMultipleDB, Integer> {
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
}
|
||||
+4
-4
@@ -4,7 +4,7 @@ import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class PossessionMultipleDB {
|
||||
public class Possession {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@@ -12,11 +12,11 @@ public class PossessionMultipleDB {
|
||||
|
||||
private String name;
|
||||
|
||||
public PossessionMultipleDB() {
|
||||
public Possession() {
|
||||
super();
|
||||
}
|
||||
|
||||
public PossessionMultipleDB(final String name) {
|
||||
public Possession(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
@@ -58,7 +58,7 @@ public class PossessionMultipleDB {
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final PossessionMultipleDB other = (PossessionMultipleDB) obj;
|
||||
final Possession other = (Possession) obj;
|
||||
if (id != other.id) {
|
||||
return false;
|
||||
}
|
||||
+6
-6
@@ -6,7 +6,7 @@ import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserMultipleDB {
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@@ -18,13 +18,13 @@ public class UserMultipleDB {
|
||||
private Integer status;
|
||||
|
||||
@OneToMany
|
||||
List<PossessionMultipleDB> possessionList;
|
||||
List<Possession> possessionList;
|
||||
|
||||
public UserMultipleDB() {
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserMultipleDB(String name, String email, Integer status) {
|
||||
public User(String name, String email, Integer status) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
@@ -70,11 +70,11 @@ public class UserMultipleDB {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public List<PossessionMultipleDB> getPossessionList() {
|
||||
public List<Possession> getPossessionList() {
|
||||
return possessionList;
|
||||
}
|
||||
|
||||
public void setPossessionList(List<PossessionMultipleDB> possessionList) {
|
||||
public void setPossessionList(List<Possession> possessionList) {
|
||||
this.possessionList = possessionList;
|
||||
}
|
||||
|
||||
+7
-7
@@ -20,8 +20,8 @@ import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||
import com.baeldung.multipledb.dao.user.PossessionRepository;
|
||||
import com.baeldung.multipledb.dao.user.UserRepository;
|
||||
import com.baeldung.multipledb.model.product.Product;
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.Possession;
|
||||
import com.baeldung.multipledb.model.user.User;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||
@@ -42,15 +42,15 @@ public class JpaMultipleDBIntegrationTest {
|
||||
@Test
|
||||
@Transactional("userTransactionManager")
|
||||
public void whenCreatingUser_thenCreated() {
|
||||
UserMultipleDB user = new UserMultipleDB();
|
||||
User user = new User();
|
||||
user.setName("John");
|
||||
user.setEmail("[email protected]");
|
||||
user.setAge(20);
|
||||
PossessionMultipleDB p = new PossessionMultipleDB("sample");
|
||||
Possession p = new Possession("sample");
|
||||
p = possessionRepository.save(p);
|
||||
user.setPossessionList(Collections.singletonList(p));
|
||||
user = userRepository.save(user);
|
||||
final Optional<UserMultipleDB> result = userRepository.findById(user.getId());
|
||||
final Optional<User> result = userRepository.findById(user.getId());
|
||||
assertTrue(result.isPresent());
|
||||
System.out.println(result.get().getPossessionList());
|
||||
assertEquals(1, result.get().getPossessionList().size());
|
||||
@@ -59,14 +59,14 @@ public class JpaMultipleDBIntegrationTest {
|
||||
@Test
|
||||
@Transactional("userTransactionManager")
|
||||
public void whenCreatingUsersWithSameEmail_thenRollback() {
|
||||
UserMultipleDB user1 = new UserMultipleDB();
|
||||
User user1 = new User();
|
||||
user1.setName("John");
|
||||
user1.setEmail("[email protected]");
|
||||
user1.setAge(20);
|
||||
user1 = userRepository.save(user1);
|
||||
assertTrue(userRepository.findById(user1.getId()).isPresent());
|
||||
|
||||
UserMultipleDB user2 = new UserMultipleDB();
|
||||
User user2 = new User();
|
||||
user2.setName("Tom");
|
||||
user2.setEmail("[email protected]");
|
||||
user2.setAge(10);
|
||||
|
||||
@@ -508,7 +508,6 @@
|
||||
|
||||
<module>maven-modules</module>
|
||||
<module>maven-archetype</module>
|
||||
<!-- <module>maven-java-11</module> --> <!-- we haven't upgraded to Java 11 -->
|
||||
<module>maven-polyglot</module>
|
||||
|
||||
<module>mesos-marathon</module>
|
||||
@@ -697,7 +696,6 @@
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-compress</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-http-2</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
@@ -1019,7 +1017,6 @@
|
||||
|
||||
<module>maven-modules</module>
|
||||
<module>maven-archetype</module>
|
||||
<!-- <module>maven-java-11</module> --> <!-- we haven't upgraded to Java 11 -->
|
||||
<module>maven-polyglot</module>
|
||||
|
||||
<module>mesos-marathon</module>
|
||||
@@ -1200,7 +1197,6 @@
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-compress</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
|
||||
+4
-2
@@ -10,6 +10,7 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import springfox.bean.validators.plugins.Validators;
|
||||
import springfox.documentation.builders.StringElementFacetBuilder;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
|
||||
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
|
||||
@@ -30,8 +31,9 @@ public class EmailAnnotationPlugin implements ModelPropertyBuilderPlugin {
|
||||
public void apply(ModelPropertyContext context) {
|
||||
Optional<Email> email = annotationFromBean(context, Email.class);
|
||||
if (email.isPresent()) {
|
||||
context.getBuilder().pattern(email.get().regexp());
|
||||
context.getBuilder().example("[email protected]");
|
||||
context.getSpecificationBuilder().facetBuilder(StringElementFacetBuilder.class)
|
||||
.pattern(email.get().regexp());
|
||||
context.getSpecificationBuilder().example("[email protected]");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
<module>spring-cloud-ribbon-retry</module>
|
||||
<module>spring-cloud-circuit-breaker</module>
|
||||
<module>spring-cloud-eureka-self-preservation</module>
|
||||
<!--<module>spring-cloud-openfeign</module>--> <!-- Fixing in JAVA-2820 -->
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -12,6 +12,7 @@ This module contains articles about Spring Data REST
|
||||
- [Customizing HTTP Endpoints in Spring Data REST](https://www.baeldung.com/spring-data-rest-customize-http-endpoints)
|
||||
- [Spring Boot with SQLite](https://www.baeldung.com/spring-boot-sqlite)
|
||||
- [Spring Data Web Support](https://www.baeldung.com/spring-data-web-support)
|
||||
- [Spring REST and HAL Browser](https://www.baeldung.com/spring-rest-hal)
|
||||
|
||||
### The Course
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
@@ -32,6 +32,15 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-hal-browser -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-hal-browser</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user