Skip to content

Commit

Permalink
Java 11 migration: patterns (t-v) (#1085)
Browse files Browse the repository at this point in the history
* Moves visitor pattern to java 11

* Moves value-object pattern to java 11

* Moves unit-of-work pattern to java 11

* Moves typeobjectpattern pattern to java 11

* Moves twin pattern to java 11

* Moves trampoline pattern to java 11

* Moves tolerant-reader pattern to java 11

* Moves tls pattern to java 11

* Moves throttling pattern to java 11

* Moves thread-pool pattern to java 11

* Moves template-method pattern to java 11
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 14, 2019
1 parent 160b737 commit 50467c9
Show file tree
Hide file tree
Showing 45 changed files with 366 additions and 409 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@
import org.junit.jupiter.api.Test;

/**
*
* Application test
*
*/
public class AppTest {

@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

package com.iluwatar.templatemethod;

import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.junit.jupiter.api.Test;

/**
* Date: 12/29/15 - 18:15 PM
*
Expand All @@ -41,8 +41,8 @@ public class HalflingThiefTest {
*/
@Test
public void testSteal() {
final StealingMethod method = mock(StealingMethod.class);
final HalflingThief thief = new HalflingThief(method);
final var method = mock(StealingMethod.class);
final var thief = new HalflingThief(method);

thief.steal();
verify(method).steal();
Expand All @@ -55,13 +55,13 @@ public void testSteal() {
*/
@Test
public void testChangeMethod() {
final StealingMethod initialMethod = mock(StealingMethod.class);
final HalflingThief thief = new HalflingThief(initialMethod);
final var initialMethod = mock(StealingMethod.class);
final var thief = new HalflingThief(initialMethod);

thief.steal();
verify(initialMethod).steal();

final StealingMethod newMethod = mock(StealingMethod.class);
final var newMethod = mock(StealingMethod.class);
thief.changeMethod(newMethod);

thief.steal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

package com.iluwatar.templatemethod;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
Expand All @@ -33,11 +36,9 @@
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Date: 12/30/15 - 18:12 PM
*
* @param <M> Type of StealingMethod
* @author Jeroen Meulemeester
*/
Expand Down
7 changes: 2 additions & 5 deletions thread-pool/src/main/java/com/iluwatar/threadpool/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void main(String[] args) {
LOGGER.info("Program started");

// Create a list of tasks to be executed
List<Task> tasks = List.of(
var tasks = List.of(
new PotatoPeelingTask(3),
new PotatoPeelingTask(6),
new CoffeeMakingTask(2),
Expand All @@ -82,10 +82,7 @@ public static void main(String[] args) {
// Allocate new worker for each task
// The worker is executed when a thread becomes
// available in the thread pool
for (int i = 0; i < tasks.size(); i++) {
var worker = new Worker(tasks.get(i));
executor.execute(worker);
}
tasks.stream().map(Worker::new).forEach(executor::execute);
// All tasks were executed, now shutdown
executor.shutdown();
while (!executor.isTerminated()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@

/**
* Application test
*
* @author ilkka
*
* @author ilkka
*/
public class AppTest {

@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}
33 changes: 15 additions & 18 deletions thread-pool/src/test/java/com/iluwatar/threadpool/TaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@

package com.iluwatar.threadpool;

import org.junit.jupiter.api.Test;
import static java.time.Duration.ofMillis;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTimeout;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

import static java.time.Duration.ofMillis;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;

/**
* Date: 12/30/15 - 18:22 PM
* Test for Tasks using a Thread Pool
* Date: 12/30/15 - 18:22 PM Test for Tasks using a Thread Pool
*
* @param <T> Type of Task
* @author Jeroen Meulemeester
*/
Expand Down Expand Up @@ -87,22 +85,21 @@ public TaskTest(final IntFunction<T> factory, final int expectedExecutionTime) {
@Test
public void testIdGeneration() throws Exception {
assertTimeout(ofMillis(10000), () -> {
final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
final var service = Executors.newFixedThreadPool(THREAD_COUNT);

final List<Callable<Integer>> tasks = new ArrayList<>();
for (int i = 0; i < TASK_COUNT; i++) {
tasks.add(() -> factory.apply(1).getId());
}
final var tasks = IntStream.range(0, TASK_COUNT)
.<Callable<Integer>>mapToObj(i -> () -> factory.apply(1).getId())
.collect(Collectors.toCollection(ArrayList::new));

final List<Integer> ids = service.invokeAll(tasks)
final var ids = service.invokeAll(tasks)
.stream()
.map(TaskTest::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());

service.shutdownNow();

final long uniqueIdCount = ids.stream()
final var uniqueIdCount = ids.stream()
.distinct()
.count();

Expand All @@ -117,7 +114,7 @@ public void testIdGeneration() throws Exception {
*/
@Test
public void testTimeMs() {
for (int i = 0; i < 10; i++) {
for (var i = 0; i < 10; i++) {
assertEquals(this.expectedExecutionTime * i, this.factory.apply(i).getTimeMs());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

package com.iluwatar.threadpool;

import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;

import org.junit.jupiter.api.Test;

/**
* Date: 12/30/15 - 18:21 PM
*
Expand All @@ -42,8 +42,8 @@ public class WorkerTest {
*/
@Test
public void testRun() {
final Task task = mock(Task.class);
final Worker worker = new Worker(task);
final var task = mock(Task.class);
final var worker = new Worker(task);
verifyZeroInteractions(task);

worker.run();
Expand Down
7 changes: 4 additions & 3 deletions throttling/src/main/java/com/iluwatar/throttling/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -73,14 +74,14 @@ public static void main(String[] args) {
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
var timer = new ThrottleTimerImpl(10, callsCount);
var service = new B2BService(timer, callsCount);
for (int i = 0; i < 20; i++) {
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
IntStream.range(0, 20).forEach(i -> {
service.dummyCustomerApi(tenant);
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
try {
Thread.sleep(1);
} catch (InterruptedException e) {
LOGGER.error("Thread interrupted: {}", e.getMessage());
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public long getCount(String tenantName) {
*/
public void reset() {
LOGGER.debug("Resetting the map.");
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
tenantCallsCount.put(e.getKey(), new AtomicLong(0));
}
tenantCallsCount.replaceAll((k, v) -> new AtomicLong(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class AppTest {

@Test
public void test() {
final String[] args = {};
App.main(args);
App.main(new String[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

package com.iluwatar.throttling;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.iluwatar.throttling.timer.Throttler;
import org.junit.jupiter.api.Disabled;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* B2BServiceTest class to test the B2BService
*/
Expand All @@ -38,15 +38,14 @@ public class B2BServiceTest {

@Test
public void dummyCustomerApiTest() {
Tenant tenant = new Tenant("testTenant", 2, callsCount);
var tenant = new Tenant("testTenant", 2, callsCount);
// In order to assure that throttling limits will not be reset, we use an empty throttling implementation
Throttler timer = () -> { };
B2BService service = new B2BService(timer, callsCount);
var timer = (Throttler) () -> {
};
var service = new B2BService(timer, callsCount);

for (int i = 0; i < 5; i++) {
service.dummyCustomerApi(tenant);
}
long counter = callsCount.getCount(tenant.getName());
IntStream.range(0, 5).mapToObj(i -> tenant).forEach(service::dummyCustomerApi);
var counter = callsCount.getCount(tenant.getName());
assertEquals(2, counter, "Counter limit must be reached");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

package com.iluwatar.throttling;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.security.InvalidParameterException;

import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;

/**
* TenantTest to test the creation of Tenant with valid parameters.
Expand All @@ -37,7 +36,7 @@ public class TenantTest {
@Test
public void constructorTest() {
assertThrows(InvalidParameterException.class, () -> {
Tenant tenant = new Tenant("FailTenant", -1, new CallsCount());
new Tenant("FailTenant", -1, new CallsCount());
});
}
}

0 comments on commit 50467c9

Please sign in to comment.