1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.prefs.tests.java.util.prefs;
18
19import java.util.HashMap;
20import java.util.Map;
21import java.util.Properties;
22import java.util.Set;
23import java.util.prefs.AbstractPreferences;
24import java.util.prefs.BackingStoreException;
25import java.util.prefs.Preferences;
26
27public class MockAbstractPreferences extends AbstractPreferences {
28    static final int NORMAL = 0;
29
30    static final int backingException = 1;
31
32    static final int runtimeException = 2;
33
34    static final int returnNull = 3;
35
36    int result = NORMAL;
37
38    Properties attr = new Properties();
39
40    Map<String, MockAbstractPreferences> childs = new HashMap<String, MockAbstractPreferences>();
41
42    private int flushedTimes;
43
44    private int syncTimes;
45
46    protected MockAbstractPreferences(AbstractPreferences parent, String name) {
47        this(parent, name, false);
48
49    }
50
51    protected MockAbstractPreferences(AbstractPreferences parent, String name,
52            boolean newNode) {
53        super(parent, name);
54        super.newNode = newNode;
55        if (parent instanceof MockAbstractPreferences) {
56            ((MockAbstractPreferences) parent).addChild(this);
57        }
58    }
59
60    public int getFlushedTimes() {
61        return flushedTimes;
62    }
63
64    public void resetFlushedTimes() {
65        flushedTimes = 0;
66    }
67
68    public int getSyncTimes() {
69        return syncTimes;
70    }
71
72    public void resetSyncTimes() {
73        syncTimes = 0;
74    }
75
76    private void addChild(MockAbstractPreferences c) {
77        childs.put(c.name(), c);
78    }
79
80    public void setResult(int r) {
81        result = r;
82    }
83
84    public Object lock() {
85        return lock;
86    }
87
88    @Override
89    protected String[] childrenNamesSpi() throws BackingStoreException {
90        checkException();
91        if (result == returnNull)
92            return null;
93        String[] r = new String[childs.size()];
94        childs.keySet().toArray(r);
95        return r;
96    }
97
98    private void checkException() throws BackingStoreException {
99        switch (result) {
100        case NORMAL:
101            return;
102        case backingException:
103            throw new BackingStoreException("test");
104        case runtimeException:
105            throw new MockRuntimeException("test");
106        }
107    }
108
109    public AbstractPreferences publicChildSpi(String name) {
110        return childSpi(name);
111    }
112
113    @Override
114    protected AbstractPreferences childSpi(String name) {
115        try {
116            checkException();
117        } catch (BackingStoreException e) {
118        }
119        if (result == returnNull)
120            return null;
121        AbstractPreferences r = childs.get(name);
122        if (r == null) {
123            r = new MockAbstractPreferences(this, name, true);
124
125        }
126        return r;
127    }
128
129    @Override
130    protected void flushSpi() throws BackingStoreException {
131        checkException();
132        flushedTimes++;
133    }
134
135    @Override
136    protected String getSpi(String key) {
137        try {
138            checkException();
139        } catch (BackingStoreException e) {
140        }
141        if (null == key) {
142            return null;
143        }
144        return result == returnNull ? null : attr.getProperty(key);
145    }
146
147    @Override
148    protected String[] keysSpi() throws BackingStoreException {
149        checkException();
150        Set<Object> keys = attr.keySet();
151        String[] results = new String[keys.size()];
152        keys.toArray(results);
153        return result == returnNull ? null : results;
154    }
155
156    @Override
157    protected void putSpi(String name, String value) {
158        try {
159            checkException();
160        } catch (BackingStoreException e) {
161        }
162        if (name == null || value == null) {
163            return;
164        }
165        attr.put(name, value);
166    }
167
168    @Override
169    protected void removeNodeSpi() throws BackingStoreException {
170        checkException();
171        Preferences p = parent();
172        if (p instanceof MockAbstractPreferences) {
173            ((MockAbstractPreferences) p).childs.remove(name());
174        } else {
175            String[] children = p.childrenNames();
176            for (String child : children) {
177                p.node(child).removeNode();
178            }
179        }
180    }
181
182    @Override
183    protected void removeSpi(String key) {
184        try {
185            checkException();
186        } catch (BackingStoreException e) {
187        }
188        if (null == key) {
189            return;
190        }
191        attr.remove(key);
192    }
193
194    @Override
195    protected void syncSpi() throws BackingStoreException {
196        checkException();
197        syncTimes++;
198    }
199
200    public boolean getNewNode() {
201        return newNode;
202    }
203
204    public Object getLock() {
205        return lock;
206    }
207
208    public void protectedAbstractMethod() {
209        try {
210            childrenNamesSpi();
211        } catch (BackingStoreException e) {
212        }
213        childSpi("mock");
214        try {
215            flushSpi();
216        } catch (BackingStoreException e1) {
217        }
218        getSpi(null);
219        isRemoved();
220        try {
221            keysSpi();
222        } catch (BackingStoreException e2) {
223        }
224        putSpi(null, null);
225        try {
226            removeNodeSpi();
227        } catch (BackingStoreException e3) {
228        }
229        removeSpi(null);
230        try {
231            syncSpi();
232        } catch (BackingStoreException e4) {
233        }
234    }
235
236    public boolean isRemovedImpl() {
237        return super.isRemoved();
238    }
239
240    public AbstractPreferences getChildImpl(String name)
241    throws BackingStoreException {
242        return super.getChild(name);
243    }
244
245    public AbstractPreferences[] cachedChildrenImpl() {
246        return super.cachedChildren();
247    }
248
249}
250
251class MockRuntimeException extends RuntimeException {
252
253    private static final long serialVersionUID = 1L;
254
255    public MockRuntimeException(String s) {
256        super(s);
257    }
258
259    public MockRuntimeException() {
260        super();
261    }
262}
263
264