PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/external/apache-harmony/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/StringTest.java

https://gitlab.com/brian0218/rk3288_r-box_android4.4.2_sdk
Java | 745 lines | 533 code | 100 blank | 112 comment | 2 complexity | e21371327bcbff0c3f7db48172c08e5d MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. package org.apache.harmony.luni.tests.java.lang;
  18. import java.io.UnsupportedEncodingException;
  19. import java.lang.reflect.Constructor;
  20. import java.nio.charset.Charset;
  21. import java.util.Arrays;
  22. import java.util.SortedMap;
  23. import junit.framework.TestCase;
  24. public class StringTest extends TestCase {
  25. private static final Constructor<String> UNSAFE_CONSTRUCTOR;
  26. static {
  27. Constructor<String> uc;
  28. try {
  29. uc = String.class.getDeclaredConstructor(new Class[] { int.class,
  30. int.class, char[].class });
  31. uc.setAccessible(true);
  32. } catch (Exception e) {
  33. uc = null;
  34. }
  35. UNSAFE_CONSTRUCTOR = uc;
  36. }
  37. private static String newString(int start, int len, char[] data) throws Exception {
  38. if (UNSAFE_CONSTRUCTOR == null) {
  39. return new String(data, start, len);
  40. }
  41. return UNSAFE_CONSTRUCTOR.newInstance(Integer.valueOf(start), Integer.valueOf(len),
  42. data);
  43. }
  44. /**
  45. * @tests java.lang.String#String()
  46. */
  47. public void test_Constructor() {
  48. assertEquals("Created incorrect string", "", new String());
  49. }
  50. /**
  51. * @tests java.lang.String#String(byte[])
  52. */
  53. public void test_Constructor$B() {
  54. assertEquals("Failed to create string", "HelloWorld", new String(
  55. "HelloWorld".getBytes()));
  56. }
  57. /**
  58. * @tests java.lang.String#String(byte[], int)
  59. */
  60. @SuppressWarnings("deprecation")
  61. public void test_Constructor$BI() {
  62. String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0);
  63. assertEquals("Incorrect string returned: " + s, "ABCDE", s);
  64. s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1);
  65. assertFalse("Did not use nonzero hibyte", s.equals("ABCDE"));
  66. }
  67. /**
  68. * @tests java.lang.String#String(byte[], int, int)
  69. */
  70. public void test_Constructor$BII() {
  71. byte[] hwba = "HelloWorld".getBytes();
  72. assertEquals("Failed to create string", "HelloWorld", new String(hwba,
  73. 0, hwba.length));
  74. try {
  75. new String(new byte[0], 0, Integer.MAX_VALUE);
  76. fail("No IndexOutOfBoundsException");
  77. } catch (IndexOutOfBoundsException e) {
  78. }
  79. }
  80. /**
  81. * @tests java.lang.String#String(byte[], int, int, int)
  82. */
  83. @SuppressWarnings("deprecation")
  84. public void test_Constructor$BIII() {
  85. String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 1, 3);
  86. assertEquals("Incorrect string returned: " + s, "BCD", s);
  87. s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1, 0, 5);
  88. assertFalse("Did not use nonzero hibyte", s.equals("ABCDE"));
  89. }
  90. /**
  91. * @tests java.lang.String#String(byte[], int, int, java.lang.String)
  92. */
  93. public void test_Constructor$BIILjava_lang_String() throws Exception {
  94. String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "8859_1");
  95. assertEquals("Incorrect string returned: " + s, "ABCDE", s);
  96. try {
  97. new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "");
  98. fail("Should throw UnsupportedEncodingException");
  99. } catch (UnsupportedEncodingException e) {
  100. //expected
  101. }
  102. }
  103. /**
  104. * @tests java.lang.String#String(byte[], java.lang.String)
  105. */
  106. public void test_Constructor$BLjava_lang_String() throws Exception {
  107. String s = new String(new byte[] { 65, 66, 67, 68, 69 }, "8859_1");
  108. assertEquals("Incorrect string returned: " + s, "ABCDE", s);
  109. }
  110. /**
  111. * @tests java.lang.String#String(char[])
  112. */
  113. public void test_Constructor$C() {
  114. assertEquals("Failed Constructor test", "World", new String(new char[] {
  115. 'W', 'o', 'r', 'l', 'd' }));
  116. }
  117. /**
  118. * @tests java.lang.String#String(char[], int, int)
  119. */
  120. public void test_Constructor$CII() throws Exception {
  121. char[] buf = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
  122. String s = new String(buf, 0, buf.length);
  123. assertEquals("Incorrect string created", "HelloWorld", s);
  124. try {
  125. new String(new char[0], 0, Integer.MAX_VALUE);
  126. fail("No IndexOutOfBoundsException");
  127. } catch (IndexOutOfBoundsException e) {
  128. }
  129. }
  130. /**
  131. * @tests java.lang.String#String(java.lang.String)
  132. */
  133. public void test_ConstructorLjava_lang_String() {
  134. String s = new String("Hello World");
  135. assertEquals("Failed to construct correct string", "Hello World", s);
  136. }
  137. /**
  138. * @tests java.lang.String#String(java.lang.StringBuffer)
  139. */
  140. public void test_ConstructorLjava_lang_StringBuffer() {
  141. StringBuffer sb = new StringBuffer();
  142. sb.append("HelloWorld");
  143. assertEquals("Created incorrect string", "HelloWorld", new String(sb));
  144. }
  145. /**
  146. * @tests java.lang.String#String(java.lang.StringBuilder)
  147. */
  148. public void test_ConstructorLjava_lang_StringBuilder() {
  149. StringBuilder sb = new StringBuilder(32);
  150. sb.append("HelloWorld");
  151. assertEquals("HelloWorld", new String(sb));
  152. try {
  153. new String((StringBuilder) null);
  154. fail("No NPE");
  155. } catch (NullPointerException e) {
  156. }
  157. }
  158. /**
  159. * @tests java.lang.String#String(int[],int,int)
  160. */
  161. public void test_Constructor$III() {
  162. assertEquals("HelloWorld", new String(new int[] { 'H', 'e', 'l', 'l',
  163. 'o', 'W', 'o', 'r', 'l', 'd' }, 0, 10));
  164. assertEquals("Hello", new String(new int[] { 'H', 'e', 'l', 'l', 'o',
  165. 'W', 'o', 'r', 'l', 'd' }, 0, 5));
  166. assertEquals("World", new String(new int[] { 'H', 'e', 'l', 'l', 'o',
  167. 'W', 'o', 'r', 'l', 'd' }, 5, 5));
  168. assertEquals("", new String(new int[] { 'H', 'e', 'l', 'l', 'o', 'W',
  169. 'o', 'r', 'l', 'd' }, 5, 0));
  170. assertEquals("\uD800\uDC00", new String(new int[] { 0x010000 }, 0, 1));
  171. assertEquals("\uD800\uDC00a\uDBFF\uDFFF", new String(new int[] {
  172. 0x010000, 'a', 0x010FFFF }, 0, 3));
  173. try {
  174. new String((int[]) null, 0, 1);
  175. fail("No NPE");
  176. } catch (NullPointerException e) {
  177. }
  178. try {
  179. new String(new int[] { 'a', 'b' }, -1, 2);
  180. fail("No IOOBE, negative offset");
  181. } catch (IndexOutOfBoundsException e) {
  182. }
  183. try {
  184. new String(new int[] { 'a', 'b' }, 0, -1);
  185. fail("No IOOBE, negative count");
  186. } catch (IndexOutOfBoundsException e) {
  187. }
  188. try {
  189. new String(new int[] { 'a', 'b' }, 0, -1);
  190. fail("No IOOBE, negative count");
  191. } catch (IndexOutOfBoundsException e) {
  192. }
  193. try {
  194. new String(new int[] { 'a', 'b' }, 0, 3);
  195. fail("No IOOBE, too large");
  196. } catch (IndexOutOfBoundsException e) {
  197. }
  198. }
  199. /**
  200. * @tests java.lang.String#contentEquals(CharSequence)
  201. */
  202. public void test_contentEqualsLjava_lang_CharSequence() throws Exception {
  203. String s = "abc";
  204. assertTrue(s.contentEquals((CharSequence) new StringBuffer("abc")));
  205. assertFalse(s.contentEquals((CharSequence) new StringBuffer("def")));
  206. assertFalse(s.contentEquals((CharSequence) new StringBuffer("ghij")));
  207. s = newString(1, 3, "_abc_".toCharArray());
  208. assertTrue(s.contentEquals((CharSequence) new StringBuffer("abc")));
  209. assertFalse(s.contentEquals((CharSequence) new StringBuffer("def")));
  210. assertFalse(s.contentEquals((CharSequence) new StringBuffer("ghij")));
  211. try {
  212. s.contentEquals((CharSequence) null);
  213. fail("No NPE");
  214. } catch (NullPointerException e) {
  215. }
  216. }
  217. /**
  218. * @tests java.lang.String#contentEquals(StringBuffer)
  219. */
  220. @SuppressWarnings("nls")
  221. public void test_boolean_contentEquals_StringBuffer() throws Exception {
  222. String s = "abc";
  223. assertTrue(s.contentEquals(new StringBuffer("abc")));
  224. assertFalse(s.contentEquals(new StringBuffer("def")));
  225. assertFalse(s.contentEquals(new StringBuffer("ghij")));
  226. s = newString(1, 3, "_abc_".toCharArray());
  227. assertTrue(s.contentEquals(new StringBuffer("abc")));
  228. assertFalse(s.contentEquals(new StringBuffer("def")));
  229. assertFalse(s.contentEquals(new StringBuffer("ghij")));
  230. try {
  231. s.contentEquals((StringBuffer) null);
  232. fail("Should throw a NullPointerException");
  233. } catch (NullPointerException e) {
  234. // expected
  235. }
  236. }
  237. /**
  238. * @tests java.lang.String#contains(CharSequence)
  239. */
  240. @SuppressWarnings("cast")
  241. public void test_containsLjava_lang_CharSequence() throws Exception {
  242. String s = "abcdefghijklmnopqrstuvwxyz";
  243. assertTrue(s.contains((CharSequence) new StringBuffer("abc")));
  244. assertTrue(s.contains((CharSequence) new StringBuffer("def")));
  245. assertFalse(s.contains((CharSequence) new StringBuffer("ac")));
  246. s = newString(1, 26, "_abcdefghijklmnopqrstuvwxyz_".toCharArray());
  247. assertTrue(s.contains((CharSequence) new StringBuffer("abc")));
  248. assertTrue(s.contains((CharSequence) new StringBuffer("def")));
  249. assertFalse(s.contains((CharSequence) new StringBuffer("ac")));
  250. try {
  251. s.contentEquals((CharSequence) null);
  252. fail("No NPE");
  253. } catch (NullPointerException e) {
  254. }
  255. }
  256. /**
  257. * @tests java.lang.String.offsetByCodePoints(int, int)'
  258. */
  259. public void test_offsetByCodePoints_II() throws Exception {
  260. int result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 2);
  261. assertEquals(3, result);
  262. result = new String("abcd").offsetByCodePoints(3, -1);
  263. assertEquals(2, result);
  264. result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 3);
  265. assertEquals(4, result);
  266. result = new String("a\uD800\uDC00b").offsetByCodePoints(3, -1);
  267. assertEquals(1, result);
  268. result = new String("a\uD800\uDC00b").offsetByCodePoints(3, 0);
  269. assertEquals(3, result);
  270. result = new String("\uD800\uDC00bc").offsetByCodePoints(3, 0);
  271. assertEquals(3, result);
  272. result = new String("a\uDC00bc").offsetByCodePoints(3, -1);
  273. assertEquals(2, result);
  274. result = new String("a\uD800bc").offsetByCodePoints(3, -1);
  275. assertEquals(2, result);
  276. result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
  277. .offsetByCodePoints(0, 2);
  278. assertEquals(3, result);
  279. result = newString(2, 4, "__abcd__".toCharArray()).offsetByCodePoints(
  280. 3, -1);
  281. assertEquals(2, result);
  282. result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
  283. .offsetByCodePoints(0, 3);
  284. assertEquals(4, result);
  285. result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
  286. .offsetByCodePoints(3, -1);
  287. assertEquals(1, result);
  288. result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
  289. .offsetByCodePoints(3, 0);
  290. assertEquals(3, result);
  291. result = newString(2, 4, "__\uD800\uDC00bc__".toCharArray())
  292. .offsetByCodePoints(3, 0);
  293. assertEquals(3, result);
  294. result = newString(2, 4, "__a\uDC00bc__".toCharArray())
  295. .offsetByCodePoints(3, -1);
  296. assertEquals(2, result);
  297. result = newString(2, 4, "__a\uD800bc__".toCharArray())
  298. .offsetByCodePoints(3, -1);
  299. assertEquals(2, result);
  300. String s = "abc";
  301. try {
  302. s.offsetByCodePoints(-1, 1);
  303. fail("No IOOBE for negative index.");
  304. } catch (IndexOutOfBoundsException e) {
  305. }
  306. try {
  307. s.offsetByCodePoints(0, 4);
  308. fail("No IOOBE for offset that's too large.");
  309. } catch (IndexOutOfBoundsException e) {
  310. }
  311. try {
  312. s.offsetByCodePoints(3, -4);
  313. fail("No IOOBE for offset that's too small.");
  314. } catch (IndexOutOfBoundsException e) {
  315. }
  316. try {
  317. s.offsetByCodePoints(3, 1);
  318. fail("No IOOBE for index that's too large.");
  319. } catch (IndexOutOfBoundsException e) {
  320. }
  321. try {
  322. s.offsetByCodePoints(4, -1);
  323. fail("No IOOBE for index that's too large.");
  324. } catch (IndexOutOfBoundsException e) {
  325. }
  326. s = newString(2,3,"__abc__".toCharArray());
  327. try {
  328. s.offsetByCodePoints(-1, 1);
  329. fail("No IOOBE for negative index.");
  330. } catch (IndexOutOfBoundsException e) {
  331. }
  332. try {
  333. s.offsetByCodePoints(0, 4);
  334. fail("No IOOBE for offset that's too large.");
  335. } catch (IndexOutOfBoundsException e) {
  336. }
  337. try {
  338. s.offsetByCodePoints(3, -4);
  339. fail("No IOOBE for offset that's too small.");
  340. } catch (IndexOutOfBoundsException e) {
  341. }
  342. try {
  343. s.offsetByCodePoints(3, 1);
  344. fail("No IOOBE for index that's too large.");
  345. } catch (IndexOutOfBoundsException e) {
  346. }
  347. try {
  348. s.offsetByCodePoints(4, -1);
  349. fail("No IOOBE for index that's too large.");
  350. } catch (IndexOutOfBoundsException e) {
  351. }
  352. }
  353. /**
  354. * @tests java.lang.StringBuilder.codePointAt(int)
  355. */
  356. public void test_codePointAtI() throws Exception {
  357. String s = "abc";
  358. assertEquals('a', s.codePointAt(0));
  359. assertEquals('b', s.codePointAt(1));
  360. assertEquals('c', s.codePointAt(2));
  361. s = newString(2,3,"__abc__".toCharArray());
  362. assertEquals('a', s.codePointAt(0));
  363. assertEquals('b', s.codePointAt(1));
  364. assertEquals('c', s.codePointAt(2));
  365. s = "\uD800\uDC00";
  366. assertEquals(0x10000, s.codePointAt(0));
  367. assertEquals('\uDC00', s.codePointAt(1));
  368. s = newString(2,2,"__\uD800\uDC00__".toCharArray());
  369. assertEquals(0x10000, s.codePointAt(0));
  370. assertEquals('\uDC00', s.codePointAt(1));
  371. s = "abc";
  372. try {
  373. s.codePointAt(-1);
  374. fail("No IOOBE on negative index.");
  375. } catch (IndexOutOfBoundsException e) {
  376. }
  377. try {
  378. s.codePointAt(s.length());
  379. fail("No IOOBE on index equal to length.");
  380. } catch (IndexOutOfBoundsException e) {
  381. }
  382. try {
  383. s.codePointAt(s.length() + 1);
  384. fail("No IOOBE on index greater than length.");
  385. } catch (IndexOutOfBoundsException e) {
  386. }
  387. s = newString(2,3,"__abc__".toCharArray());
  388. try {
  389. s.codePointAt(-1);
  390. fail("No IOOBE on negative index.");
  391. } catch (IndexOutOfBoundsException e) {
  392. }
  393. try {
  394. s.codePointAt(s.length());
  395. fail("No IOOBE on index equal to length.");
  396. } catch (IndexOutOfBoundsException e) {
  397. }
  398. try {
  399. s.codePointAt(s.length() + 1);
  400. fail("No IOOBE on index greater than length.");
  401. } catch (IndexOutOfBoundsException e) {
  402. }
  403. }
  404. /**
  405. * @tests java.lang.StringBuilder.codePointBefore(int)
  406. */
  407. public void test_codePointBeforeI() throws Exception {
  408. String s = "abc";
  409. assertEquals('a', s.codePointBefore(1));
  410. assertEquals('b', s.codePointBefore(2));
  411. assertEquals('c', s.codePointBefore(3));
  412. s = newString(2,3,"__abc__".toCharArray());
  413. assertEquals('a', s.codePointBefore(1));
  414. assertEquals('b', s.codePointBefore(2));
  415. assertEquals('c', s.codePointBefore(3));
  416. s = "\uD800\uDC00";
  417. assertEquals(0x10000, s.codePointBefore(2));
  418. assertEquals('\uD800', s.codePointBefore(1));
  419. s = newString(2,2,"__\uD800\uDC00__".toCharArray());
  420. assertEquals(0x10000, s.codePointBefore(2));
  421. assertEquals('\uD800', s.codePointBefore(1));
  422. s = "abc";
  423. try {
  424. s.codePointBefore(0);
  425. fail("No IOOBE on zero index.");
  426. } catch (IndexOutOfBoundsException e) {
  427. }
  428. try {
  429. s.codePointBefore(-1);
  430. fail("No IOOBE on negative index.");
  431. } catch (IndexOutOfBoundsException e) {
  432. }
  433. try {
  434. s.codePointBefore(s.length() + 1);
  435. fail("No IOOBE on index greater than length.");
  436. } catch (IndexOutOfBoundsException e) {
  437. }
  438. s = newString(2,3,"__abc__".toCharArray());
  439. try {
  440. s.codePointBefore(0);
  441. fail("No IOOBE on zero index.");
  442. } catch (IndexOutOfBoundsException e) {
  443. }
  444. try {
  445. s.codePointBefore(-1);
  446. fail("No IOOBE on negative index.");
  447. } catch (IndexOutOfBoundsException e) {
  448. }
  449. try {
  450. s.codePointBefore(s.length() + 1);
  451. fail("No IOOBE on index greater than length.");
  452. } catch (IndexOutOfBoundsException e) {
  453. }
  454. }
  455. /**
  456. * @tests java.lang.StringBuilder.codePointCount(int, int)
  457. */
  458. public void test_codePointCountII() throws Exception {
  459. assertEquals(1, "\uD800\uDC00".codePointCount(0, 2));
  460. assertEquals(1, "\uD800\uDC01".codePointCount(0, 2));
  461. assertEquals(1, "\uD801\uDC01".codePointCount(0, 2));
  462. assertEquals(1, "\uDBFF\uDFFF".codePointCount(0, 2));
  463. assertEquals(3, "a\uD800\uDC00b".codePointCount(0, 4));
  464. assertEquals(4, "a\uD800\uDC00b\uD800".codePointCount(0, 5));
  465. assertEquals(1, newString(2,2,"__\uD800\uDC00__".toCharArray()).codePointCount(0, 2));
  466. assertEquals(1, newString(2,2,"__\uD800\uDC01__".toCharArray()).codePointCount(0, 2));
  467. assertEquals(1, newString(2,2,"__\uD801\uDC01__".toCharArray()).codePointCount(0, 2));
  468. assertEquals(1, newString(2,2,"__\uDBFF\uDFFF__".toCharArray()).codePointCount(0, 2));
  469. assertEquals(3, newString(2,4,"__a\uD800\uDC00b__".toCharArray()).codePointCount(0, 4));
  470. assertEquals(4, newString(2,5,"__a\uD800\uDC00b\uD800__".toCharArray()).codePointCount(0, 5));
  471. String s = "abc";
  472. try {
  473. s.codePointCount(-1, 2);
  474. fail("No IOOBE for negative begin index.");
  475. } catch (IndexOutOfBoundsException e) {
  476. }
  477. try {
  478. s.codePointCount(0, 4);
  479. fail("No IOOBE for end index that's too large.");
  480. } catch (IndexOutOfBoundsException e) {
  481. }
  482. try {
  483. s.codePointCount(3, 2);
  484. fail("No IOOBE for begin index larger than end index.");
  485. } catch (IndexOutOfBoundsException e) {
  486. }
  487. s = newString(2, 3, "__abc__".toCharArray());
  488. try {
  489. s.codePointCount(-1, 2);
  490. fail("No IOOBE for negative begin index.");
  491. } catch (IndexOutOfBoundsException e) {
  492. }
  493. try {
  494. s.codePointCount(0, 4);
  495. fail("No IOOBE for end index that's too large.");
  496. } catch (IndexOutOfBoundsException e) {
  497. }
  498. try {
  499. s.codePointCount(3, 2);
  500. fail("No IOOBE for begin index larger than end index.");
  501. } catch (IndexOutOfBoundsException e) {
  502. }
  503. }
  504. /**
  505. * @tests {@link java.lang.String#String(byte[], int, int, Charset)}
  506. *
  507. * @since 1.6
  508. */
  509. public void test_ConstructorBIIL() throws Exception {
  510. // can construct normally
  511. new String(new byte[8], 0, 4, Charset.defaultCharset());
  512. new String(new byte[8], 8, 0, Charset.defaultCharset());
  513. new String(new byte[0], 0, 0, Charset.defaultCharset());
  514. // throws exceptions
  515. try {
  516. new String(new byte[8], 0, 9, Charset.defaultCharset());
  517. fail("should throw StringIndexOutOfBoundsException");
  518. } catch (StringIndexOutOfBoundsException e) {
  519. // expected
  520. }
  521. try {
  522. new String(new byte[8], 9, 0, Charset.defaultCharset());
  523. fail("should throw StringIndexOutOfBoundsException");
  524. } catch (StringIndexOutOfBoundsException e) {
  525. // expected
  526. }
  527. try {
  528. new String(new byte[8], -1, 0, Charset.defaultCharset());
  529. fail("should throw StringIndexOutOfBoundsException");
  530. } catch (StringIndexOutOfBoundsException e) {
  531. // expected
  532. }
  533. try {
  534. new String(new byte[8], 9, -1, Charset.defaultCharset());
  535. fail("should throw StringIndexOutOfBoundsException");
  536. } catch (StringIndexOutOfBoundsException e) {
  537. // expected
  538. }
  539. try {
  540. new String(null, -1, 0, Charset.defaultCharset());
  541. fail();
  542. } catch (NullPointerException expected) {
  543. } catch (StringIndexOutOfBoundsException expected) {
  544. }
  545. try {
  546. new String(null, 0, -1, Charset.defaultCharset());
  547. fail();
  548. } catch (NullPointerException expected) {
  549. } catch (StringIndexOutOfBoundsException expected) {
  550. }
  551. try {
  552. new String(null, 0, 9, Charset.defaultCharset());
  553. fail("should throw NullPointerException");
  554. } catch (NullPointerException e) {
  555. // expected
  556. }
  557. try {
  558. new String(null, 0, 0, Charset.defaultCharset());
  559. fail("should throw NullPointerException");
  560. } catch (NullPointerException e) {
  561. // expected
  562. }
  563. try {
  564. new String(null, -1, 0, (Charset)null);
  565. fail("should throw NullPointerException");
  566. } catch (NullPointerException e) {
  567. // expected
  568. }
  569. try {
  570. new String(new byte[8], -1, 0, (Charset)null);
  571. fail();
  572. } catch (NullPointerException expected) {
  573. } catch (StringIndexOutOfBoundsException expected) {
  574. }
  575. try {
  576. new String(new byte[8], 0, 9, (Charset)null);
  577. fail();
  578. } catch (NullPointerException expected) {
  579. } catch (StringIndexOutOfBoundsException expected) {
  580. }
  581. try {
  582. new String(new byte[8], 0, 4, (Charset)null);
  583. fail("should throw NullPointerException");
  584. } catch (NullPointerException e) {
  585. // expected
  586. }
  587. }
  588. /**
  589. * @tests {@link java.lang.String#String(byte[], Charset)}
  590. *
  591. * @since 1.6
  592. */
  593. public void test_ConstructorBL() throws Exception {
  594. new String(new byte[8], Charset.defaultCharset());
  595. try {
  596. new String(new byte[8],(Charset)null);
  597. fail("should throw NullPointerException");
  598. } catch (NullPointerException e) {
  599. // expected
  600. }
  601. try {
  602. new String(new byte[0],(Charset)null);
  603. fail("should throw NullPointerException");
  604. } catch (NullPointerException e) {
  605. // expected
  606. }
  607. try {
  608. new String(null,Charset.defaultCharset());
  609. fail("should throw NullPointerException");
  610. } catch (NullPointerException e) {
  611. // expected
  612. }
  613. new String(new byte[0], Charset.defaultCharset());
  614. }
  615. /**
  616. * @tests {@link java.lang.String#isEmpty()}
  617. *
  618. * @since 1.6
  619. */
  620. public void test_isEmpty() throws Exception {
  621. assertTrue(new String(new byte[0], Charset.defaultCharset()).isEmpty());
  622. assertTrue(new String(new byte[8], Charset.defaultCharset()).substring(0, 0).isEmpty());
  623. }
  624. /**
  625. * @tests {@link java.lang.String#getBytes(Charset)}
  626. *
  627. * @since 1.6
  628. */
  629. public void test_getBytesLCharset() throws Exception {
  630. byte[] emptyBytes = new byte[0];
  631. byte[] someBytes = new byte[]{'T','h','i','s',' ',' ','i','s',' ','t','e','s','t',' ','b','y','t','e','s'};
  632. assertEquals(0, new String(emptyBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset()).length);
  633. try{
  634. new String(emptyBytes, Charset.defaultCharset()).getBytes((Charset)null);
  635. fail("should throw NPE");
  636. } catch (NullPointerException e){
  637. // correct
  638. }
  639. assertTrue(bytesEquals(someBytes,new String(someBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset())));
  640. SortedMap<String, Charset> charsets = Charset.availableCharsets();
  641. }
  642. boolean bytesEquals(byte[] bytes1, byte[] bytes2){
  643. return Arrays.toString(bytes1).equals(Arrays.toString(bytes2));
  644. }
  645. }