GCloudNaturalLanguageUtil { public static String truncateToSize(String content, int size) { byte[] bytes = content.getBytes(StandardCharsets.UTF_8); if (bytes.length <= size) { return content; } bytes = Arrays.copyOf(bytes, size); int i = size - 1; while ((bytes[i] & 0x80) != 0) { i--; } if (i < (size - 1)) { bytes = Arrays.copyOf(bytes, i + 1); } return _truncateToWord(new String(bytes, StandardCharsets.UTF_8)); }  static String getDocumentPayload(String content, String type); static String getType(String mimeType); static String truncateToSize(String content, int size);  }
@Test public void testTruncateToSizeEmptyString() { Assert.assertEquals( StringPool.BLANK, GCloudNaturalLanguageUtil.truncateToSize( StringPool.BLANK, RandomTestUtil.randomInt())); }
@Test(expected = NullPointerException.class) public void testTruncateToSizeNullString() { GCloudNaturalLanguageUtil.truncateToSize( null, RandomTestUtil.randomInt()); }
@Test public void testTruncateToSizeSingleWordEqualToMax() { int size = _randomSize(); String text = RandomTestUtil.randomString(size); Assert.assertEquals( text, GCloudNaturalLanguageUtil.truncateToSize(text, size)); }
@Test public void testTruncateToSizeSingleWordGreaterThanMax() { int size = _randomSize(); Assert.assertEquals( StringPool.BLANK, GCloudNaturalLanguageUtil.truncateToSize( RandomTestUtil.randomString(size + 1), size)); }
@Test public void testTruncateToSizeSingleWordSmallerThanMax() { int size = _randomSize(); String text = RandomTestUtil.randomString(size - 1); Assert.assertEquals( text, GCloudNaturalLanguageUtil.truncateToSize(text, size)); }
@Test public void testTruncateToSizeTextGreaterThanMax() { int size = _randomSize(); Assert.assertEquals( StringPool.BLANK, GCloudNaturalLanguageUtil.truncateToSize( RandomTestUtil.randomString(size + 1) + StringPool.SPACE + RandomTestUtil.randomString(size + 1), size)); }
@Test public void testTruncateToSizeTextGreaterThanMaxWithTwoWordsSmallerThanSize() { int size = _randomSize(); String text = RandomTestUtil.randomString(size) + StringPool.SPACE + RandomTestUtil.randomString(size); Assert.assertEquals( text, GCloudNaturalLanguageUtil.truncateToSize( text + StringPool.SPACE + RandomTestUtil.randomString(size), text.length() + 1)); }
@Test public void testTruncateToSizeTextGreaterThanMaxWithWordSmallerThanSize() { int size = _randomSize(); String text = RandomTestUtil.randomString(size); Assert.assertEquals( text, GCloudNaturalLanguageUtil.truncateToSize( text + StringPool.SPACE + text, size + 1)); }
@Test public void testTruncateToSizeTextSmallerThanMax() { int size = _randomSize(); String text = RandomTestUtil.randomString(size / 2) + StringPool.SPACE + RandomTestUtil.randomString(size / 2); Assert.assertEquals( text, GCloudNaturalLanguageUtil.truncateToSize(text, size + 1)); }
@Test public void testTruncateToSizeUnicodeTextEqualToMax() { String text = "中國哲學書電子化計劃 中國哲學書電子化計劃 中國哲學書電子化計劃"; byte[] bytes = text.getBytes(StandardCharsets.UTF_8); Assert.assertEquals( text, GCloudNaturalLanguageUtil.truncateToSize(text, bytes.length)); }
@Test public void testTruncateToSizeUnicodeTextGreaterThanMax() { String text = "中國哲學書電子化計劃 中國哲學書電子化計劃 中國哲學書電子化計劃"; byte[] bytes = text.getBytes(StandardCharsets.UTF_8); Assert.assertEquals( text.substring(0, text.lastIndexOf(CharPool.SPACE)), GCloudNaturalLanguageUtil.truncateToSize(text, bytes.length - 1)); }