001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.listener.ListenerProvider;
012: import com.tc.util.Assert;
013: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
014:
015: import java.util.EnumMap;
016: import java.util.LinkedList;
017: import java.util.concurrent.BrokenBarrierException;
018: import java.util.concurrent.CyclicBarrier;
019:
020: public final class EnumMapTestApp extends
021: AbstractErrorCatchingTransparentApp {
022:
023: enum Fruit {
024: APPLE("Apple"), ORANGE("Orange"), BANANA("Banana"), PEAR("Pear");
025:
026: private final String name;
027:
028: Fruit(final String name) {
029: this .name = name;
030: }
031:
032: public String getName() {
033: return name;
034: }
035:
036: }
037:
038: private static final LinkedList<String> AVAILABLE_FRUIT = new LinkedList<String>();
039:
040: private static void createAvailableFruit() {
041: synchronized (AVAILABLE_FRUIT) {
042: if (AVAILABLE_FRUIT.isEmpty()) {
043: for (Fruit fruit : Fruit.values()) {
044: AVAILABLE_FRUIT.add(fruit.getName());
045: }
046: }
047: }
048: }
049:
050: static {
051: createAvailableFruit();
052: }
053:
054: private final EnumMap<Fruit, String> clusteredFruitBasket;
055: private final CyclicBarrier barrier;
056:
057: public static void visitL1DSOConfig(final ConfigVisitor visitor,
058: final DSOClientConfigHelper config) {
059: final String testClass = EnumMapTestApp.class.getName();
060: config.addIncludePattern(testClass + "$*");
061: config.addWriteAutolock("* " + testClass + "*.*(..)");
062:
063: final TransparencyClassSpec spec = config
064: .getOrCreateSpec(testClass);
065: spec.addRoot("AVAILABLE_FRUIT", "AVAILABLE_FRUIT");
066: spec.addRoot("clusteredFruitBasket", "clusteredFruitBasket");
067: spec.addRoot("barrier", "barrier");
068: }
069:
070: public EnumMapTestApp(final String appId,
071: final ApplicationConfig cfg,
072: final ListenerProvider listenerProvider) {
073: super (appId, cfg, listenerProvider);
074: clusteredFruitBasket = new EnumMap<Fruit, String>(Fruit.class);
075: barrier = new CyclicBarrier(getParticipantCount());
076: }
077:
078: protected void runTest() throws Throwable {
079: barrier.await();
080: addFruitToBasket(getNextPieceOfFruit());
081: waitForBasketToFill();
082: verifyBasketOperations();
083: }
084:
085: private Fruit getNextPieceOfFruit() {
086: final Fruit fruitToAddToBasket;
087: synchronized (AVAILABLE_FRUIT) {
088: final String fruitName = AVAILABLE_FRUIT.remove();
089: Fruit foundFruit = null;
090: for (Fruit f : Fruit.values()) {
091: if (f.getName().equals(fruitName)) {
092: foundFruit = f;
093: break;
094: }
095: }
096: Assert.assertNotNull(foundFruit);
097: fruitToAddToBasket = foundFruit;
098: }
099: return fruitToAddToBasket;
100: }
101:
102: private void addFruitToBasket(final Fruit fruit) {
103: synchronized (clusteredFruitBasket) {
104: clusteredFruitBasket.put(fruit, fruit.getName());
105: }
106: }
107:
108: private void waitForBasketToFill() throws InterruptedException,
109: BrokenBarrierException {
110: barrier.await();
111: }
112:
113: private void verifyBasketOperations() {
114: synchronized (clusteredFruitBasket) {
115: Assert.assertEquals(Fruit.values().length,
116: clusteredFruitBasket.size());
117: final EnumMap<Fruit, String> equivalentBasket = new EnumMap<Fruit, String>(
118: Fruit.class);
119: for (Fruit fruit : Fruit.values()) {
120: // Write
121: Assert.assertEquals(fruit.getName(),
122: clusteredFruitBasket.remove(fruit));
123: Assert.assertFalse(clusteredFruitBasket
124: .containsKey(fruit));
125: Assert.assertFalse(clusteredFruitBasket.keySet()
126: .contains(fruit));
127: Assert.assertFalse(clusteredFruitBasket
128: .containsValue(fruit.getName()));
129: Assert.assertFalse(clusteredFruitBasket.values()
130: .contains(fruit.getName()));
131: clusteredFruitBasket.put(fruit, fruit.getName());
132:
133: // Read
134: Assert.assertTrue(clusteredFruitBasket
135: .containsKey(fruit));
136: Assert.assertTrue(clusteredFruitBasket.keySet()
137: .contains(fruit));
138: Assert.assertTrue(clusteredFruitBasket
139: .containsValue(fruit.getName()));
140: Assert.assertTrue(clusteredFruitBasket.values()
141: .contains(fruit.getName()));
142: Assert.assertEquals(fruit.getName(),
143: clusteredFruitBasket.get(fruit));
144:
145: // For our later .equals() comparison
146: equivalentBasket.put(fruit, fruit.getName());
147: }
148: Assert.assertTrue(clusteredFruitBasket
149: .equals(equivalentBasket));
150: clusteredFruitBasket.clear();
151: Assert.assertTrue(clusteredFruitBasket.isEmpty());
152: clusteredFruitBasket.putAll(equivalentBasket);
153: Assert.assertTrue(clusteredFruitBasket
154: .equals(equivalentBasket));
155: }
156: }
157:
158: }
|