Build optimization 6.07 (#2219)

* refactor testng

* refactor testng

* Remove test suites from surefire

* Refactor

* Refactor
This commit is contained in:
Grzegorz Piwowarek
2017-07-06 23:22:41 +02:00
committed by GitHub
parent c99bb7fced
commit b9b230f83e
66 changed files with 524 additions and 1311 deletions
-24
View File
@@ -1,24 +0,0 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
-49
View File
@@ -1,49 +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>baeldung</groupId>
<artifactId>springdatainjectiondemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringDataInjectionDemo</name>
<description>Spring Data Injection Demp</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -1,12 +0,0 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDataInjectionDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataInjectionDemoApplication.class, args);
}
}
@@ -1,46 +0,0 @@
package com.baeldung.didemo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.didemo.model.Order;
import com.baeldung.didemo.service.CustomerServiceConstructorDI;
import com.baeldung.didemo.service.CustomerServiceConstructorWithoutSpringDI;
import com.baeldung.didemo.service.CustomerServiceFieldDI;
import com.baeldung.didemo.service.CustomerServiceSetterDI;
import com.baeldung.didemo.service.OrderService;
@RestController
@RequestMapping(value = "/orders")
public class OrderController {
@Autowired
CustomerServiceConstructorDI constructorDI;
@Autowired
CustomerServiceFieldDI fieldDI;
@Autowired
CustomerServiceSetterDI setterDI;
@RequestMapping(method = RequestMethod.GET)
public List<Order> getOrdersFieldDI(@RequestParam(required = false) String dIMethod) {
if ("setter".equals(dIMethod)) {
return setterDI.getCustomerOrders(1l);
} else if ("constructor".equals(dIMethod)) {
return constructorDI.getCustomerOrders(1l);
} else if ("field".equals(dIMethod)) {
return fieldDI.getCustomerOrders(1l);
} else {
OrderService orderSvc = new OrderService();
CustomerServiceConstructorWithoutSpringDI customerSvc = new CustomerServiceConstructorWithoutSpringDI(orderSvc);
return customerSvc.getCustomerOrders(1l);
}
}
}
@@ -1,26 +0,0 @@
package com.baeldung.didemo.model;
public class Order {
private Integer id;
private String item;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
// other order properties..
}
@@ -1,25 +0,0 @@
package com.baeldung.didemo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.didemo.model.Order;
@Service
public class CustomerServiceConstructorDI {
OrderService orderService;
@Autowired
public CustomerServiceConstructorDI(OrderService orderService) {
super();
this.orderService = orderService;
}
public List<Order> getCustomerOrders(Long customerId) {
return orderService.getOrdersForCustomer(customerId);
}
}
@@ -1,20 +0,0 @@
package com.baeldung.didemo.service;
import java.util.List;
import com.baeldung.didemo.model.Order;
public class CustomerServiceConstructorWithoutSpringDI {
OrderService orderService;
public CustomerServiceConstructorWithoutSpringDI(OrderService orderService) {
super();
this.orderService = orderService;
}
public List<Order> getCustomerOrders(Long customerId) {
return orderService.getOrdersForCustomer(customerId);
}
}
@@ -1,19 +0,0 @@
package com.baeldung.didemo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.didemo.model.Order;
@Service
public class CustomerServiceFieldDI {
@Autowired
OrderService orderService;
public List<Order> getCustomerOrders(Long customerId) {
return orderService.getOrdersForCustomer(customerId);
}
}
@@ -1,24 +0,0 @@
package com.baeldung.didemo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.didemo.model.Order;
@Service
public class CustomerServiceSetterDI {
OrderService orderService;
public List<Order> getCustomerOrders(Long customerId) {
return orderService.getOrdersForCustomer(customerId);
}
@Autowired
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
}
@@ -1,30 +0,0 @@
package com.baeldung.didemo.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.baeldung.didemo.model.Order;
@Service
public class OrderService {
public List<Order> getOrdersForCustomer(Long id) {
List<Order> orders = new ArrayList<Order>();
Order order1 = new Order();
order1.setId(1);
order1.setItem("Pizza");
Order order2 = new Order();
order2.setId(1);
order2.setItem("Garlic Bread");
orders.add(order1);
orders.add(order2);
return orders;
}
}
@@ -1,65 +0,0 @@
package com.baeldung.didemo;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.didemo.model.Order;
import com.baeldung.didemo.service.CustomerServiceConstructorDI;
import com.baeldung.didemo.service.CustomerServiceFieldDI;
import com.baeldung.didemo.service.CustomerServiceSetterDI;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataInjectionDemoApplicationTests {
@Autowired
CustomerServiceConstructorDI constructorDI;
@Autowired
CustomerServiceFieldDI fieldDI;
@Autowired
CustomerServiceSetterDI setterDI;
@Test
public void givenConstructorDI_whenNumberOfOrdersIsTwo_thenCorrect() {
List<Order> orders = constructorDI.getCustomerOrders(1l);
Assert.assertNotNull(orders);
Assert.assertTrue(orders.size() == 2);
}
@Test
public void givenFieldDI_whenNumberOfOrdersIsTwo_thenCorrect() {
List<Order> orders = fieldDI.getCustomerOrders(1l);
Assert.assertNotNull(orders);
Assert.assertTrue(orders.size() == 2);
}
@Test
public void givenSetterDI_whenNumberOfOrdersIsTwo_thenCorrect() {
List<Order> orders = setterDI.getCustomerOrders(1l);
Assert.assertNotNull(orders);
Assert.assertTrue(orders.size() == 2);
}
@Test
public void givenAllThreeTypesOfDI_whenNumberOfOrdersIsEqualInAll_thenCorrect() {
List<Order> ordersSetter = setterDI.getCustomerOrders(1l);
List<Order> ordersConstructor = constructorDI.getCustomerOrders(1l);
List<Order> ordersField = fieldDI.getCustomerOrders(1l);
Assert.assertNotNull(ordersSetter);
Assert.assertNotNull(ordersConstructor);
Assert.assertNotNull(ordersField);
Assert.assertTrue(ordersSetter.size() == 2 && ordersConstructor.size() == ordersSetter.size() && ordersField.size() == ordersSetter.size());
}
}
-24
View File
@@ -1,24 +0,0 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
-50
View File
@@ -1,50 +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>baeldung</groupId>
<artifactId>call-all-getters</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>call-all-getters</name>
<description>Calling all getters using Introspector</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -1,18 +0,0 @@
package com.baeldung;
import java.util.List;
import com.baeldung.reflection.model.Customer;
import com.baeldung.reflection.util.Utils;
public class CallAllGettersApplication {
public static void main(String[] args) throws Exception {
Customer customer = new Customer(1, "Himanshu", null, null);
List<String> nullProps = Utils.getNullPropertiesList(customer);
System.out.println(nullProps);
}
}
@@ -1,63 +0,0 @@
package com.baeldung.reflection.model;
/**
*
* @author himanshumantri
*
*/
public class Customer {
private Integer id;
private String name;
private String emailId;
private Long phoneNumber;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Customer [id=").append(id).append(", name=").append(name).append(", emailId=").append(emailId).append(", phoneNumber=")
.append(phoneNumber).append("]");
return builder.toString();
}
public Customer(Integer id, String name, String emailId, Long phoneNumber) {
super();
this.id = id;
this.name = name;
this.emailId = emailId;
this.phoneNumber = phoneNumber;
}
public Long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(Long phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
@@ -1,43 +0,0 @@
package com.baeldung.reflection.util;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.baeldung.reflection.model.Customer;
public class Utils {
public static List<String> getNullPropertiesList(Customer customer) throws Exception {
PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors();
List<PropertyDescriptor> propDescList = Arrays.asList(propDescArr);
List<String> nullProps = propDescList.stream()
.filter(nulls(customer))
.map(PropertyDescriptor::getName)
.collect(Collectors.toList());
return nullProps;
}
private static Predicate<PropertyDescriptor> nulls(Customer customer) {
Predicate<PropertyDescriptor> isNull = new Predicate<PropertyDescriptor>() {
@Override
public boolean test(PropertyDescriptor pd) {
Method getterMethod = pd.getReadMethod();
boolean result = false;
try {
result = (getterMethod != null && getterMethod.invoke(customer) == null);
} catch (Exception e) {
// Handle the exception
e.printStackTrace();
}
return result;
}
};
return isNull;
}
}
@@ -1,26 +0,0 @@
package com.baeldung;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.reflection.model.Customer;
import com.baeldung.reflection.util.Utils;
public class CallAllGettersApplicationTests {
@Test
public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception {
Customer customer = new Customer(1, "Himanshu", null, null);
List<String> result = Utils.getNullPropertiesList(customer);
List<String> expectedFieldNames = Arrays.asList("emailId","phoneNumber");
Assert.assertTrue(result.size() == expectedFieldNames.size());
Assert.assertTrue(result.containsAll(expectedFieldNames));
}
}
@@ -9,6 +9,8 @@ import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertTrue;
public class ArrayCopyUtilUnitTest {
private static Employee[] employees;
private static final int MAX = 2;
@@ -46,10 +48,10 @@ public class ArrayCopyUtilUnitTest {
System.arraycopy(array, 2, copiedArray, 0, 3);
Assert.assertTrue(3 == copiedArray.length);
Assert.assertTrue(copiedArray[0] == array[2]);
Assert.assertTrue(copiedArray[1] == array[3]);
Assert.assertTrue(copiedArray[2] == array[4]);
assertTrue(3 == copiedArray.length);
assertTrue(copiedArray[0] == array[2]);
assertTrue(copiedArray[1] == array[3]);
assertTrue(copiedArray[2] == array[4]);
}
@Test
@@ -58,10 +60,10 @@ public class ArrayCopyUtilUnitTest {
int[] copiedArray = Arrays.copyOfRange(array, 1, 4);
Assert.assertTrue(3 == copiedArray.length);
Assert.assertTrue(copiedArray[0] == array[1]);
Assert.assertTrue(copiedArray[1] == array[2]);
Assert.assertTrue(copiedArray[2] == array[3]);
assertTrue(3 == copiedArray.length);
assertTrue(copiedArray[0] == array[1]);
assertTrue(copiedArray[1] == array[2]);
assertTrue(copiedArray[2] == array[3]);
}
@Test
@@ -73,9 +75,9 @@ public class ArrayCopyUtilUnitTest {
Assert.assertArrayEquals(copiedArray, array);
array[0] = 9;
Assert.assertTrue(copiedArray[0] != array[0]);
assertTrue(copiedArray[0] != array[0]);
copiedArray[1] = 12;
Assert.assertTrue(copiedArray[1] != array[1]);
assertTrue(copiedArray[1] != array[1]);
}
@Test
@@ -85,7 +87,7 @@ public class ArrayCopyUtilUnitTest {
Assert.assertArrayEquals(copiedArray, employees);
employees[0].setName(employees[0].getName()+"_Changed");
//change in employees' element caused change in the copied array
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
}
@Test
@@ -96,9 +98,9 @@ public class ArrayCopyUtilUnitTest {
Assert.assertArrayEquals(copiedArray, array);
array[0] = 9;
Assert.assertTrue(copiedArray[0] != array[0]);
assertTrue(copiedArray[0] != array[0]);
copiedArray[1] = 12;
Assert.assertTrue(copiedArray[1] != array[1]);
assertTrue(copiedArray[1] != array[1]);
}
@Test
@@ -108,7 +110,7 @@ public class ArrayCopyUtilUnitTest {
Assert.assertArrayEquals(copiedArray, employees);;
employees[0].setName(employees[0].getName()+"_Changed");
//change in employees' element changed the copied array
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
}
@Test
@@ -138,7 +140,7 @@ public class ArrayCopyUtilUnitTest {
Assert.assertArrayEquals(copiedArray, employees);
employees[0].setName(employees[0].getName()+"_Changed");
//change in employees' element didn't change in the copied array
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
}
@Test
@@ -24,7 +24,7 @@ public class CompletableFutureLongRunningUnitTest {
assertEquals("Hello", result);
}
public Future<String> calculateAsync() throws InterruptedException {
private Future<String> calculateAsync() throws InterruptedException {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
@@ -44,7 +44,7 @@ public class CompletableFutureLongRunningUnitTest {
assertEquals("Hello", result);
}
public Future<String> calculateAsyncWithCancellation() throws InterruptedException {
private Future<String> calculateAsyncWithCancellation() throws InterruptedException {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
@@ -24,8 +24,8 @@ public class LongAccumulatorUnitTest {
//when
Runnable accumulateAction = () -> IntStream
.rangeClosed(0, numberOfIncrements)
.forEach(accumulator::accumulate);
.rangeClosed(0, numberOfIncrements)
.forEach(accumulator::accumulate);
for (int i = 0; i < numberOfThreads; i++) {
executorService.execute(accumulateAction);
@@ -17,7 +17,7 @@ public class CopyOnWriteArrayListUnitTest {
public void givenCopyOnWriteList_whenIterateAndAddElementToUnderneathList_thenShouldNotChangeIterator() {
//given
final CopyOnWriteArrayList<Integer> numbers =
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
//when
Iterator<Integer> iterator = numbers.iterator();
@@ -42,7 +42,7 @@ public class CopyOnWriteArrayListUnitTest {
public void givenCopyOnWriteList_whenIterateOverItAndTryToRemoveElement_thenShouldThrowException() {
//given
final CopyOnWriteArrayList<Integer> numbers =
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
//when
Iterator<Integer> iterator = numbers.iterator();
@@ -4,7 +4,11 @@ import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.concurrent.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static junit.framework.TestCase.assertEquals;
@@ -19,7 +23,7 @@ public class DelayQueueIntegrationTest {
int delayOfEachProducedMessageMilliseconds = 500;
DelayQueueConsumer consumer = new DelayQueueConsumer(queue, numberOfElementsToProduce);
DelayQueueProducer producer
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
//when
executor.submit(producer);
@@ -41,7 +45,7 @@ public class DelayQueueIntegrationTest {
int delayOfEachProducedMessageMilliseconds = 10_000;
DelayQueueConsumer consumer = new DelayQueueConsumer(queue, numberOfElementsToProduce);
DelayQueueProducer producer
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
//when
executor.submit(producer);
@@ -63,7 +67,7 @@ public class DelayQueueIntegrationTest {
int delayOfEachProducedMessageMilliseconds = -10_000;
DelayQueueConsumer consumer = new DelayQueueConsumer(queue, numberOfElementsToProduce);
DelayQueueProducer producer
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
//when
executor.submit(producer);
@@ -1,10 +1,10 @@
package com.baeldung.concurrent.future;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.concurrent.ForkJoinPool;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FactorialSquareCalculatorUnitTest {
@@ -8,7 +8,12 @@ import org.junit.rules.TestName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -9,65 +9,65 @@ import static junit.framework.TestCase.assertEquals;
public class SharedObjectWithLockManualTest {
@Test
public void whenLockAcquired_ThenLockedIsTrue() {
final SharedObjectWithLock object = new SharedObjectWithLock();
@Test
public void whenLockAcquired_ThenLockedIsTrue() {
final SharedObjectWithLock object = new SharedObjectWithLock();
final int threadCount = 2;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
final int threadCount = 2;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
executeThreads(object, threadCount, service);
executeThreads(object, threadCount, service);
assertEquals(true, object.isLocked());
assertEquals(true, object.isLocked());
service.shutdown();
}
service.shutdown();
}
@Test
public void whenLocked_ThenQueuedThread() {
final int threadCount = 4;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
final SharedObjectWithLock object = new SharedObjectWithLock();
@Test
public void whenLocked_ThenQueuedThread() {
final int threadCount = 4;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
final SharedObjectWithLock object = new SharedObjectWithLock();
executeThreads(object, threadCount, service);
executeThreads(object, threadCount, service);
assertEquals(object.hasQueuedThreads(), true);
assertEquals(object.hasQueuedThreads(), true);
service.shutdown();
service.shutdown();
}
}
public void whenTryLock_ThenQueuedThread() {
final SharedObjectWithLock object = new SharedObjectWithLock();
public void whenTryLock_ThenQueuedThread() {
final SharedObjectWithLock object = new SharedObjectWithLock();
final int threadCount = 2;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
final int threadCount = 2;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
executeThreads(object, threadCount, service);
executeThreads(object, threadCount, service);
assertEquals(true, object.isLocked());
assertEquals(true, object.isLocked());
service.shutdown();
}
service.shutdown();
}
@Test
public void whenGetCount_ThenCorrectCount() throws InterruptedException {
final int threadCount = 4;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
final SharedObjectWithLock object = new SharedObjectWithLock();
@Test
public void whenGetCount_ThenCorrectCount() throws InterruptedException {
final int threadCount = 4;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
final SharedObjectWithLock object = new SharedObjectWithLock();
executeThreads(object, threadCount, service);
Thread.sleep(1000);
assertEquals(object.getCounter(), 4);
executeThreads(object, threadCount, service);
Thread.sleep(1000);
assertEquals(object.getCounter(), 4);
service.shutdown();
service.shutdown();
}
}
private void executeThreads(SharedObjectWithLock object, int threadCount, ExecutorService service) {
for (int i = 0; i < threadCount; i++) {
service.execute(object::perform);
}
}
private void executeThreads(SharedObjectWithLock object, int threadCount, ExecutorService service) {
for (int i = 0; i < threadCount; i++) {
service.execute(object::perform);
}
}
}
@@ -1,6 +1,5 @@
package com.baeldung.concurrent.locks;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
@@ -10,49 +9,49 @@ import static junit.framework.TestCase.assertEquals;
public class SynchronizedHashMapWithRWLockManualTest {
@Test
public void whenWriting_ThenNoReading() {
SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock();
final int threadCount = 3;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
@Test
public void whenWriting_ThenNoReading() {
SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock();
final int threadCount = 3;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
executeWriterThreads(object, threadCount, service);
executeWriterThreads(object, threadCount, service);
assertEquals(object.isReadLockAvailable(), false);
assertEquals(object.isReadLockAvailable(), false);
service.shutdown();
}
service.shutdown();
}
@Test
public void whenReading_ThenMultipleReadingAllowed() {
SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock();
final int threadCount = 5;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
@Test
public void whenReading_ThenMultipleReadingAllowed() {
SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock();
final int threadCount = 5;
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
executeReaderThreads(object, threadCount, service);
executeReaderThreads(object, threadCount, service);
assertEquals(object.isReadLockAvailable(), true);
assertEquals(object.isReadLockAvailable(), true);
service.shutdown();
}
service.shutdown();
}
private void executeWriterThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) {
for (int i = 0; i < threadCount; i++) {
service.execute(() -> {
try {
object.put("key" + threadCount, "value" + threadCount);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
private void executeWriterThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) {
for (int i = 0; i < threadCount; i++) {
service.execute(() -> {
try {
object.put("key" + threadCount, "value" + threadCount);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
private void executeReaderThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) {
for (int i = 0; i < threadCount; i++)
service.execute(() -> {
object.get("key" + threadCount);
});
}
private void executeReaderThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) {
for (int i = 0; i < threadCount; i++)
service.execute(() -> {
object.get("key" + threadCount);
});
}
}
@@ -42,7 +42,7 @@ public class PriorityBlockingQueueIntegrationTest {
try {
Integer poll = queue.take();
LOG.debug("Polled: " + poll);
} catch (InterruptedException e) {
} catch (InterruptedException ignored) {
}
}
});
@@ -1,13 +1,13 @@
package com.baeldung.concurrent.synchronize;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class BaeldungSychronizedBlockTest {
@@ -17,7 +17,7 @@ public class BaeldungSychronizedBlockTest {
BaeldungSynchronizedBlocks synchronizedBlocks = new BaeldungSynchronizedBlocks();
IntStream.range(0, 1000)
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, synchronizedBlocks.getCount());
@@ -28,7 +28,7 @@ public class BaeldungSychronizedBlockTest {
ExecutorService service = Executors.newCachedThreadPool();
IntStream.range(0, 1000)
.forEach(count -> service.submit(BaeldungSynchronizedBlocks::performStaticSyncTask));
.forEach(count -> service.submit(BaeldungSynchronizedBlocks::performStaticSyncTask));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, BaeldungSynchronizedBlocks.getStaticCount());

Some files were not shown because too many files have changed in this diff Show More