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-2007 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 org.netbeans.modules.cnd.repository.impl;
043:
044: import java.io.DataInput;
045: import java.io.DataOutput;
046: import java.io.IOException;
047: import junit.framework.*;
048: import org.netbeans.junit.*;
049: import org.netbeans.modules.cnd.repository.api.Repository;
050: import org.netbeans.modules.cnd.repository.api.RepositoryAccessor;
051: import org.netbeans.modules.cnd.repository.spi.Key;
052: import org.netbeans.modules.cnd.repository.spi.Persistent;
053: import org.netbeans.modules.cnd.repository.spi.PersistentFactory;
054: import org.netbeans.modules.cnd.test.BaseTestCase;
055:
056: /**
057: * Tests Repository.tryGet()
058: * @author Vladimir Kvashin
059: */
060: public class TryGetTest extends BaseTestCase {
061:
062: private abstract class BaseKey implements Key {
063:
064: private String key;
065:
066: public BaseKey(String key) {
067: this .key = key;
068: }
069:
070: public int getSecondaryAt(int level) {
071: return 0;
072: }
073:
074: public String getAt(int level) {
075: return key;
076: }
077:
078: public String getUnit() {
079: return "Repository_Test_Unit";
080: }
081:
082: public int getSecondaryDepth() {
083: return 0;
084: }
085:
086: public PersistentFactory getPersistentFactory() {
087: return factory;
088: }
089:
090: public int getDepth() {
091: return 1;
092: }
093:
094: }
095:
096: private class SmallKey extends BaseKey {
097: public SmallKey(String key) {
098: super (key);
099: }
100:
101: public Key.Behavior getBehavior() {
102: return Key.Behavior.Default;
103: }
104: }
105:
106: private class LargeKey extends BaseKey {
107: public LargeKey(String key) {
108: super (key);
109: }
110:
111: public Key.Behavior getBehavior() {
112: return Key.Behavior.LargeAndMutable;
113: }
114: }
115:
116: private static class Value implements Persistent {
117: private String value;
118:
119: public Value(String value) {
120: this .value = value;
121: }
122:
123: public String toString() {
124: return value + " @" + hashCode();
125: }
126:
127: public boolean equals(Object obj) {
128: if (obj instanceof Value) {
129: return value.equals(((Value) obj).value);
130: }
131: return false;
132: }
133:
134: public int hashCode() {
135: return value.hashCode();
136: }
137:
138: }
139:
140: private class Factory implements PersistentFactory {
141:
142: public boolean canWrite(Persistent obj) {
143: return true;
144: }
145:
146: public void write(DataOutput out, Persistent obj)
147: throws IOException {
148: assert obj instanceof Value;
149: out.writeUTF(((Value) obj).value);
150: }
151:
152: public Persistent read(DataInput in) throws IOException {
153: readFlag = true;
154: String value = in.readUTF();
155: return new Value(value);
156: }
157: }
158:
159: private PersistentFactory factory;
160: private Repository repository;
161: private boolean readFlag;
162:
163: public TryGetTest(java.lang.String testName) {
164: super (testName);
165: }
166:
167: public static void main(java.lang.String[] args) {
168: junit.textui.TestRunner.run(suite());
169: }
170:
171: public static Test suite() {
172: TestSuite suite = new NbTestSuite(TryGetTest.class);
173: return suite;
174: }
175:
176: @Override
177: protected void setUp() throws Exception {
178: repository = RepositoryAccessor.getRepository();
179: factory = new Factory();
180: readFlag = false;
181: super .setUp();
182: }
183:
184: @Override
185: protected void tearDown() throws Exception {
186: super .tearDown();
187: }
188:
189: public void testTryGet() {
190: _test(new SmallKey("small_1"), new Value("small_obj_1"));
191: _test(new LargeKey("large_1"), new Value("large_obj_1"));
192:
193: }
194:
195: private void _test(Key key, Value value) {
196:
197: repository.put(key, value);
198:
199: Persistent v2 = repository.get(key);
200: assertNotNull(v2);
201: assertEquals(value, v2);
202:
203: readFlag = false;
204:
205: v2 = _tryGet(key);
206: assertNotNull(v2);
207: assertEquals(value, v2);
208:
209: repository.debugClear();
210:
211: v2 = _tryGet(key);
212: assertNull(v2);
213: }
214:
215: private void sleep(long millis) {
216: try {
217: Thread.sleep(millis);
218: } catch (InterruptedException ex) {
219: }
220: }
221:
222: private Persistent _tryGet(Key key) {
223: readFlag = false;
224: Persistent p = repository.tryGet(key);
225: assertFalse("tryGet shouldn't cause reading object from disk",
226: readFlag);
227: return p;
228: }
229: }
|