001: /*
002: * All content copyright (c) 2003-2006 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.AbstractTransparentApp;
014:
015: import java.util.Random;
016:
017: public class ArrayTestApp extends AbstractTransparentApp {
018:
019: private String[] myArrayTestRoot;
020: final private String[] stringAry = { "hee", "hoo", "haa",
021: "terracotta", "google", "yahoo", "apple" };
022: final private static long runtime = 1000 * 200; // 200 seconds
023:
024: public ArrayTestApp(String appId, ApplicationConfig cfg,
025: ListenerProvider listenerProvider) {
026: super (appId, cfg, listenerProvider);
027: this .myArrayTestRoot = new String[] { "hee", "hoo", "haa",
028: "terracotta", "google", "yahoo", "apple" };
029: }
030:
031: public void run() {
032: Random rand = new Random();
033: long end = System.currentTimeMillis() + runtime;
034: try {
035: while (end > System.currentTimeMillis()) {
036: synchronized (myArrayTestRoot) {
037: int idx = rand.nextInt(myArrayTestRoot.length);
038: // System.out.println(myArrayTestRoot[rand.nextInt(myArrayTestRoot.length)]);
039: Assert.assertTrue(myArrayTestRoot[idx]
040: .equals(stringAry[idx]));
041: }
042: Thread.sleep((int) (Math.random() * 10));
043: }
044: } catch (Exception e) {
045: e.printStackTrace();
046: }
047:
048: arrayIndexTestCase();
049:
050: testNullArrayAccess();
051: }
052:
053: private void testNullArrayAccess() {
054: Object[] o = null;
055:
056: try {
057: if (o[3] == null) {
058: throw new AssertionError();
059: }
060: } catch (NullPointerException npe) {
061: // expecte
062: }
063: }
064:
065: private void arrayIndexTestCase() {
066: // We had a bug where ArrayIndexOutOfBoundsException failed to release a monitor, this is the test case for it
067: try {
068: for (int i = 0; true; i++) {
069: Object o = myArrayTestRoot[i];
070:
071: // silence warning about unread local variable
072: if (o == null) {
073: continue;
074: }
075: }
076: } catch (ArrayIndexOutOfBoundsException aioobe) {
077: //
078: }
079:
080: try {
081: Object o = myArrayTestRoot[-1];
082: if (true || o == o) {
083: throw new AssertionError();
084: }
085: } catch (ArrayIndexOutOfBoundsException aioobe) {
086: //
087: }
088:
089: }
090:
091: public void setArray(String[] blah) {
092: myArrayTestRoot = blah;
093: }
094:
095: public static void visitL1DSOConfig(ConfigVisitor visitor,
096: DSOClientConfigHelper config) {
097: String testClass = ArrayTestApp.class.getName();
098: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
099:
100: String methodExpression = "* " + testClass + "*.*(..)";
101: config.addWriteAutolock(methodExpression);
102: spec.addRoot("myArrayTestRoot", "myArrayTestRoot");
103:
104: }
105: }
|