001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.FxContext;
038: import com.flexive.shared.cache.FxCacheException;
039: import com.flexive.shared.configuration.DivisionData;
040: import com.flexive.shared.interfaces.StatelessTest;
041: import com.flexive.shared.structure.FxEnvironment;
042: import org.testng.annotations.BeforeClass;
043: import org.testng.annotations.Test;
044:
045: import java.io.Serializable;
046: import java.util.ArrayList;
047: import java.util.List;
048:
049: /**
050: * Class for testing EJB transactions.
051: *
052: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
053: */
054: @Test(groups="ejb")
055: public class TransactionTest {
056: private static final String PATH = "/TransactionTest";
057:
058: /**
059: * An object that's never equal to other objects.
060: */
061: private static class NeverEqualObject implements Serializable {
062: private static final long serialVersionUID = -1938180841347334758L;
063:
064: @Override
065: public boolean equals(Object obj) {
066: return false;
067: }
068:
069: @Override
070: public int hashCode() {
071: return 0;
072: }
073: }
074:
075: /**
076: * An object that's always equal to other objects.
077: */
078: private static class AlwaysEqualObject implements Serializable {
079: private static final long serialVersionUID = 238120062737195505L;
080:
081: @Override
082: public boolean equals(Object obj) {
083: return true;
084: }
085:
086: @Override
087: public int hashCode() {
088: return 0;
089: }
090: }
091:
092: private static class GetEnvironmentThread implements Runnable {
093: private final List<String> environmentIds;
094:
095: public GetEnvironmentThread(List<String> environmentIds) {
096: this .environmentIds = environmentIds;
097: }
098:
099: public void run() {
100: FxContext.get().setDivisionId(DivisionData.DIVISION_TEST);
101: FxContext.get().setContextPath("flexiveTest");
102: FxEnvironment environment = CacheAdmin.getEnvironment();
103: synchronized (environmentIds) {
104: environmentIds.add(environment.toString());
105: }
106: }
107:
108: }
109:
110: private StatelessTest statelessTest;
111:
112: @BeforeClass
113: public void beforeTestClass() {
114: statelessTest = EJBLookup.getStatelessTestInterface();
115: }
116:
117: @Test
118: public void testSimpleTransactionRollbackCache()
119: throws FxCacheException {
120: // create an object that's always unequal to other objects
121: Object value = new NeverEqualObject();
122: final String key = value.getClass().toString();
123: CacheAdmin.getInstance().put(PATH, key, value);
124: statelessTest.cachePutRollback(PATH, key,
125: new NeverEqualObject());
126: assert CacheAdmin.getInstance().get(PATH, key) == value : "Transaction rollback, but original object removed from cache: "
127: + value
128: + " (cached object = "
129: + CacheAdmin.getInstance().get(PATH, key) + ")";
130: }
131:
132: @Test
133: public void testSimpleTransactionRollbackCacheEqual()
134: throws FxCacheException {
135: // create an object that's always equal to other objects
136: Object value = new AlwaysEqualObject();
137: final String key = value.getClass().toString();
138: CacheAdmin.getInstance().put(PATH, key, value);
139: statelessTest.cachePutRollback(PATH, key,
140: new AlwaysEqualObject());
141: assert CacheAdmin.getInstance().get(PATH, key) == value : "Transaction rollback, but original object removed from cache: "
142: + value
143: + " (cached object = "
144: + CacheAdmin.getInstance().get(PATH, key) + ")";
145: }
146:
147: @Test
148: public void testMultithreadedEnvironment() {
149: final int numThreads = 10;
150: Thread[] threads = new Thread[numThreads];
151: List<String> environmentIds = new ArrayList<String>();
152: for (int i = 0; i < numThreads; i++) {
153: threads[i] = new Thread(new GetEnvironmentThread(
154: environmentIds));
155: }
156: for (Thread thread : threads) {
157: thread.start();
158: }
159: for (Thread thread : threads) {
160: try {
161: thread.join();
162: } catch (InterruptedException e) {
163: // ignore
164: }
165: }
166: // assert that all environments are the same object
167: FxEnvironment environment = CacheAdmin.getEnvironment();
168: for (String id : environmentIds) {
169: assert id.equals(environment.toString()) : "Thread got different environment instance: "
170: + id
171: + " (main thread: "
172: + environment.toString()
173: + ")";
174: }
175: }
176:
177: }
|