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