Skip to content

Commit

Permalink
Replaced TestValue in AbstractLazyValueTest with the generic TestObj.
Browse files Browse the repository at this point in the history
  • Loading branch information
kelemen committed Jul 15, 2017
1 parent 189b50b commit 0ad4abf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jtrim2.concurrent.Tasks;
import org.jtrim2.testutils.JTrimTests;
import org.jtrim2.utils.AbstractLazyValueTest.TestValue;
import org.jtrim2.testutils.TestObj;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public abstract class AbstractLazyValueTest extends JTrimTests<Function<Supplier<TestValue>, Supplier<TestValue>>> {
public AbstractLazyValueTest(Collection<? extends Function<Supplier<TestValue>, Supplier<TestValue>>> factories) {
public abstract class AbstractLazyValueTest extends JTrimTests<Function<Supplier<TestObj>, Supplier<TestObj>>> {
public AbstractLazyValueTest(Collection<? extends Function<Supplier<TestObj>, Supplier<TestObj>>> factories) {
super(factories);
}

Expand All @@ -27,33 +26,33 @@ protected static <T> Supplier<T> mockSupplier() {
return (Supplier<T>) mock(Supplier.class);
}

protected static Supplier<TestValue> mockFactory(String str) {
protected static Supplier<TestObj> mockFactory(String str) {
@SuppressWarnings("unchecked")
Supplier<TestValue> result = mockSupplier();
Supplier<TestObj> result = mockSupplier();
doAnswer((InvocationOnMock invocation) -> {
return new TestValue(str);
return new TestObj(str);
}).when(result).get();
return result;
}

protected static TestValue verifyResult(String expected, Supplier<TestValue> factory) {
TestValue result = factory.get();
assertEquals(new TestValue(expected), result);
protected static TestObj verifyResult(String expected, Supplier<TestObj> factory) {
TestObj result = factory.get();
assertEquals(new TestObj(expected), result);
return result;
}

@Test
public void testLazyValueSerializedCall() throws Exception {
testAll(lazyFactory -> {
Supplier<TestValue> src = mockFactory("Test-Value1");
Supplier<TestObj> src = mockFactory("Test-Value1");

Supplier<TestValue> lazy = lazyFactory.apply(src);
Supplier<TestObj> lazy = lazyFactory.apply(src);

verifyZeroInteractions(src);
TestValue value1 = verifyResult("Test-Value1", lazy);
TestObj value1 = verifyResult("Test-Value1", lazy);
verify(src).get();

TestValue value2 = verifyResult("Test-Value1", lazy);
TestObj value2 = verifyResult("Test-Value1", lazy);
verifyNoMoreInteractions(src);

assertSame(value1, value2);
Expand All @@ -65,15 +64,15 @@ public void testLazyValueConcurrent() throws Exception {
int threadCount = Math.min(Runtime.getRuntime().availableProcessors() * 2, 4);
testAll(lazyFactory -> {
for (int i = 0; i < 100; i++) {
Supplier<TestValue> src = mockFactory("Test-Value1");
Supplier<TestValue> lazy = lazyFactory.apply(src);
Supplier<TestObj> src = mockFactory("Test-Value1");
Supplier<TestObj> lazy = lazyFactory.apply(src);

Set<TestValue> results = Collections.synchronizedSet(
Set<TestObj> results = Collections.synchronizedSet(
Collections.newSetFromMap(new IdentityHashMap<>()));

Runnable[] testTasks = new Runnable[threadCount];
Arrays.fill(testTasks, (Runnable) () -> {
TestValue value = verifyResult("Test-Value1", lazy);
TestObj value = verifyResult("Test-Value1", lazy);
results.add(value);
});
Tasks.runConcurrently(testTasks);
Expand All @@ -92,9 +91,9 @@ public void testLazyValueConcurrent() throws Exception {
@Test
public void testLazyValueToStringNotInitialized() throws Exception {
testAll(lazyFactory -> {
TestValue expected = new TestValue("Test-Value35");
TestObj expected = new TestObj("Test-Value35");
String expectedStr = expected.toString();
Supplier<TestValue> lazy = lazyFactory.apply(() -> expected);
Supplier<TestObj> lazy = lazyFactory.apply(() -> expected);

String strValue = lazy.toString();
assertFalse(strValue, strValue.contains(expectedStr));
Expand All @@ -105,43 +104,13 @@ public void testLazyValueToStringNotInitialized() throws Exception {
@Test
public void testLazyValueToStringInitialized() throws Exception {
testAll(lazyFactory -> {
TestValue expected = new TestValue("Test-Value35");
TestObj expected = new TestObj("Test-Value35");
String expectedStr = expected.toString();
Supplier<TestValue> lazy = lazyFactory.apply(() -> expected);
Supplier<TestObj> lazy = lazyFactory.apply(() -> expected);
lazy.get();

String strValue = lazy.toString();
assertTrue(strValue, strValue.contains(expectedStr));
});
}

public static final class TestValue {
private final String str;

public TestValue(String str) {
this.str = str;
}

@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + Objects.hashCode(this.str);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;

final TestValue other = (TestValue) obj;
return Objects.equals(this.str, other.str);
}

@Override
public String toString() {
return "TestValue{" + str + '}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ public void testLazyValueConcurrentLocked() throws Exception {
int threadCount = Math.min(Runtime.getRuntime().availableProcessors() * 2, 4);
testAll(lazyFactory -> {
for (int i = 0; i < 100; i++) {
Supplier<TestValue> src = mockFactory("Test-Value1");
Supplier<TestValue> lazy = lazyFactory.apply(src);
Supplier<TestObj> src = mockFactory("Test-Value1");
Supplier<TestObj> lazy = lazyFactory.apply(src);

Set<TestValue> results = Collections.synchronizedSet(
Set<TestObj> results = Collections.synchronizedSet(
Collections.newSetFromMap(new IdentityHashMap<>()));

Runnable[] testTasks = new Runnable[threadCount];
Arrays.fill(testTasks, (Runnable) () -> {
TestValue value = verifyResult("Test-Value1", lazy);
TestObj value = verifyResult("Test-Value1", lazy);
results.add(value);
});
Tasks.runConcurrently(testTasks);
Expand Down Expand Up @@ -150,16 +150,16 @@ public SafeLazyNonNullValueTest() {

public abstract static class AbstractLazyNullableValueTest extends AbstractLazyValueTest {
public AbstractLazyNullableValueTest(
Collection<? extends Function<Supplier<TestValue>, Supplier<TestValue>>> factories) {
Collection<? extends Function<Supplier<TestObj>, Supplier<TestObj>>> factories) {
super(factories);
}

@Test
public void testLazyValueWithNullFactory() throws Exception {
testAll(lazyFactory -> {
Supplier<TestValue> src = mockSupplier();
Supplier<TestObj> src = mockSupplier();

Supplier<TestValue> lazy = lazyFactory.apply(src);
Supplier<TestObj> lazy = lazyFactory.apply(src);

verifyZeroInteractions(src);
assertNull("Call1", lazy.get());
Expand All @@ -173,7 +173,7 @@ public void testLazyValueWithNullFactory() throws Exception {
@Test
public void testLazyValueToStringInitializedToNull() throws Exception {
testAll(lazyFactory -> {
Supplier<TestValue> lazy = lazyFactory.apply(() -> null);
Supplier<TestObj> lazy = lazyFactory.apply(() -> null);
lazy.get();

String strValue = lazy.toString();
Expand All @@ -184,21 +184,21 @@ public void testLazyValueToStringInitializedToNull() throws Exception {

public abstract static class AbstractLazyNonNullValueTest extends AbstractLazyValueTest {
public AbstractLazyNonNullValueTest(
Collection<? extends Function<Supplier<TestValue>, Supplier<TestValue>>> factories) {
Collection<? extends Function<Supplier<TestObj>, Supplier<TestObj>>> factories) {
super(factories);
}

@Test
public void testLazyValueWithNullFactory() throws Exception {
testAll(lazyFactory -> {
Object secondResult = new TestValue("OBJ-2");
Supplier<TestValue> src = mockSupplier();
Object secondResult = new TestObj("OBJ-2");
Supplier<TestObj> src = mockSupplier();
doReturn(null)
.doReturn(secondResult)
.when(src)
.get();

Supplier<TestValue> lazy = lazyFactory.apply(src);
Supplier<TestObj> lazy = lazyFactory.apply(src);

verifyZeroInteractions(src);
assertNull("Call1", lazy.get());
Expand All @@ -215,7 +215,7 @@ public void testLazyValueWithNullFactory() throws Exception {
@Test
public void testLazyValueToStringInitializedToNull() throws Exception {
testAll(lazyFactory -> {
Supplier<TestValue> lazy = lazyFactory.apply(() -> null);
Supplier<TestObj> lazy = lazyFactory.apply(() -> null);
lazy.get();

String strValue = lazy.toString();
Expand Down

0 comments on commit 0ad4abf

Please sign in to comment.