001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package threaddemo.util;
043:
044: import java.lang.reflect.InvocationTargetException;
045: import java.util.ArrayList;
046: import java.util.Arrays;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.StringTokenizer;
050: import junit.framework.TestCase;
051: import threaddemo.locking.RWLock;
052: import threaddemo.locking.Locks;
053: import threaddemo.locking.PrivilegedLock;
054: import threaddemo.util.TwoWaySupport;
055: import threaddemo.util.TwoWaySupport.DerivationResult;
056:
057: /**
058: * Test the two-way support.
059: * @author Jesse Glick
060: */
061: public class TwoWaySupportTest extends TestCase {
062:
063: private PrivilegedLock p;
064: private SimpleTWS s;
065:
066: protected void setUp() throws Exception {
067: p = new PrivilegedLock();
068: RWLock l = Locks.readWrite(p);
069: s = new SimpleTWS(l);
070: p.enterWrite();
071: }
072:
073: protected void tearDown() throws Exception {
074: p.exitWrite();
075: }
076:
077: public void testBasicDerivation() throws Exception {
078: assertNull(s.getValueNonBlocking());
079: assertNull(s.getStaleValueNonBlocking());
080: assertEquals(
081: Arrays.asList(new String[] { "initial", "value" }), s
082: .getValueBlocking());
083: assertEquals(
084: Arrays.asList(new String[] { "initial", "value" }), s
085: .getValueNonBlocking());
086: assertEquals(
087: Arrays.asList(new String[] { "initial", "value" }), s
088: .getStaleValueNonBlocking());
089: s.setString("new value");
090: assertNull(s.getValueNonBlocking());
091: assertEquals(
092: Arrays.asList(new String[] { "initial", "value" }), s
093: .getStaleValueNonBlocking());
094: assertEquals(Arrays.asList(new String[] { "new", "value" }), s
095: .getValueBlocking());
096: assertEquals(Arrays.asList(new String[] { "new", "value" }), s
097: .getValueNonBlocking());
098: assertEquals(Arrays.asList(new String[] { "new", "value" }), s
099: .getStaleValueNonBlocking());
100: s.setString("");
101: assertNull(s.getValueNonBlocking());
102: assertEquals(Arrays.asList(new String[] { "new", "value" }), s
103: .getStaleValueNonBlocking());
104: try {
105: Object v = s.getValueBlocking();
106: fail("Should not be computed: " + v.toString());
107: } catch (InvocationTargetException e) {
108: assertEquals("empty string", e.getTargetException()
109: .getMessage());
110: }
111: assertNull(s.getValueNonBlocking());
112: assertEquals(Arrays.asList(new String[] { "new", "value" }), s
113: .getStaleValueNonBlocking());
114: }
115:
116: // XXX to test:
117: // - mutation
118: // - initiation
119: // - asynchronous access
120: // - delayed computation
121: // - firing of changes
122: // - forgetting
123:
124: /**
125: * Underlying model: text string (String)
126: * Broken if underlying model is ""!
127: */
128: private static final class SimpleTWS extends
129: TwoWaySupport<List<String>, String, List<String>> {
130:
131: private String string = "initial value";
132:
133: public SimpleTWS(RWLock l) {
134: super (l);
135: }
136:
137: public String getString() {
138: return string;
139: }
140:
141: public void setString(String s) {
142: this .string = s;
143: invalidate(s);
144: }
145:
146: // Impl TWS:
147:
148: protected String composeUnderlyingDeltas(
149: String underlyingDelta1, String underlyingDelta2) {
150: return underlyingDelta2;
151: }
152:
153: protected DerivationResult<List<String>, List<String>> doDerive(
154: List<String> oldValue, String undval) throws Exception {
155: if (undval == null) {
156: undval = getString();
157: }
158: if (undval.length() == 0)
159: throw new Exception("empty string");
160: List<String> v = new ArrayList<String>();
161: StringTokenizer tok = new StringTokenizer(undval);
162: while (tok.hasMoreTokens()) {
163: v.add(tok.nextToken());
164: }
165: return new DerivationResult<List<String>, List<String>>(v,
166: oldValue != null ? v : null);
167: }
168:
169: protected List<String> doRecreate(List<String> oldValue,
170: List<String> l) throws Exception {
171: StringBuffer b = new StringBuffer();
172: Iterator<String> i = l.iterator();
173: if (i.hasNext()) {
174: b.append(i.next());
175: }
176: while (i.hasNext()) {
177: b.append(' ');
178: b.append(i.next());
179: }
180: string = b.toString();
181: return l;
182: }
183:
184: }
185:
186: }
|