PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/notnoop/apns/integration/ApnsConnectionTest.java

http://github.com/notnoop/java-apns
Java | 187 lines | 127 code | 23 blank | 37 comment | 0 complexity | 0508729622810e725922adb088681848 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Mahmood Ali. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.notnoop.apns.integration;
  31. import com.notnoop.apns.APNS;
  32. import com.notnoop.apns.ApnsService;
  33. import com.notnoop.apns.EnhancedApnsNotification;
  34. import com.notnoop.apns.SimpleApnsNotification;
  35. import com.notnoop.apns.utils.ApnsServerStub;
  36. import com.notnoop.apns.utils.junit.DumpThreadsOnErrorRule;
  37. import com.notnoop.apns.utils.junit.Repeat;
  38. import com.notnoop.apns.utils.junit.RepeatRule;
  39. import com.notnoop.exceptions.NetworkIOException;
  40. import org.junit.After;
  41. import org.junit.Before;
  42. import org.junit.Rule;
  43. import org.junit.Test;
  44. import org.junit.rules.TestName;
  45. import static com.notnoop.apns.utils.FixedCertificates.LOCALHOST;
  46. import static com.notnoop.apns.utils.FixedCertificates.clientContext;
  47. import static com.notnoop.apns.utils.FixedCertificates.clientMultiKeyContext;
  48. import static org.junit.Assert.assertArrayEquals;
  49. import static org.junit.Assert.assertTrue;
  50. import static org.junit.Assert.fail;
  51. @SuppressWarnings("ALL")
  52. public class ApnsConnectionTest {
  53. @Rule
  54. public TestName testName = new TestName();
  55. @Rule
  56. public RepeatRule rr = new RepeatRule();
  57. @Rule
  58. public DumpThreadsOnErrorRule dumpRule = new DumpThreadsOnErrorRule();
  59. ApnsServerStub server;
  60. static SimpleApnsNotification msg1 = new SimpleApnsNotification("a87d8878d878a79", "{\"aps\":{}}");
  61. static SimpleApnsNotification msg2 = new SimpleApnsNotification("a87d8878d878a88", "{\"aps\":{}}");
  62. static EnhancedApnsNotification eMsg1 = new EnhancedApnsNotification(EnhancedApnsNotification.INCREMENT_ID(),
  63. 1, "a87d8878d878a88", "{\"aps\":{}}");
  64. static EnhancedApnsNotification eMsg2 = new EnhancedApnsNotification(EnhancedApnsNotification.INCREMENT_ID(),
  65. 1, "a87d8878d878a88", "{\"aps\":{}}");
  66. static EnhancedApnsNotification eMsg3 = new EnhancedApnsNotification(EnhancedApnsNotification.INCREMENT_ID(),
  67. 1, "a87d8878d878a88", "{\"aps\":{}}");
  68. private int gatewayPort;
  69. @Before
  70. public void startup() {
  71. System.out.println("****** "+testName.getMethodName());
  72. server = ApnsServerStub.prepareAndStartServer();
  73. gatewayPort = server.getEffectiveGatewayPort();
  74. }
  75. @After
  76. public void tearDown() {
  77. server.stop();
  78. server = null;
  79. }
  80. @Repeat(count = 50)
  81. @Test(timeout = 2000)
  82. public void sendOneSimple() throws InterruptedException {
  83. ApnsService service =
  84. APNS.newService().withSSLContext(clientContext())
  85. .withGatewayDestination(LOCALHOST, gatewayPort)
  86. .build();
  87. server.stopAt(msg1.length());
  88. service.push(msg1);
  89. server.getMessages().acquire();
  90. assertArrayEquals(msg1.marshall(), server.getReceived().toByteArray());
  91. }
  92. @Repeat(count = 50)
  93. @Test(timeout = 2000)
  94. public void sendOneQueued() throws InterruptedException {
  95. ApnsService service =
  96. APNS.newService().withSSLContext(clientContext())
  97. .withGatewayDestination(LOCALHOST, gatewayPort)
  98. .asQueued()
  99. .build();
  100. server.stopAt(msg1.length());
  101. service.push(msg1);
  102. server.getMessages().acquire();
  103. assertArrayEquals(msg1.marshall(), server.getReceived().toByteArray());
  104. }
  105. @Test
  106. public void sendOneSimpleWithoutTimeout() throws InterruptedException {
  107. server.getToWaitBeforeSend().set(2000);
  108. ApnsService service =
  109. APNS.newService().withSSLContext(clientContext())
  110. .withGatewayDestination(LOCALHOST, gatewayPort)
  111. .withReadTimeout(5000)
  112. .build();
  113. server.stopAt(msg1.length());
  114. service.push(msg1);
  115. server.getMessages().acquire();
  116. assertArrayEquals(msg1.marshall(), server.getReceived().toByteArray());
  117. }
  118. /**
  119. * Unlike in the feedback case, push messages won't expose the socket timeout,
  120. * as the read is done in a separate monitoring thread.
  121. *
  122. * Therefore, normal behavior is expected in this test.
  123. *
  124. * @throws InterruptedException
  125. */
  126. @Test
  127. public void sendOneSimpleWithTimeout() throws InterruptedException {
  128. server.getToWaitBeforeSend().set(5000);
  129. ApnsService service =
  130. APNS.newService().withSSLContext(clientContext())
  131. .withGatewayDestination(LOCALHOST, gatewayPort)
  132. .withReadTimeout(1000)
  133. .build();
  134. server.stopAt(msg1.length());
  135. service.push(msg1);
  136. server.getMessages().acquire();
  137. assertArrayEquals(msg1.marshall(), server.getReceived().toByteArray());
  138. }
  139. @Test(timeout = 2000)
  140. public void sendOneSimpleMultiKey() throws InterruptedException {
  141. ApnsService service =
  142. APNS.newService().withSSLContext(clientMultiKeyContext("notnoop-client"))
  143. .withGatewayDestination(LOCALHOST, gatewayPort)
  144. .build();
  145. server.stopAt(msg1.length());
  146. service.push(msg1);
  147. server.getMessages().acquire();
  148. assertArrayEquals(msg1.marshall(), server.getReceived().toByteArray());
  149. }
  150. @Test(timeout = 2000)
  151. public void sendOneSimpleClientCertFail() throws InterruptedException {
  152. ApnsService service =
  153. APNS.newService().withSSLContext(clientMultiKeyContext("notused"))
  154. .withGatewayDestination(LOCALHOST, gatewayPort)
  155. .build();
  156. server.stopAt(msg1.length());
  157. try {
  158. service.push(msg1);
  159. fail();
  160. } catch (NetworkIOException e) {
  161. assertTrue("Expected bad_certifcate exception", e.getMessage().contains("bad_certificate"));
  162. }
  163. }
  164. }