package net.sf.yal10n.tmx;

/*
 * 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.
 */

import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.Collection;

import net.sf.yal10n.analyzer.NullLog;
import net.sf.yal10n.analyzer.ResourceAnalyzer;
import net.sf.yal10n.analyzer.ResourceBundle;
import net.sf.yal10n.settings.DashboardConfiguration;
import net.sf.yal10n.settings.Repository;
import net.sf.yal10n.svn.SVNUtil;
import net.sf.yal10n.svn.SVNUtilMock;

import org.codehaus.plexus.util.IOUtil;
import org.junit.Assert;
import org.junit.Test;

/**
 * Unit test for {@link TranslationMemoryRenderer}.
 */
public class TranslationMemoryRendererTest
{
    /**
     * Test to convert a single bundle.
     * @throws Exception any error
     */
    @Test
    public void testSingleBundle() throws Exception
    {
        File outputDirectory = new File( "./target/test-output/translationmemory/" );
        File completeDirectory = new File( outputDirectory, "/reports" );
        if ( !completeDirectory.exists() )
        {
            Assert.assertTrue( "Could create output directory for test: " + completeDirectory,
                    completeDirectory.mkdirs() );
        }
        Assert.assertTrue( "Not a directory: " + completeDirectory, completeDirectory.isDirectory() );

        TranslationMemoryRenderer renderer = new TranslationMemoryRenderer( outputDirectory.getCanonicalPath() );
        ResourceBundle bundle = createBundle().iterator().next();
        renderer.render( bundle  );

        String relativeTmxUrl = bundle.toBundleModel( bundle.getLanguages() ).getRelativeTmxUrl();
        Assert.assertTrue( new File( completeDirectory, relativeTmxUrl ).exists() );
        String fileData = IOUtil.toString( new FileInputStream( new File( completeDirectory, relativeTmxUrl ) ),
                "UTF-8" );
        Assert.assertTrue( fileData.contains( "tuid=\"http://svn/repo/messages:not.translated.missing\"" ) );
        Assert.assertTrue( fileData.contains( "<prop type=\"x-key\">not.translated.missing</prop>" ) );
        Assert.assertTrue( fileData.contains( "xml:lang=\"de\"" ) );
        Assert.assertTrue( fileData.contains( "this is a sample for for unit testing in locale German" ) );
    }

    /**
     * Test to convert a colletion of bundles.
     * @throws Exception any error
     */
    @Test
    public void testCollection() throws Exception
    {
        File outputDirectory = new File( "./target/test-output/translationmemory/" );
        File completeDirectory = new File( outputDirectory, "/reports" );
        if ( !completeDirectory.exists() )
        {
            Assert.assertTrue( "Could create output directory for test: " + completeDirectory,
                    completeDirectory.mkdirs() );
        }
        Assert.assertTrue( "Not a directory: " + completeDirectory, completeDirectory.isDirectory() );

        TranslationMemoryRenderer renderer = new TranslationMemoryRenderer( outputDirectory.getCanonicalPath() );
        Collection<ResourceBundle> bundle = createBundle();
        renderer.render( bundle  );

        String relativeTmxUrl = "all-translations.tmx";
        Assert.assertTrue( new File( outputDirectory, relativeTmxUrl ).exists() );
        String fileData = IOUtil.toString( new FileInputStream( new File( outputDirectory, relativeTmxUrl ) ),
                "UTF-8" );
        Assert.assertTrue( fileData.contains( "http://svn/repo/messages:not.translated.same" ) );
    }

    private Collection<ResourceBundle> createBundle() throws Exception
    {
        SVNUtil svnUtil = new SVNUtilMock( null );
        ResourceAnalyzer analyzer = new ResourceAnalyzer( svnUtil, new NullLog() );
        DashboardConfiguration config = new DashboardConfiguration();
        Repository repo = new Repository();
        repo.setUrl( "http://svn/repo" );
        repo.setIncludes( Arrays.asList( "**/messages*.properties" ) );
        String repoId = SVNUtil.toRepoId( "", repo.getUrl() );
        config.getRepositories().add( repo );

        analyzer.analyze( repo.getUrl(), new File( "./target/test-classes/unit/subdirectory/" ).getCanonicalPath(),
                config, repo, repoId );

        return analyzer.getBundles().values();
    }
}

@@ -2021,33 +2021,32 @@
 .render( bundle 
- 
 );%0A%0A        Stri
@@ -2369,37 +2369,15 @@
     
-Assert.assertTrue( fileData.c
+assertC
 onta
@@ -2439,18 +2439,26 @@
 ssing%5C%22%22
- )
+, fileData
  );%0A    
@@ -2465,37 +2465,15 @@
     
-Assert.assertTrue( fileData.c
+assertC
 onta
@@ -2529,18 +2529,26 @@
 %3C/prop%3E%22
- )
+, fileData
  );%0A    
@@ -2555,37 +2555,15 @@
     
-Assert.assertTrue( fileData.c
+assertC
 onta
@@ -2584,18 +2584,26 @@
 =%5C%22de%5C%22%22
- )
+, fileData
  );%0A    
@@ -2610,37 +2610,15 @@
     
-Assert.assertTrue( fileData.c
+assertC
 onta
@@ -2678,21 +2678,276 @@
  German%22
- ) );
+, fileData );%0A    %7D%0A%0A    private static void assertContains( String expected, String data )%0A    %7B%0A        if ( !data.contains( expected ) )%0A        %7B%0A            Assert.fail( %22Expected to find %5C%22%22 + expected + %22%5C%22, but didn't. Full data:%5Cn%22 + data );%0A        %7D
 %0A    %7D%0A%0A
@@ -2984,16 +2984,17 @@
  a colle
+c
 tion of 
@@ -4083,37 +4083,15 @@
     
-Assert.assertTrue( fileData.c
+assertC
 onta
@@ -4141,18 +4141,26 @@
 ed.same%22
- )
+, fileData
  );%0A    

Improve unit test errors to find out, why TranslationMemoryRendererTest is failing.