PageRenderTime 95ms CodeModel.GetById 86ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/util/APIConfigFactoryTest.java

http://mobicents.googlecode.com/
Java | 95 lines | 65 code | 9 blank | 21 comment | 0 complexity | 067e873b5debeec0036b568e11017589 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.util;
24
25import static org.testng.Assert.assertEquals;
26import static org.testng.Assert.assertNotNull;
27import static org.testng.Assert.assertNotSame;
28import static org.testng.Assert.assertSame;
29
30import org.mobicents.protocols.smpp.util.APIConfig;
31import org.mobicents.protocols.smpp.util.APIConfigFactory;
32import org.mobicents.protocols.smpp.util.InvalidConfigurationException;
33import org.mobicents.protocols.smpp.util.PropertiesAPIConfig;
34import org.testng.annotations.Test;
35
36@Test
37public class APIConfigFactoryTest {
38
39    public void testPropertiesAPIConfigIsTheDefault() {
40        APIConfigFactory.reset();
41        APIConfig config = APIConfigFactory.getConfig();
42        assertNotNull(config);
43        assertEquals(config.getClass(), PropertiesAPIConfig.class);
44    }
45    
46    public void testGetConfigReturnsACachedClass() {
47        APIConfigFactory.reset();
48        APIConfig config1 = APIConfigFactory.getConfig();
49        APIConfig config2 = APIConfigFactory.getConfig();
50        assertNotNull(config1);
51        assertNotNull(config2);
52        assertSame(config1, config2);
53    }
54    
55    public void testNewInstanceIsInstantiatedWhenCachingIsDisabled() {
56        try {
57            System.setProperty(APIConfigFactory.CACHE_CONFIG_PROP, "false");
58            APIConfigFactory.reset();
59            APIConfig config1 = APIConfigFactory.getConfig();
60            APIConfig config2 = APIConfigFactory.getConfig();
61            assertNotNull(config1);
62            assertNotNull(config2);
63            assertNotSame(config1, config2);
64        } finally {
65            System.clearProperty(APIConfigFactory.CACHE_CONFIG_PROP);
66        }
67    }
68    
69    public void testLoadConfigLoadsSpecifiedConfigClass() throws Exception {
70        try {
71            System.setProperty(
72                    APIConfigFactory.CONFIG_CLASS_PROP,
73                    "org.mobicents.protocols.smpp.util.NullAPIConfig");
74            APIConfigFactory.reset();
75            APIConfig config = APIConfigFactory.loadConfig();
76            assertNotNull(config);
77            assertEquals(config.getClass(), NullAPIConfig.class);
78        } finally {
79            System.clearProperty(APIConfigFactory.CONFIG_CLASS_PROP);
80        }
81    }
82
83    @Test(expectedExceptions = {InvalidConfigurationException.class})
84    public void testLoadConfigThrowsExceptionWhenConfigClassDoesNotExist() {
85        try {
86            System.setProperty(
87                    APIConfigFactory.CONFIG_CLASS_PROP,
88                    "org.mobicents.smpp.util.NonExistentAPIConfig");
89            APIConfigFactory.reset();
90            APIConfigFactory.loadConfig();
91        } finally {
92            System.clearProperty(APIConfigFactory.CONFIG_CLASS_PROP);
93        }
94    }
95}