package org.sagebionetworks.logging.collate;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.sagebionetworks.logging.reader.LogReader;
import org.sagebionetworks.logging.reader.LogReader.LogReaderFactory;

public class LogCollationUtilsTest {

	private List<File> files;

	@Before
	public void setUp() throws IOException {
		List<String> fileNames = Arrays.asList("test1", "test2", "test3");

		files = new ArrayList<File>();
		for (String fileName : fileNames) {
			File e = new File(fileName);
			e.createNewFile();
			files.add(e);
		}
	}

	@After
	public void tearDown() {
		for (File file : files) {
			file.delete();
		}
	}

	@Test
	public void testInitializeReader() throws IOException {
		LogReaderFactory<LogReader> mockFactory = mock(LogReaderFactory.class, RETURNS_DEEP_STUBS);

		List<LogReader> logReaders = LogCollationUtils.initializeReaders(mockFactory, files);
		ArgumentCaptor<BufferedReader> captor = ArgumentCaptor.forClass(BufferedReader.class);
		verify(mockFactory, times(files.size())).create(captor.capture());
		List<BufferedReader> readers = captor.getAllValues();

		assertEquals(files.size(), readers.size());
		assertEquals(readers.size(), logReaders.size());
	}

	@Test(expected=FileNotFoundException.class)
	public void testInitializeReaderFakeFiles() throws Exception {
		files.add(new File(""));
		LogReaderFactory<LogReader> mockFactory = mock(LogReaderFactory.class, RETURNS_DEEP_STUBS);

		List<LogReader> logReaders = LogCollationUtils.initializeReaders(mockFactory, files);
	}
}

@@ -109,16 +109,87 @@
 ckito.*;
+%0Aimport static org.sagebionetworks.logging.collate.LogCollationUtils.*;
 %0A%0Aimport
@@ -209,24 +209,55 @@
 eredReader;%0A
+import java.io.BufferedWriter;%0A
 import java.
@@ -295,32 +295,90 @@
 FoundException;%0A
+import java.io.FileWriter;%0Aimport java.io.FilenameFilter;%0A
 import java.io.I
@@ -573,16 +573,77 @@
 Captor;%0A
+import org.sagebionetworks.logging.reader.ActivityLogReader;%0A
 import o
@@ -1247,16 +1247,49 @@
 ption %7B%0A
+%09%09@SuppressWarnings(%22unchecked%22)%0A
 %09%09LogRea
@@ -1775,24 +1775,744 @@
 ize());%0A%09%7D%0A%0A
+%09@Test%0A%09public void testCollate() throws Exception %7B%0A%09%09File dir = new File(%22src/test/resources%22);%0A%09%09if (!dir.exists() %7C%7C !dir.isDirectory())%0A%09%09%09fail(%22Missing necessary test resource directory.%22);%0A%0A%09%09List%3CFile%3E asList = Arrays.asList(dir.listFiles(new FilenameFilter() %7B%0A%09%09%09@Override%0A%09%09%09public boolean accept(File dir, String name) %7B%0A%09%09%09%09String shouldMatch = %22repo-activity.2012-09-16-10-30.log%22;%0A%09%09%09%09return name.equals(shouldMatch);%0A%09%09%09%7D%0A%09%09%7D));%0A%0A%09%09File tempFile = File.createTempFile(%22logOut%22, %22.collated%22, dir);%0A%09%09tempFile.deleteOnExit();%0A%09%09collateLogs(primeCollationMap(initializeReaders(new ActivityLogReader.ActivityLogReaderFactory(),%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09asList)),%0A%09%09%09%09%09new BufferedWriter(new FileWriter(tempFile)));%0A%09%7D%0A%0A
 %09@Test(expec
@@ -2734,39 +2734,10 @@
 );%0A%0A
+
 %09%09
-List%3CLogReader%3E logReaders = 
 LogC

Add test that actually collates the files.