Skip to content

Commit 793a3c8

Browse files
authored
Add proxy example with test (#34)
1 parent 5f63d1d commit 793a3c8

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

Common/src/main/java/pl/mperor/lab/common/TestUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ default E getThird() {
4747
return getNext(2);
4848
}
4949

50+
default E getForth() {
51+
return getNext(3);
52+
}
53+
5054
private E getNext(int index) {
5155
if (this.isEmpty() && index >= size()) {
5256
throw new NoSuchElementException();

Common/src/test/java/pl/mperor/lab/common/UtilsTest.java renamed to Common/src/test/java/pl/mperor/lab/common/ReadableOutputTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.NoSuchElementException;
77
import java.util.stream.Stream;
88

9-
class UtilsTest {
9+
class ReadableOutputTest {
1010

1111
@Test
1212
public void testReadableOutputReturnsReadableList() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
public interface ExecutableService {
4+
void execute();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
class ServiceImpl implements ExecutableService {
4+
5+
ServiceImpl() {
6+
System.out.println("Service has been initialized!");
7+
}
8+
9+
@Override
10+
public void execute() {
11+
System.out.println("Method has been executed!");
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
import java.util.function.Supplier;
4+
5+
public enum ServiceProvider implements Supplier<ExecutableService> {
6+
INSTANCE;
7+
8+
@Override
9+
public ExecutableService get() {
10+
return new ServiceProxy();
11+
}
12+
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
class ServiceProxy implements ExecutableService {
4+
5+
private ExecutableService service;
6+
7+
@Override
8+
public void execute() {
9+
System.out.println("Before executing service method!");
10+
lazyLoadingService();
11+
service.execute();
12+
System.out.println("After executing service method!");
13+
}
14+
15+
private void lazyLoadingService() {
16+
if (service == null) {
17+
service = new ServiceImpl();
18+
}
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.platform.commons.util.StringUtils;
6+
import pl.mperor.lab.common.TestUtils;
7+
8+
public class ServiceProviderTest {
9+
10+
@Test
11+
public void testServiceUsingProxyUnderneath() {
12+
var out = TestUtils.setTempSystemOut();
13+
var service = ServiceProvider.INSTANCE.get();
14+
Assertions.assertTrue(StringUtils.isBlank(out.all()),
15+
"Lazy loading service should not be initialized!");
16+
Assertions.assertInstanceOf(ServiceProxy.class, service);
17+
18+
service.execute();
19+
var outLines = out.lines();
20+
Assertions.assertEquals(outLines.getFirst(), "Before executing service method!");
21+
Assertions.assertEquals(outLines.getSecond(), "Service has been initialized!");
22+
Assertions.assertEquals(outLines.getThird(), "Method has been executed!");
23+
Assertions.assertEquals(outLines.getForth(), "After executing service method!");
24+
TestUtils.resetSystemOut();
25+
}
26+
27+
}

0 commit comments

Comments
 (0)