summaryrefslogtreecommitdiff
path: root/src/org/libreoffice/tools/tests/LanguageHelperTest.java
blob: 78792124a75f56590a1475be2be3d1c82951183b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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<String, Object> model = new HashMap<String, Object>();
        model.put( VAR_SOMETEXT_NAME, VAR_SOMETEXT_VALUE );
        return model;
    }
}