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.mina.statemachine.context;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.mina.statemachine.context.AbstractStateContextLookup;
26  import org.apache.mina.statemachine.context.DefaultStateContextFactory;
27  import org.apache.mina.statemachine.context.StateContext;
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * Tests {@link AbstractStateContextLookup}.
33   *
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 589475 $, $Date: 2007-10-29 04:12:59 +0100 (Mon, 29 Oct 2007) $
36   */
37  public class AbstractStateContextLookupTest extends TestCase {
38  
39      public void testLookup() throws Exception {
40          Map<String, StateContext> map = new HashMap<String, StateContext>();
41          AbstractStateContextLookup lookup = new AbstractStateContextLookup(
42                                               new DefaultStateContextFactory()) {
43              protected boolean supports(Class<?> c) {
44                  return Map.class.isAssignableFrom(c);
45              }
46              @SuppressWarnings("unchecked")
47              protected StateContext lookup(Object eventArg) {
48                  Map<String, StateContext> map = (Map<String, StateContext>) eventArg;
49                  return map.get("context");
50              }
51              @SuppressWarnings("unchecked")
52              protected void store(Object eventArg, StateContext context) {
53                  Map<String, StateContext> map = (Map<String, StateContext>) eventArg;
54                  map.put("context", context);
55              }
56          };
57          Object[] args1 = new Object[] {new Object(), map, new Object()};
58          Object[] args2 = new Object[] {map, new Object()};
59          StateContext sc = lookup.lookup(args1);
60          assertSame(map.get("context"), sc);
61          assertSame(map.get("context"), lookup.lookup(args1));
62          assertSame(map.get("context"), lookup.lookup(args2));
63      }
64      
65  }