View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package sk.baka.ambient.commons;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.InputStream;
23  import java.io.UnsupportedEncodingException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Set;
30  
31  import junit.framework.Assert;
32  
33  /***
34   * Test utilities.
35   * 
36   * @author Martin Vysny
37   */
38  public final class TestUtils {
39  	private TestUtils() {
40  		throw new Error();
41  	}
42  
43  	/***
44  	 * Converts given string char-by-char to a byte array. Each char is
45  	 * converted to a single byte. Only characters 0-127 are accepted.
46  	 * 
47  	 * @param str
48  	 *            the string to convert.
49  	 * @return the byte array.
50  	 */
51  	public static byte[] toAscii(final String str) {
52  		final byte[] result = new byte[str.length()];
53  		for (int i = 0; i < str.length(); i++) {
54  			final char c = str.charAt(i);
55  			if ((c < 0) || (c > 127)) {
56  				throw new IllegalArgumentException("String " + str
57  						+ " contains invalid character " + c + " (code "
58  						+ (int) c + ")");
59  			}
60  			result[i] = (byte) c;
61  		}
62  		return result;
63  	}
64  
65  	/***
66  	 * Converts given string to a byte array and returns an input stream.
67  	 * 
68  	 * @param str
69  	 *            the string to convert.
70  	 * @param encoding
71  	 *            the encoding
72  	 * @return the byte array.
73  	 * @throws UnsupportedEncodingException
74  	 */
75  	public static InputStream stringToInputStream(final String str,
76  			final String encoding) throws UnsupportedEncodingException {
77  		return new ByteArrayInputStream(str.getBytes(encoding));
78  	}
79  
80  	/***
81  	 * Compares given collection as lists, item by item. Ordering does matter.
82  	 * 
83  	 * @param <T>
84  	 *            the item type
85  	 * @param expected
86  	 *            expected list
87  	 * @param got
88  	 *            actual list.
89  	 */
90  	public static <T> void compareLists(final Collection<? extends T> expected,
91  			final Collection<? extends T> got) {
92  		final List<T> missing = new ArrayList<T>();
93  		final List<T> excess = new ArrayList<T>();
94  		final List<String> mismatches = new ArrayList<String>();
95  		final Iterator<? extends T> i1 = expected.iterator();
96  		final Iterator<? extends T> i2 = got.iterator();
97  		int index = 0;
98  		for (; i1.hasNext() || i2.hasNext(); index++) {
99  			if (!i2.hasNext()) {
100 				missing.add(i1.next());
101 				continue;
102 			}
103 			if (!i1.hasNext()) {
104 				excess.add(i2.next());
105 				continue;
106 			}
107 			final T t1 = i1.next();
108 			final T t2 = i2.next();
109 			if (!MiscUtils.nullEquals(t1, t2)) {
110 				mismatches.add("At " + index + ": expected " + t1 + " but got "
111 						+ t2);
112 			}
113 		}
114 		if (!missing.isEmpty() || !excess.isEmpty() || !mismatches.isEmpty()) {
115 			final StringBuilder b = new StringBuilder();
116 			if (!mismatches.isEmpty()) {
117 				b.append("| mismatches: ");
118 				b.append(mismatches);
119 			}
120 			if (!missing.isEmpty()) {
121 				b.append("| missing: ");
122 				b.append(missing);
123 			}
124 			if (!excess.isEmpty()) {
125 				b.append("| excess: ");
126 				b.append(excess);
127 			}
128 			Assert.fail(b.toString());
129 		}
130 	}
131 
132 	/***
133 	 * Compares given collection as sets. Ordering does not matter.
134 	 * 
135 	 * @param <T>
136 	 *            the item type
137 	 * @param expected
138 	 *            expected list
139 	 * @param got
140 	 *            actual list.
141 	 */
142 	public static <T> void compareSets(final Collection<? extends T> expected,
143 			final Collection<? extends T> got) {
144 		final Set<T> missing = new HashSet<T>(expected);
145 		missing.removeAll(got);
146 		final Set<T> excess = new HashSet<T>(got);
147 		excess.removeAll(expected);
148 		if (!missing.isEmpty() || !excess.isEmpty()) {
149 			final StringBuilder b = new StringBuilder();
150 			if (!missing.isEmpty()) {
151 				b.append("| missing: ");
152 				b.append(missing);
153 			}
154 			if (!excess.isEmpty()) {
155 				b.append("| excess: ");
156 				b.append(excess);
157 			}
158 			Assert.fail(b.toString());
159 		}
160 	}
161 }