samples/deadlock/test/org/apidesign/deadlock/SynchronizedFieldsInternallyTest.java
1 package org.apidesign.deadlock;
4 import static org.junit.Assert.*;
6 public class SynchronizedFieldsInternallyTest {
7 public SynchronizedFieldsInternallyTest() {
11 public void increment() {
12 SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally();
17 public void unsafeDecrement() {
18 SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally();
20 instance.unsafeDecrement();
21 } catch (java.lang.AssertionError ex) {
25 fail("This will fail as unsafeDecrement is not synchronized, and that" +
26 "is why it cannot access the field using getter and setter"
31 public void fixUnsafeDecrementFromOutside() {
32 SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally();
34 synchronized (instance) {
35 instance.unsafeDecrement();
37 } catch (AssertionError ex) {
41 fail("Unlike the SynchronizedFieldsTest, the fix by wrapping instance" +
42 "into own synchronized block will not help, neither any other" +
43 "fix, the lock is really private"