1 package org.apidesign.samples;
3 import java.util.logging.Level;
4 import java.util.logging.Logger;
5 import org.junit.After;
6 import org.junit.AfterClass;
7 import org.junit.Before;
8 import org.junit.BeforeClass;
10 import static org.junit.Assert.*;
12 public class StringBufferTest {
14 public void createRegular() {
15 StringBuffer sb = new StringBuffer();
16 assertAddAndToString(sb);
20 public void createUnsynchronized() throws InterruptedException {
21 final StringBuffer sb = StringBuffer.createUnsynchronized();
23 class Lock extends Thread {
34 } catch (InterruptedException ex) {
35 Logger.getLogger(StringBufferTest.class.getName()).log(Level.SEVERE, null, ex);
40 public void waitLocked() throws InterruptedException {
51 Lock lock = new Lock();
55 assertEquals("result is really locked", 1, lock.state);
57 assertAddAndToString(sb);
59 assertEquals("result is still locked", 1, lock.state);
62 private void assertAddAndToString(StringBuffer sb) {
63 sb.append("Hello").append(" ");
64 sb.append("API").append(" Design!");
66 assertEquals("Hello API Design!", sb.toString());