001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.test.validator;
013:
014: import junit.framework.Assert;
015: import junit.framework.TestCase;
016: import net.sf.oval.Validator;
017: import net.sf.oval.configuration.annotation.IsInvariant;
018: import net.sf.oval.constraint.MaxLength;
019: import net.sf.oval.constraint.NotNull;
020:
021: /**
022: * @author Sebastian Thomschke
023: */
024: public class ConcurrencyTest extends TestCase {
025: public final static class TestEntity1 {
026: @NotNull
027: @MaxLength(5)
028: private String name;
029:
030: public String getName() {
031: return name;
032: }
033: }
034:
035: public final static class TestEntity2 {
036:
037: public String name;
038:
039: @NotNull
040: @MaxLength(5)
041: @IsInvariant
042: public String getName() {
043: return name;
044: }
045: }
046:
047: public void testConcurrency() throws InterruptedException {
048: final Validator validator = new Validator();
049:
050: final TestEntity1 sharedEntity = new TestEntity1();
051:
052: final Thread thread1 = new Thread(new Runnable() {
053: public void run() {
054: final TestEntity1 entity = new TestEntity1();
055:
056: for (int i = 0; i < 100; i++) {
057: Assert.assertEquals(1, validator.validate(
058: sharedEntity).size());
059:
060: entity.name = null;
061: Assert.assertEquals(1, validator.validate(entity)
062: .size());
063:
064: entity.name = "1234";
065: Assert.assertEquals(0, validator.validate(entity)
066: .size());
067:
068: entity.name = "123456";
069: Assert.assertEquals(1, validator.validate(entity)
070: .size());
071:
072: try {
073: Thread.sleep(5);
074: } catch (InterruptedException e) {
075: Thread.currentThread().interrupt();
076: }
077: }
078: }
079: });
080: final Thread thread2 = new Thread(new Runnable() {
081: public void run() {
082: final TestEntity2 entity = new TestEntity2();
083:
084: for (int i = 0; i < 100; i++) {
085: Assert.assertEquals(1, validator.validate(
086: sharedEntity).size());
087:
088: entity.name = null;
089: Assert.assertEquals(1, validator.validate(entity)
090: .size());
091:
092: entity.name = "1234";
093: Assert.assertEquals(0, validator.validate(entity)
094: .size());
095:
096: entity.name = "123456";
097: Assert.assertEquals(1, validator.validate(entity)
098: .size());
099:
100: try {
101: Thread.sleep(5);
102: } catch (InterruptedException e) {
103: Thread.currentThread().interrupt();
104: }
105: }
106: }
107: });
108: thread1.run();
109: thread2.run();
110: thread1.join();
111: thread2.join();
112: }
113: }
|