package org.libreoffice.tools.tests; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import java.io.File; import java.io.StringWriter; import java.net.URISyntaxException; import java.util.Arrays; import java.util.HashMap; import org.junit.Before; import org.junit.Test; import org.libreoffice.tools.language.Language; import org.libreoffice.tools.language.LanguageHelper; import freemarker.template.Template; /** * ATM, the fixture of this class is the main language configuration: it * would be more complex to generate the fixture here. * * @author cbosdonnat * */ public class LanguageHelperTest { private static final String TESTS_LANGS_DIR = "test_langs"; private static final String WORKING_LANG = "working"; private static final Object WORKING_DESCRIPTION = "Working test lang"; private static final String MISSING_LANG = "missing"; private static final String UNCOMPLETE_LANG = "uncomplete"; private static final String SIMPLE_TPL = "simple.tpl"; private static final String NESTED_TPL = "nested.tpl"; private static final String VAR_SOMETEXT_NAME = "sometext"; private static final String VAR_SOMETEXT_VALUE = "some text"; @Before public void setup( ) throws URISyntaxException { File jarFile = new File(LanguageHelperTest.class.getProtectionDomain() .getCodeSource().getLocation().toURI()); File dir = new File( jarFile.getParentFile(), TESTS_LANGS_DIR ); LanguageHelper.setLanguagesDir( dir ); } @Test public void testGetLanguages( ) { String[] result = LanguageHelper.getLanguagesIds(); String[] expected = new String[] { WORKING_LANG, UNCOMPLETE_LANG }; // The order in results aren't guaranteed Arrays.sort( result ); Arrays.sort( expected ); assertArrayEquals( "Different values", expected, result ); } @Test public void testGetDescriptionOk( ) { Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); assertNotNull( langDef ); String actual = langDef.getDescription( ); assertEquals( WORKING_DESCRIPTION , actual ); } @Test public void testGetLanguageMissing( ) { Language langDef = LanguageHelper.getLanguage( MISSING_LANG ); assertNull( langDef ); } @Test public void testGetLanguageMissingProp( ) { Language langDef = LanguageHelper.getLanguage( UNCOMPLETE_LANG ); assertNotNull( langDef ); String actual = langDef.getDescription(); assertNull( actual ); } @Test public void testSimpleTemplate( ) throws Exception { Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); assertNotNull( langDef ); Template tpl = langDef.getTemplate( SIMPLE_TPL ); StringWriter out = new StringWriter(); tpl.process( getTestTemplateModel(), out ); out.flush(); String expected = VAR_SOMETEXT_VALUE; assertEquals( expected, out.toString() ); } @Test public void testNestedTemplate( ) throws Exception { Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); assertNotNull( langDef ); Template tpl = langDef.getTemplate( NESTED_TPL ); StringWriter out = new StringWriter(); tpl.process( getTestTemplateModel(), out ); out.flush(); String expected = VAR_SOMETEXT_VALUE + "\n" + VAR_SOMETEXT_VALUE; assertEquals( expected, out.toString() ); } private Object getTestTemplateModel() { HashMap model = new HashMap(); model.put( VAR_SOMETEXT_NAME, VAR_SOMETEXT_VALUE ); return model; } }