TestUtils.java revision 20225d57609d6a5e482c088fdad60c29212d31a0
1/*
2 * Copyright (C) 2010 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.email;
18
19import java.nio.ByteBuffer;
20import java.nio.CharBuffer;
21
22import junit.framework.TestCase;
23
24/**
25 * Utility methods used only by tests.
26 */
27public class TestUtils extends TestCase /* It tests itself */ {
28    /** Shortcut to create byte array */
29    public static byte[] b(int... array) {
30        if (array == null) {
31            return null;
32        }
33        byte[] ret = new byte[array.length];
34        for (int i = 0; i < ret.length; i++) {
35            ret[i] = (byte) array[i];
36        }
37        return ret;
38    }
39
40    /** Converts a String from UTF-8 */
41    public static String fromUtf8(byte[] b) {
42        if (b == null) {
43            return null;
44        }
45        final CharBuffer cb = Utility.UTF_8.decode(ByteBuffer.wrap(b));
46        return new String(cb.array(), 0, cb.length());
47    }
48
49    public void testUtf8() {
50        assertNull(fromUtf8(null));
51        assertEquals("", fromUtf8(new byte[] {}));
52        assertEquals("a", fromUtf8(b('a')));
53        assertEquals("ABC", fromUtf8(b('A', 'B', 'C')));
54        assertEquals("\u65E5\u672C\u8A9E",
55                fromUtf8(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E)));
56    }
57}
58