001: /*
002: * Copyright (C) 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 20. September 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.converters.extended;
012:
013: import com.thoughtworks.acceptance.objects.Software;
014: import com.thoughtworks.xstream.converters.SingleValueConverter;
015:
016: import junit.framework.TestCase;
017:
018: import java.beans.PropertyEditorSupport;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: /**
023: * @author Jörg Schaible
024: */
025: public class PropertyEditorCapableConverterTest extends TestCase {
026:
027: public static class SoftwarePropertyEditor extends
028: PropertyEditorSupport {
029:
030: public String getAsText() {
031: Software software = (Software) getValue();
032: return software.vendor + ":" + software.name;
033: }
034:
035: public void setAsText(String text)
036: throws IllegalArgumentException {
037: int idx = text.indexOf(':');
038: setValue(new Software(text.substring(0, idx), text
039: .substring(idx + 1)));
040: }
041:
042: }
043:
044: public void testCanBeUsedForCustomTypes() {
045: Software software = new Software("Joe Walnes", "XStream");
046: SingleValueConverter converter = new PropertyEditorCapableConverter(
047: SoftwarePropertyEditor.class, Software.class);
048: assertTrue(converter.canConvert(Software.class));
049: assertEquals("Joe Walnes:XStream", converter.toString(software));
050: assertEquals(software, converter
051: .fromString("Joe Walnes:XStream"));
052: }
053:
054: public void testConcurrentConversion() throws InterruptedException {
055: final SingleValueConverter converter = new PropertyEditorCapableConverter(
056: SoftwarePropertyEditor.class, Software.class);
057:
058: final Map exceptions = new HashMap();
059: final ThreadGroup tg = new ThreadGroup(getName()) {
060: public void uncaughtException(Thread t, Throwable e) {
061: exceptions.put(e, t.getName());
062: super .uncaughtException(t, e);
063: }
064: };
065:
066: final Map references = new HashMap();
067: final int[] counter = new int[1];
068: counter[0] = 0;
069: final Thread[] threads = new Thread[10];
070: for (int i = 0; i < threads.length; ++i) {
071: final String name = "JUnit Thread:" + i;
072: references.put(name, new Software("JUnit Thread", Integer
073: .toString(i)));
074: threads[i] = new Thread(tg, name) {
075:
076: public void run() {
077: int i = 0;
078: try {
079: synchronized (this ) {
080: notifyAll();
081: wait();
082: }
083: final Software software = (Software) references
084: .get(Thread.currentThread().getName());
085: while (i < 1000 && !interrupted()) {
086: String formatted = converter
087: .toString(software);
088: Thread.yield();
089: assertEquals(software, converter
090: .fromString(formatted));
091: ++i;
092: }
093: } catch (InterruptedException e) {
094: fail("Unexpected InterruptedException");
095: }
096: synchronized (counter) {
097: counter[0] += i;
098: }
099: }
100:
101: };
102: }
103:
104: for (int i = 0; i < threads.length; ++i) {
105: synchronized (threads[i]) {
106: threads[i].start();
107: threads[i].wait();
108: }
109: }
110:
111: for (int i = 0; i < threads.length; ++i) {
112: synchronized (threads[i]) {
113: threads[i].notifyAll();
114: }
115: }
116:
117: Thread.sleep(1000);
118:
119: for (int i = 0; i < threads.length; ++i) {
120: threads[i].interrupt();
121: }
122: for (int i = 0; i < threads.length; ++i) {
123: synchronized (threads[i]) {
124: threads[i].join();
125: }
126: }
127:
128: assertEquals("Exceptions have been thrown: " + exceptions, 0,
129: exceptions.size());
130: assertTrue(
131: "Each thread should have made at least 1 conversion",
132: counter[0] >= threads.length);
133: }
134:
135: }
|