PageRenderTime 34ms CodeModel.GetById 23ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/encoding/EncodingFactoryTest.java

http://mobicents.googlecode.com/
Java | 165 lines | 129 code | 15 blank | 21 comment | 0 complexity | 2f10018e6f0980f2d3c63b2921c4d3ac MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.smpp.encoding;
 24
 25import static org.testng.Assert.assertEquals;
 26import static org.testng.Assert.assertFalse;
 27import static org.testng.Assert.assertNotNull;
 28import static org.testng.Assert.assertNull;
 29import static org.testng.Assert.assertTrue;
 30
 31import java.util.Iterator;
 32import java.util.Locale;
 33
 34import org.testng.annotations.Test;
 35
 36import org.mobicents.protocols.smpp.SMPPRuntimeException;
 37import org.mobicents.protocols.smpp.encoding.ASCIIEncoding;
 38import org.mobicents.protocols.smpp.encoding.AlphabetEncoding;
 39import org.mobicents.protocols.smpp.encoding.BinaryEncoding;
 40import org.mobicents.protocols.smpp.encoding.DefaultAlphabetEncoding;
 41import org.mobicents.protocols.smpp.encoding.EncodingFactory;
 42import org.mobicents.protocols.smpp.encoding.HPRoman8Encoding;
 43import org.mobicents.protocols.smpp.encoding.Latin1Encoding;
 44import org.mobicents.protocols.smpp.encoding.MessageEncoding;
 45import org.mobicents.protocols.smpp.encoding.UCS2Encoding;
 46import org.mobicents.protocols.smpp.encoding.UTF16Encoding;
 47import org.mobicents.protocols.smpp.util.APIConfig;
 48import org.mobicents.protocols.smpp.util.APIConfigFactory;
 49import org.mobicents.protocols.smpp.util.PropertiesAPIConfig;
 50
 51@Test
 52public class EncodingFactoryTest {
 53
 54    public void testEncodingFactory() {
 55        EncodingFactory factory = EncodingFactory.getInstance();
 56        assertNotNull(factory.getDefaultAlphabet());
 57        assertEquals(factory.getEncoding(0).getClass(), DefaultAlphabetEncoding.class);
 58        assertEquals(factory.getEncoding(1).getClass(), ASCIIEncoding.class);
 59        assertEquals(factory.getEncoding(3).getClass(), Latin1Encoding.class);
 60        assertEquals(factory.getEncoding(4).getClass(), BinaryEncoding.class);
 61        assertEquals(factory.getEncoding(8).getClass(), UCS2Encoding.class);
 62        assertNull(factory.getEncoding(10));
 63        assertNull(factory.getEncoding(100));
 64        assertNull(factory.getEncoding(189));
 65        assertEquals(DefaultAlphabetEncoding.class,
 66                factory.getAlphabet("en").getClass());
 67        Locale chinese = Locale.CHINESE;
 68        Locale korean = Locale.KOREAN;
 69        assertEquals(UCS2Encoding.class,
 70                factory.getAlphabet(chinese.getLanguage()).getClass());
 71        assertEquals(UCS2Encoding.class,
 72                factory.getAlphabet(korean.getLanguage()).getClass());
 73    }
 74    
 75    public void testAddEncoding() {
 76        class TestEncoding1 extends AlphabetEncoding {
 77            TestEncoding1() {
 78                super(100);
 79            }
 80        }
 81        EncodingFactory factory = new EncodingFactory();
 82        factory.addEncoding(new TestEncoding1());
 83        assertNotNull(factory.getEncoding(100));
 84        assertEquals(factory.getEncoding(100).getClass(), TestEncoding1.class);
 85    }
 86    
 87    @Test(expectedExceptions = {SMPPRuntimeException.class})
 88    public void testAddNullEncodingThrowsException() {
 89        EncodingFactory factory = new EncodingFactory();
 90        factory.addEncoding((Class<AlphabetEncoding>) null);
 91    }
 92
 93    @Test(expectedExceptions = {SMPPRuntimeException.class})
 94    public void testExceptionIsThrownWhenEncodingConstructorExceptions() {
 95        class TestEncoding2 extends AlphabetEncoding {
 96            TestEncoding2() {
 97                super(101);
 98                throw new RuntimeException("This exception is expected.");
 99            }
100        }
101        EncodingFactory factory = new EncodingFactory();
102        factory.addEncoding(TestEncoding2.class);
103    }
104    
105    public void testGetAllEncodings() {
106        Iterator<MessageEncoding<?>> encodings =
107            EncodingFactory.getInstance().getAllEncodings();
108        assertNotNull(encodings);
109        assertTrue(encodings.hasNext());
110    }
111
112    public void testDefaultAlphabetUsesGsmDefaultAlphabetIfNotOtherwiseSpecified() throws Exception {
113        assertNull(System.getProperty(EncodingFactory.DEFAULT_ALPHABET_PROPNAME));
114        APIConfig config = APIConfigFactory.getConfig();
115        assertFalse(config.isSet(APIConfig.DEFAULT_ALPHABET));
116        EncodingFactory factory = new EncodingFactory();
117        assertEquals(factory.getDefaultAlphabet().getClass(), DefaultAlphabetEncoding.class);
118    }
119    
120    public void testInstantiatingDefaultAlphabetFromSystemProperty() {
121        try {
122            System.setProperty(
123                    EncodingFactory.DEFAULT_ALPHABET_PROPNAME,
124                    "org.mobicents.protocols.smpp.encoding.UTF16Encoding");
125            EncodingFactory factory = new EncodingFactory();
126            assertNotNull(factory.getDefaultAlphabet());
127            assertEquals(factory.getDefaultAlphabet().getClass(), UTF16Encoding.class);
128        } finally {
129            System.clearProperty(EncodingFactory.DEFAULT_ALPHABET_PROPNAME);
130        }
131    }
132    
133    public void testInstantiatingDefaultAlphabetFromAPIConfig() throws Exception {
134        try {
135            assertNull(System.getProperty(EncodingFactory.DEFAULT_ALPHABET_PROPNAME));
136            PropertiesAPIConfig config = new PropertiesAPIConfig();
137            config.initialise();
138            config.setProperty(APIConfig.DEFAULT_ALPHABET, ASCIIEncoding.class.getName());
139            APIConfigFactory.setCachedConfig(config);
140            EncodingFactory factory = new EncodingFactory();
141            assertNotNull(factory.getDefaultAlphabet());
142            assertEquals(factory.getDefaultAlphabet().getClass(), ASCIIEncoding.class);
143        } finally {
144            APIConfigFactory.reset();
145        }
146    }
147    
148    public void testDefaultAlphabetSpecifiedInSystemPropertyOverridesAPIConfig() throws Exception {
149        try {
150            System.setProperty(
151                    EncodingFactory.DEFAULT_ALPHABET_PROPNAME,
152                    UCS2Encoding.class.getName());
153            PropertiesAPIConfig config = new PropertiesAPIConfig();
154            config.initialise();
155            config.setProperty(APIConfig.DEFAULT_ALPHABET, HPRoman8Encoding.class.getName());
156            APIConfigFactory.setCachedConfig(config);
157            EncodingFactory factory = new EncodingFactory();
158            assertNotNull(factory.getDefaultAlphabet());
159            assertEquals(factory.getDefaultAlphabet().getClass(), UCS2Encoding.class);
160        } finally {
161            System.clearProperty(EncodingFactory.DEFAULT_ALPHABET_PROPNAME);
162            APIConfigFactory.reset();
163        }
164    }
165}