blob: 46c43a7eed7fc5ee3d028f82de5ca8f013182b78 [file] [log] [blame]
Jesse Wilsone3424552011-05-16 18:30:05 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 tests.io;
18
19import java.lang.reflect.InvocationHandler;
20import java.lang.reflect.InvocationTargetException;
21import java.lang.reflect.Method;
22import java.lang.reflect.Proxy;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27import libcore.io.ErrnoException;
28import libcore.io.Libcore;
29import libcore.io.Os;
30import libcore.io.OsConstants;
31
32/**
33 * A mocking interceptor that wraps another {@link Os} to add faults. This can
34 * be useful to test otherwise hard-to-test scenarios such as a full disk.
35 */
36public final class MockOs {
37 private final InheritableThreadLocal<Map<String, List<Fault>>> faults
38 = new InheritableThreadLocal<Map<String, List<Fault>>>() {
39 @Override protected Map<String, List<Fault>> initialValue() {
40 return new HashMap<String, List<Fault>>();
41 }
42 };
43
44 private final InvocationHandler invocationHandler = new InvocationHandler() {
45 @Override public Object invoke(Object proxy, Method method, Object[] args)
46 throws Throwable {
47 Map<String, List<Fault>> threadFaults = faults.get();
48 List<Fault> methodFaults = threadFaults.get(method.getName());
49 if (methodFaults == null || methodFaults.isEmpty()) {
50 try {
51 return method.invoke(delegate, args);
52 } catch (InvocationTargetException e) {
53 throw e.getCause();
54 }
55 }
56
57 Fault fault = methodFaults.remove(0);
58 fault.trigger(method);
59 return null;
60 }
61 };
62
63 private final Os mockOs = (Os) Proxy.newProxyInstance(MockOs.class.getClassLoader(),
64 new Class[] { Os.class }, invocationHandler);
65 private Os delegate;
66
67 public void install() {
68 if (delegate != null) {
Jesse Wilson6f778cc2011-05-17 16:02:01 -070069 throw new IllegalStateException("MockOs already installed!");
Jesse Wilsone3424552011-05-16 18:30:05 -070070 }
71 delegate = Libcore.os;
72 Libcore.os = mockOs;
73 }
74
75 public void uninstall() {
76 if (delegate == null) {
Jesse Wilson6f778cc2011-05-17 16:02:01 -070077 throw new IllegalStateException("MockOs not installed!");
Jesse Wilsone3424552011-05-16 18:30:05 -070078 }
79 Libcore.os = delegate;
80 }
81
82 public void enqueueFault(String methodName) {
83 enqueueFault(methodName, OsConstants.EIO);
84 }
85
86 public void enqueueFault(String methodName, int errno) {
87 Map<String, List<Fault>> threadFaults = faults.get();
88 List<Fault> methodFaults = threadFaults.get(methodName);
89 if (methodFaults == null) {
90 methodFaults = new ArrayList<Fault>();
91 threadFaults.put(methodName, methodFaults);
92 }
93 methodFaults.add(new Fault(errno));
94 }
95
96 private static class Fault {
97 private final int errno;
98 public Fault(int errno) {
99 this.errno = errno;
100 }
101
102 public void trigger(Method method) {
103 throw new ErrnoException(method.getName(), errno);
104 }
105 }
106}