jtulach@103: package org.apidesign.deadlock; jtulach@103: jtulach@103: import org.junit.Test; jtulach@103: import static org.junit.Assert.*; jtulach@103: jtulach@103: public class SynchronizedFieldsInternallyTest { jtulach@103: public SynchronizedFieldsInternallyTest() { jtulach@103: } jtulach@103: jtulach@103: @Test jtulach@103: public void increment() { jtulach@103: SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally(); jtulach@103: instance.increment(); jtulach@103: } jtulach@103: jtulach@103: @Test jtulach@103: public void unsafeDecrement() { jtulach@103: SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally(); jtulach@103: try { jtulach@103: instance.unsafeDecrement(); jtulach@103: } catch (java.lang.AssertionError ex) { jtulach@103: // OK jtulach@103: return; jtulach@103: } jtulach@103: fail("This will fail as unsafeDecrement is not synchronized, and that" + jtulach@103: "is why it cannot access the field using getter and setter" jtulach@103: ); jtulach@103: } jtulach@103: jtulach@103: @Test jtulach@103: public void fixUnsafeDecrementFromOutside() { jtulach@103: SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally(); jtulach@103: try { jtulach@103: synchronized (instance) { jtulach@103: instance.unsafeDecrement(); jtulach@103: } jtulach@103: } catch (AssertionError ex) { jtulach@103: // OK jtulach@103: return; jtulach@103: } jtulach@103: fail("Unlike the SynchronizedFieldsTest, the fix by wrapping instance" + jtulach@103: "into own synchronized block will not help, neither any other" + jtulach@103: "fix, the lock is really private" jtulach@103: ); jtulach@103: } jtulach@103: }