提交 5b188b5d 编写于 作者: P Patrick Forhan

Merge pull request #52 from square/jw/tests

Re-enable 'android' and 'io' tests. Switch to FEST.
......@@ -31,6 +31,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
......@@ -2,21 +2,21 @@
package retrofit.android;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.fest.assertions.Assertions.assertThat;
/** @author Eric Burke (eric@squareup.com) */
public class ShakeDetectorTest {
public void testInitialShaking() {
@Test public void testInitialShaking() {
ShakeDetector.SampleQueue q = new ShakeDetector.SampleQueue();
assertFalse("shaking", q.isShaking());
assertThat(q.isShaking()).isFalse();
}
/** Tests LG Ally sample rate. */
public void testShakingSampleCount3() {
@Test public void testShakingSampleCount3() {
ShakeDetector.SampleQueue q = new ShakeDetector.SampleQueue();
// These times approximate the data rate of the slowest device we've
......@@ -29,26 +29,26 @@ public class ShakeDetectorTest {
q.add(1600000000L, false);
q.add(1900000000L, false);
assertContent(q, false, false, false, false);
assertFalse("shaking", q.isShaking());
assertThat(q.isShaking()).isFalse();
// The oldest two entries will be removed.
q.add(2200000000L, true);
q.add(2500000000L, true);
assertContent(q, false, false, true, true);
assertFalse("shaking", q.isShaking());
assertThat(q.isShaking()).isFalse();
// Another entry should be removed, now 3 out of 4 are true.
q.add(2800000000L, true);
assertContent(q, false, true, true, true);
assertTrue("shaking", q.isShaking());
assertThat(q.isShaking()).isTrue();
q.add(3100000000L, false);
assertContent(q, true, true, true, false);
assertTrue("shaking", q.isShaking());
assertThat(q.isShaking()).isTrue();
q.add(3400000000L, false);
assertContent(q, true, true, false, false);
assertFalse("shaking", q.isShaking());
assertThat(q.isShaking()).isFalse();
}
private void assertContent(ShakeDetector.SampleQueue q, boolean... expected) {
......@@ -59,20 +59,20 @@ public class ShakeDetectorTest {
sb.append(String.format("[%b,%d] ", s.accelerating, s.timestamp));
}
assertEquals(sb.toString(), expected.length, samples.size());
assertThat(samples.size()).isEqualTo(expected.length).as(sb.toString());
for (int i = 0; i < expected.length; i++) {
assertEquals("sample[" + i + "] accelerating",
expected[i], samples.get(i).accelerating);
assertThat(samples.get(i).accelerating).isEqualTo(expected[i])
.as("sample[" + i + "] accelerating");
}
}
public void testClear() {
@Test public void testClear() {
ShakeDetector.SampleQueue q = new ShakeDetector.SampleQueue();
q.add(1000000000L, true);
q.add(1200000000L, true);
q.add(1400000000L, true);
assertTrue("shaking", q.isShaking());
assertThat(q.isShaking()).isTrue();
q.clear();
assertFalse("shaking", q.isShaking());
assertThat(q.isShaking()).isFalse();
}
}
......@@ -43,6 +43,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
......
......@@ -3,13 +3,13 @@ package retrofit.http;
import com.google.gson.Gson;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Test;
import retrofit.io.ByteSink;
import javax.inject.Provider;
......@@ -19,14 +19,20 @@ import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.Executor;
import static org.easymock.EasyMock.*;
import static org.easymock.EasyMock.anyInt;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.fest.assertions.Assertions.assertThat;
/**
* Fetcher test cases.
*
* @author Bob Lee (bob@squareup.com)
*/
public class FetcherTest extends TestCase {
public class FetcherTest {
private static final String URL = "http://crazybob.org/";
......@@ -39,7 +45,7 @@ public class FetcherTest extends TestCase {
private ProgressListener progressListener
= createMock(ProgressListener.class);
public void testSuccessfulFetch() throws Exception {
@Test public void testSuccessfulFetch() throws Exception {
DummyInputStream in = new DummyInputStream();
final SingletonHttpClient httpClient = new SingletonHttpClient(response);
DummyExecutor executor = new DummyExecutor();
......@@ -65,12 +71,10 @@ public class FetcherTest extends TestCase {
fetcher.fetch(URL, sinkFactory, callback, progressListener);
verifyAll();
assertEquals(1, executor.calls);
assertTrue(uiThread.calls > 1); // result + progress updates
assertEquals(1, httpClient.calls);
byte[] output = sink.toArray();
assertTrue("Expected {1, 2, 3}. Got " + Arrays.toString(output),
Arrays.equals(new byte[] { 1, 2, 3 }, output));
assertThat(executor.calls).isEqualTo(1);
assertThat(uiThread.calls).isGreaterThan(1); // result + progress updates
assertThat(httpClient.calls).isEqualTo(1);
assertThat(sink.toArray()).isEqualTo(new byte[] { 1, 2, 3 });
}
private void replayAll() {
......
......@@ -2,20 +2,24 @@
package retrofit.http;
import com.google.gson.Gson;
import org.apache.http.HttpMessage;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Test;
import javax.inject.Named;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.Set;
import java.util.UUID;
import javax.inject.Named;
import junit.framework.TestCase;
import org.apache.http.HttpMessage;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
/** @author Eric Denman (edenman@squareup.com) */
public class HttpRequestBuilderTest extends TestCase {
public class HttpRequestBuilderTest {
public static final String API_URL = "http://taqueria.com/lengua/taco";
public static final Headers BLANK_HEADERS = new Headers() {
@Override public void setOn(HttpMessage message, String mimeType) {
......@@ -36,28 +40,28 @@ public class HttpRequestBuilderTest extends TestCase {
private void expectParams(String path, String... expected) {
Set<String> calculated = HttpRequestBuilder.getPathParameters(path);
assertEquals(expected.length, calculated.size());
assertThat(calculated.size()).isEqualTo(expected.length);
for (String val : expected) {
assertTrue(calculated.contains(val));
assertThat(calculated).contains(val);
}
}
public void testNormalGet() throws Exception {
@Test public void testNormalGet() throws Exception {
Method method =
MyService.class.getMethod("normalGet", String.class, Callback.class);
String expectedId = UUID.randomUUID().toString();
Object[] args = new Object[] {expectedId, new MyCallback()};
HttpUriRequest request = build(method, args);
assertTrue(request instanceof HttpGet);
assertThat(request).isInstanceOf(HttpGet.class);
HttpGet put = (HttpGet) request;
// Make sure the url param got translated.
final String uri = put.getURI().toString();
assertEquals(API_URL + "/foo/bar?id=" + expectedId, uri);
assertThat(uri).isEqualTo(API_URL + "/foo/bar?id=" + expectedId);
}
public void testGetWithPathParam() throws Exception {
@Test public void testGetWithPathParam() throws Exception {
Method method =
MyService.class.getMethod("getWithPathParam", String.class, String.class, Callback.class);
String expectedId = UUID.randomUUID().toString();
......@@ -65,15 +69,15 @@ public class HttpRequestBuilderTest extends TestCase {
Object[] args = new Object[] {expectedId, category, new MyCallback()};
HttpUriRequest request = build(method, args);
assertTrue(request instanceof HttpGet);
assertThat(request).isInstanceOf(HttpGet.class);
HttpGet put = (HttpGet) request;
// Make sure the url param got translated.
final String uri = put.getURI().toString();
assertEquals(API_URL + "/foo/" + expectedId + "/bar?category=" + category, uri);
assertThat(uri).isEqualTo(API_URL + "/foo/" + expectedId + "/bar?category=" + category);
}
public void testSingleEntityWithPathParams() throws Exception {
@Test public void testSingleEntityWithPathParams() throws Exception {
Method method =
MyService.class.getMethod("singleEntityPut", MyJsonObj.class, String.class, Callback.class);
String expectedId = UUID.randomUUID().toString();
......@@ -81,21 +85,21 @@ public class HttpRequestBuilderTest extends TestCase {
Object[] args = new Object[] {new MyJsonObj(bodyText), expectedId, new MyCallback()};
HttpUriRequest request = build(method, args);
assertTrue(request instanceof HttpPut);
assertThat(request).isInstanceOf(HttpPut.class);
HttpPut put = (HttpPut) request;
// Make sure the url param got translated.
final String uri = put.getURI().toString();
assertEquals(API_URL + "/foo/bar/" + expectedId, uri);
assertThat(uri).isEqualTo(API_URL + "/foo/bar/" + expectedId);
// Make sure the request body has the json string.
ByteArrayOutputStream out = new ByteArrayOutputStream();
put.getEntity().writeTo(out);
final String requestBody = out.toString();
assertEquals("{\"bodyText\":\"" + bodyText + "\"}", requestBody);
assertThat(requestBody).isEqualTo("{\"bodyText\":\"" + bodyText + "\"}");
}
public void testNormalPutWithPathParams() throws Exception {
@Test public void testNormalPutWithPathParams() throws Exception {
Method method =
MyService.class.getMethod("normalPut", String.class, String.class, Callback.class);
String expectedId = UUID.randomUUID().toString();
......@@ -103,21 +107,21 @@ public class HttpRequestBuilderTest extends TestCase {
Object[] args = new Object[] {expectedId, bodyText, new MyCallback()};
HttpUriRequest request = build(method, args);
assertTrue(request instanceof HttpPut);
assertThat(request).isInstanceOf(HttpPut.class);
HttpPut put = (HttpPut) request;
// Make sure the url param got translated.
final String uri = put.getURI().toString();
assertEquals(API_URL + "/foo/bar/" + expectedId, uri);
assertThat(uri).isEqualTo(API_URL + "/foo/bar/" + expectedId);
// Make sure the request body has the json string.
ByteArrayOutputStream out = new ByteArrayOutputStream();
put.getEntity().writeTo(out);
final String requestBody = out.toString();
assertEquals("id=" + expectedId + "&body=" + bodyText, requestBody);
assertThat(requestBody).isEqualTo("id=" + expectedId + "&body=" + bodyText);
}
public void testSingleEntityWithTooManyParams() throws Exception {
@Test public void testSingleEntityWithTooManyParams() throws Exception {
Method method =
MyService.class.getMethod("tooManyParams", MyJsonObj.class, String.class, String.class,
Callback.class);
......@@ -127,12 +131,11 @@ public class HttpRequestBuilderTest extends TestCase {
try {
build(method, args);
fail("Didn't throw exception with too many params");
} catch (IllegalArgumentException e) {
// Expected
} catch (IllegalArgumentException expected) {
}
}
public void testSingleEntityWithNoPathParam() throws Exception {
@Test public void testSingleEntityWithNoPathParam() throws Exception {
Method method =
MyService.class.getMethod("singleEntityNoPathParam", MyJsonObj.class, Callback.class);
String bodyText = UUID.randomUUID().toString();
......@@ -140,20 +143,18 @@ public class HttpRequestBuilderTest extends TestCase {
try {
build(method, args);
fail("Didn't throw exception with too few params");
} catch (IllegalArgumentException e) {
// Expected
} catch (IllegalArgumentException expected) {
}
}
public void testRegularWithNoPathParam() throws Exception {
@Test public void testRegularWithNoPathParam() throws Exception {
Method method = MyService.class.getMethod("regularNoPathParam", String.class, Callback.class);
String otherParam = UUID.randomUUID().toString();
Object[] args = new Object[] {otherParam, new MyCallback()};
try {
build(method, args);
fail("Didn't throw exception with too few params");
} catch (IllegalArgumentException e) {
// Expected
} catch (IllegalArgumentException expected) {
}
}
......
package retrofit.http;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executor;
import javax.inject.Named;
import javax.inject.Provider;
import junit.framework.TestCase;
import org.apache.http.HttpMessage;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
......@@ -21,6 +15,13 @@ import org.apache.http.message.BasicStatusLine;
import org.easymock.Capture;
import org.easymock.IAnswer;
import org.junit.Before;
import org.junit.Test;
import javax.inject.Named;
import javax.inject.Provider;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executor;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
......@@ -29,8 +30,9 @@ import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.fest.assertions.Assertions.assertThat;
public class RestAdapterTest extends TestCase {
public class RestAdapterTest {
private static final String ID = "123";
private static final String ENTITY = "entity";
private static final String ENTITY_PATH_PARAM = "entity/{id}";
......@@ -47,7 +49,7 @@ public class RestAdapterTest extends TestCase {
private HttpResponse mockResponse;
private Gson gson = new Gson();
@Override @Before public void setUp() throws Exception {
@Before public void setUp() throws Exception {
mockHttpClient = createMock(HttpClient.class);
mockExecutor = createMock(Executor.class);
mockMainThread = createMock(MainThread.class);
......@@ -65,7 +67,8 @@ public class RestAdapterTest extends TestCase {
mockHeaders, gson, HttpProfiler.NONE);
}
@SuppressWarnings("unchecked") public void testServiceDeleteSimple() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceDeleteSimple() throws IOException {
expectLifecycle(HttpDelete.class, GET_DELETE_SIMPLE_URL);
replayAll();
......@@ -74,7 +77,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServiceDeleteParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceDeleteParam() throws IOException {
expectLifecycle(HttpDelete.class, GET_DELETE_SIMPLE_URL + "id=" + ID);
replayAll();
......@@ -83,7 +87,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServiceDeleteWithFixedParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceDeleteWithFixedParam() throws IOException {
expectLifecycle(HttpDelete.class, GET_DELETE_SIMPLE_URL + "filter=merchant&"
+ "id=" + ID);
replayAll();
......@@ -94,7 +99,7 @@ public class RestAdapterTest extends TestCase {
}
@SuppressWarnings("unchecked")
public void testServiceDeleteWithMultipleFixedParam() throws IOException {
@Test public void testServiceDeleteWithMultipleFixedParam() throws IOException {
expectLifecycle(HttpDelete.class, GET_DELETE_SIMPLE_URL
+ "filter=merchant&name2=value2&"+ "id=" + ID);
replayAll();
......@@ -104,7 +109,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServiceDeletePathParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceDeletePathParam() throws IOException {
expectLifecycle(HttpDelete.class, PATH_URL_PREFIX + ID + "?");
replayAll();
......@@ -113,7 +119,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServiceGetSimple() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceGetSimple() throws IOException {
expectLifecycle(HttpGet.class, GET_DELETE_SIMPLE_URL);
replayAll();
......@@ -122,7 +129,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServiceGetParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceGetParam() throws IOException {
expectLifecycle(HttpGet.class, GET_DELETE_SIMPLE_URL + "id=" + ID);
replayAll();
......@@ -131,7 +139,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServiceGetWithFixedParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceGetWithFixedParam() throws IOException {
expectLifecycle(HttpGet.class, GET_DELETE_SIMPLE_URL + "filter=merchant&"
+ "id=" + ID);
replayAll();
......@@ -142,7 +151,7 @@ public class RestAdapterTest extends TestCase {
}
@SuppressWarnings("unchecked")
public void testServiceGetWithMultipleFixedParams() throws IOException {
@Test public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpGet.class, GET_DELETE_SIMPLE_URL
+ "filter=merchant&name2=value2&"+ "id=" + ID);
replayAll();
......@@ -152,7 +161,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServiceGetPathParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServiceGetPathParam() throws IOException {
expectLifecycle(HttpGet.class, PATH_URL_PREFIX + ID + "?");
replayAll();
......@@ -161,7 +171,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePostSimple() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePostSimple() throws IOException {
expectLifecycle(HttpPost.class, BASE_URL);
replayAll();
......@@ -170,7 +181,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePostSimpleClientError() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePostSimpleClientError() throws IOException {
expectLifecycleClientError(HttpPost.class, BASE_URL);
replayAll();
......@@ -179,7 +191,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePostSimpleServerError() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePostSimpleServerError() throws IOException {
expectLifecycleServerError(HttpPost.class, BASE_URL);
replayAll();
......@@ -188,7 +201,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePostParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePostParam() throws IOException {
expectLifecycle(HttpPost.class, BASE_URL);
replayAll();
......@@ -197,7 +211,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePostPathParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePostPathParam() throws IOException {
expectLifecycle(HttpPost.class, PATH_URL_PREFIX + ID);
replayAll();
......@@ -206,7 +221,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePutSimple() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePutSimple() throws IOException {
expectLifecycle(HttpPut.class, BASE_URL);
replayAll();
......@@ -215,7 +231,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePutParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePutParam() throws IOException {
expectLifecycle(HttpPut.class, BASE_URL);
replayAll();
......@@ -224,7 +241,8 @@ public class RestAdapterTest extends TestCase {
verifyAll();
}
@SuppressWarnings("unchecked") public void testServicePutPathParam() throws IOException {
@SuppressWarnings("unchecked")
@Test public void testServicePutPathParam() throws IOException {
expectLifecycle(HttpPut.class, PATH_URL_PREFIX + ID);
replayAll();
......@@ -319,7 +337,7 @@ public class RestAdapterTest extends TestCase {
expectLastCall().andAnswer(new IAnswer<Object>() {
@Override public Object answer() throws Throwable {
T request = expectedRequestClass.cast(capture.getValue());
assertEquals("uri should match expectations", expectedUri, request.getURI().toString());
assertThat(request.getURI().toString()).isEqualTo(expectedUri);
return null;
}
});
......
......@@ -29,6 +29,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
// Copyright 2011 Square, Inc.
package retrofit.io;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
/**
* @author Paul Hawke (psh@squareup.com)
*/
public class FilesTest {
public void testDelete() throws Exception {
@Test public void testDelete() throws Exception {
File tmpFile = File.createTempFile("prefix", ".tmp");
PrintWriter pw = new PrintWriter(new FileWriter(tmpFile));
pw.println("content");
pw.close();
assertTrue(tmpFile.exists());
assertTrue(Files.delete(tmpFile));
assertFalse(tmpFile.exists());
assertThat(tmpFile.exists()).isTrue();
assertThat(Files.delete(tmpFile)).isTrue();
assertThat(tmpFile.exists()).isFalse();
}
public void testDeleteFileThatDoesntExist() throws Exception {
@Test public void testDeleteFileThatDoesntExist() throws Exception {
File tmpFile = File.createTempFile("foobar", ".tmp");
assertTrue("unable to delete temporary file", tmpFile.delete());
assertThat(tmpFile.delete()).as("unable to delete temporary file").isTrue();
assertFalse(tmpFile.exists());
assertTrue(Files.delete(tmpFile));
assertFalse(tmpFile.exists());
assertThat(tmpFile.exists()).isFalse();
assertThat(Files.delete(tmpFile)).isTrue();
assertThat(tmpFile.exists()).isFalse();
}
public void testDeleteNullFile() throws Exception {
@Test public void testDeleteNullFile() throws Exception {
try {
Files.delete(null);
fail("Expected an IAE");
......
......@@ -3,6 +3,7 @@ package retrofit.io;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
......@@ -13,7 +14,7 @@ import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.fail;
/**
......@@ -30,7 +31,7 @@ public class QueueFileTest {
* Takes up 33401 bytes in the queue (N*(N+1)/2+4*N). Picked 254 instead of
* 255 so that the number of bytes isn't a multiple of 4.
*/
private static int N = 254; //
private static int N = 254;
private static byte[][] values = new byte[N][];
static {
......@@ -44,27 +45,27 @@ public class QueueFileTest {
private File file;
@Before protected void setUp() throws Exception {
@Before public void setUp() throws Exception {
file = File.createTempFile("test.queue", null);
file.delete();
}
@After protected void tearDown() throws Exception {
@After public void tearDown() throws Exception {
file.delete();
}
public void testAddOneElement() throws IOException {
@Test public void testAddOneElement() throws IOException {
// This test ensures that we update 'first' correctly.
QueueFile queue = new QueueFile(file);
byte[] expected = values[253];
queue.add(expected);
assertEquals(expected, queue.peek());
assertThat(queue.peek()).isEqualTo(expected);
queue.close();
queue = new QueueFile(file);
assertEquals(expected, queue.peek());
assertThat(queue.peek()).isEqualTo(expected);
}
public void testAddAndRemoveElements() throws IOException {
@Test public void testAddAndRemoveElements() throws IOException {
long start = System.nanoTime();
Queue<byte[]> expected = new LinkedList<byte[]>();
......@@ -79,7 +80,7 @@ public class QueueFileTest {
// Leave N elements in round N, 15 total for 5 rounds. Removing all the
// elements would be like starting with an empty queue.
for (int i = 0; i < N - round - 1; i++) {
assertEquals(expected.remove(), queue.peek());
assertThat(queue.peek()).isEqualTo(expected.remove());
queue.remove();
}
queue.close();
......@@ -87,10 +88,10 @@ public class QueueFileTest {
// Remove and validate remaining 15 elements.
QueueFile queue = new QueueFile(file);
assertEquals(15, queue.size());
assertEquals(expected.size(), queue.size());
assertThat(queue.size()).isEqualTo(15);
assertThat(queue.size()).isEqualTo(expected.size());
while (!expected.isEmpty()) {
assertEquals(expected.remove(), queue.peek());
assertThat(queue.peek()).isEqualTo(expected.remove());
queue.remove();
}
queue.close();
......@@ -102,7 +103,7 @@ public class QueueFileTest {
}
/** Tests queue expansion when the data crosses EOF. */
public void testSplitExpansion() throws IOException {
@Test public void testSplitExpansion() throws IOException {
// This should result in 3560 bytes.
int max = 80;
......@@ -116,7 +117,7 @@ public class QueueFileTest {
// Remove all but 1.
for (int i = 1; i < max; i++) {
assertEquals(expected.remove(), queue.peek());
assertThat(queue.peek()).isEqualTo(expected.remove());
queue.remove();
}
......@@ -127,14 +128,14 @@ public class QueueFileTest {
}
while (!expected.isEmpty()) {
assertEquals(expected.remove(), queue.peek());
assertThat(queue.peek()).isEqualTo(expected.remove());
queue.remove();
}
queue.close();
}
public void testFailedAdd() throws IOException {
@Test public void testFailedAdd() throws IOException {
QueueFile queueFile = new QueueFile(file);
queueFile.add(values[253]);
queueFile.close();
......@@ -155,13 +156,13 @@ public class QueueFileTest {
queueFile.close();
queueFile = new QueueFile(file);
assertEquals(2, queueFile.size());
assertEquals(values[253], queueFile.peek());
assertThat(queueFile.size()).isEqualTo(2);
assertThat(queueFile.peek()).isEqualTo(values[253]);
queueFile.remove();
assertEquals(values[251], queueFile.peek());
assertThat(queueFile.peek()).isEqualTo(values[251]);
}
public void testFailedRemoval() throws IOException {
@Test public void testFailedRemoval() throws IOException {
QueueFile queueFile = new QueueFile(file);
queueFile.add(values[253]);
queueFile.close();
......@@ -177,15 +178,15 @@ public class QueueFileTest {
queueFile.close();
queueFile = new QueueFile(file);
assertEquals(1, queueFile.size());
assertEquals(values[253], queueFile.peek());
assertThat(queueFile.size()).isEqualTo(1);
assertThat(queueFile.peek()).isEqualTo(values[253]);
queueFile.add(values[99]);
queueFile.remove();
assertEquals(values[99], queueFile.peek());
assertThat(queueFile.peek()).isEqualTo(values[99]);
}
public void testFailedExpansion() throws IOException {
@Test public void testFailedExpansion() throws IOException {
QueueFile queueFile = new QueueFile(file);
queueFile.add(values[253]);
queueFile.close();
......@@ -203,16 +204,16 @@ public class QueueFileTest {
queueFile = new QueueFile(file);
assertEquals(1, queueFile.size());
assertEquals(values[253], queueFile.peek());
assertEquals(4096, queueFile.fileLength);
assertThat(queueFile.size()).isEqualTo(1);
assertThat(queueFile.peek()).isEqualTo(values[253]);
assertThat(queueFile.fileLength).isEqualTo(4096);
queueFile.add(values[99]);
queueFile.remove();
assertEquals(values[99], queueFile.peek());
assertThat(queueFile.peek()).isEqualTo(values[99]);
}
public void testPeekWithElementReader() throws IOException {
@Test public void testPeekWithElementReader() throws IOException {
QueueFile queueFile = new QueueFile(file);
final byte[] a = {1, 2};
queueFile.add(a);
......@@ -221,19 +222,19 @@ public class QueueFileTest {
queueFile.peek(new QueueFile.ElementReader() {
@Override public void read(InputStream in, int length) throws IOException {
assertEquals(length, 2);
assertThat(length).isEqualTo(2);
byte[] actual = new byte[length];
in.read(actual);
assertEquals(a, actual);
assertThat(actual).isEqualTo(a);
}
});
queueFile.peek(new QueueFile.ElementReader() {
@Override public void read(InputStream in, int length) throws IOException {
assertEquals(length, 2);
assertEquals(1, in.read());
assertEquals(2, in.read());
assertEquals(-1, in.read());
assertThat(length).isEqualTo(2);
assertThat(in.read()).isEqualTo(1);
assertThat(in.read()).isEqualTo(2);
assertThat(in.read()).isEqualTo(-1);
}
});
......@@ -241,18 +242,18 @@ public class QueueFileTest {
queueFile.peek(new QueueFile.ElementReader() {
@Override public void read(InputStream in, int length) throws IOException {
assertEquals(length, 3);
assertThat(length).isEqualTo(3);
byte[] actual = new byte[length];
in.read(actual);
assertEquals(b, actual);
assertThat(actual).isEqualTo(b);
}
});
assertEquals(b, queueFile.peek());
assertEquals(1, queueFile.size());
assertThat(queueFile.peek()).isEqualTo(b);
assertThat(queueFile.size()).isEqualTo(1);
}
public void testForEach() throws IOException {
@Test public void testForEach() throws IOException {
QueueFile queueFile = new QueueFile(file);
final byte[] a = {1, 2};
......@@ -264,15 +265,15 @@ public class QueueFileTest {
QueueFile.ElementReader elementReader = new QueueFile.ElementReader() {
@Override public void read(InputStream in, int length) throws IOException {
if (iteration[0] == 0) {
assertEquals(length, 2);
assertThat(length).isEqualTo(2);
byte[] actual = new byte[length];
in.read(actual);
assertEquals(a, actual);
assertThat(actual).isEqualTo(a);
} else if (iteration[0] == 1) {
assertEquals(length, 3);
assertThat(length).isEqualTo(3);
byte[] actual = new byte[length];
in.read(actual);
assertEquals(b, actual);
assertThat(actual).isEqualTo(b);
} else {
fail();
}
......@@ -282,8 +283,8 @@ public class QueueFileTest {
queueFile.forEach(elementReader);
assertEquals(a, queueFile.peek());
assertEquals(2, iteration[0]);
assertThat(queueFile.peek()).isEqualTo(a);
assertThat(iteration[0]).isEqualTo(2);
}
/**
......@@ -291,7 +292,7 @@ public class QueueFileTest {
* QueueFile was forced to expand in size and a portion of the final Element
* had been wrapped into space at the beginning of the file.
*/
public void testFileExpansionDoesntCorruptWrappedElements()
@Test public void testFileExpansionDoesntCorruptWrappedElements()
throws IOException {
QueueFile queue = new QueueFile(file);
......@@ -326,9 +327,8 @@ public class QueueFileTest {
queue.remove();
for (int i = 0; i < value.length; i++) {
assertEquals(
"Block " + (blockNum + 1) + " corrupted at byte index " + i,
(byte) (blockNum + 1), value[i]);
assertThat(value[i]).isEqualTo((byte) (blockNum + 1))
.as("Block " + (blockNum + 1) + " corrupted at byte index " + i);
}
}
......@@ -342,7 +342,7 @@ public class QueueFileTest {
* Elements have been written to empty buffer space at the start does the
* expansion correctly update all their positions?
*/
public void testFileExpansionCorrectlyMovesElements() throws IOException {
@Test public void testFileExpansionCorrectlyMovesElements() throws IOException {
QueueFile queue = new QueueFile(file);
// Create test data - 1k blocks marked consecutively 1, 2, 3, 4 and 5.
......@@ -394,9 +394,8 @@ public class QueueFileTest {
queue.remove();
for (int i = 0; i < value.length; i++) {
assertEquals("Block " + (expectedBlockNumber) +
" corrupted at byte index " + i,
expectedBlockNumber, value[i]);
assertThat(value[i]).isEqualTo(expectedBlockNumber)
.as("Block " + expectedBlockNumber + " corrupted at byte index " + i);
}
}
......
// Copyright 2010 Square, Inc.
package retrofit.io;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;
/** @author Eric Burke (eric@squareup.com) */
public class TypedByteArrayTest {
public void testEquals() {
TypedByteArray a1 = new TypedByteArray(new byte[]{10, 20}, MimeType.GIF);
TypedByteArray a2 = new TypedByteArray(new byte[]{10, 20}, MimeType.GIF);
TypedByteArray b = new TypedByteArray(new byte[]{8, 12}, MimeType.GIF);
@Test public void testEquals() {
TypedByteArray a1 = new TypedByteArray(new byte[]{ 10, 20 }, MimeType.GIF);
TypedByteArray a2 = new TypedByteArray(new byte[]{ 10, 20 }, MimeType.GIF);
TypedByteArray b = new TypedByteArray(new byte[]{ 8, 12 }, MimeType.GIF);
assertEquals("equals", a1, a2);
assertEquals("hashCode", a1.hashCode(), a2.hashCode());
assertFalse("equals", a1.equals(b));
assertFalse("hashCode", a1.hashCode() == b.hashCode());
assertThat(a1).isEqualTo(a2);
assertThat(a1.hashCode()).isEqualTo(a2.hashCode());
assertThat(a1).isNotEqualTo(b);
assertThat(a1.hashCode()).isNotEqualTo(b.hashCode());
}
}
// Copyright 2010 Square, Inc.
package retrofit.io;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.fest.assertions.Assertions.assertThat;
/** @author Eric Burke (eric@squareup.com) */
public class TypedFileTest {
public void testNotEquals() {
@Test public void testNotEquals() {
TypedFile a = new TypedFile(new File("a.png"), MimeType.PNG);
TypedFile b = new TypedFile(new File("b.png"), MimeType.PNG);
assertFalse("equals", a.equals(b));
assertFalse("hash code", a.hashCode() == b.hashCode());
assertThat(a).isNotEqualTo(b);
assertThat(a.hashCode()).isNotEqualTo(b.hashCode());
}
public void testEquals() {
@Test public void testEquals() {
TypedFile a1 = new TypedFile(new File("a.png"), MimeType.PNG);
TypedFile a2 = new TypedFile(new File("a.png"), MimeType.PNG);
assertEquals(a1, a2);
assertEquals("hash code", a1.hashCode(), a2.hashCode());
assertThat(a1).isEqualTo(a2);
assertThat(a1.hashCode()).isEqualTo(a2.hashCode());
}
public void testToString() {
@Test public void testToString() {
File file = new File("/path/to/file.png");
assertEquals(file.getAbsolutePath() + " (PNG)",
new TypedFile(file, MimeType.PNG).toString());
assertThat(new TypedFile(file, MimeType.PNG).toString())
.isEqualTo(file.getAbsolutePath() + " (PNG)");
}
public void testLength() throws IOException {
@Test public void testLength() throws IOException {
File tempFile = File.createTempFile("foo", ".tmp");
try {
TypedFile typedFile = new TypedFile(tempFile, MimeType.PNG);
assertEquals("length", 0, typedFile.length());
assertThat(typedFile.length()).isZero();
writeToFile(tempFile, new byte[]{0, 1, 2, 3, 4});
assertEquals("file length", 5, tempFile.length());
assertEquals("typed file length", 5, typedFile.length());
assertThat(tempFile.length()).isEqualTo(5);
assertThat(typedFile.length()).isEqualTo(5);
} finally {
//noinspection ResultOfMethodCallIgnored
......
// Copyright 2010 Square, Inc.
package retrofit.io.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
/** @author Eric Burke (eric@squareup.com) */
public class ObjectsTest {
public void testNonNull() {
@Test public void testNonNull() {
Objects.nonNull(10, "whatever");
String message = "fail";
try {
Objects.nonNull(null, "fail");
Objects.nonNull(null, message);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
assertEquals("fail", expected.getMessage());
assertThat(expected.getMessage()).isEqualTo(message);
}
}
}
......@@ -59,6 +59,7 @@
<!-- Test Dependencies -->
<junit.version>4.10</junit.version>
<fest.version>1.4</fest.version>
<easymock.version>3.1</easymock.version>
</properties>
......@@ -138,6 +139,11 @@
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<version>${fest.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册