1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
21 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import static junit.framework.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.aaf.cadi.Capacitor;
32 import java.lang.reflect.*;
34 public class JU_Capacitor {
35 private Capacitor cap;
36 public final static String TEST_DATA =
37 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
38 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
39 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" +
40 "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
41 "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
42 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
46 cap = new Capacitor();
50 public void singleByteTest() throws Exception {
51 assertEquals(cap.read(), -1);
53 Field curr_field = Capacitor.class.getDeclaredField("curr");
54 curr_field.setAccessible(true);
55 Field idx_field = Capacitor.class.getDeclaredField("idx");
56 idx_field.setAccessible(true);
57 assertNull(curr_field.get(cap));
58 assertEquals(idx_field.get(cap), 0);
60 for(int iter = 0; iter < 20; ++iter) {
61 for(int i = 0; i < 20; ++i) {
62 cap.put((byte)('a' + i));
65 byte[] array = new byte[20];
66 for(int i = 0; i < 20; ++i) {
67 array[i]=(byte)cap.read();
69 assertEquals("abcdefghijklmnopqrst", new String(array));
70 assertEquals(-1, cap.read());
75 for(int i = 0; i < 500; i++) {
79 byte[] array = new byte[500];
80 for(int i = 0; i < 500; ++i) {
81 array[i]=(byte)cap.read();
83 assertEquals((new String(array)).length(), 500);
84 assertEquals(-1, cap.read());
88 public void availableTest() {
89 assertEquals(cap.available(), 0);
90 for(int i = 0; i < 100; ++i) {
93 // The Capacitor can hold 256 bytes. After reading 100 bytes,
94 // it should have 156 available
95 assertEquals(cap.available(), 156);
99 public void byteArrayTest() {
100 byte[] arrayA = TEST_DATA.getBytes();
101 assertEquals(cap.read(arrayA, 0, arrayA.length), -1);
103 cap.put(arrayA, 0, arrayA.length);
105 byte[] arrayB = new byte[arrayA.length];
107 assertEquals(arrayA.length, cap.read(arrayB, 0, arrayB.length));
108 assertEquals(TEST_DATA, new String(arrayB));
109 assertEquals(-1, cap.read());
112 String b = "This is some content that we want to read";
113 byte[] a = b.getBytes();
114 byte[] c = new byte[b.length()]; // we want to use this to test reading offsets, etc
116 for(int i = 0; i < a.length; i += 11) {
117 cap.put(a, i, Math.min(11, a.length-i));
121 for(int i = 0; i < c.length; i += read) {
122 read = cap.read(c, i, Math.min(3, c.length-i));
124 assertEquals(b, new String(c));
128 public void resetTest() throws Exception {
130 Field curr_field = Capacitor.class.getDeclaredField("curr");
131 curr_field.setAccessible(true);
132 Field idx_field = Capacitor.class.getDeclaredField("idx");
133 idx_field.setAccessible(true);
134 assertNull(curr_field.get(cap));
135 assertEquals(idx_field.get(cap), 0);
139 assertNotNull(curr_field.get(cap));
140 assertEquals(idx_field.get(cap), 1);
144 public void skipTest() throws Exception {
145 // capacitor can't skip if nothing has been put into it
146 assertEquals(cap.skip(10), 0);
148 // The Capacitor can hold 256 bytes. If we try to skip 100 bytes,
149 // it should only skip 1 byte, leaving 255 remaining
150 assertEquals(cap.skip(100), 255);
152 // Skipping 200 bytes leaves 0 remaining
153 assertEquals(cap.skip(200), 0);