Lines 20-27
Link Here
|
20 |
import org.netbeans.junit.*; |
20 |
import org.netbeans.junit.*; |
21 |
|
21 |
|
22 |
public class MutexTest extends NbTestCase { |
22 |
public class MutexTest extends NbTestCase { |
23 |
private Mutex.Privileged p; |
23 |
Mutex.Privileged p; |
24 |
private Mutex m; |
24 |
Mutex m; |
25 |
|
25 |
|
26 |
|
26 |
|
27 |
|
27 |
|
Lines 44-49
Link Here
|
44 |
protected void setUp () { |
44 |
protected void setUp () { |
45 |
p = new Mutex.Privileged (); |
45 |
p = new Mutex.Privileged (); |
46 |
m = new Mutex (p); |
46 |
m = new Mutex (p); |
|
|
47 |
Mutex.beStrict = true; |
47 |
} |
48 |
} |
48 |
|
49 |
|
49 |
public void testReadWriteRead() throws Exception { |
50 |
public void testReadWriteRead() throws Exception { |
Lines 529-534
Link Here
|
529 |
p.enterWriteAccess(); |
530 |
p.enterWriteAccess(); |
530 |
p.exitWriteAccess(); |
531 |
p.exitWriteAccess(); |
531 |
} |
532 |
} |
|
|
533 |
|
534 |
public void testNoWayToDoReadAndThenWrite () { |
535 |
class R implements Runnable { |
536 |
public void run () { |
537 |
m.writeAccess (this); |
538 |
} |
539 |
} |
540 |
|
541 |
try { |
542 |
m.readAccess (new R ()); |
543 |
fail ("This is supposed to throw an IllegalStateException"); |
544 |
} catch (IllegalStateException ex) { |
545 |
// ok, this is expected |
546 |
} |
547 |
} |
548 |
|
549 |
public void testNoWayToDoWriteThenReadAndThenWrite () { |
550 |
class R implements Runnable { |
551 |
public boolean second; |
552 |
public boolean end; |
553 |
public boolean ending; |
554 |
|
555 |
public void run () { |
556 |
if (end) { |
557 |
ending = true; |
558 |
return; |
559 |
} |
560 |
|
561 |
if (second) { |
562 |
end = true; |
563 |
m.writeAccess (this); |
564 |
} else { |
565 |
second = true; |
566 |
m.readAccess (this); |
567 |
} |
568 |
} |
569 |
} |
570 |
R r = new R (); |
571 |
try { |
572 |
m.writeAccess (r); |
573 |
fail ("This is supposed to throw an IllegalStateException"); |
574 |
} catch (IllegalStateException ex) { |
575 |
// ok, this is expected |
576 |
assertTrue ("We were in the write access section", r.second); |
577 |
assertTrue ("We were before the writeAcess(this)", r.end); |
578 |
assertFalse ("We never reached ending", r.ending); |
579 |
} |
580 |
} |
581 |
|
582 |
public void testIsOrIsNotInReadOrWriteAccess () { |
583 |
new ReadWriteChecking ("No r/w", Boolean.FALSE, Boolean.FALSE).run (); |
584 |
m.readAccess (new ReadWriteChecking ("r but no w", Boolean.TRUE, Boolean.FALSE)); |
585 |
m.writeAccess (new ReadWriteChecking ("w but no r", Boolean.FALSE, Boolean.TRUE)); |
586 |
m.readAccess (new Runnable () { |
587 |
public void run () { |
588 |
m.postReadRequest (new ReadWriteChecking ("+r -w", Boolean.TRUE, Boolean.FALSE)); |
589 |
} |
590 |
}); |
591 |
m.readAccess (new Runnable () { |
592 |
public void run () { |
593 |
m.postWriteRequest (new ReadWriteChecking ("-r +w", Boolean.FALSE, Boolean.TRUE)); |
594 |
} |
595 |
}); |
596 |
m.writeAccess (new Runnable () { |
597 |
public void run () { |
598 |
m.postReadRequest (new ReadWriteChecking ("+r -w", Boolean.TRUE, Boolean.FALSE)); |
599 |
} |
600 |
}); |
601 |
m.writeAccess (new Runnable () { |
602 |
public void run () { |
603 |
m.postWriteRequest (new ReadWriteChecking ("-r +w", Boolean.FALSE, Boolean.TRUE)); |
604 |
} |
605 |
}); |
606 |
|
607 |
// write->read->test (downgrade from write to read) |
608 |
m.writeAccess (new Runnable () { |
609 |
public boolean second; |
610 |
|
611 |
public void run () { |
612 |
if (!second) { |
613 |
second = true; |
614 |
m.readAccess (this); |
615 |
return; |
616 |
} |
617 |
|
618 |
class P implements Runnable { |
619 |
public boolean exec; |
620 |
public void run () { |
621 |
exec = true; |
622 |
} |
623 |
} |
624 |
P r = new P (); |
625 |
P w = new P (); |
626 |
m.postWriteRequest (w); |
627 |
m.postReadRequest (r); |
628 |
assertFalse ("Writer not executed", w.exec); |
629 |
assertFalse ("Reader not executed", r.exec); |
630 |
|
631 |
m.readAccess (new ReadWriteChecking ("+r no w", Boolean.FALSE, Boolean.FALSE)); |
632 |
} |
633 |
}); |
634 |
|
635 |
new ReadWriteChecking ("None at the end", Boolean.FALSE, Boolean.FALSE).run (); |
636 |
} |
637 |
|
638 |
private class ReadWriteChecking implements Runnable { |
639 |
public Boolean read; |
640 |
public Boolean write; |
641 |
public String msg; |
642 |
|
643 |
public ReadWriteChecking (String msg, Boolean read, Boolean write) { |
644 |
assertNotNull ("Msg cannot be null", msg); |
645 |
this.msg = msg; |
646 |
this.read = read; |
647 |
this.write = write; |
648 |
} |
649 |
|
650 |
protected void finalize () { |
651 |
assertNull ("Run method was not called!", msg); |
652 |
} |
653 |
|
654 |
public void run () { |
655 |
if (write != null) assertEquals (msg, write.booleanValue (), m.isWriteAccess ()); |
656 |
if (read != null) assertEquals (msg, read.booleanValue (), m.isReadAccess ()); |
657 |
msg = null; |
658 |
} |
659 |
} |
660 |
|
532 |
|
661 |
|
533 |
private static class State implements Runnable { |
662 |
private static class State implements Runnable { |
534 |
public int state; |
663 |
public int state; |