PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/notnoop/apns/internal/ApnsFeedbackParsingUtils.java

http://github.com/notnoop/java-apns
Java | 162 lines | 108 code | 21 blank | 33 comment | 15 complexity | 481c6727a2618a045fdaeccdca35617b MD5 | raw file
  1/*
  2 *  Copyright 2009, Mahmood Ali.
  3 *  All rights reserved.
  4 *
  5 *  Redistribution and use in source and binary forms, with or without
  6 *  modification, are permitted provided that the following conditions are
  7 *  met:
  8 *
  9 *    * Redistributions of source code must retain the above copyright
 10 *      notice, this list of conditions and the following disclaimer.
 11 *    * Redistributions in binary form must reproduce the above
 12 *      copyright notice, this list of conditions and the following disclaimer
 13 *      in the documentation and/or other materials provided with the
 14 *      distribution.
 15 *    * Neither the name of Mahmood Ali. nor the names of its
 16 *      contributors may be used to endorse or promote products derived from
 17 *      this software without specific prior written permission.
 18 *
 19 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 20 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 21 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 22 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 23 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 24 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 25 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 26 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 27 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 28 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 29 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 30 */
 31package com.notnoop.apns.internal;
 32
 33import java.util.Arrays;
 34import java.util.Collection;
 35import java.util.Date;
 36import java.util.Map;
 37import java.util.Random;
 38import static org.hamcrest.CoreMatchers.*;
 39import static org.junit.Assert.*;
 40
 41public class ApnsFeedbackParsingUtils {
 42    static byte[] pack(byte[]... args) {
 43        int total = 0;
 44        for (byte[] arg : args)
 45            total += arg.length;
 46
 47        byte[] result = new byte[total];
 48
 49        int index = 0;
 50        for (byte[] arg : args) {
 51            System.arraycopy(arg, 0, result, index, arg.length);
 52            index += arg.length;
 53        }
 54        return result;
 55    }
 56
 57    static byte[] simpleDevice = new byte[32];
 58    static byte[] firstDevice = new byte[32];
 59    static byte[] secondDevice = new byte[32];
 60    static byte[] thirdDevice = new byte[32];
 61    static {
 62        Random random = new Random();
 63        do {
 64            random.nextBytes(simpleDevice);
 65            random.nextBytes(firstDevice);
 66            random.nextBytes(secondDevice);
 67            random.nextBytes(thirdDevice);
 68        } while (Arrays.equals(firstDevice, secondDevice)
 69                   || (Arrays.equals(secondDevice, thirdDevice))
 70                   || (Arrays.equals(firstDevice, thirdDevice)));
 71    }
 72
 73    static int simpleDate;
 74    public static byte[] simple = pack(
 75            /* time_t */  new byte[] {0, 0, 0, 0},
 76            /* length */  new byte[] { 0, 32 },
 77            /* device token */ simpleDevice
 78            );
 79
 80    static int firstDate = 10;
 81    static int secondDate = 1 << 8;
 82    static int thirdDate = secondDate;
 83    public static byte[] three = pack(
 84            /* first message */
 85            /* time_t */  new byte[] {0, 0, 0, 10},
 86            /* length */  new byte[] { 0, 32 },
 87            /* device token */ firstDevice,
 88
 89            /* second device */
 90            /* time_t */  new byte[] {0, 0, 1, 0},
 91            /* length */  new byte[] { 0, 32 },
 92            /* device token */ secondDevice,
 93
 94            /* Duplicate time */
 95            /* time_t */  new byte[] {0, 0, 1, 0},
 96            /* length */  new byte[] { 0, 32 },
 97            /* device token */ thirdDevice
 98    );
 99
100    protected static void checkRawSimple(Map<byte[], Integer> simpleParsed) {
101        assertEquals(1, simpleParsed.size());
102        assertThat(simpleParsed.keySet(), hasItem(simpleDevice));
103
104        for (Map.Entry<byte[], Integer> e : simpleParsed.entrySet()) {
105            byte[] device = e.getKey();
106            Integer date = e.getValue();
107            if (Arrays.equals(simpleDevice, device)) {
108                assertEquals(simpleDate, (int)date);
109            } else {
110                fail("Unexpected value in collection");
111            }
112        }
113    }
114
115    protected static void checkRawThree(Map<byte[], Integer> threeParsed) {
116        assertEquals(3, threeParsed.size());
117        Collection<byte[]> devices = threeParsed.keySet();
118        assertThat(devices, hasItems(firstDevice, secondDevice, thirdDevice));
119
120        for (Map.Entry<byte[], Integer> e : threeParsed.entrySet()) {
121            byte[] device = e.getKey();
122            Integer date = e.getValue();
123            if (Arrays.equals(firstDevice, device)) {
124                assertEquals(firstDate, (int)date);
125            } else if (Arrays.equals(secondDevice, device)) {
126                assertEquals(secondDate, (int)date);
127            } else if (Arrays.equals(thirdDevice, device)) {
128                assertEquals(thirdDate, (int)date);
129            } else {
130                fail("Unexpected value in collection");
131            }
132        }
133
134    }
135
136    public static void checkParsedSimple(Map<String, Date> simpleParsed) {
137        Date sd = new Date(simpleDate * 1000L);
138        String deviceToken = Utilities.encodeHex(simpleDevice);
139
140        assertEquals(1, simpleParsed.size());
141        assertThat(simpleParsed.keySet(), hasItem(deviceToken));
142        assertEquals(sd, simpleParsed.get(deviceToken));
143    }
144
145    public static void checkParsedThree(Map<String, Date> threeParsed) {
146        Date d1 = new Date(firstDate * 1000L);
147        String dt1 = Utilities.encodeHex(firstDevice);
148
149        Date d2 = new Date(secondDate * 1000L);
150        String dt2 = Utilities.encodeHex(secondDevice);
151
152        Date d3 = new Date(thirdDate * 1000L);
153        String dt3 = Utilities.encodeHex(thirdDevice);
154
155        assertEquals(3, threeParsed.size());
156        assertThat(threeParsed.keySet(), hasItems(dt1, dt2, dt3));
157        assertEquals(d1, threeParsed.get(dt1));
158        assertEquals(d2, threeParsed.get(dt2));
159        assertEquals(d3, threeParsed.get(dt3));
160    }
161
162}