001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.listener.ListenerProvider;
013: import com.tc.util.Assert;
014: import com.tctest.runner.AbstractTransparentApp;
015:
016: import java.util.HashMap;
017: import java.util.Hashtable;
018: import java.util.Iterator;
019: import java.util.LinkedHashMap;
020: import java.util.Map;
021: import java.util.Set;
022: import java.util.Vector;
023:
024: public class LinkedHashMapClassAdapterTestApp extends
025: AbstractTransparentApp {
026:
027: private final CyclicBarrier barrier;
028: private final Map linkedHashMap;
029: private int nodeId;
030:
031: public LinkedHashMapClassAdapterTestApp(String appId,
032: ApplicationConfig cfg, ListenerProvider listenerProvider) {
033: super (appId, cfg, listenerProvider);
034: nodeId += 1;
035: barrier = new CyclicBarrier(getParticipantCount());
036: linkedHashMap = new CustomLinkedHashMap();
037: }
038:
039: public static void visitL1DSOConfig(final ConfigVisitor visitor,
040: final DSOClientConfigHelper config) {
041: config.addWriteAutolock("* *..*.*(..)");
042:
043: config.getOrCreateSpec(Element.class.getName());
044: config.addWriteAutolock("* " + Element.class.getName()
045: + "*.*(..)");
046:
047: config.getOrCreateSpec(CyclicBarrier.class.getName());
048: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
049: + "*.*(..)");
050: config.addWriteAutolock("* " + Hashtable.class.getName()
051: + "*.*(..)");
052: config.addWriteAutolock("* " + Vector.class.getName()
053: + "*.*(..)");
054:
055: config.getOrCreateSpec(CustomLinkedHashMap.class.getName());
056: config.addWriteAutolock("* "
057: + CustomLinkedHashMap.class.getName() + "*.*(..)");
058:
059: final String testClass = LinkedHashMapClassAdapterTestApp.class
060: .getName();
061: final TransparencyClassSpec spec = config
062: .getOrCreateSpec(testClass);
063: spec.addRoot("barrier", "barrier");
064: spec.addRoot("linkedHashMap", "linkedHashMap");
065: spec.addRoot("nodeId", "nodeId");
066: }
067:
068: public void run() {
069: try {
070: putTesting();
071: getTesting();
072: removeTesting();
073: clearTesting();
074: //putAllTesting();
075: barrier.barrier();
076: } catch (Throwable t) {
077: notifyError(t);
078: }
079: }
080:
081: private void putTesting() throws Exception {
082: Element elem1 = new Element("key1", "value1");
083: Element elem2 = new Element("key2", "value2");
084: Element elem3 = new Element("key3", "value3");
085: put(elem1);
086: put(elem2);
087: put(elem3);
088: Assert.assertTrue(linkedHashMap.containsKey("key1"));
089: //Assert.assertTrue(linkedHashMap.containsValue(elem1));
090: Assert.assertTrue(linkedHashMap.containsKey("key2"));
091: //Assert.assertTrue(linkedHashMap.containsValue(elem2));
092: Assert.assertTrue(linkedHashMap.containsKey("key3"));
093: //Assert.assertTrue(linkedHashMap.containsValue(elem3));
094: barrier.barrier();
095: }
096:
097: private void getTesting() throws Exception {
098: String expected = "value1";
099: Element actual = get("key1");
100: Assert.assertEquals(expected, actual.getValue());
101: barrier.barrier();
102: }
103:
104: private void removeTesting() throws Exception {
105: synchronized (linkedHashMap) {
106: linkedHashMap.remove("key1");
107: Assert.assertFalse(linkedHashMap.containsKey("key1"));
108: }
109: barrier.barrier();
110: }
111:
112: private void clearTesting() throws Exception {
113: synchronized (linkedHashMap) {
114: linkedHashMap.clear();
115: Assert.assertTrue(linkedHashMap.isEmpty());
116: }
117: barrier.barrier();
118: }
119:
120: private void putAllTesting() throws Exception {
121: synchronized (linkedHashMap) {
122: final Map expect = new HashMap();
123: expect.put("key1", new Element("key1", "value1"));
124: expect.put("key2", new Element("key2", "value2"));
125: expect.put("key3", new Element("key3", "value3"));
126: expect.put("key4", new Element("key4", "value4"));
127: expect.put("key5", new Element("key5", "value1"));
128: linkedHashMap.clear();
129: linkedHashMap.putAll(expect);
130: Assert.assertEquals(expect.size(), linkedHashMap.size());
131: final Set expectEntries = expect.entrySet();
132: final Set actualEntries = linkedHashMap.entrySet();
133: for (Iterator iExpect = expectEntries.iterator(), iActual = actualEntries
134: .iterator(); iExpect.hasNext();) {
135: Assert.assertEquals(iExpect.next(), iActual.next());
136: }
137: }
138: barrier.barrier();
139: }
140:
141: private void put(final Element element) {
142: synchronized (linkedHashMap) {
143: linkedHashMap.put(element.getKey(), element);
144: }
145: }
146:
147: private Element get(final String key) {
148: synchronized (linkedHashMap) {
149: return (Element) linkedHashMap.get(key);
150: }
151: }
152:
153: private static final class CustomLinkedHashMap extends
154: LinkedHashMap {
155: private static final int INITIAL_CAPACITY = 100;
156: private static final float GROWTH_FACTOR = .75F;
157:
158: public CustomLinkedHashMap() {
159: super (INITIAL_CAPACITY, GROWTH_FACTOR, true);
160: }
161:
162: protected final boolean removeEldestEntry(Map.Entry eldest) {
163: Element element = (Element) eldest.getValue();
164: return (element.getValue() == null);
165: }
166: }
167:
168: private final class Element {
169: private final String key;
170: private final Object value;
171:
172: public Element(final String key, final Object value) {
173: this .key = key;
174: this .value = value;
175: }
176:
177: public String getKey() {
178: return this .key;
179: }
180:
181: public Object getValue() {
182: return this.value;
183: }
184: }
185: }
|