/src/test/java/com/notnoop/apns/utils/StringTruncationTest.java

http://github.com/notnoop/java-apns · Java · 89 lines · 48 code · 10 blank · 31 comment · 0 complexity · 469c2b10c487d212c4d1dcbb1f3ea83f 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.utils;
  32. import org.junit.Assert;
  33. import org.junit.experimental.theories.DataPoints;
  34. import org.junit.experimental.theories.Theories;
  35. import org.junit.experimental.theories.Theory;
  36. import org.junit.runner.RunWith;
  37. import com.notnoop.apns.internal.Utilities;
  38. // Test inspired by http://stackoverflow.com/questions/119328/how-do-i-truncate-a-java-string-to-fit-in-a-given-number-of-bytes-once-utf-8-enc
  39. @RunWith(Theories.class)
  40. public class StringTruncationTest {
  41. @DataPoints public static Object[][] dataPoints = {
  42. {"abcd", 0, 0},
  43. {"abcd", 1, 1},
  44. {"abcd", 2, 2},
  45. {"abcd", 3, 3},
  46. {"abcd", 4, 4},
  47. {"abcd", 5, 4},
  48. {"a\u0080b", 0, 0},
  49. {"a\u0080b", 1, 1},
  50. {"a\u0080b", 2, 1},
  51. {"a\u0080b", 3, 3},
  52. {"a\u0080b", 4, 4},
  53. {"a\u0080b", 5, 4},
  54. {"a\u0800b", 0, 0},
  55. {"a\u0800b", 1, 1},
  56. {"a\u0800b", 2, 1},
  57. {"a\u0800b", 3, 1},
  58. {"a\u0800b", 4, 4},
  59. {"a\u0800b", 5, 5},
  60. {"a\u0800b", 6, 5},
  61. };
  62. @DataPoints public static Object[][] surrogatePairs = {
  63. {"\uD834\uDD1E", 0, 0},
  64. {"\uD834\uDD1E", 1, 0},
  65. {"\uD834\uDD1E", 2, 0},
  66. {"\uD834\uDD1E", 3, 0},
  67. {"\uD834\uDD1E", 4, 4},
  68. {"\uD834\uDD1E", 5, 4}
  69. };
  70. @Theory
  71. public void truncateUTF8Properly(Object[] p) {
  72. String str = (String)p[0];
  73. int maxBytes = (Integer)p[1];
  74. int expectedBytes = (Integer)p[2];
  75. String result = Utilities.truncateWhenUTF8(str, maxBytes);
  76. byte[] utf8 = Utilities.toUTF8Bytes(result);
  77. Assert.assertEquals(expectedBytes, utf8.length);
  78. }
  79. }