package org.culturegraph.mf.stream.converter;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.io.StringReader;

import org.culturegraph.mf.framework.ObjectReceiver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class FormetaRecordsReaderTest {

	private static String SINGLE_RECORD = "l: v";

	private static String RECORD_LITERAL = " l: v,";
	private static String RECORD_GROUP = " r{l: v}";
	private static String RECORD_NESTED_GROUP = " r{ e { l: v } }";
	private static String RECORD_QUOTED_LITERAL = " 'l x': v,";
	private static String RECORD_LEFT_BRACE_IN_QUOTES = " '{': l,";
	private static String RECORD_RIGHT_BRACE_IN_QUOTES = " r{ l: '}' }";
	private static String RECORD_COLON_IN_QUOTES = " ':': v,";
	private static String RECORD_COMMA_IN_QUOTES = " l: ',v:v',";
	private static String RECORD_ESCAPED_LEFT_BRACE = " \\{: v,";
	private static String RECORD_ESCAPED_RIGHT_BRACE = " r{ l: \\} }";
	private static String RECORD_ESCAPED_COLON = " \\:: v,";
	private static String RECORD_ESCAPED_COMMA = " l: \\,v\\:v,";
	private static String RECORD_ESCAPED_QUOTE = " '\\',': v";

	private FormetaRecordsReader formetaRecordsReader;

	@Mock
	private ObjectReceiver<String> receiver;

	@Before
	public void setup() {
		MockitoAnnotations.initMocks(this);
		formetaRecordsReader = new FormetaRecordsReader();
		formetaRecordsReader.setReceiver(receiver);
	}

	@After
	public void cleanup() {
		formetaRecordsReader.closeStream();
	}

	@Test
	public void shouldProcessSingleRecord() {
		final StringReader reader = new StringReader(SINGLE_RECORD);

		formetaRecordsReader.process(reader);

		verify(receiver).process(SINGLE_RECORD);
		verifyNoMoreInteractions(receiver);
	}

	@Test
	public void shouldSplitBetweenTopLevelElements() {
		final String records = RECORD_LITERAL +
				RECORD_GROUP +
				RECORD_NESTED_GROUP +
				RECORD_QUOTED_LITERAL +
				RECORD_LEFT_BRACE_IN_QUOTES +
				RECORD_RIGHT_BRACE_IN_QUOTES +
				RECORD_COLON_IN_QUOTES +
				RECORD_COMMA_IN_QUOTES +
				RECORD_ESCAPED_LEFT_BRACE +
				RECORD_ESCAPED_RIGHT_BRACE +
				RECORD_ESCAPED_COLON +
				RECORD_ESCAPED_COMMA +
				RECORD_ESCAPED_QUOTE;

		final StringReader reader = new StringReader(records);

		formetaRecordsReader.process(reader);

		verify(receiver).process(RECORD_LITERAL);
		verify(receiver).process(RECORD_GROUP);
		verify(receiver).process(RECORD_NESTED_GROUP);
		verify(receiver).process(RECORD_QUOTED_LITERAL);
		verify(receiver).process(RECORD_LEFT_BRACE_IN_QUOTES);
		verify(receiver).process(RECORD_RIGHT_BRACE_IN_QUOTES);
		verify(receiver).process(RECORD_COLON_IN_QUOTES);
		verify(receiver).process(RECORD_COMMA_IN_QUOTES);
		verify(receiver).process(RECORD_ESCAPED_LEFT_BRACE);
		verify(receiver).process(RECORD_ESCAPED_RIGHT_BRACE);
		verify(receiver).process(RECORD_ESCAPED_COLON);
		verify(receiver).process(RECORD_ESCAPED_COMMA);
		verify(receiver).process(RECORD_ESCAPED_QUOTE);
		verifyNoMoreInteractions(receiver);
	}
}

@@ -1,8 +1,615 @@
+/*%0A *  Copyright 2014 Christoph B%C3%B6hme%0A *%0A *  Licensed under the Apache License, Version 2.0 the %22License%22;%0A *  you may not use this file except in compliance with the License.%0A *  You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A *  Unless required by applicable law or agreed to in writing, software%0A *  distributed under the License is distributed on an %22AS IS%22 BASIS,%0A *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A *  See the License for the specific language governing permissions and%0A *  limitations under the License.%0A */%0A
 package 
@@ -970,22 +970,119 @@
 tions;%0A%0A
-public
+/**%0A * Tests for class %7B@link FormetaRecordsReader%7D.%0A *%0A * @author Christoph B%C3%B6hme%0A *%0A */%0Apublic final 
  class F
@@ -1120,24 +1120,30 @@
 vate static 
+final 
 String SINGL
@@ -1170,32 +1170,38 @@
 %09private static 
+final 
 String RECORD_LI
@@ -1226,32 +1226,38 @@
 %09private static 
+final 
 String RECORD_GR
@@ -1282,32 +1282,38 @@
 %09private static 
+final 
 String RECORD_NE
@@ -1353,32 +1353,38 @@
 %09private static 
+final 
 String RECORD_QU
@@ -1420,32 +1420,38 @@
 %09private static 
+final 
 String RECORD_LE
@@ -1491,32 +1491,38 @@
 %09private static 
+final 
 String RECORD_RI
@@ -1567,32 +1567,38 @@
 %09private static 
+final 
 String RECORD_CO
@@ -1633,32 +1633,38 @@
 %09private static 
+final 
 String RECORD_CO
@@ -1702,32 +1702,38 @@
 %09private static 
+final 
 String RECORD_ES
@@ -1771,32 +1771,38 @@
 %09private static 
+final 
 String RECORD_ES
@@ -1845,32 +1845,38 @@
 %09private static 
+final 
 String RECORD_ES
@@ -1909,32 +1909,38 @@
 %09private static 
+final 
 String RECORD_ES
@@ -1986,16 +1986,22 @@
  static 
+final 
 String R

Add comments and remove trailing whitespace