|
| 1 | +package org.json.stream; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +/** |
| 7 | + * Test the BufferedAppendable class. |
| 8 | + * |
| 9 | + * @author run2000 |
| 10 | + * @version 2016-7-8 |
| 11 | + */ |
| 12 | +public final class AppendableTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void testBuffer() throws Exception { |
| 16 | + StringBuffer builder = new StringBuffer(); |
| 17 | + |
| 18 | + try (BufferedAppendable test = new BufferedAppendable(16).with(builder)) { |
| 19 | + test.append("This is a test."); |
| 20 | + } |
| 21 | + |
| 22 | + Assert.assertEquals("This is a test.", builder.toString()); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testBuffer1() throws Exception { |
| 27 | + StringBuffer builder = new StringBuffer(); |
| 28 | + |
| 29 | + try (BufferedAppendable test = new BufferedAppendable(16).with(builder)) { |
| 30 | + test.append("This is a test 123."); |
| 31 | + } |
| 32 | + |
| 33 | + Assert.assertEquals("This is a test 123.", builder.toString()); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testBuffer2() throws Exception { |
| 38 | + StringBuffer builder = new StringBuffer(); |
| 39 | + |
| 40 | + try (BufferedAppendable test = new BufferedAppendable(16).with(builder)) { |
| 41 | + test.append("This is "); |
| 42 | + test.append("a test 123."); |
| 43 | + } |
| 44 | + |
| 45 | + Assert.assertEquals("This is a test 123.", builder.toString()); |
| 46 | + } |
| 47 | + @Test |
| 48 | + public void testBufferSub() throws Exception { |
| 49 | + StringBuffer builder = new StringBuffer(); |
| 50 | + |
| 51 | + try (BufferedAppendable test = new BufferedAppendable(16).with(builder)) { |
| 52 | + test.append("This is a test.", 0, 15); |
| 53 | + } |
| 54 | + |
| 55 | + Assert.assertEquals("This is a test.", builder.toString()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testBuffer1Sub() throws Exception { |
| 60 | + StringBuffer builder = new StringBuffer(); |
| 61 | + |
| 62 | + try (BufferedAppendable test = new BufferedAppendable(16).with(builder)) { |
| 63 | + test.append("This is a test 123.", 0, 19); |
| 64 | + } |
| 65 | + |
| 66 | + Assert.assertEquals("This is a test 123.", builder.toString()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testBuffer2Sub() throws Exception { |
| 71 | + StringBuffer builder = new StringBuffer(); |
| 72 | + |
| 73 | + try (BufferedAppendable test = new BufferedAppendable(16).with(builder)) { |
| 74 | + test.append("This is a test 123.", 0, 8); |
| 75 | + test.append("This is a test 123.", 8, 19); |
| 76 | + } |
| 77 | + |
| 78 | + Assert.assertEquals("This is a test 123.", builder.toString()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testBuffer2Char() throws Exception { |
| 83 | + StringBuffer builder = new StringBuffer(); |
| 84 | + |
| 85 | + try (BufferedAppendable test = new BufferedAppendable(16).with(builder)) { |
| 86 | + test.append('T'); |
| 87 | + test.append('h'); |
| 88 | + test.append('i'); |
| 89 | + test.append('s'); |
| 90 | + test.append(' '); |
| 91 | + test.append('i'); |
| 92 | + test.append('s'); |
| 93 | + test.append(' '); |
| 94 | + test.append('a'); |
| 95 | + test.append(' '); |
| 96 | + test.append('t'); |
| 97 | + test.append('e'); |
| 98 | + test.append('s'); |
| 99 | + test.append('t'); |
| 100 | + test.append(' '); |
| 101 | + test.append('1'); |
| 102 | + test.append('2'); |
| 103 | + test.append('3'); |
| 104 | + test.append('.'); |
| 105 | + } |
| 106 | + |
| 107 | + Assert.assertEquals("This is a test 123.", builder.toString()); |
| 108 | + } |
| 109 | +} |
0 commit comments