/*
 * Copyright 2013 Ray Tsang
 * 
 * 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.jdeferred.impl;

import java.util.concurrent.Callable;

import org.jdeferred.Deferred;
import org.jdeferred.DoneCallback;
import org.jdeferred.DonePipe;
import org.jdeferred.FailCallback;
import org.jdeferred.FailPipe;
import org.junit.Test;

public class PipedPromiseTest extends AbstractDeferredTest {
	@Test
	public void testDoneRewireFilter() {
		final ValueHolder<Integer> preRewireValue = new ValueHolder<Integer>();
		final ValueHolder<Integer> postRewireValue = new ValueHolder<Integer>();
		
		Callable<Integer> task = new Callable<Integer>() {
			public Integer call() {
				return 100;
			}
		};
		
		deferredManager.when(task).then(new DonePipe<Integer, Integer, Void, Void>() {
			@Override
			public Deferred<Integer, Void, Void> pipeDone(Integer result) {
				preRewireValue.set(result);
				return new DeferredObject<Integer, Void, Void>().resolve(1000);
			}
		}).done(new DoneCallback<Integer>() {
			@Override
			public void onDone(Integer value) {
				postRewireValue.set(value);
			}
		});
		
		waitForCompletion();
		preRewireValue.assertEquals(100);
		postRewireValue.assertEquals(1000);
	}
	
	@Test
	public void testFailRewireFilter() {
		final ValueHolder<String> preRewireValue = new ValueHolder<String>();
		final ValueHolder<String> postRewireValue = new ValueHolder<String>();
		
		Callable<Integer> task = new Callable<Integer>() {
			public Integer call() {
				throw new RuntimeException("oops");
			}
		};
		
		deferredManager.when(task).then(null, new FailPipe<Throwable, Integer, String, Void>() {
			@Override
			public Deferred<Integer, String, Void> pipeFail(Throwable result) {
				preRewireValue.set(result.getMessage());
				return new DeferredObject<Integer, String, Void>().reject("ouch");
			}
		}).fail(new FailCallback<String>() {
			@Override
			public void onFail(String result) {
				postRewireValue.set(result);
			}
		});
		
		waitForCompletion();
		preRewireValue.assertEquals("oops");
		postRewireValue.assertEquals("ouch");
	}
	
	@Test
	public void testNullDoneRewireFilter() {
		final ValueHolder<Boolean> failed = new ValueHolder<Boolean>(false);
		final ValueHolder<Integer> postRewireValue = new ValueHolder<Integer>();
		
		Callable<Integer> task = new Callable<Integer>() {
			public Integer call() {
				return 100;
			}
		};
		
		deferredManager.when(task).then(null, new FailPipe<Throwable, Integer, String, Void>() {
			@Override
			public Deferred<Integer, String, Void> pipeFail(Throwable result) {
				return new DeferredObject<Integer, String, Void>().reject("ouch");
			}
		}).done(new DoneCallback<Integer>() {
			@Override
			public void onDone(Integer result) {
				postRewireValue.set(result);
			}
		}).fail(new FailCallback<String>() {
			@Override
			public void onFail(String result) {
				failed.set(true);
			}
		});
		
		waitForCompletion();
		failed.assertEquals(false);
		postRewireValue.assertEquals(100);
	}
	
	@Test
	public void testDoneRewireToFail() {
		final ValueHolder<Integer> preRewireValue = new ValueHolder<Integer>();
		final ValueHolder<Integer> postRewireValue = new ValueHolder<Integer>();
		final ValueHolder<String> failed = new ValueHolder<String>();
		
		deferredManager.when(new Callable<Integer>() {
			public Integer call() {
				return 10;
			}
		}).then(new DonePipe<Integer, Integer, String, Void>() {
			@Override
			public Deferred<Integer, String, Void> pipeDone(Integer result) {
				preRewireValue.set(result);
				if (result < 100) {
					return new DeferredObject<Integer, String, Void>().reject("less than 100");
				} else {
					return new DeferredObject<Integer, String, Void>().resolve(result);
				}
			}
		}).done(new DoneCallback<Integer>() {
			@Override
			public void onDone(Integer result) {
				postRewireValue.set(result);
			}
		}).fail(new FailCallback<String>() {
			@Override
			public void onFail(String result) {
				failed.set(result);
			}
		});
		
		waitForCompletion();
		preRewireValue.assertEquals(10);
		postRewireValue.assertEquals(null);
		failed.assertEquals("less than 100");
	}
}

@@ -659,39 +659,8 @@
 e;%0A%0A
-import org.jdeferred.Deferred;%0A
 impo
@@ -682,32 +682,32 @@
 d.DoneCallback;%0A
+
 import org.jdefe
@@ -783,24 +783,54 @@
 d.FailPipe;%0A
+import org.jdeferred.Promise;%0A
 import org.j
@@ -1308,32 +1308,31 @@
 e%0A%09%09%09public 
-Deferred
+Promise
 %3CInteger, Vo
@@ -2149,32 +2149,31 @@
 e%0A%09%09%09public 
-Deferred
+Promise
 %3CInteger, St
@@ -2996,32 +2996,31 @@
 e%0A%09%09%09public 
-Deferred
+Promise
 %3CInteger, St
@@ -3928,24 +3928,23 @@
 %09public 
-Deferred
+Promise
 %3CInteger

Change the pipe result types in core tests.