package org.fedorahosted.tennera.jgettext;

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;

import org.fedorahosted.tennera.jgettext.catalog.parse.ExtendedCatalogParser;
import org.fedorahosted.tennera.jgettext.catalog.write.CatalogWriter;
import org.junit.Test;


public class RoundTripTests {
	
	@Test
	public void testRoundtrip1() throws Throwable{
		File original = getResource("/roundtrip/sample.po");
		testRoundTrip(original);
	}

	@Test
	public void testWordWrappingInMsgId() throws Throwable{
		File original = getResource("/roundtrip/msgid_wordwrap.po");
		testRoundTrip(original);
	}
	
	@Test
	public void testEmptyLineNote() throws Throwable{
		File original = getResource("/roundtrip/emptylines_notes.po");
		testRoundTrip(original);
	}
	
	@Test
	public void testMalformedObsoleteUnits() throws Throwable{
		File original = getResource("/roundtrip/malformed_obsoleteunits.po");
		testRoundTrip(original);
	}
	
	@Test
	public void testNonAsciiHeader() throws Throwable{
		File original = getResource("/roundtrip/nonascii_header.po");
		testRoundTrip(original);
	}
	
	@Test
	public void testMultilineContext() throws Throwable{
		File original = getResource("/roundtrip/multiline_context.po");
		testRoundTrip(original);
	}
	
	private void testRoundTrip(String message, File f) throws Throwable{
		String output = roundtrip(f);
		String originalString = readToString(f); 
		assertEquals(message, originalString, output);
	}
	
	private void testRoundTrip(File f) throws Throwable{
		testRoundTrip(null, f);
	}
	
	private String roundtrip(File original) throws Throwable{
		ExtendedCatalogParser parser = new ExtendedCatalogParser(original);
		parser.catalog();
		CatalogWriter writer = new CatalogWriter(parser.getCatalog());
		StringWriter outputWriter = new StringWriter();
		writer.writeTo(outputWriter);
		outputWriter.flush();
		return outputWriter.toString();
	}
	
	private File getResource(String file){
		return new File( getClass().getResource(file).getFile() );
	}
	
    private static String readToString(File file) {

    	BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
	    	reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
	 
	        String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
            	reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        return sb.toString();
    }

	
}

@@ -864,16 +864,34 @@
 undtrip/
+translate-toolkit/
 emptylin
@@ -897,32 +897,32 @@
 nes_notes.po%22);%0A
-
 %09%09testRoundTrip(
@@ -1046,16 +1046,34 @@
 undtrip/
+translate-toolkit/
 malforme
@@ -1125,32 +1125,205 @@
 l);%0A%09%7D%0A%09%0A%09@Test%0A
+%09public void testMalformedUnits() throws Throwable%7B%0A%09%09File original = getResource(%22/roundtrip/translate-toolkit/malformed_units.po%22);%0A%09%09testRoundTrip(original);%0A%09%7D%0A%09%0A%09@Test%0A
 %09public void tes
@@ -1400,16 +1400,34 @@
 undtrip/
+translate-toolkit/
 nonascii
@@ -1525,32 +1525,32 @@
 rows Throwable%7B%0A
-
 %09%09File original 
@@ -1575,16 +1575,34 @@
 undtrip/
+translate-toolkit/
 multilin

Change the path of PO files in RoundTrip Tests