001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import junit.framework.TestCase;
023: import org.apache.axis2.AxisFault;
024: import org.apache.axis2.context.ConfigurationContext;
025: import org.apache.axis2.context.MessageContext;
026: import org.apache.axis2.engine.AxisConfiguration;
027:
028: public class ThreadContextMigratorTest extends TestCase {
029: private static String TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID = "Test-ThreadContextMigrator-List";
030: private MessageContext messageContext;
031:
032: public void setUp() {
033: messageContext = new ConfigurationContext(
034: new AxisConfiguration()).createMessageContext();
035: }
036:
037: public void testEmptyMigratorStructure() throws Exception {
038: ThreadContextMigratorUtil.performMigrationToThread(
039: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
040: ThreadContextMigratorUtil.performMigrationToContext(
041: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
042: ThreadContextMigratorUtil.performThreadCleanup(
043: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
044: ThreadContextMigratorUtil.performContextCleanup(
045: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
046: }
047:
048: public void testMigration() throws Exception {
049: TestMigrator testMigrator1 = new TestMigrator();
050: TestMigrator testMigrator2 = new TestMigrator();
051: ThreadContextMigratorUtil.addThreadContextMigrator(
052: messageContext.getConfigurationContext(),
053: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, testMigrator1);
054: ThreadContextMigratorUtil.addThreadContextMigrator(
055: messageContext.getConfigurationContext(),
056: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, testMigrator2);
057: ThreadContextMigratorUtil.performMigrationToThread(
058: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
059: assertTrue(testMigrator1.migratedToThread);
060: assertTrue(testMigrator2.migratedToThread);
061: ThreadContextMigratorUtil.performMigrationToContext(
062: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
063: assertTrue(testMigrator1.migratedToContext);
064: assertTrue(testMigrator2.migratedToContext);
065: ThreadContextMigratorUtil.performThreadCleanup(
066: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
067: assertTrue(testMigrator1.cleanedThread);
068: assertTrue(testMigrator2.cleanedThread);
069: ThreadContextMigratorUtil.performContextCleanup(
070: TEST_THREAD_CONTEXT_MIGRATOR_LIST_ID, messageContext);
071: assertTrue(testMigrator1.cleanedContext);
072: assertTrue(testMigrator2.cleanedContext);
073: }
074:
075: class TestMigrator implements ThreadContextMigrator {
076: boolean migratedToThread;
077: boolean cleanedThread;
078: boolean migratedToContext;
079: boolean cleanedContext;
080:
081: public void migrateContextToThread(MessageContext messageContext)
082: throws AxisFault {
083: migratedToThread = true;
084: }
085:
086: public void cleanupThread(MessageContext messageContext) {
087: cleanedThread = true;
088: }
089:
090: public void migrateThreadToContext(MessageContext messageContext)
091: throws AxisFault {
092: migratedToContext = true;
093: }
094:
095: public void cleanupContext(MessageContext messageContext) {
096: cleanedContext = true;
097: }
098:
099: }
100: }
|