/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. */
  31. package com.notnoop.apns.internal;
  32. import org.junit.Assert;
  33. import org.junit.Test;
  34. public class UtilitiesTest {
  35. @Test
  36. public void testEncodeAndDecode() {
  37. String encodedHex = "a1b2d4";
  38. byte[] decoded = Utilities.decodeHex(encodedHex);
  39. String encoded = Utilities.encodeHex(decoded);
  40. Assert.assertEquals(encodedHex.toLowerCase(), encoded.toLowerCase());
  41. }
  42. @Test
  43. public void testParsingBytes() {
  44. Assert.assertEquals(0xFF00FF00, Utilities.parseBytes(0xFF, 0, 0xFF, 0));
  45. Assert.assertEquals(0x00FF00FF, Utilities.parseBytes(0, 0xFF, 0, 0xFF));
  46. Assert.assertEquals(0xDEADBEEF, Utilities.parseBytes(0xDE, 0xAD, 0xBE, 0xEF));
  47. Assert.assertEquals(0x0EADBEEF, Utilities.parseBytes(0x0E, 0xAD, 0xBE, 0xEF));
  48. Assert.assertTrue(Utilities.parseBytes(0xF0, 0,0,0) < 0);
  49. Assert.assertTrue(Utilities.parseBytes(0x80, 0,0,0) < 0);
  50. Assert.assertTrue(Utilities.parseBytes(0x70, 0,0,0) > 0);
  51. }
  52. @Test
  53. public void testEncodingUTF8() {
  54. String m = "esemény";
  55. // See http://en.wikipedia.org/wiki/Unicode_equivalence#Example
  56. //
  57. // Oh the joy as different platforms choose to normalize Unicode differently ... but both are valid.
  58. //
  59. // This is intended to fix a problem under jdk 6, I was not able to reproduce it with jdk 7u51 on OSX Mavericks
  60. // (Java seems to also use expected_NFC here).
  61. byte[] expected_NFC = {
  62. 'e', 's', 'e', 'm', (byte)0x00C3, (byte)0x00A9, 'n', 'y'
  63. };
  64. byte[] expected_NFD = {
  65. 'e', 's', 'e', 'm', (byte)0x00cc, (byte)0x0081, (byte)0x0061, 'n', 'y'
  66. };
  67. final byte[] bytes = Utilities.toUTF8Bytes(m);
  68. if (bytes.length == 8) {
  69. Assert.assertArrayEquals(expected_NFC, bytes);
  70. } else {
  71. Assert.assertArrayEquals(expected_NFD, bytes);
  72. }
  73. }
  74. }