merge from upstream
This commit is contained in:
@@ -3,8 +3,18 @@ package com.baeldung.concurrent.daemon;
|
||||
public class NewThread extends Thread {
|
||||
|
||||
public void run() {
|
||||
while (true)
|
||||
for (int i = 0; i < 10; i++)
|
||||
System.out.println("New Thread is running...");
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
while (true) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
System.out.println("New Thread is running..." + i);
|
||||
}
|
||||
|
||||
// prevent the Thread to run forever. It will finish it's execution after 2 seconds
|
||||
if (System.currentTimeMillis() - startTime > 2000) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.concurrent.executorservice;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class DelayedCallable implements Callable<String> {
|
||||
|
||||
private String name;
|
||||
private long period;
|
||||
|
||||
public DelayedCallable(String name, long period) {
|
||||
this.name = name;
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public String call() {
|
||||
|
||||
try {
|
||||
Thread.sleep(period);
|
||||
} catch (InterruptedException ex) {
|
||||
// handle exception
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+3
@@ -3,11 +3,13 @@ package com.baeldung.concurrent.daemon;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DaemonThreadTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void whenCallIsDaemon_thenCorrect() {
|
||||
NewThread daemonThread = new NewThread();
|
||||
NewThread userThread = new NewThread();
|
||||
@@ -20,6 +22,7 @@ public class DaemonThreadTest {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalThreadStateException.class)
|
||||
@Ignore
|
||||
public void givenUserThread_whenSetDaemonWhileRunning_thenIllegalThreadStateException() {
|
||||
NewThread daemonThread = new NewThread();
|
||||
daemonThread.start();
|
||||
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
package com.baeldung.concurrent.executorservice;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
public class WaitingForThreadsToFinishTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishTest.class);
|
||||
private final static ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
|
||||
|
||||
@Test
|
||||
public void givenMultipleThreads_whenInvokeAll_thenMainThreadShouldWaitForAllToFinish() {
|
||||
|
||||
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Callable<String>> callables = Arrays.asList(new DelayedCallable("fast thread", 100), new DelayedCallable("slow thread", 3000));
|
||||
|
||||
try {
|
||||
long startProcessingTime = System.currentTimeMillis();
|
||||
List<Future<String>> futures = WORKER_THREAD_POOL.invokeAll(callables);
|
||||
|
||||
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime;
|
||||
assertTrue(totalProcessingTime >= 3000);
|
||||
|
||||
String firstThreadResponse = futures.get(0)
|
||||
.get();
|
||||
assertTrue("First response should be from the fast thread", "fast thread".equals(firstThreadResponse));
|
||||
|
||||
String secondThreadResponse = futures.get(1)
|
||||
.get();
|
||||
assertTrue("Last response should be from the slow thread", "slow thread".equals(secondThreadResponse));
|
||||
|
||||
} catch (ExecutionException | InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
WORKER_THREAD_POOL.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleThreads_whenUsingCompletionService_thenMainThreadShouldWaitForAllToFinish() {
|
||||
|
||||
CompletionService<String> service = new ExecutorCompletionService<>(WORKER_THREAD_POOL);
|
||||
|
||||
List<Callable<String>> callables = Arrays.asList(new DelayedCallable("fast thread", 100), new DelayedCallable("slow thread", 3000));
|
||||
|
||||
for (Callable<String> callable : callables) {
|
||||
service.submit(callable);
|
||||
}
|
||||
|
||||
WORKER_THREAD_POOL.shutdown();
|
||||
|
||||
try {
|
||||
|
||||
long startProcessingTime = System.currentTimeMillis();
|
||||
|
||||
Future<String> future = service.take();
|
||||
String firstThreadResponse = future.get();
|
||||
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime;
|
||||
|
||||
assertTrue("First response should be from the fast thread", "fast thread".equals(firstThreadResponse));
|
||||
assertTrue(totalProcessingTime >= 100 && totalProcessingTime < 1000);
|
||||
LOG.debug("Thread finished after: " + totalProcessingTime + " milliseconds");
|
||||
|
||||
future = service.take();
|
||||
String secondThreadResponse = future.get();
|
||||
totalProcessingTime = System.currentTimeMillis() - startProcessingTime;
|
||||
|
||||
assertTrue("Last response should be from the slow thread", "slow thread".equals(secondThreadResponse));
|
||||
assertTrue(totalProcessingTime >= 3000 && totalProcessingTime < 4000);
|
||||
LOG.debug("Thread finished after: " + totalProcessingTime + " milliseconds");
|
||||
|
||||
} catch (ExecutionException | InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleThreads_whenUsingCompletableFutures_thenMainThreadShouldWaitForAllToFinish() {
|
||||
|
||||
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "Hello";
|
||||
});
|
||||
|
||||
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
|
||||
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "Beautiful";
|
||||
});
|
||||
|
||||
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
|
||||
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "World";
|
||||
});
|
||||
|
||||
long startProcessingTime = System.currentTimeMillis();
|
||||
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3);
|
||||
combinedFuture.join();
|
||||
|
||||
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime;
|
||||
assertTrue(totalProcessingTime >= 5000 && totalProcessingTime < 6000);
|
||||
|
||||
LOG.debug("Responses from all threads are available after " + totalProcessingTime + " milliseconds");
|
||||
|
||||
try {
|
||||
String thread1Response = future1.get();
|
||||
assertTrue(thread1Response.equals("Hello"));
|
||||
|
||||
String thread2Response = future2.get();
|
||||
assertTrue(thread2Response.equals("Beautiful"));
|
||||
|
||||
String thread3Response = future3.get();
|
||||
assertTrue(thread3Response.equals("World"));
|
||||
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
WORKER_THREAD_POOL.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ import java.util.Set;
|
||||
|
||||
public class EchoServer {
|
||||
|
||||
private static final String POISON_PILL = "POISON_PILL";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Selector selector = Selector.open();
|
||||
ServerSocketChannel serverSocket = ServerSocketChannel.open();
|
||||
@@ -30,23 +32,36 @@ public class EchoServer {
|
||||
SelectionKey key = iter.next();
|
||||
|
||||
if (key.isAcceptable()) {
|
||||
SocketChannel client = serverSocket.accept();
|
||||
client.configureBlocking(false);
|
||||
client.register(selector, SelectionKey.OP_READ);
|
||||
register(selector, serverSocket);
|
||||
}
|
||||
|
||||
if (key.isReadable()) {
|
||||
SocketChannel client = (SocketChannel) key.channel();
|
||||
client.read(buffer);
|
||||
buffer.flip();
|
||||
client.write(buffer);
|
||||
buffer.clear();
|
||||
answerWithEcho(buffer, key);
|
||||
}
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void answerWithEcho(ByteBuffer buffer, SelectionKey key) throws IOException {
|
||||
SocketChannel client = (SocketChannel) key.channel();
|
||||
client.read(buffer);
|
||||
if (new String(buffer.array()).trim().equals(POISON_PILL)) {
|
||||
client.close();
|
||||
System.out.println("Not accepting client messages anymore");
|
||||
}
|
||||
|
||||
buffer.flip();
|
||||
client.write(buffer);
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
private static void register(Selector selector, ServerSocketChannel serverSocket) throws IOException {
|
||||
SocketChannel client = serverSocket.accept();
|
||||
client.configureBlocking(false);
|
||||
client.register(selector, SelectionKey.OP_READ);
|
||||
}
|
||||
|
||||
public static Process start() throws IOException, InterruptedException {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.drools;
|
||||
|
||||
import org.kie.api.KieServices;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
|
||||
import com.baeldung.drools.model.Fact;
|
||||
import com.baeldung.drools.model.Result;
|
||||
|
||||
public class BackwardChaining {
|
||||
public static void main(String[] args) {
|
||||
Result result = new BackwardChaining().backwardChaining();
|
||||
System.out.println(result.getValue());
|
||||
result.getFacts().stream().forEach(System.out::println);
|
||||
}
|
||||
|
||||
public Result backwardChaining() {
|
||||
Result result = new Result();
|
||||
KieServices ks = KieServices.Factory.get();
|
||||
KieContainer kContainer = ks.getKieClasspathContainer();
|
||||
KieSession ksession = kContainer.newKieSession("ksession-backward-chaining");
|
||||
ksession.setGlobal("result", result);
|
||||
ksession.insert(new Fact("Asia", "Planet Earth"));
|
||||
// ksession.insert(new Fact("China", "Asia"));
|
||||
ksession.insert(new Fact("Great Wall of China", "China"));
|
||||
|
||||
ksession.fireAllRules();
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.drools.model;
|
||||
|
||||
import org.kie.api.definition.type.Position;
|
||||
|
||||
public class Fact {
|
||||
|
||||
@Position(0)
|
||||
private String element;
|
||||
|
||||
@Position(1)
|
||||
private String place;
|
||||
|
||||
public Fact(String element, String place) {
|
||||
this.element = element;
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
public String getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void setElement(String element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public String getPlace() {
|
||||
return place;
|
||||
}
|
||||
|
||||
public void setPlace(String place) {
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((element == null) ? 0 : element.hashCode());
|
||||
result = prime * result + ((place == null) ? 0 : place.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Fact other = (Fact) obj;
|
||||
if (element == null) {
|
||||
if (other.element != null)
|
||||
return false;
|
||||
} else if (!element.equals(other.element))
|
||||
return false;
|
||||
if (place == null) {
|
||||
if (other.place != null)
|
||||
return false;
|
||||
} else if (!place.equals(other.place))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Fact{" + "element='" + element + '\'' + ", place='" + place + '\'' + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.drools.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Result {
|
||||
private String value;
|
||||
private List<String> facts = new ArrayList<>();
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public List<String> getFacts() {
|
||||
return facts;
|
||||
}
|
||||
|
||||
public void setFacts(List<String> facts) {
|
||||
this.facts = facts;
|
||||
}
|
||||
|
||||
public void addFact(String fact) {
|
||||
this.facts.add(fact);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
groupId=com.baeldung.drools
|
||||
artifactId=DroosBackwardChaining
|
||||
version=1.0.0-SNAPSHOT
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung
|
||||
|
||||
import com.baeldung.drools.model.Fact;
|
||||
|
||||
global com.baeldung.drools.model.Result result;
|
||||
|
||||
dialect "mvel"
|
||||
|
||||
query belongsTo(String x, String y)
|
||||
Fact(x, y;)
|
||||
or
|
||||
(Fact(z, y;) and belongsTo(x, z;))
|
||||
end
|
||||
|
||||
rule "Great Wall of China BELONGS TO Planet Earth"
|
||||
when
|
||||
belongsTo("Great Wall of China", "Planet Earth";)
|
||||
then
|
||||
result.setValue("Decision one taken: Great Wall of China BELONGS TO Planet Earth");
|
||||
end
|
||||
|
||||
rule "Great Wall of China DOES NOT BELONG TO of Planet Earth"
|
||||
when
|
||||
not belongsTo("Great Wall of China", "Planet Earth";)
|
||||
then
|
||||
result.setValue("Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth");
|
||||
end
|
||||
|
||||
rule "print all facts"
|
||||
when
|
||||
belongsTo(element, place;)
|
||||
then
|
||||
result.addFact(element + " IS ELEMENT OF " + place);
|
||||
end
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.kie.api.KieServices;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
|
||||
import com.baeldung.drools.model.Fact;
|
||||
import com.baeldung.drools.model.Result;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@RunWith(value = JUnit4.class)
|
||||
public class BackwardChainingTest {
|
||||
private Result result;
|
||||
private KieServices ks;
|
||||
private KieContainer kContainer;
|
||||
private KieSession ksession;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
result = new Result();
|
||||
ks = KieServices.Factory.get();
|
||||
kContainer = ks.getKieClasspathContainer();
|
||||
ksession = kContainer.newKieSession("ksession-backward-chaining");
|
||||
ksession.setGlobal("result", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenWallOfChinaIsGiven_ThenItBelongsToPlanetEarth() {
|
||||
|
||||
ksession.setGlobal("result", result);
|
||||
ksession.insert(new Fact("Asia", "Planet Earth"));
|
||||
ksession.insert(new Fact("China", "Asia"));
|
||||
ksession.insert(new Fact("Great Wall of China", "China"));
|
||||
|
||||
ksession.fireAllRules();
|
||||
|
||||
// Assert Decision one
|
||||
assertEquals(result.getValue(), "Decision one taken: Great Wall of China BELONGS TO Planet Earth");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChinaIsNotGiven_ThenWallOfChinaDoesNotBelongToPlanetEarth() {
|
||||
ksession.insert(new Fact("Asia", "Planet Earth"));
|
||||
// ksession.insert(new Location("China", "Asia")); // not provided to force Decision two
|
||||
ksession.insert(new Fact("Great Wall of China", "China"));
|
||||
|
||||
ksession.fireAllRules();
|
||||
|
||||
// Assert Decision two
|
||||
assertEquals(result.getValue(), "Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
@@ -15,19 +16,32 @@ import wildfly.beans.UserBeanLocal;
|
||||
* Servlet implementation class TestEJBServlet
|
||||
*/
|
||||
public class TestEJBServlet extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@EJB
|
||||
private UserBeanLocal userBean;
|
||||
@EJB
|
||||
private UserBeanLocal userBean;
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
List<User> users = userBean.getUsers();
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
List<User> users = userBean.getUsers();
|
||||
|
||||
response.getWriter()
|
||||
.append("The number of users is: " + users.size());
|
||||
}
|
||||
PrintWriter out = response.getWriter();
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
out.println("<html>");
|
||||
out.println("<head><title>Users</title></head>");
|
||||
out.println("<body>");
|
||||
out.println("<center><h1>List of users:</h1>");
|
||||
out.println("<table border=\"1\" align=\"center\" style=\"width:50%\">");
|
||||
for (User user : users) {
|
||||
out.println("<tr>");
|
||||
out.print("<td>" + user.getUsername() + "</td>");
|
||||
out.print("<td>" + user.getEmail() + "</td>");
|
||||
out.println("</tr>");
|
||||
}
|
||||
}
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
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.forketyfork.guest</groupId>
|
||||
<groupId>com.stackify.guest</groupId>
|
||||
<artifactId>spring-mvc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
package com.forketyfork.guest.springmvc;
|
||||
package com.stackify.guest.springmvc;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {"com.forketyfork.guest.springmvc"})
|
||||
@ComponentScan(basePackages = {"com.stackify.guest.springmvc"})
|
||||
public class Spring5Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.forketyfork.guest.springmvc.model;
|
||||
package com.stackify.guest.springmvc.model;
|
||||
|
||||
public class LoginData {
|
||||
|
||||
+3
-2
@@ -1,6 +1,5 @@
|
||||
package com.forketyfork.guest.springmvc.web;
|
||||
package com.stackify.guest.springmvc.web;
|
||||
|
||||
import com.forketyfork.guest.springmvc.model.LoginData;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -8,6 +7,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.stackify.guest.springmvc.model.LoginData;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Controller
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.forketyfork.guest.springmvc.web;
|
||||
package com.stackify.guest.springmvc.web;
|
||||
|
||||
public class MyInputResource {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.forketyfork.guest.springmvc.web;
|
||||
package com.stackify.guest.springmvc.web;
|
||||
|
||||
public class MyOutputResource {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.forketyfork.guest.springmvc.web;
|
||||
package com.stackify.guest.springmvc.web;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -1 +1,3 @@
|
||||
## Relevant articles:
|
||||
|
||||
- [Dynamic Mapping with Hibernate](http://www.baeldung.com/hibernate-dynamic-mapping)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.junit5;
|
||||
|
||||
public class Greetings {
|
||||
|
||||
public static String sayHello() {
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.junit5.Greetings;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class GreetingsTest {
|
||||
|
||||
@Test
|
||||
void whenCallingSayHello_thenReturnHello() {
|
||||
assertTrue("Hello".equals(Greetings.sayHello()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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.templatemethodpattern</groupId>
|
||||
<artifactId>templatemethodpattern</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.templatemethodpattern.application;
|
||||
|
||||
import com.baeldung.templatemethodpattern.model.Computer;
|
||||
import com.baeldung.templatemethodpattern.model.HighEndComputer;
|
||||
import com.baeldung.templatemethodpattern.model.StandardComputer;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Computer standardComputer = new StandardComputer();
|
||||
standardComputer.buildComputer();
|
||||
standardComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
|
||||
|
||||
Computer highEndComputer = new HighEndComputer();
|
||||
highEndComputer.buildComputer();
|
||||
highEndComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.templatemethodpattern.model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Computer {
|
||||
|
||||
protected Map<String, String> computerParts = new HashMap<>();
|
||||
|
||||
public final void buildComputer() {
|
||||
addMotherboard();
|
||||
addProcessor();
|
||||
addMemory();
|
||||
addHardDrive();
|
||||
addGraphicCard();
|
||||
addSoundCard();
|
||||
}
|
||||
|
||||
public abstract void addProcessor();
|
||||
|
||||
public abstract void addMotherboard();
|
||||
|
||||
public abstract void addMemory();
|
||||
|
||||
public abstract void addHardDrive();
|
||||
|
||||
public abstract void addGraphicCard();
|
||||
|
||||
public abstract void addSoundCard();
|
||||
|
||||
public Map<String, String> getComputerParts() {
|
||||
return computerParts;
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.templatemethodpattern.model;
|
||||
|
||||
public class HighEndComputer extends Computer {
|
||||
|
||||
@Override
|
||||
public void addProcessor() {
|
||||
computerParts.put("Processor", "High End Processor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMotherboard() {
|
||||
computerParts.put("Motherboard", "High End Motherboard");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMemory() {
|
||||
computerParts.put("Memory", "16GB");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addHardDrive() {
|
||||
computerParts.put("Hard Drive", "2TB Hard Drive");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGraphicCard() {
|
||||
computerParts.put("Graphic Card", "High End Graphic Card");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSoundCard() {
|
||||
computerParts.put("Sound Card", "High End Sound Card");
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.templatemethodpattern.model;
|
||||
|
||||
public class StandardComputer extends Computer {
|
||||
|
||||
@Override
|
||||
public void addProcessor() {
|
||||
computerParts.put("Processor", "Standard Processor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMotherboard() {
|
||||
computerParts.put("Motherboard", "Standard Motherboard");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMemory() {
|
||||
computerParts.put("Memory", "8GB");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addHardDrive() {
|
||||
computerParts.put("Hard Drive", "1TB Hard Drive");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGraphicCard() {
|
||||
computerParts.put("Graphic Card", "Standard Graphic Card");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSoundCard() {
|
||||
computerParts.put("Sound Card", "Standard Sound Card");
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package com.baeldung.templatemethodpatterntest;
|
||||
|
||||
import com.baeldung.templatemethodpattern.model.HighEndComputer;
|
||||
import com.baeldung.templatemethodpattern.model.StandardComputer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TemplateMethodPatternTest {
|
||||
|
||||
private static StandardComputer standardComputer;
|
||||
private static HighEndComputer highEndComputer;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpStandardComputerInstance() {
|
||||
standardComputer = new StandardComputer();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpHighEndComputerInstance() {
|
||||
highEndComputer = new HighEndComputer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
|
||||
standardComputer.addProcessor();
|
||||
Assert.assertEquals("Standard Processor", standardComputer
|
||||
.getComputerParts().get("Processor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
||||
standardComputer.addMotherboard();
|
||||
Assert.assertEquals("Standard Motherboard", standardComputer
|
||||
.getComputerParts().get("Motherboard"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardMemory_whenAddingMemory_thenEqualAssertion() {
|
||||
standardComputer.addMemory();
|
||||
Assert.assertEquals("8GB", standardComputer
|
||||
.getComputerParts().get("Memory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardHardDrive_whenAddingHardDrive_thenEqualAssertion() {
|
||||
standardComputer.addHardDrive();
|
||||
Assert.assertEquals("1TB Hard Drive", standardComputer
|
||||
.getComputerParts().get("Hard Drive"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardGraphicaCard_whenAddingGraphicCard_thenEqualAssertion() {
|
||||
standardComputer.addGraphicCard();
|
||||
Assert.assertEquals("Standard Graphic Card", standardComputer
|
||||
.getComputerParts().get("Graphic Card"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardSoundCard_whenAddingSoundCard_thenEqualAssertion() {
|
||||
standardComputer.addSoundCard();
|
||||
Assert.assertEquals("Standard Sound Card", standardComputer
|
||||
.getComputerParts().get("Sound Card"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllStandardParts_whenBuildingComputer_thenSixParts() {
|
||||
standardComputer.buildComputer();
|
||||
Assert.assertEquals(6, standardComputer
|
||||
.getComputerParts().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
|
||||
highEndComputer.addProcessor();
|
||||
Assert.assertEquals("High End Processor", highEndComputer
|
||||
.getComputerParts().get("Processor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
||||
highEndComputer.addMotherboard();
|
||||
Assert.assertEquals("High End Motherboard", highEndComputer
|
||||
.getComputerParts().get("Motherboard"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHighEndMemory_whenAddingMemory_thenEqualAssertion() {
|
||||
highEndComputer.addMemory();
|
||||
Assert.assertEquals("16GB", highEndComputer
|
||||
.getComputerParts().get("Memory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHighEndHardDrive_whenAddingHardDrive_thenEqualAssertion() {
|
||||
highEndComputer.addHardDrive();
|
||||
Assert.assertEquals("2TB Hard Drive", highEndComputer
|
||||
.getComputerParts().get("Hard Drive"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHighEndGraphicCard_whenAddingGraphicCard_thenEqualAssertion() {
|
||||
highEndComputer.addGraphicCard();
|
||||
Assert.assertEquals("High End Graphic Card", highEndComputer
|
||||
.getComputerParts().get("Graphic Card"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHighEndSoundCard_whenAddingSoundCard_thenEqualAssertion() {
|
||||
highEndComputer.addSoundCard();
|
||||
Assert.assertEquals("High End Sound Card", highEndComputer
|
||||
.getComputerParts().get("Sound Card"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllHighEndParts_whenBuildingComputer_thenSixParts() {
|
||||
highEndComputer.buildComputer();
|
||||
Assert.assertEquals(6, highEndComputer.getComputerParts().size());
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,8 @@ public class WebClientController {
|
||||
WebClient.RequestHeadersSpec<?> requestSpec2 = uri2.body(BodyInserters.fromObject("data"));
|
||||
|
||||
// inserters
|
||||
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserter1 = BodyInserters.fromPublisher(Subscriber::onComplete, String.class);
|
||||
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserter1 = BodyInserters
|
||||
.fromPublisher(Subscriber::onComplete, String.class);
|
||||
|
||||
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
||||
map.add("key1", "value1");
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Spring Boot Admin
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user