/*
 * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, v. 2.1. This program is distributed in the
 * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details. You should have received a
 * copy of the GNU Lesser General Public License, v.2.1 along with this
 * distribution; if not, write to the Free Software Foundation, Inc.,
 *
 * Red Hat Author(s): Steve Ebersole
 */
package org.jboss.jgettext.catalog.parse;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.fedorahosted.tennera.jgettext.Catalog;
import org.fedorahosted.tennera.jgettext.Message;
import org.fedorahosted.tennera.jgettext.catalog.parse.ExtendedCatalogParser;
import org.fedorahosted.tennera.jgettext.catalog.parse.ParseException;
import org.fedorahosted.tennera.jgettext.catalog.write.MessageWritingProcessor;
import org.fedorahosted.tennera.jgettext.util.NoOpWriter;

import org.junit.Test;

/**
 * TestExtendedCatalogParser implementation
 *
 * @author Steve Ebersole
 */
public class TestExtendedCatalogParser{
	
	@Test
	public void testBasic() throws Throwable {
		File poFile = new File( getClass().getResource( "/valid/sample.po" ).getFile() );
		ExtendedCatalogParser parser = new ExtendedCatalogParser( poFile );
		parser.catalog();
		Catalog catalog = parser.getCatalog();
		LocalProcessor processor = new LocalProcessor();
		catalog.processMessages( processor );

		assertNotNull( catalog.locateHeader() );
		assertEquals( 7, processor.entryCount );
		assertEquals( 3, processor.obsoleteCount );
	}

	@Test
	public void testObsoleteEntries() throws Throwable {
		File poFile = new File( getClass().getResource( "/valid/obsolete.po" ).getFile() );
		ExtendedCatalogParser parser = new ExtendedCatalogParser( poFile );
		parser.catalog();
		Catalog catalog = parser.getCatalog();
		LocalProcessor processor = new LocalProcessor();
		catalog.processMessages( processor );

		assertNotNull( catalog.locateHeader() );
		assertEquals( 5, processor.entryCount );
		assertEquals( 4, processor.obsoleteCount ); // - header...
	}

	@Test
	public void testPartialObsoleteEntries() throws Throwable {
		File poFile = new File( getClass().getResource( "/invalid/mixed_up_obsolete.po" ).getFile() );
		ExtendedCatalogParser parser = new ExtendedCatalogParser( poFile );
		try {
			parser.catalog();
			fail( "was expecting exception" );
		}
		catch ( ParseException expected ) {
		}
	}

	public static class LocalProcessor extends MessageWritingProcessor {
		private int entryCount;
		private int obsoleteCount;

		public LocalProcessor() {
			super( new NoOpWriter() );
		}

		protected void messageStart(Message message) throws IOException {
			entryCount++;
			if ( message.isObsolete() ) {
				obsoleteCount++;
			}
			writer.write( "-----------------------------------------------------------------------------------\n" );
		}

		protected void messageEnd(Message message) throws IOException {
			writer.write( "-----------------------------------------------------------------------------------\n" );
		}
	}
}

@@ -1661,97 +1661,143 @@
 ;%0A%09%09
-LocalProcessor processor = new LocalProcessor();%0A%09%09catalog.processMessages( processor );%0A
+%0A%09%09int entryCount = 0;%0A%09%09int obsoleteCount = 0;%0A%09%09for(Message m : catalog)%7B%0A%09%09%09entryCount++;%0A%09%09%09if(m.isObsolete()) obsoleteCount++;%0A%09%09%7D
 %0A%09%09a
@@ -1855,26 +1855,16 @@
 als( 7, 
-processor.
 entryCou
@@ -1888,26 +1888,16 @@
 als( 3, 
-processor.
 obsolete
@@ -2191,98 +2191,143 @@
 ();%0A
+%0A
 %09%09
-LocalProcessor processor = new LocalProcessor();%0A%09%09catalog.processMessages( processor );
+int entryCount = 0;%0A%09%09int obsoleteCount = 0;%0A%09%09for(Message m : catalog)%7B%0A%09%09%09entryCount++;%0A%09%09%09if(m.isObsolete()) obsoleteCount++;%0A%09%09%7D
 %0A%0A%09%09
@@ -2386,26 +2386,16 @@
 als( 5, 
-processor.
 entryCou
@@ -2423,18 +2423,8 @@
  4, 
-processor.
 obso
@@ -2809,635 +2809,7 @@
 %7D%0A%09%7D
-%0A%0A%09public static class LocalProcessor extends MessageWritingProcessor %7B%0A%09%09private int entryCount;%0A%09%09private int obsoleteCount;%0A%0A%09%09public LocalProcessor() %7B%0A%09%09%09super( new NoOpWriter() );%0A%09%09%7D%0A%0A%09%09protected void messageStart(Message message) throws IOException %7B%0A%09%09%09entryCount++;%0A%09%09%09if ( message.isObsolete() ) %7B%0A%09%09%09%09obsoleteCount++;%0A%09%09%09%7D%0A%09%09%09writer.write( %22-----------------------------------------------------------------------------------%5Cn%22 );%0A%09%09%7D%0A%0A%09%09protected void messageEnd(Message message) throws IOException %7B%0A%09%09%09writer.write( %22-----------------------------------------------------------------------------------%5Cn%22 );%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

update test cases