KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > mock > avalon > MockStore


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