/*
 * 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.stream.source;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeFalse;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.culturegraph.mf.stream.pipe.ObjectBuffer;
import org.culturegraph.mf.util.ResourceUtil;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
 * Tests for class {@link FileOpener}.
 *
 * @author Christoph Böhme
 *
 */
public final class FileOpenerTest {

	private static final String DATA = "Überfacture";

	@Rule
	public TemporaryFolder tempFolder = new TemporaryFolder();

	@Test
	public void testUtf8IsDefaultEncoding() throws IOException {
		assumeFalse("Default encoding is UTF-8: It is not possible to test whether " +
						"FileOpener sets the encoding to UTF-8 correctly.",
				StandardCharsets.UTF_8.equals(Charset.defaultCharset()));

		final File testFile = createTestFile();

		final FileOpener opener = new FileOpener();
		final ObjectBuffer<Reader> buffer = new ObjectBuffer<>();
		opener.setReceiver(buffer);
		opener.process(testFile.getAbsolutePath());
		opener.closeStream();

		assertEquals(DATA, ResourceUtil.readAll(buffer.pop()));
	}

	private File createTestFile() throws IOException {
		final File file = tempFolder.newFile();
		try (OutputStream stream = new FileOutputStream(file)) {
			stream.write(DATA.getBytes(StandardCharsets.UTF_8));
		}
		return file;
	}

}

@@ -740,16 +740,58 @@
 meFalse;
+%0Aimport static org.mockito.Mockito.verify;
 %0A%0Aimport
@@ -1025,30 +1025,30 @@
 .mf.
-stream.pipe
+framework
 .Object
-Buff
+Receiv
 er;%0A
@@ -1178,16 +1178,180 @@
 yFolder;
+%0Aimport org.mockito.ArgumentCaptor;%0Aimport org.mockito.Captor;%0Aimport org.mockito.Mock;%0Aimport org.mockito.junit.MockitoJUnit;%0Aimport org.mockito.junit.MockitoRule;
 %0A%0A/**%0A *
@@ -1532,58 +1532,230 @@
 lic 
-TemporaryFolder tempFolder = new TemporaryFolder()
+MockitoRule mockitoRule = MockitoJUnit.rule();%0A%0A%09@Rule%0A%09public TemporaryFolder tempFolder = new TemporaryFolder();%0A%0A%09@Mock%0A%09private ObjectReceiver%3CReader%3E receiver;%0A%0A%09@Captor%0A%09private ArgumentCaptor%3CReader%3E processedObject
 ;%0A%0A%09
@@ -2117,68 +2117,8 @@
 ();%0A
-%09%09final ObjectBuffer%3CReader%3E buffer = new ObjectBuffer%3C%3E();%0A
 %09%09op
@@ -2134,20 +2134,22 @@
 eceiver(
-buff
+receiv
 er);%0A%09%09o
@@ -2216,16 +2216,71 @@
 eam();%0A%0A
+%09%09verify(receiver).process(processedObject.capture());%0A
 %09%09assert
@@ -2317,18 +2317,32 @@
 All(
-buffer.pop
+processedObject.getValue
 ()))

Refactor FileOpenerTest to Mockito