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 */
020package org.apache.mina.statemachine.context;
021
022import static org.junit.Assert.assertSame;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.junit.Test;
028
029/**
030 * Tests {@link AbstractStateContextLookup}.
031 *
032 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
033 */
034public class AbstractStateContextLookupTest {
035    @Test
036    public void testLookup() throws Exception {
037        Map<String, StateContext> map = new HashMap<String, StateContext>();
038        AbstractStateContextLookup lookup = new AbstractStateContextLookup(new DefaultStateContextFactory()) {
039            protected boolean supports(Class<?> c) {
040                return Map.class.isAssignableFrom(c);
041            }
042
043            @SuppressWarnings("unchecked")
044            protected StateContext lookup(Object eventArg) {
045                Map<String, StateContext> map = (Map<String, StateContext>) eventArg;
046                return map.get("context");
047            }
048
049            @SuppressWarnings("unchecked")
050            protected void store(Object eventArg, StateContext context) {
051                Map<String, StateContext> map = (Map<String, StateContext>) eventArg;
052                map.put("context", context);
053            }
054        };
055        Object[] args1 = new Object[] { new Object(), map, new Object() };
056        Object[] args2 = new Object[] { map, new Object() };
057        StateContext sc = lookup.lookup(args1);
058        assertSame(map.get("context"), sc);
059        assertSame(map.get("context"), lookup.lookup(args1));
060        assertSame(map.get("context"), lookup.lookup(args2));
061    }
062
063}