Created
February 17, 2014 15:26
-
-
Save Ramasubramanian/9052610 to your computer and use it in GitHub Desktop.
unit test case for Lazy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.raam.lazy; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertNotEquals; | |
import static org.junit.Assert.assertTrue; | |
import java.util.Random; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.ScheduledThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.Test; | |
public class LazyTest { | |
@Test(expected = NullPointerException.class) | |
public void testNullReturningFactory() { | |
Lazy<String> nullLazy = Lazy.create(() -> null); | |
nullLazy.value(); | |
} | |
@Test(expected = NullPointerException.class) | |
public void testNullReturningFactoryToString() { | |
Lazy<String> nullLazy = Lazy.create(() -> null); | |
nullLazy.toString(); | |
} | |
@Test | |
public void testOptional() { | |
Lazy<String> nullLazy = Lazy.create(() -> null); | |
assertFalse(nullLazy.optional().isPresent()); | |
nullLazy = Lazy.create(() -> null, false); | |
assertFalse(nullLazy.optional().isPresent()); | |
Lazy<String> lazy = Lazy.create(() -> "Test"); | |
assertTrue(lazy.optional().isPresent()); | |
assertEquals(lazy.optional().get(), "Test"); | |
lazy = Lazy.create(() -> "Test", false); | |
assertTrue(lazy.optional().isPresent()); | |
assertEquals(lazy.optional().get(), "Test"); | |
} | |
@Test | |
public void testCreated() { | |
Lazy<String> lazy = Lazy.create(() -> "Test"); | |
assertFalse(lazy.created()); | |
assertEquals(lazy.value(), "Test"); | |
assertTrue(lazy.created()); | |
lazy = Lazy.create(this::testMethod); | |
assertFalse(lazy.created()); | |
assertEquals(lazy.toString(), "Test"); | |
assertTrue(lazy.created()); | |
} | |
@Test | |
public void testConstructors() { | |
Lazy<String> lazy = Lazy.create(() -> "Test"); | |
assertNotEquals(lazy.getClass(), Lazy.class); | |
lazy = Lazy.create(() -> "Test", false); | |
assertEquals(lazy.getClass(), Lazy.class); | |
lazy = Lazy.create(String.class); | |
assertEquals("", lazy.value()); | |
assertNotEquals(lazy.getClass(), Lazy.class); | |
lazy = Lazy.create(String.class, false); | |
assertEquals("", lazy.value()); | |
assertEquals(lazy.getClass(), Lazy.class); | |
} | |
private String testMethod() { | |
return "Test"; | |
} | |
@Test | |
public void testToString() { | |
Lazy<String> lazy = Lazy.create(() -> "Test"); | |
assertFalse(lazy.created()); | |
assertEquals(lazy.toString(), "Test"); | |
assertTrue(lazy.created()); | |
} | |
static class ThreadDetails { | |
public final String id; | |
public final Long timestamp; | |
public ThreadDetails(String id, Long timestamp) { | |
this.id = id; | |
this.timestamp = timestamp; | |
} | |
@Override | |
public String toString() { | |
return "Created By::" + id + " at " + timestamp; | |
} | |
} | |
@Test | |
public void testMultiThread() throws InterruptedException, ExecutionException { | |
Lazy<ThreadDetails> lazy = Lazy.create(() -> new ThreadDetails(Thread.currentThread().toString(), System.currentTimeMillis())); | |
Runnable r = () -> System.out.println(Thread.currentThread().toString() + " -> " + lazy.value()); | |
Random gen = new Random(); | |
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(50); | |
Thread.sleep(1000); | |
for (int i = 0; i < 50; i++) { | |
executor.schedule(r, gen.nextInt(10), TimeUnit.MILLISECONDS); | |
} | |
Thread.sleep(4000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment