PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/notnoop/apns/internal/UtilitiesTest.java

http://github.com/notnoop/java-apns
Java | 87 lines | 38 code | 13 blank | 36 comment | 3 complexity | 4330a822b610aa00bca40b42da197036 MD5 | raw file
 1/*
 2 *  Copyright 2009, Mahmood Ali.
 3 *  All rights reserved.
 4 *
 5 *  Redistribution and use in source and binary forms, with or without
 6 *  modification, are permitted provided that the following conditions are
 7 *  met:
 8 *
 9 *    * Redistributions of source code must retain the above copyright
10 *      notice, this list of conditions and the following disclaimer.
11 *    * Redistributions in binary form must reproduce the above
12 *      copyright notice, this list of conditions and the following disclaimer
13 *      in the documentation and/or other materials provided with the
14 *      distribution.
15 *    * Neither the name of Mahmood Ali. nor the names of its
16 *      contributors may be used to endorse or promote products derived from
17 *      this software without specific prior written permission.
18 *
19 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31package com.notnoop.apns.internal;
32
33import org.junit.Assert;
34import org.junit.Test;
35
36public class UtilitiesTest {
37
38    @Test
39    public void testEncodeAndDecode() {
40        String encodedHex = "a1b2d4";
41
42        byte[] decoded = Utilities.decodeHex(encodedHex);
43        String encoded = Utilities.encodeHex(decoded);
44
45        Assert.assertEquals(encodedHex.toLowerCase(), encoded.toLowerCase());
46    }
47
48    @Test
49    public void testParsingBytes() {
50        Assert.assertEquals(0xFF00FF00, Utilities.parseBytes(0xFF, 0, 0xFF, 0));
51        Assert.assertEquals(0x00FF00FF, Utilities.parseBytes(0, 0xFF, 0, 0xFF));
52        Assert.assertEquals(0xDEADBEEF, Utilities.parseBytes(0xDE, 0xAD, 0xBE, 0xEF));
53        Assert.assertEquals(0x0EADBEEF, Utilities.parseBytes(0x0E, 0xAD, 0xBE, 0xEF));
54
55        Assert.assertTrue(Utilities.parseBytes(0xF0, 0,0,0) < 0);
56        Assert.assertTrue(Utilities.parseBytes(0x80, 0,0,0) < 0);
57        Assert.assertTrue(Utilities.parseBytes(0x70, 0,0,0) > 0);
58    }
59
60    @Test
61    public void testEncodingUTF8() {
62        String m = "esemény";
63
64        // See http://en.wikipedia.org/wiki/Unicode_equivalence#Example
65        //
66        // Oh the joy as different platforms choose to normalize Unicode differently ... but both are valid.
67        //
68        // This is intended to fix a problem under jdk 6, I was not able to reproduce it with jdk 7u51 on OSX Mavericks
69        // (Java seems to also use expected_NFC here).
70        byte[] expected_NFC = {
71                'e', 's', 'e', 'm', (byte)0x00C3, (byte)0x00A9, 'n', 'y'
72        };
73
74        byte[] expected_NFD = {
75                'e', 's', 'e', 'm', (byte)0x00cc, (byte)0x0081, (byte)0x0061, 'n', 'y'
76        };
77
78        final byte[] bytes = Utilities.toUTF8Bytes(m);
79
80        if (bytes.length == 8) {
81            Assert.assertArrayEquals(expected_NFC, bytes);
82        } else {
83            Assert.assertArrayEquals(expected_NFD, bytes);
84        }
85
86    }
87}