PageRenderTime 33ms CodeModel.GetById 18ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/version/VersionFactoryTest.java

http://mobicents.googlecode.com/
Java | 119 lines | 86 code | 11 blank | 22 comment | 2 complexity | 1ef54b51aac7fcc6eb5ef06440cda0dc 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.version;
 24
 25import static org.testng.Assert.assertSame;
 26import static org.testng.Assert.fail;
 27
 28import org.testng.annotations.Test;
 29
 30import org.mobicents.protocols.smpp.util.APIConfig;
 31import org.mobicents.protocols.smpp.util.APIConfigFactory;
 32import org.mobicents.protocols.smpp.util.PropertiesAPIConfig;
 33import org.mobicents.protocols.smpp.version.SMPPVersion;
 34import org.mobicents.protocols.smpp.version.VersionException;
 35import org.mobicents.protocols.smpp.version.VersionFactory;
 36
 37@Test
 38public class VersionFactoryTest {
 39
 40    public void testGetDefaultVersionNoConfig() {
 41        PropertiesAPIConfig cfg = new PropertiesAPIConfig();
 42        cfg.initialise();
 43        cfg.remove(APIConfig.DEFAULT_VERSION);
 44        APIConfigFactory.setCachedConfig(cfg);
 45        assertSame(VersionFactory.getDefaultVersion(), SMPPVersion.VERSION_5_0);
 46        APIConfigFactory.reset();
 47    }
 48
 49    @Test(expectedExceptions = {VersionException.class})
 50    public void testGetDefaultVersionStrictWithValidConfig() {
 51        PropertiesAPIConfig cfg = new PropertiesAPIConfig();
 52        cfg.initialise();
 53        APIConfigFactory.setCachedConfig(cfg);
 54        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0x33");
 55        assertSame(VersionFactory.getDefaultVersion(), SMPPVersion.VERSION_3_3);
 56        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0x34");
 57        assertSame(VersionFactory.getDefaultVersion(), SMPPVersion.VERSION_3_4);
 58        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0x50");
 59        assertSame(VersionFactory.getDefaultVersion(), SMPPVersion.VERSION_5_0);
 60        
 61        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0x1a");
 62        try {
 63            VersionFactory.getDefaultVersion();
 64        } finally {
 65            APIConfigFactory.reset();
 66        }
 67    }
 68
 69    @Test(expectedExceptions = {VersionException.class})
 70    public void testGetDefaultVersionLaxWithValidConfig() {
 71        PropertiesAPIConfig cfg = new PropertiesAPIConfig();
 72        cfg.initialise();
 73        APIConfigFactory.setCachedConfig(cfg);
 74        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0x33");
 75        assertSame(VersionFactory.getDefaultVersion(), SMPPVersion.VERSION_3_3);
 76        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0x34");
 77        assertSame(VersionFactory.getDefaultVersion(), SMPPVersion.VERSION_3_4);
 78        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0x50");
 79        assertSame(VersionFactory.getDefaultVersion(), SMPPVersion.VERSION_5_0);
 80        cfg.setProperty(APIConfig.DEFAULT_VERSION, "0xab");
 81        try {
 82            VersionFactory.getDefaultVersion();
 83        } finally {
 84            APIConfigFactory.reset();
 85        }
 86    }
 87    
 88    public void testGetVersionStrict() throws Exception {
 89        PropertiesAPIConfig cfg = new PropertiesAPIConfig();
 90        cfg.initialise();
 91        APIConfigFactory.setCachedConfig(cfg);
 92        cfg.setProperty(APIConfig.LAX_VERSIONS, "false");
 93        assertSame(VersionFactory.getVersion(0x33), SMPPVersion.VERSION_3_3);
 94        assertSame(VersionFactory.getVersion(0x34), SMPPVersion.VERSION_3_4);
 95        assertSame(VersionFactory.getVersion(0x50), SMPPVersion.VERSION_5_0);
 96        final int[] falseVersions = { 0, 0x13, 0x2c, 0x32, 0x40, 0x80 };
 97        for (int i = 0; i < falseVersions.length; i++) {
 98            try {
 99                VersionFactory.getVersion(falseVersions[i]);
100                fail("Successfully got a version from value "
101                        + falseVersions[i]);
102            } catch (VersionException x) {
103                // This is expected.
104            }
105        }
106        APIConfigFactory.reset();
107    }
108
109    public void testGetVersionLax() throws Exception {
110        PropertiesAPIConfig cfg = new PropertiesAPIConfig();
111        cfg.initialise();
112        APIConfigFactory.setCachedConfig(cfg);
113        cfg.setProperty(APIConfig.LAX_VERSIONS, "true");
114        for (int i = 0; i <= 0x33; i++) {
115            assertSame(VersionFactory.getVersion(i), SMPPVersion.VERSION_3_3);
116        }
117        APIConfigFactory.reset();
118    }
119}