package org.jdeferred.impl;

import org.jdeferred.DeferredFutureTask;
import org.jdeferred.DoneCallback;
import org.jdeferred.Promise;
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.Callable;

public class CancelTaskTest extends AbstractDeferredTest {

	@Test
	public void testCancelTask() {
		DeferredFutureTask<String, Void> deferredFutureTask =
				new DeferredFutureTask<String, Void>(new Callable<String>() {
					@Override
					public String call() throws Exception {
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
						}

						return "Hello";
					}
				});

		Promise<String, Throwable, Void> promise = deferredManager.when(deferredFutureTask);

		deferredFutureTask.cancel(false);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}

		promise.then(new DoneCallback<String>() {
			@Override
			public void onDone(String result) {
				Assert.fail("Shouldn't be called, because task was cancelled");
			}
		});

		Assert.assertTrue(deferredFutureTask.isCancelled());
	}
}

@@ -22,16 +22,53 @@
 .impl;%0A%0A
+import org.jdeferred.AlwaysCallback;%0A
 import o
@@ -100,16 +100,16 @@
 reTask;%0A
-
 import o
@@ -165,16 +165,52 @@
 romise;%0A
+import org.jdeferred.Promise.State;%0A
 import o
@@ -250,16 +250,16 @@
 .Test;%0A%0A
-
 import j
@@ -287,16 +287,67 @@
 allable;
+%0Aimport java.util.concurrent.CancellationException;
 %0A%0Apublic
@@ -1115,16 +1115,278 @@
 );%0A%09%09%09%7D%0A
+%09%09%7D).always(new AlwaysCallback%3CString, Throwable%3E() %7B%0A%09%09%09@Override%0A%09%09%09public void onAlways(State state, String resolved, Throwable rejected) %7B%0A%09%09%09%09Assert.assertEquals(State.REJECTED, state);%0A%09%09%09%09Assert.assertTrue(rejected instanceof CancellationException);%0A%09%09%09%7D%0A
 %09%09%7D);%0A%0A%09

Check promise state after future task was cancelled. Related to #87