001: /*
002: * Copyright (C) 2006, 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 22. March 2006 by Joerg Schaible
010: */
011: package com.thoughtworks.acceptance;
012:
013: import java.util.Arrays;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Map;
017:
018: import com.thoughtworks.acceptance.objects.Software;
019: import com.thoughtworks.acceptance.someobjects.WithNamedList;
020:
021: /**
022: * @author Jörg Schaible
023: */
024: public class ConcurrencyTest extends AbstractAcceptanceTest {
025:
026: public void testConcurrentXStreaming() throws InterruptedException {
027: xstream.alias("thing", WithNamedList.class);
028: xstream.addImplicitCollection(WithNamedList.class, "things");
029:
030: final List reference = Arrays.asList(new String[] { "A", "B",
031: "C", "D" });
032: final WithNamedList[] namedLists = new WithNamedList[5];
033: for (int i = 0; i < namedLists.length; ++i) {
034: namedLists[i] = new WithNamedList("Name " + (i + 1));
035: namedLists[i].things.add(new Software("walnes",
036: "XStream 1." + i));
037: namedLists[i].things.add(reference);
038: namedLists[i].things
039: .add(new RuntimeException("JUnit " + i)); // a Serializable
040: }
041:
042: final Map exceptions = new HashMap();
043: final ThreadGroup tg = new ThreadGroup(getName()) {
044: public void uncaughtException(Thread t, Throwable e) {
045: exceptions.put(e, t.getName());
046: super .uncaughtException(t, e);
047: }
048: };
049:
050: final Object object = Arrays.asList(namedLists);
051: final String xml = xstream.toXML(object);
052: final int[] counter = new int[1];
053: counter[0] = 0;
054: final Thread[] threads = new Thread[5];
055: for (int i = 0; i < threads.length; ++i) {
056: threads[i] = new Thread(tg, "JUnit Thread " + i) {
057:
058: public void run() {
059: int i = 0;
060: try {
061: synchronized (this ) {
062: notifyAll();
063: wait();
064: }
065: while (!interrupted()) {
066: assertBothWays(object, xml);
067: ++i;
068: }
069: } catch (InterruptedException e) {
070: fail("Unexpected InterruptedException");
071: }
072: synchronized (counter) {
073: counter[0] += i;
074: }
075: }
076:
077: };
078: }
079:
080: for (int i = 0; i < threads.length; ++i) {
081: synchronized (threads[i]) {
082: threads[i].start();
083: threads[i].wait();
084: }
085: }
086:
087: for (int i = 0; i < threads.length; ++i) {
088: synchronized (threads[i]) {
089: threads[i].notifyAll();
090: }
091: }
092:
093: Thread.sleep(1000);
094:
095: for (int i = 0; i < threads.length; ++i) {
096: threads[i].interrupt();
097: }
098: for (int i = 0; i < threads.length; ++i) {
099: synchronized (threads[i]) {
100: threads[i].join();
101: }
102: }
103:
104: assertEquals("Exceptions has been thrown: " + exceptions, 0,
105: exceptions.size());
106: assertTrue(
107: "Each thread should have made at least 1 conversion",
108: counter[0] >= threads.length);
109: }
110: }
|