Jesse Wilson | e342455 | 2011-05-16 18:30:05 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package tests.io; |
| 18 | |
| 19 | import java.lang.reflect.InvocationHandler; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.lang.reflect.Proxy; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import libcore.io.ErrnoException; |
| 28 | import libcore.io.Libcore; |
| 29 | import libcore.io.Os; |
| 30 | import 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 | */ |
| 36 | public 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 Wilson | 6f778cc | 2011-05-17 16:02:01 -0700 | [diff] [blame] | 69 | throw new IllegalStateException("MockOs already installed!"); |
Jesse Wilson | e342455 | 2011-05-16 18:30:05 -0700 | [diff] [blame] | 70 | } |
| 71 | delegate = Libcore.os; |
| 72 | Libcore.os = mockOs; |
| 73 | } |
| 74 | |
| 75 | public void uninstall() { |
| 76 | if (delegate == null) { |
Jesse Wilson | 6f778cc | 2011-05-17 16:02:01 -0700 | [diff] [blame] | 77 | throw new IllegalStateException("MockOs not installed!"); |
Jesse Wilson | e342455 | 2011-05-16 18:30:05 -0700 | [diff] [blame] | 78 | } |
| 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 | } |