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 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
25 import static junit.framework.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.aaf.cadi.Capacitor;
33 import java.lang.reflect.*;
35 public class JU_Capacitor {
36 private Capacitor cap;
37 public final static String TEST_DATA =
38 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
39 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
40 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" +
41 "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
42 "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
43 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
47 cap = new Capacitor();
51 public void singleByteTest() throws Exception {
52 assertEquals(cap.read(), -1);
54 Field curr_field = Capacitor.class.getDeclaredField("curr");
55 curr_field.setAccessible(true);
56 Field idx_field = Capacitor.class.getDeclaredField("idx");
57 idx_field.setAccessible(true);
58 assertNull(curr_field.get(cap));
59 assertEquals(idx_field.get(cap), 0);
61 for(int iter = 0; iter < 20; ++iter) {
62 for(int i = 0; i < 20; ++i) {
63 cap.put((byte)('a' + i));
66 byte[] array = new byte[20];
67 for(int i = 0; i < 20; ++i) {
68 array[i]=(byte)cap.read();
70 assertEquals("abcdefghijklmnopqrst", new String(array));
71 assertEquals(-1, cap.read());
76 for(int i = 0; i < 500; i++) {
80 byte[] array = new byte[500];
81 for(int i = 0; i < 500; ++i) {
82 array[i]=(byte)cap.read();
84 assertEquals((new String(array)).length(), 500);
85 assertEquals(-1, cap.read());
89 public void availableTest() {
90 assertEquals(cap.available(), 0);
91 for(int i = 0; i < 100; ++i) {
94 // The Capacitor can hold 256 bytes. After reading 100 bytes,
95 // it should have 156 available
96 assertEquals(cap.available(), 156);
100 public void byteArrayTest() {
101 byte[] arrayA = TEST_DATA.getBytes();
102 assertEquals(cap.read(arrayA, 0, arrayA.length), -1);
104 cap.put(arrayA, 0, arrayA.length);
106 byte[] arrayB = new byte[arrayA.length];
108 assertEquals(arrayA.length, cap.read(arrayB, 0, arrayB.length));
109 assertEquals(TEST_DATA, new String(arrayB));
110 assertEquals(-1, cap.read());
113 String b = "This is some content that we want to read";
114 byte[] a = b.getBytes();
115 byte[] c = new byte[b.length()]; // we want to use this to test reading offsets, etc
117 for(int i = 0; i < a.length; i += 11) {
118 cap.put(a, i, Math.min(11, a.length-i));
122 for(int i = 0; i < c.length; i += read) {
123 read = cap.read(c, i, Math.min(3, c.length-i));
125 assertEquals(b, new String(c));
129 public void resetTest() throws Exception {
131 Field curr_field = Capacitor.class.getDeclaredField("curr");
132 curr_field.setAccessible(true);
133 Field idx_field = Capacitor.class.getDeclaredField("idx");
134 idx_field.setAccessible(true);
135 assertNull(curr_field.get(cap));
136 assertEquals(idx_field.get(cap), 0);
140 assertNotNull(curr_field.get(cap));
141 assertEquals(idx_field.get(cap), 1);
145 public void skipTest() throws Exception {
146 // capacitor can't skip if nothing has been put into it
147 assertEquals(cap.skip(10), 0);
149 // The Capacitor can hold 256 bytes. If we try to skip 100 bytes,
150 // it should only skip 1 byte, leaving 255 remaining
151 assertEquals(cap.skip(100), 255);
153 // Skipping 200 bytes leaves 0 remaining
154 assertEquals(cap.skip(200), 0);