View Javadoc

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  
21  package org.apache.james.test.mock.avalon;
22  
23  import org.apache.avalon.cornerstone.services.store.Store;
24  import org.apache.avalon.framework.configuration.Configuration;
25  import org.apache.avalon.framework.configuration.ConfigurationException;
26  import org.apache.avalon.framework.service.ServiceException;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  public class MockStore implements Store {
32  
33      Map m_storedObjectMap = new HashMap();
34  
35      public void add(Object key, Object obj) {
36          m_storedObjectMap.put(key, obj);
37      }
38      
39      public Object select(Object object) throws ServiceException {
40          Object result = get(object);
41          return result;
42      }
43  
44      private Object get(Object object) {
45          return m_storedObjectMap.get(extractKeyObject(object));
46      }
47  
48      private Object extractKeyObject(Object object) {
49          if (object instanceof Configuration) {
50              Configuration repConf = (Configuration) object;
51              try {
52                  String attribute = repConf.getAttribute("destinationURL");
53                  String[] strings = attribute.split("/");
54                  if (strings.length > 0) {
55                      object = strings[strings.length-1];
56                  }
57              } catch (ConfigurationException e) {
58                  throw new RuntimeException("test configuration setup failed");
59              }
60              
61          }
62          return object;
63      }
64  
65      public boolean isSelectable(Object object) {
66          return get(object) != null;
67      }
68  
69      public void release(Object object) {
70          //trivial implementation
71      }
72  }