### Question:
SynchronizedPrintStream { public static PrintStream create(OutputStream out, Charset charset, Object sharedLock) { Enhancer e = new Enhancer(); e.setSuperclass(PrintStream.class); e.setCallback(new SynchronizedMethodInterceptor(sharedLock)); e.setNamingPolicy(new PrefixOverridingNamingPolicy(SynchronizedPrintStream.class.getName())); e.setUseFactory(false); return (PrintStream) e.create( new Class[]{OutputStream.class, boolean.class, String.class}, new Object[]{out, false, charset.name()} ); }  static PrintStream create(OutputStream out, Charset charset, Object sharedLock);  }### Answer:
@Test public void synchronizes_all_methods_on_the_lock_given_as_parameter() { SpyOutputStream spy = new SpyOutputStream(); PrintStream printStream = SynchronizedPrintStream.create(spy, Charset.defaultCharset(), lock); printStream.println("foo"); assertThat("was called", spy.wasCalled, is(true)); assertThat("used the lock", spy.lockWasHeldByCurrentThread, is(true)); }
@Test public void does_not_deadlock_if_somebody_locks_in_the_PrintStream_externally() throws Exception { final int ITERATIONS = 10; PrintStream printStream = SynchronizedPrintStream.create(new NullOutputStream(), Charset.defaultCharset(), lock); runConcurrently(() -> { synchronized (printStream) { for (int i = 0; i < ITERATIONS; i++) { Thread.yield(); printStream.print("X"); } } }, () -> { for (int i = 0; i < ITERATIONS; i++) { Thread.yield(); printStream.print("X"); } }); }
@Test public void the_class_name_in_stack_traces_gives_a_hint_of_who_generated_the_proxy_class() { PrintStream printStream = SynchronizedPrintStream.create(new NullOutputStream(), Charset.defaultCharset(), lock); assertThat(printStream.getClass().getName(), startsWith(SynchronizedPrintStream.class.getName())); }
@Test public void does_not_expose_the_CGLIB_Factory_interface() { PrintStream printStream = SynchronizedPrintStream.create(new NullOutputStream(), Charset.defaultCharset(), lock); assertThat(printStream, not(instanceOf(Factory.class))); }