@Test public void testExplicitThreadCanCreateAndUseSubject() throws InterruptedException { final AtomicReference<List<String>> inputReference = new AtomicReference<List<String>>(null); final AtomicBoolean outputReference = new AtomicBoolean(false); Thread childThread = new Thread(() -> { List<String> subjectInChildThread = inputReference.get(); subjectInChildThread.add(testString); if (Iterables.getOnlyElement(subjectInChildThread).equals(testString)) { outputReference.set(Boolean.TRUE); } }); @SuppressWarnings("unchecked") List<String> subject = ThreadConfinedProxy.newProxyInstance(List.class, new ArrayList<String>(), ThreadConfinedProxy.Strictness.VALIDATE, childThread); inputReference.set(subject); childThread.start(); childThread.join(10000); assertTrue(outputReference.get()); }
@Test public void testExplicitThreadCannotAndUseSubjectFromMainThread() throws InterruptedException { final AtomicReference<List<String>> inputReference = new AtomicReference<List<String>>(null); final AtomicBoolean outputReference = new AtomicBoolean(false); Thread childThread = new Thread(() -> { outputReference.set(true); List<String> subjectInChildThread = inputReference.get(); subjectInChildThread.add(testString); // Should fail outputReference.set(false); }); @SuppressWarnings("unchecked") List<String> subject = ThreadConfinedProxy.newProxyInstance(List.class, new ArrayList<String>(), ThreadConfinedProxy.Strictness.VALIDATE); inputReference.set(subject); childThread.start(); childThread.join(10000); assertTrue(outputReference.get()); }