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.EnumSet;
016: import java.util.Iterator;
017: import java.util.concurrent.BrokenBarrierException;
018: import java.util.concurrent.CyclicBarrier;
019:
020: public class EnumSetTestApp extends AbstractErrorCatchingTransparentApp {
021:
022: enum Utensil {
023: FORK("Fork"), SPOON("Spoon"), KNIFE("Knife");
024:
025: private final String name;
026:
027: Utensil(final String name) {
028: this .name = name;
029: }
030:
031: public String getName() {
032: return name;
033: }
034:
035: }
036:
037: private static final EnumSet<Utensil> FULL_FLATWARE_SET = EnumSet
038: .allOf(Utensil.class);
039: private static final EnumSet<Utensil> AVAILABLE_UTENSILS = EnumSet
040: .allOf(Utensil.class);
041:
042: private final EnumSet<Utensil> clusteredFlatwareSet;
043: private final CyclicBarrier barrier;
044:
045: public static void visitL1DSOConfig(final ConfigVisitor visitor,
046: final DSOClientConfigHelper config) {
047: final String testClass = EnumSetTestApp.class.getName();
048: config.addIncludePattern(testClass + "$*");
049: config.addWriteAutolock("* " + testClass + "*.*(..)");
050:
051: final TransparencyClassSpec spec = config
052: .getOrCreateSpec(testClass);
053: spec.addRoot("AVAILABLE_UTENSILS", "AVAILABLE_UTENSILS");
054: spec.addRoot("clusteredFlatwareSet", "clusteredFlatwareSet");
055: spec.addRoot("barrier", "barrier");
056: }
057:
058: public EnumSetTestApp(final String appId,
059: final ApplicationConfig cfg,
060: final ListenerProvider listenerProvider) {
061: super (appId, cfg, listenerProvider);
062: clusteredFlatwareSet = EnumSet.noneOf(Utensil.class);
063: barrier = new CyclicBarrier(getParticipantCount());
064: }
065:
066: protected void runTest() throws Throwable {
067: barrier.await();
068: addUtensilToFlatwareSet(getNextUtensil());
069: waitForCompleteFlatwareSet();
070: verifySetOperations();
071: }
072:
073: private Utensil getNextUtensil() {
074: final Utensil utensilToAddToFlatwareSet;
075: synchronized (AVAILABLE_UTENSILS) {
076: final Iterator<Utensil> iterator = AVAILABLE_UTENSILS
077: .iterator();
078: utensilToAddToFlatwareSet = iterator.next();
079: Assert.assertNotNull(utensilToAddToFlatwareSet);
080: iterator.remove();
081: Assert.assertFalse(AVAILABLE_UTENSILS
082: .contains(utensilToAddToFlatwareSet));
083: }
084: return utensilToAddToFlatwareSet;
085: }
086:
087: private void addUtensilToFlatwareSet(final Utensil utensil) {
088: synchronized (clusteredFlatwareSet) {
089: clusteredFlatwareSet.add(utensil);
090: }
091: }
092:
093: private void waitForCompleteFlatwareSet()
094: throws InterruptedException, BrokenBarrierException {
095: barrier.await();
096: }
097:
098: private void verifySetOperations() {
099: synchronized (clusteredFlatwareSet) {
100: // Read operations
101: Assert.assertFalse(clusteredFlatwareSet.isEmpty());
102: assertFlatwareSetCompleteness(true);
103: for (Utensil utensil : Utensil.values()) {
104: Assert.assertTrue(clusteredFlatwareSet
105: .contains(utensil));
106: }
107:
108: // Write operations
109: for (Utensil utensil : Utensil.values()) {
110: Assert.assertTrue(clusteredFlatwareSet.remove(utensil));
111: assertFlatwareSetCompleteness(false);
112: Assert.assertTrue(clusteredFlatwareSet.add(utensil));
113: assertFlatwareSetCompleteness(true);
114: }
115:
116: clusteredFlatwareSet.clear();
117: assertEmpty();
118: Assert.assertTrue(clusteredFlatwareSet
119: .addAll(FULL_FLATWARE_SET));
120: assertFlatwareSetCompleteness(true);
121:
122: Assert.assertTrue(clusteredFlatwareSet
123: .removeAll(FULL_FLATWARE_SET));
124: assertEmpty();
125: Assert.assertTrue(clusteredFlatwareSet
126: .addAll(FULL_FLATWARE_SET));
127: assertFlatwareSetCompleteness(true);
128: }
129: }
130:
131: private void assertFlatwareSetCompleteness(
132: final boolean shouldBeComplete) {
133: synchronized (clusteredFlatwareSet) {
134: Assert.assertEquals(shouldBeComplete, clusteredFlatwareSet
135: .containsAll(FULL_FLATWARE_SET));
136: Assert.assertEquals(shouldBeComplete, clusteredFlatwareSet
137: .equals(FULL_FLATWARE_SET));
138: if (shouldBeComplete) {
139: Assert.assertEquals(clusteredFlatwareSet.size(),
140: Utensil.values().length);
141: } else {
142: Assert.assertTrue(clusteredFlatwareSet.size() < Utensil
143: .values().length);
144: }
145: }
146: }
147:
148: private void assertEmpty() {
149: synchronized (clusteredFlatwareSet) {
150: Assert.assertTrue(clusteredFlatwareSet.isEmpty());
151: Assert.assertEquals(0, clusteredFlatwareSet.size());
152: assertFlatwareSetCompleteness(false);
153: }
154: }
155:
156: }
|