/*
 * Copyright 2013, 2014 Deutsche Nationalbibliothek
 *
 * 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.culturegraph.mf.framework;



import org.junit.Assert;
import org.junit.Test;


/**
 * Tests Contract of {@link DefaultSender}.
 * @author Markus M Geipel
 *
 */
public final class DefaultSenderTest {

	/**
	 *  onCloseStream() must be called only once even if closeSteam is called several times
	 */
	@Test
	public void testMultipleCloseStreamInvocations() {
		final CloseCounter closeCounter = new CloseCounter();

		Assert.assertEquals(0, closeCounter.getCount());
		Assert.assertFalse(closeCounter.isClosed());

		closeCounter.closeStream();
		Assert.assertEquals(1, closeCounter.getCount());
		Assert.assertTrue(closeCounter.isClosed());

		closeCounter.closeStream();
		Assert.assertEquals(1, closeCounter.getCount());
		Assert.assertTrue(closeCounter.isClosed());

		closeCounter.closeStream();
		Assert.assertEquals(1, closeCounter.getCount());
		Assert.assertTrue(closeCounter.isClosed());

	}

	/**
	 *	counts invocation of onCloseStream()
	 */
	protected static final class CloseCounter extends DefaultSender<ObjectReceiver<Object>>{
		private int count;

		@Override
		protected void onCloseStream() {
			++count;
		}

		public int getCount() {
			return count;
		}
	}

}

@@ -1,11 +1,45 @@
 /*%0A
+ * Copyright 2016 Christoph B%C3%B6hme%0A
  * Copyr
@@ -686,17 +686,59 @@
 k;%0A%0A
-%0A
+import static org.junit.Assert.assertFalse;
 %0Aimport 
 org.
@@ -725,32 +725,39 @@
 rtFalse;%0Aimport 
+static 
 org.junit.Assert
@@ -756,17 +756,192 @@
 t.Assert
-;
+.assertTrue;%0Aimport static org.mockito.Mockito.never;%0Aimport static org.mockito.Mockito.spy;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 %0Aimport 
@@ -957,17 +957,16 @@
 .Test;%0A%0A
-%0A
 /**%0A * T
@@ -974,19 +974,17 @@
 sts 
-Contract of
+for class
  %7B@l
@@ -1003,16 +1003,19 @@
 ender%7D.%0A
+ *%0A
  * @auth
@@ -1039,478 +1039,322 @@
 l%0A *
-%0A */%0Apublic final class DefaultSenderTest %7B%0A%0A%09/**%0A%09 *  onCloseStream() must be called only once even if closeSteam is called several times%0A%09 */%0A%09@Test%0A%09public void testMultipleCloseStreamInvocations() %7B%0A%09%09final CloseCounter closeCounter = new CloseCounter();%0A%0A%09%09Assert.assertEquals(0, closeCounter.getCount
+ @author Christoph B%C3%B6hme (refactored to Mockito)%0A *%0A */%0Apublic final class DefaultSenderTest %7B%0A%0A%09@Test%0A%09public void shouldCallOnCloseStreamOnlyOnce() %7B%0A%09%09final DefaultSender%3CStreamReceiver%3E defaultSender =%0A%09%09%09%09spy(new DefaultSender%3C%3E
 ());%0A
+%0A
 %09%09
-Assert.assertFalse(closeCounter.isClosed());%0A%0A%09%09closeCounter.closeStream();%0A%09%09Assert.assertEquals(1, closeCounter.getCount());%0A%09%09Assert.assertTrue(closeCount
+verify(defaultSender, never()).onCloseStream();%0A%09%09assertFalse(defaultSend
 er.i
@@ -1364,34 +1364,35 @@
 osed());%0A%0A%09%09
-closeCount
+defaultSend
 er.closeStre
@@ -1401,89 +1401,84 @@
 ();%0A
+%0A
 %09%09
-Assert.assertEquals(1, closeCounter.getCount());%0A%09%09Assert.assertTrue(closeCount
+verify(defaultSender, times(1)).onCloseStream();%0A%09%09assertTrue(defaultSend
 er.i
@@ -1492,26 +1492,27 @@
 ());%0A%0A%09%09
-closeCount
+defaultSend
 er.close
@@ -1525,140 +1525,43 @@
 ();%0A
+%0A
 %09%09
-Assert.assertEquals(1, closeCounter.getCount());%0A%09%09Assert.assertTrue(closeCounter.isClosed());%0A%0A%09%7D%0A%0A%09/**%0A%09 *%09counts invocation of 
+verify(defaultSender, times(1)).
 onCl
@@ -1575,236 +1575,49 @@
 am()
+;
 %0A%09
- */%0A%09protected static final class CloseCounter extends DefaultSender%3CObjectReceiver%3CObject%3E%3E%7B%0A%09%09private int count;%0A%0A%09%09@Override%0A%09%09protected void onCloseStream() %7B%0A%09%09%09++count;%0A%09%09%7D%0A%0A%09%09public int getCount() %7B%0A%09%09%09return count;%0A%09%09%7D
+%09assertTrue(defaultSender.isClosed());
 %0A%09%7D%0A

Refactor test cases for DefaultSender