|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.axis2.context; |
| 21 | + |
| 22 | +import junit.framework.TestCase; |
| 23 | + |
| 24 | +import org.apache.axis2.description.AxisService; |
| 25 | +import org.apache.axis2.description.AxisServiceGroup; |
| 26 | +import org.apache.axis2.engine.AxisConfiguration; |
| 27 | + |
| 28 | +/** |
| 29 | + * Covers the AXIS2-5788 fix: ConfigurationContext.getServiceGroupContext must |
| 30 | + * no longer unconditionally touch the returned context -- callers must be |
| 31 | + * able to opt out of the touch via the new |
| 32 | + * {@code getServiceGroupContext(String, boolean)} overload. |
| 33 | + */ |
| 34 | +public class ConfigurationContextServiceGroupLookupTest extends TestCase { |
| 35 | + |
| 36 | + private static final String SOAP_SESSION_ID = "soap-session-sgc"; |
| 37 | + private static final String APP_SESSION_SG_NAME = "app-session-sg"; |
| 38 | + |
| 39 | + private ConfigurationContext configurationContext; |
| 40 | + private ServiceGroupContext soapSessionContext; |
| 41 | + private ServiceGroupContext appSessionContext; |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void setUp() throws Exception { |
| 45 | + AxisConfiguration axisConfiguration = new AxisConfiguration(); |
| 46 | + |
| 47 | + // A SOAP-session-scoped group (ends up in serviceGroupContextMap). |
| 48 | + AxisServiceGroup soapSessionGroup = new AxisServiceGroup(axisConfiguration); |
| 49 | + soapSessionGroup.setServiceGroupName("soap-session-group"); |
| 50 | + soapSessionGroup.addService(new AxisService("soap-svc")); |
| 51 | + axisConfiguration.addServiceGroup(soapSessionGroup); |
| 52 | + |
| 53 | + // An application-session-scoped group (ends up in |
| 54 | + // applicationSessionServiceGroupContexts). |
| 55 | + AxisServiceGroup appSessionGroup = new AxisServiceGroup(axisConfiguration); |
| 56 | + appSessionGroup.setServiceGroupName(APP_SESSION_SG_NAME); |
| 57 | + appSessionGroup.addService(new AxisService("app-svc")); |
| 58 | + axisConfiguration.addServiceGroup(appSessionGroup); |
| 59 | + |
| 60 | + configurationContext = new ConfigurationContext(axisConfiguration); |
| 61 | + |
| 62 | + soapSessionContext = new ServiceGroupContext(configurationContext, soapSessionGroup); |
| 63 | + soapSessionContext.setId(SOAP_SESSION_ID); |
| 64 | + configurationContext.addServiceGroupContextIntoSoapSessionTable(soapSessionContext); |
| 65 | + |
| 66 | + appSessionContext = new ServiceGroupContext(configurationContext, appSessionGroup); |
| 67 | + configurationContext.addServiceGroupContextIntoApplicationScopeTable(appSessionContext); |
| 68 | + } |
| 69 | + |
| 70 | + /** Null id must short-circuit to null without mutation or NPE. */ |
| 71 | + public void testNullIdReturnsNull() { |
| 72 | + assertNull(configurationContext.getServiceGroupContext(null)); |
| 73 | + assertNull(configurationContext.getServiceGroupContext(null, true)); |
| 74 | + assertNull(configurationContext.getServiceGroupContext(null, false)); |
| 75 | + } |
| 76 | + |
| 77 | + /** Unknown id returns null from both overloads. */ |
| 78 | + public void testUnknownIdReturnsNull() { |
| 79 | + assertNull(configurationContext.getServiceGroupContext("no-such-id")); |
| 80 | + assertNull(configurationContext.getServiceGroupContext("no-such-id", true)); |
| 81 | + assertNull(configurationContext.getServiceGroupContext("no-such-id", false)); |
| 82 | + } |
| 83 | + |
| 84 | + /** Legacy single-arg lookup must still touch (back-compat). */ |
| 85 | + public void testSingleArgLookupTouchesSoapSessionContext() { |
| 86 | + soapSessionContext.setLastTouchedTime(0L); |
| 87 | + |
| 88 | + ServiceGroupContext found = configurationContext.getServiceGroupContext(SOAP_SESSION_ID); |
| 89 | + |
| 90 | + assertSame(soapSessionContext, found); |
| 91 | + assertTrue("single-arg lookup must update lastTouchedTime", |
| 92 | + found.getLastTouchedTime() > 0L); |
| 93 | + } |
| 94 | + |
| 95 | + /** Two-arg lookup with touch=true matches the legacy behaviour. */ |
| 96 | + public void testTwoArgLookupWithTouchTrueTouchesSoapSessionContext() { |
| 97 | + soapSessionContext.setLastTouchedTime(0L); |
| 98 | + |
| 99 | + ServiceGroupContext found = |
| 100 | + configurationContext.getServiceGroupContext(SOAP_SESSION_ID, true); |
| 101 | + |
| 102 | + assertSame(soapSessionContext, found); |
| 103 | + assertTrue("touch=true must update lastTouchedTime", |
| 104 | + found.getLastTouchedTime() > 0L); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * The AXIS2-5788 fix proper: touch=false must NOT mutate lastTouchedTime |
| 109 | + * on the returned context. |
| 110 | + */ |
| 111 | + public void testTwoArgLookupWithTouchFalseDoesNotTouchSoapSessionContext() { |
| 112 | + soapSessionContext.setLastTouchedTime(0L); |
| 113 | + |
| 114 | + ServiceGroupContext found = |
| 115 | + configurationContext.getServiceGroupContext(SOAP_SESSION_ID, false); |
| 116 | + |
| 117 | + assertSame(soapSessionContext, found); |
| 118 | + assertEquals("touch=false must leave lastTouchedTime unchanged", |
| 119 | + 0L, found.getLastTouchedTime()); |
| 120 | + } |
| 121 | + |
| 122 | + /** The same no-touch contract must hold for application-session contexts. */ |
| 123 | + public void testTwoArgLookupWithTouchFalseDoesNotTouchAppSessionContext() { |
| 124 | + appSessionContext.setLastTouchedTime(0L); |
| 125 | + |
| 126 | + ServiceGroupContext found = |
| 127 | + configurationContext.getServiceGroupContext(APP_SESSION_SG_NAME, false); |
| 128 | + |
| 129 | + assertSame(appSessionContext, found); |
| 130 | + assertEquals("touch=false must leave lastTouchedTime unchanged", |
| 131 | + 0L, found.getLastTouchedTime()); |
| 132 | + } |
| 133 | + |
| 134 | + /** And the legacy-touching contract must also hold for app-session contexts. */ |
| 135 | + public void testSingleArgLookupTouchesAppSessionContext() { |
| 136 | + appSessionContext.setLastTouchedTime(0L); |
| 137 | + |
| 138 | + ServiceGroupContext found = |
| 139 | + configurationContext.getServiceGroupContext(APP_SESSION_SG_NAME); |
| 140 | + |
| 141 | + assertSame(appSessionContext, found); |
| 142 | + assertTrue("single-arg lookup must update lastTouchedTime for app-session SGC", |
| 143 | + found.getLastTouchedTime() > 0L); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Calling the no-touch variant repeatedly must continue to leave |
| 148 | + * lastTouchedTime alone -- guards against a future refactor that |
| 149 | + * re-introduces the observer effect for only the first call. |
| 150 | + */ |
| 151 | + public void testRepeatedNoTouchLookupsNeverMutate() { |
| 152 | + soapSessionContext.setLastTouchedTime(0L); |
| 153 | + |
| 154 | + for (int i = 0; i < 5; i++) { |
| 155 | + ServiceGroupContext found = |
| 156 | + configurationContext.getServiceGroupContext(SOAP_SESSION_ID, false); |
| 157 | + assertSame(soapSessionContext, found); |
| 158 | + assertEquals("iteration " + i + ": touch=false must stay read-only", |
| 159 | + 0L, found.getLastTouchedTime()); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments