DpmTestUtils.java revision d932f7689d799b07a6776bc7c59749f672528239
1/*
2 * Copyright (C) 2015 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 com.android.server.devicepolicy;
18
19import android.os.FileUtils;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.Log;
23import android.util.Printer;
24
25import org.junit.Assert;
26
27import java.io.File;
28import java.util.List;
29
30public class DpmTestUtils {
31    private DpmTestUtils() {
32    }
33
34    public static void clearDir(File dir) {
35        if (dir.exists()) {
36            Assert.assertTrue("failed to delete dir", FileUtils.deleteContents(dir));
37        }
38        dir.mkdirs();
39        Log.i(DpmTestBase.TAG, "Created " + dir);
40    }
41
42    public static int getListSizeAllowingNull(List<?> list) {
43        return list == null ? 0 : list.size();
44    }
45
46    public static <T extends Parcelable> T cloneParcelable(T source) {
47        Parcel p = Parcel.obtain();
48        p.writeParcelable(source, 0);
49        p.setDataPosition(0);
50        final T clone = p.readParcelable(DpmTestUtils.class.getClassLoader());
51        p.recycle();
52        return clone;
53    }
54
55    public static Printer LOG_PRINTER = new Printer() {
56        @Override
57        public void println(String x) {
58            Log.i(DpmTestBase.TAG, x);
59        }
60    };
61}
62