| 1 | package ezvcard.util; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| 5 | import org.junit.Before; |
| 6 | import org.junit.Test; |
| 7 | |
| 8 | /* |
| 9 | Copyright (c) 2012-2023, Michael Angstadt |
| 10 | All rights reserved. |
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without |
| 13 | modification, are permitted provided that the following conditions are met: |
| 14 | |
| 15 | 1. Redistributions of source code must retain the above copyright notice, this |
| 16 | list of conditions and the following disclaimer. |
| 17 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | this list of conditions and the following disclaimer in the documentation |
| 19 | and/or other materials provided with the distribution. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | The views and conclusions contained in the software and documentation are those |
| 33 | of the authors and should not be interpreted as representing official policies, |
| 34 | either expressed or implied, of the FreeBSD Project. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * @author Michael Angstadt |
| 39 | */ |
| 40 | public class ClearableStringBuilderTest { |
| 41 | private ClearableStringBuilder sb; |
| 42 | |
| 43 | @Before |
| 44 | public void before() { |
| 45 | sb = new ClearableStringBuilder(); |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void get() { |
| 50 | assertEquals("", sb.get()); |
| 51 | |
| 52 | sb.append("foo"); |
| 53 | assertEquals("foo", sb.get()); |
| 54 | |
| 55 | /* |
| 56 | * Internal buffer should not be modified when get() is called. |
| 57 | */ |
| 58 | assertEquals("foo", sb.get()); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void clear() { |
| 63 | sb.append("foo"); |
| 64 | assertEquals("foo", sb.get()); |
| 65 | |
| 66 | sb.clear(); |
| 67 | assertEquals("", sb.get()); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void getAndClear() { |
| 72 | sb.append("foo"); |
| 73 | assertEquals("foo", sb.getAndClear()); |
| 74 | assertEquals("", sb.get()); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void append() { |
| 79 | sb.append('f').append("oo").append(" bar!".toCharArray(), 0, 4); |
| 80 | assertEquals("foo bar", sb.get()); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void length() { |
| 85 | assertEquals(0, sb.length()); |
| 86 | |
| 87 | sb.append("foo"); |
| 88 | assertEquals(3, sb.length()); |
| 89 | } |
| 90 | } |