forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdkAsserts.java
More file actions
243 lines (220 loc) · 9.91 KB
/
SdkAsserts.java
File metadata and controls
243 lines (220 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (c) 2016. Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.test.util;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.junit.Assert;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.util.IOUtils;
public class SdkAsserts {
/**
* Asserts that the specified String is not null and not empty.
*
* @param str
* The String to test.
* @deprecated Use Hamcrest Matchers instead
*/
@Deprecated
public static void assertNotEmpty(String str) {
assertNotNull(str);
assertTrue(str.length() > 0);
}
/**
* Asserts that the contents in the specified file are exactly equal to the contents read from
* the specified input stream. The input stream will be closed at the end of this method. If any
* problems are encountered, or the stream's contents don't match up exactly with the file's
* contents, then this method will fail the current test.
*
* @param expected
* The file containing the expected contents.
* @param actual
* The stream that will be read, compared to the expected file contents, and finally
* closed.
*/
public static void assertFileEqualsStream(File expected, InputStream actual) {
assertFileEqualsStream(null, expected, actual);
}
/**
* Asserts that the contents in the specified file are exactly equal to the contents read from
* the specified input stream. The input stream will be closed at the end of this method. If any
* problems are encountered, or the stream's contents don't match up exactly with the file's
* contents, then this method will fail the current test.
*
* @param errmsg
* error message to be thrown when the assertion fails.
* @param expected
* The file containing the expected contents.
* @param actual
* The stream that will be read, compared to the expected file contents, and finally
* closed.
*/
public static void assertFileEqualsStream(String errmsg, File expected, InputStream actual) {
try {
InputStream expectedInputStream = new FileInputStream(expected);
assertStreamEqualsStream(errmsg, expectedInputStream, actual);
} catch (FileNotFoundException e) {
fail("Expected file " + expected.getAbsolutePath() + " doesn't exist: " + e.getMessage());
}
}
/**
* Asserts that the contents in the specified input streams are same. The input streams will be
* closed at the end of this method. If any problems are encountered, or the stream's contents
* don't match up exactly with the file's contents, then this method will fail the current test.
*
* @param expected
* expected input stream. The stream will be closed at the end.
* @param actual
* The stream that will be read, compared to the expected file contents, and finally
* closed.
*/
public static void assertStreamEqualsStream(InputStream expected, InputStream actual) {
assertStreamEqualsStream(null, expected, actual);
}
/**
* Asserts that the contents in the specified input streams are same. The input streams will be
* closed at the end of this method. If any problems are encountered, or the stream's contents
* don't match up exactly with the file's contents, then this method will fail the current test.
*
* @param errmsg
* error message to be thrown when the assertion fails.
* @param expected
* expected input stream. The stream will be closed at the end.
* @param actual
* The stream that will be read, compared to the expected file contents, and finally
* closed.
*/
public static void assertStreamEqualsStream(String errmsg,
InputStream expectedInputStream,
InputStream inputStream) {
try {
assertTrue(errmsg, doesStreamEqualStream(expectedInputStream, inputStream));
} catch (IOException e) {
fail("Error reading from stream: " + e.getMessage());
}
}
/**
* Asserts that the contents of the two files are same.
*
* @param expected
* expected file.
* @param actual
* actual file.
*/
public static void assertFileEqualsFile(File expected, File actual) {
if (expected == null || !expected.exists())
fail("Expected file doesn't exist");
if (actual == null || !actual.exists())
fail("Actual file doesn't exist");
final long expectedFileLen = expected.length();
final long fileLen = actual.length();
Assert.assertTrue("expectedFileLen=" + expectedFileLen + ", fileLen=" + fileLen + ", expectedFile=" + expected
+ ", file=" + actual, expectedFileLen == fileLen);
try {
assertStreamEqualsStream("expected file: " + expected + " vs. actual file: " + actual,
new FileInputStream(expected), new FileInputStream(actual));
} catch (Exception e) {
fail("Unable to compare files: " + e.getMessage());
}
}
/**
* Asserts that the contents in the specified string are exactly equal to the contents read from
* the specified input stream. The input stream will be closed at the end of this method. If any
* problems are encountered, or the stream's contents don't match up exactly with the string's
* contents, then this method will fail the current test.
*
* @param expected
* The string containing the expected data.
* @param actual
* The stream that will be read, compared to the expected string data, and finally
* closed.
*/
public static void assertStringEqualsStream(String expected, InputStream actual) {
try {
InputStream expectedInputStream = new ByteArrayInputStream(expected.getBytes());
assertTrue(doesStreamEqualStream(expectedInputStream, actual));
} catch (IOException e) {
fail("Error reading from stream: " + e.getMessage());
}
}
/**
* Returns true if, and only if, the contents read from the specified input streams are exactly
* equal. Both input streams will be closed at the end of this method.
*
* @param expected
* The input stream containing the expected contents.
* @param inputStream
* The stream that will be read, compared to the expected file contents, and finally
* closed.
* @return True if the two input streams contain the same data.
* @throws IOException
* If any problems are encountered comparing the file and stream.
*/
public static boolean doesStreamEqualStream(InputStream expected, InputStream actual) throws IOException {
try {
final byte[] expectedDigest = InputStreamUtils.calculateMD5Digest(expected);
final byte[] actualDigest = InputStreamUtils.calculateMD5Digest(actual);
return Arrays.equals(expectedDigest, actualDigest);
} catch (NoSuchAlgorithmException nse) {
throw new AmazonClientException(nse.getMessage(), nse);
} finally {
IOUtils.closeQuietly(expected, null);
IOUtils.closeQuietly(actual, null);
}
}
/**
* Returns true if, and only if, the contents in the specified file are exactly equal to the
* contents read from the specified input stream. The input stream will be closed at the end of
* this method.
*
* @param expectedFile
* The file containing the expected contents.
* @param inputStream
* The stream that will be read, compared to the expected file contents, and finally
* closed.
* @throws IOException
* If any problems are encountered comparing the file and stream.
*/
public static boolean doesFileEqualStream(File expectedFile, InputStream inputStream) throws IOException {
InputStream expectedInputStream = new FileInputStream(expectedFile);
return doesStreamEqualStream(expectedInputStream, inputStream);
}
/**
* Asserts that the specified AmazonServiceException is valid, meaning it has a non-empty,
* non-null value for its message, requestId, etc.
*
* @param e
* The exception to validate.
*/
public static void assertValidException(AmazonServiceException e) {
assertNotNull(e.getRequestId());
assertTrue(e.getRequestId().trim().length() > 0);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().trim().length() > 0);
assertNotNull(e.getErrorCode());
assertTrue(e.getErrorCode().trim().length() > 0);
assertNotNull(e.getServiceName());
assertTrue(e.getServiceName().startsWith("Amazon") || e.getServiceName().startsWith("AWS"));
}
}