blob: f1a238c937cda9565dfc80d28c15da48af24f3e4 [file] [log] [blame]
Omari Stephens0857d932012-05-24 19:02:44 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.tradefed.util;
17
18import com.android.tradefed.result.ByteArrayInputStreamSource;
19import com.android.tradefed.result.InputStreamSource;
20
21import junit.framework.TestCase;
22
gopinatha1ce4f12017-09-20 13:17:56 -070023import java.io.BufferedReader;
Omari Stephens0857d932012-05-24 19:02:44 -070024import java.io.ByteArrayInputStream;
Guang Zhua2b015d2015-08-09 16:24:49 -070025import java.io.ByteArrayOutputStream;
xingdaie89846a2015-04-15 12:10:02 -070026import java.io.IOException;
Omari Stephens0857d932012-05-24 19:02:44 -070027import java.io.InputStream;
Guang Zhua2b015d2015-08-09 16:24:49 -070028import java.io.OutputStreamWriter;
29import java.io.Writer;
Omari Stephens0857d932012-05-24 19:02:44 -070030
Xing Dai842a1762019-01-02 12:13:10 -080031/** Unit tests for the {@link com.android.tradefed.util.StreamUtil} utility class */
Omari Stephens0857d932012-05-24 19:02:44 -070032public class StreamUtilTest extends TestCase {
33
34 /**
Xing Dai842a1762019-01-02 12:13:10 -080035 * Verify that {@link com.android.tradefed.util.StreamUtil#getByteArrayListFromSource} works as
36 * expected.
Omari Stephens0857d932012-05-24 19:02:44 -070037 */
38 public void testGetByteArrayListFromSource() throws Exception {
39 final String contents = "this is a string";
40 final byte[] contentBytes = contents.getBytes();
jdesprezcf701b22017-08-31 16:23:10 -070041 try (final InputStreamSource source = new ByteArrayInputStreamSource(contentBytes)) {
42 final InputStream stream = source.createInputStream();
43 final ByteArrayList output = StreamUtil.getByteArrayListFromStream(stream);
44 final byte[] outputBytes = output.getContents();
Omari Stephens0857d932012-05-24 19:02:44 -070045
jdesprezcf701b22017-08-31 16:23:10 -070046 assertEquals(contentBytes.length, outputBytes.length);
47 for (int i = 0; i < contentBytes.length; ++i) {
48 assertEquals(contentBytes[i], outputBytes[i]);
49 }
Omari Stephens0857d932012-05-24 19:02:44 -070050 }
51 }
52
53 /**
Xing Dai842a1762019-01-02 12:13:10 -080054 * Verify that {@link com.android.tradefed.util.StreamUtil#getByteArrayListFromStream} works as
55 * expected.
Omari Stephens0857d932012-05-24 19:02:44 -070056 */
57 public void testGetByteArrayListFromStream() throws Exception {
58 final String contents = "this is a string";
59 final byte[] contentBytes = contents.getBytes();
60 final ByteArrayList output = StreamUtil.getByteArrayListFromStream(
61 new ByteArrayInputStream(contentBytes));
62 final byte[] outputBytes = output.getContents();
63
64 assertEquals(contentBytes.length, outputBytes.length);
65 for (int i = 0; i < contentBytes.length; ++i) {
66 assertEquals(contentBytes[i], outputBytes[i]);
67 }
68 }
69
70 /**
Xing Dai842a1762019-01-02 12:13:10 -080071 * Verify that {@link com.android.tradefed.util.StreamUtil#getStringFromSource} works as
72 * expected.
Omari Stephens0857d932012-05-24 19:02:44 -070073 */
74 public void testGetStringFromSource() throws Exception {
75 final String contents = "this is a string";
jdesprezcf701b22017-08-31 16:23:10 -070076 try (InputStreamSource source = new ByteArrayInputStreamSource(contents.getBytes())) {
77 final InputStream stream = source.createInputStream();
78 final String output = StreamUtil.getStringFromStream(stream);
79 assertEquals(contents, output);
80 }
Omari Stephens0857d932012-05-24 19:02:44 -070081 }
82
83 /**
Xing Dai842a1762019-01-02 12:13:10 -080084 * Verify that {@link com.android.tradefed.util.StreamUtil#getBufferedReaderFromStreamSrc} works
85 * as expected.
gopinatha1ce4f12017-09-20 13:17:56 -070086 */
87 public void testGetBufferedReaderFromInputStream() throws Exception {
88 final String contents = "this is a string";
gopinathf1ecfe62017-09-26 17:08:22 -070089 BufferedReader output = null;
gopinatha1ce4f12017-09-20 13:17:56 -070090 try (InputStreamSource source = new ByteArrayInputStreamSource(contents.getBytes())) {
gopinathf1ecfe62017-09-26 17:08:22 -070091 output = StreamUtil.getBufferedReaderFromStreamSrc(source);
gopinatha1ce4f12017-09-20 13:17:56 -070092 assertEquals(contents, output.readLine());
gopinathf1ecfe62017-09-26 17:08:22 -070093 } finally {
94 if (null != output) {
95 output.close();
96 }
gopinatha1ce4f12017-09-20 13:17:56 -070097 }
98 }
99
100 /**
Xing Dai842a1762019-01-02 12:13:10 -0800101 * Verify that {@link com.android.tradefed.util.StreamUtil#countLinesFromSource} works as
102 * expected.
Guang Zhu7ad61192016-12-15 17:37:04 -0800103 */
104 public void testCountLinesFromSource() throws Exception {
105 final String contents = "foo\nbar\n\foo\n";
106 final InputStreamSource source = new ByteArrayInputStreamSource(contents.getBytes());
107 assertEquals(3, StreamUtil.countLinesFromSource(source));
108 }
109
110 /**
Xing Dai842a1762019-01-02 12:13:10 -0800111 * Verify that {@link com.android.tradefed.util.StreamUtil#getStringFromStream} works as
112 * expected.
Omari Stephens0857d932012-05-24 19:02:44 -0700113 */
114 public void testGetStringFromStream() throws Exception {
115 final String contents = "this is a string";
116 final String output = StreamUtil.getStringFromStream(
117 new ByteArrayInputStream(contents.getBytes()));
118 assertEquals(contents, output);
119 }
xingdaie89846a2015-04-15 12:10:02 -0700120
121 /**
Xing Dai842a1762019-01-02 12:13:10 -0800122 * Verify that {@link com.android.tradefed.util.StreamUtil#calculateMd5(InputStream)} works as
123 * expected.
124 *
xingdaie89846a2015-04-15 12:10:02 -0700125 * @throws IOException
126 */
127 public void testCalculateMd5() throws IOException {
128 final String source = "testtesttesttesttest";
129 final String md5 = "f317f682fafe0309c6a423af0b4efa59";
130 ByteArrayInputStream inputSource = new ByteArrayInputStream(source.getBytes());
131 String actualMd5 = StreamUtil.calculateMd5(inputSource);
132 assertEquals(md5, actualMd5);
133 }
Omari Stephens0857d932012-05-24 19:02:44 -0700134
Xing Dai842a1762019-01-02 12:13:10 -0800135 /**
136 * Verify that {@link com.android.tradefed.util.StreamUtil#calculateBase64Md5(InputStream)}
137 * works as expected.
138 *
139 * @throws IOException
140 */
141 public void testCalculateBase64Md5() throws IOException {
142 final String source = "testtesttesttesttest";
143 final String base64Md5 = "8xf2gvr+AwnGpCOvC076WQ==";
144 ByteArrayInputStream inputSource = new ByteArrayInputStream(source.getBytes());
145 String actualBase64Md5 = StreamUtil.calculateBase64Md5(inputSource);
146 assertEquals(base64Md5, actualBase64Md5);
147 }
148
Guang Zhua2b015d2015-08-09 16:24:49 -0700149 public void testCopyStreams() throws Exception {
150 String text = getLargeText();
151 ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes());
152 ByteArrayOutputStream baos = new ByteArrayOutputStream();
153 StreamUtil.copyStreams(bais, baos);
154 bais.close();
155 baos.close();
156 assertEquals(text, baos.toString());
157 }
158
159 public void testCopyStreamToWriter() throws Exception {
160 String text = getLargeText();
161 ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes());
162 ByteArrayOutputStream baos = new ByteArrayOutputStream();
163 Writer writer = new OutputStreamWriter(baos);
164 StreamUtil.copyStreamToWriter(bais, writer);
165 bais.close();
166 writer.close();
167 baos.close();
168 assertEquals(text, baos.toString());
169 }
170
171 /**
172 * Returns a large chunk of text that's at least 16K in size
Guang Zhua2b015d2015-08-09 16:24:49 -0700173 */
174 private String getLargeText() {
175 String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
176 + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
177 + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
178 + "aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
179 + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
180 + "occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit "
181 + "anim id est laborum."; // 446 bytes
182 StringBuilder sb = new StringBuilder();
183 for (int i = 0; i < 40; i ++) {
184 sb.append(text);
185 }
186 return sb.toString();
187 }
188}