/
TestHelpers.java
144 lines (129 loc) · 4.72 KB
/
TestHelpers.java
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
/**
* Java Web Archive Toolkit - Software to read and validate ARC, WARC
* and GZip files. (http://jwat.org/)
* Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.jwat.gzip;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.junit.Assert;
/**
* TODO javadoc
* @author Nicholas
* Created on 28/12/2012
*/
public class TestHelpers {
protected static ClassLoader clsLdr = TestHelpers.class.getClassLoader();
public static final File getTestResourceFile(String fname) {
URL url = clsLdr.getResource( fname );
if ( url == null ) {
throw new IllegalStateException( "Could not find resource, '" + fname + "'." );
}
String path = url.getFile();
if ( path == null ) {
throw new IllegalStateException( "Could not resolve path for resource, " + fname + "'." );
}
path = path.replaceAll( "%5b", "[" );
path = path.replaceAll( "%5d", "]" );
File file = new File( path );
return file;
}
public static final InputStream getTestResourceAsStream(String fname) {
return clsLdr.getResourceAsStream( fname );
}
public static void assertArrayEquals(boolean[] expecteds, boolean[] actuals) {
Assert.assertEquals( expecteds.length, actuals.length );
for ( int i=0; i<expecteds.length; ++i ) {
Assert.assertEquals( expecteds[ i ], actuals[ i ] );
}
}
public static void assertArrayEquals(float[] expecteds, float[] actuals) {
Assert.assertEquals( expecteds.length, actuals.length );
for ( int i=0; i<expecteds.length; ++i ) {
Assert.assertEquals( (Float)expecteds[ i ], (Float)actuals[ i ] );
}
}
public static void assertArrayEquals(double[] expecteds, double[] actuals) {
Assert.assertEquals( expecteds.length, actuals.length );
for ( int i=0; i<expecteds.length; ++i ) {
Assert.assertEquals( (Double)expecteds[ i ], (Double)actuals[ i ] );
}
}
public static byte[] filterWhitespaces(byte[] in) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
for (int i=0; i<in.length; ++ i) {
c = in[ i ] & 255;
switch ( c ) {
case ' ':
case '\t':
case '\r':
case '\n':
break;
default:
out.write( c );
break;
}
}
return out.toByteArray();
}
public static void saveFile(String fname, byte[] bytes) throws IOException {
File file = new File( fname );
if ( file.exists() ) {
file.delete();
}
RandomAccessFile raf = new RandomAccessFile( file, "rw" );
raf.seek( 0L );
raf.setLength( 0L );
raf.write( bytes );
raf.close();
}
public static<T> LinkedList<T> arrayToLinkedList(T[] arr) {
//return (ArrayList<T>)Arrays.asList( arr );
LinkedList<T> list = new LinkedList<T>();
for ( int i=0; i<arr.length; ++i ) {
list.add( arr[ i ] );
}
return list;
}
public static<T> ArrayList<T> arrayToArrayList(T[] arr) {
//return (ArrayList<T>)Arrays.asList( arr );
ArrayList<T> list = new ArrayList<T>();
for ( int i=0; i<arr.length; ++i ) {
list.add( arr[ i ] );
}
return list;
}
public static<T> void assertListEquals(List<T> expecteds, List<T> actuals) {
if (expecteds == null) {
if (actuals != null) {
Assert.fail( "Expected 'null' but got: " + actuals );
}
}
else {
Assert.assertEquals( "Sizes do not match, expected + " + expecteds.size() + " got " + actuals.size(), expecteds.size(), actuals.size() );
for ( int i=0; i<expecteds.size(); ++i ) {
Assert.assertEquals( "Elements at index: " + i + " do not match!", expecteds.get( i ), actuals.get( i ) );
}
}
}
}