Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LocalizedMessageTest.java « test « i18n « bouncycastle « org « java « test « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f7c5a42020fad2a41952c034ffa48c7aef46900a (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.bouncycastle.i18n.test;

import junit.framework.TestCase;
import org.bouncycastle.i18n.LocaleString;
import org.bouncycastle.i18n.LocalizedMessage;
import org.bouncycastle.i18n.MissingEntryException;
import org.bouncycastle.i18n.filter.HTMLFilter;
import org.bouncycastle.i18n.filter.TrustedInput;
import org.bouncycastle.util.encoders.Hex;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class LocalizedMessageTest extends TestCase
{

    private static final String TEST_RESOURCE = "org.bouncycastle.i18n.test.I18nTestMessages";
    private static final String UTF8_TEST_RESOURCE = "org.bouncycastle.i18n.test.I18nUTF8TestMessages";

    /*
     * test message id's
     */
    private static final String timeTestId = "time";
    private static final String argsTestId = "arguments";
    private static final String localeTestId = "hello";
    private static final String missingTestId = "missing";
    private static final String filterTestId = "filter";
    private static final String utf8TestId = "utf8";

    /*
     * Test method for 'org.bouncycastle.i18n.LocalizedMessage.getEntry(String,
     * Locale, TimeZone)'
     */
    public void testGetEntry()
    {
        LocalizedMessage msg;

        // test different locales
        msg = new LocalizedMessage(TEST_RESOURCE, localeTestId);
        assertEquals("Hello world.", msg.getEntry("text", Locale.ENGLISH,
                TimeZone.getDefault()));
        assertEquals("Hallo Welt.", msg.getEntry("text", Locale.GERMAN,
                TimeZone.getDefault()));

        // test arguments
        Object[] args = new Object[] { "Nobody" };
        msg = new LocalizedMessage(TEST_RESOURCE, argsTestId, args);
        assertEquals("My name is Nobody.", msg.getEntry("text", Locale.ENGLISH,
                TimeZone.getDefault()));
        assertEquals("Mein Name ist Nobody.", msg.getEntry("text",
                Locale.GERMAN, TimeZone.getDefault()));

        // test timezones
        // test date 17. Aug. 13:12:00 GMT
        Date testDate = new Date(1155820320000l);
        args = new Object[] { new TrustedInput(testDate) };
        msg = new LocalizedMessage(TEST_RESOURCE, timeTestId, args);
        assertEquals("It's 1:12:00 PM GMT at Aug 17, 2006.", msg.getEntry(
                "text", Locale.ENGLISH, TimeZone.getTimeZone("GMT")));
        // NOTE: Older JDKs appear to use '.' as the time separator for German locale
        assertEquals("Es ist 13:12 Uhr GMT am 17.08.2006.", msg.getEntry(
                "text", Locale.GERMAN, TimeZone.getTimeZone("GMT")).replace("13.12", "13:12"));

        // test time with filter
        args = new Object[] { new TrustedInput(testDate) };
        msg = new LocalizedMessage(TEST_RESOURCE, timeTestId, args);
        msg.setFilter(new HTMLFilter());
        assertEquals("It's 1:12:00 PM GMT at Aug 17, 2006.", msg.getEntry(
                "text", Locale.ENGLISH, TimeZone.getTimeZone("GMT")));
        // NOTE: Older JDKs appear to use '.' as the time separator for German locale
        assertEquals("Es ist 13:12 Uhr GMT am 17.08.2006.", msg.getEntry(
                "text", Locale.GERMAN, TimeZone.getTimeZone("GMT")).replace("13.12", "13:12"));
        
        // test number
        args = new Object[] { new TrustedInput(new Float(0.2))  };
        msg = new LocalizedMessage(TEST_RESOURCE, "number", args);
        assertEquals("20%", msg.getEntry("text", Locale.ENGLISH, TimeZone.getDefault()));

        // test filters
        String untrusted = "<script>doBadThings()</script>";
        args = new Object[] { untrusted };
        msg = new LocalizedMessage(TEST_RESOURCE,filterTestId,args);
        msg.setFilter(new HTMLFilter());
        assertEquals("The following part should contain no HTML tags: "
                + "&#60script&#62doBadThings&#40&#41&#60/script&#62",
                msg.getEntry("text",Locale.ENGLISH, TimeZone.getDefault()));
        
        // test missing entry
        msg = new LocalizedMessage(TEST_RESOURCE, missingTestId);
        try
        {
            String text = msg.getEntry("text", Locale.UK, TimeZone
                    .getDefault());
            fail();
        }
        catch (MissingEntryException e)
        {
//            System.out.println(e.getDebugMsg());
        }
        
        // test missing entry
        try
        {
            URLClassLoader cl = URLClassLoader.newInstance(new URL[] {new URL("file:///nonexistent/")});
            msg = new LocalizedMessage(TEST_RESOURCE, missingTestId);
            msg.setClassLoader(cl);
            try
            {
                String text = msg.getEntry("text", Locale.UK, TimeZone
                        .getDefault());
                fail();
            }
            catch (MissingEntryException e)
            {
//                System.out.println(e.getDebugMsg());
            }
        }
        catch (MalformedURLException e)
        {
            
        }
        
        // test utf8
        try
        {
//            String expectedUtf8 = "some umlauts äöüèéà";
            String expectedUtf8 = new String(Hex.decode("736f6d6520756d6c6175747320c3a4c3b6c3bcc3a8c3a9c3a0"), "UTF-8");
            msg = new LocalizedMessage(UTF8_TEST_RESOURCE, utf8TestId, "UTF-8");
            assertEquals(expectedUtf8, msg.getEntry("text", Locale.GERMAN, TimeZone.getDefault()));
        }
        catch (UnsupportedEncodingException e)
        {
            
        }

    }
    
    public void testLocalizedArgs()
    {
        LocaleString ls = new LocaleString(TEST_RESOURCE, "name");
        
        // without filter
        Object[] args = new Object[] { ls };
        LocalizedMessage msg = new LocalizedMessage(TEST_RESOURCE, argsTestId, args);
        assertEquals("My name is John.", msg.getEntry("text", Locale.ENGLISH,
                TimeZone.getDefault()));
        assertEquals("Mein Name ist Hans.", msg.getEntry("text",
                Locale.GERMAN, TimeZone.getDefault()));
        
        // with filter
        msg.setFilter(new HTMLFilter());
        assertEquals("My name is John.", msg.getEntry("text", Locale.ENGLISH,
                TimeZone.getDefault()));
        assertEquals("Mein Name ist Hans.", msg.getEntry("text",
                Locale.GERMAN, TimeZone.getDefault()));
        
        // add extra args
        LocaleString lsExtra = new LocaleString(TEST_RESOURCE, "hello.text");
        msg.setExtraArguments(new Object[] {" ", lsExtra});
        assertEquals("My name is John. Hello world.", msg.getEntry("text", Locale.ENGLISH,
                TimeZone.getDefault()));
        assertEquals("Mein Name ist Hans. Hallo Welt.", msg.getEntry("text",
                Locale.GERMAN, TimeZone.getDefault()));
        
        // missing localized arg
        try
        {
            ls = new LocaleString(TEST_RESOURCE, "noname");
            args = new Object[] { ls };
            msg = new LocalizedMessage(TEST_RESOURCE, argsTestId, args);
            msg.getEntry("text", Locale.ENGLISH, TimeZone.getDefault());
            fail();
        }
        catch (MissingEntryException e)
        {
            assertEquals("Can't find entry noname in resource file org.bouncycastle.i18n.test.I18nTestMessages.", e.getMessage());
        }
    }

}