PageRenderTime 70ms CodeModel.GetById 57ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/primitives/AlertingPatternTest.java

http://mobicents.googlecode.com/
Java | 137 lines | 83 code | 23 blank | 31 comment | 0 complexity | 9b0819bfe8368f923ac307427d658cf7 MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual
  4 * contributors as indicated by the @authors tag. All rights reserved.
  5 * See the copyright.txt in the distribution for a full listing
  6 * of individual contributors.
  7 * 
  8 * This copyrighted material is made available to anyone wishing to use,
  9 * modify, copy, or redistribute it subject to the terms and conditions
 10 * of the GNU General Public License, v. 2.0.
 11 * 
 12 * This program 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 * General Public License for more details.
 16 * 
 17 * You should have received a copy of the GNU General Public License,
 18 * v. 2.0 along with this distribution; if not, write to the Free 
 19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 20 * MA 02110-1301, USA.
 21 */
 22package org.mobicents.protocols.ss7.map.primitives;
 23
 24import static org.testng.Assert.assertEquals;
 25import static org.testng.Assert.assertNotNull;
 26import static org.testng.Assert.assertNull;
 27import static org.testng.Assert.assertTrue;
 28
 29import java.io.ByteArrayInputStream;
 30import java.io.ByteArrayOutputStream;
 31import java.io.InputStream;
 32import java.io.ObjectInputStream;
 33import java.io.ObjectOutputStream;
 34import java.util.Arrays;
 35
 36import org.mobicents.protocols.asn.AsnInputStream;
 37import org.mobicents.protocols.asn.AsnOutputStream;
 38import org.mobicents.protocols.ss7.map.api.primitives.AlertingCategory;
 39import org.mobicents.protocols.ss7.map.api.primitives.AlertingLevel;
 40import org.testng.annotations.AfterClass;
 41import org.testng.annotations.AfterTest;
 42import org.testng.annotations.BeforeClass;
 43import org.testng.annotations.BeforeTest;
 44import org.testng.annotations.Test;
 45
 46/**
 47 * @author amit bhayani
 48 *
 49 */
 50public class AlertingPatternTest {
 51	@BeforeClass
 52	public static void setUpClass() throws Exception {
 53	}
 54
 55	@AfterClass
 56	public static void tearDownClass() throws Exception {
 57	}
 58
 59	@BeforeTest
 60	public void setUp() {
 61	}
 62
 63	@AfterTest
 64	public void tearDown() {
 65	}
 66
 67	@Test(groups = { "functional.decode","primitives"})
 68	public void testDecode() throws Exception {
 69		byte[] data = new byte[] { (byte) 0x04, 0x01, 0x07 };
 70		
 71		AsnInputStream asn = new AsnInputStream(data);
 72		int tag = asn.readTag();
 73
 74		AlertingPatternImpl addNum = new AlertingPatternImpl();
 75		addNum.decodeAll(asn);
 76		assertNull(addNum.getAlertingLevel());
 77		assertNotNull(addNum.getAlertingCategory());
 78		
 79		assertEquals( addNum.getAlertingCategory(),AlertingCategory.Category4);
 80
 81	}
 82
 83	@Test(groups = { "functional.encode","primitives"})
 84	public void testEncode() throws Exception {
 85		byte[] data = new byte[] { (byte) 0x04, 0x01, 0x07 };
 86		
 87		AlertingPatternImpl addNum = new AlertingPatternImpl(AlertingCategory.Category4);
 88		
 89		AsnOutputStream asnOS = new AsnOutputStream();
 90		addNum.encodeAll(asnOS);
 91		
 92		byte[] encodedData = asnOS.toByteArray();
 93
 94		assertTrue(Arrays.equals(data, encodedData));
 95	}
 96	
 97	@Test(groups = { "functional.serialize", "primitives" })
 98	public void testSerialization() throws Exception {
 99		AlertingPatternImpl original = new AlertingPatternImpl(AlertingCategory.Category4);
100		// serialize
101		ByteArrayOutputStream out = new ByteArrayOutputStream();
102		ObjectOutputStream oos = new ObjectOutputStream(out);
103		oos.writeObject(original);
104		oos.close();
105
106		// deserialize
107		byte[] pickled = out.toByteArray();
108		InputStream in = new ByteArrayInputStream(pickled);
109		ObjectInputStream ois = new ObjectInputStream(in);
110		Object o = ois.readObject();
111		AlertingPatternImpl copy = (AlertingPatternImpl) o;
112		
113		//test result
114		assertEquals(copy.getAlertingCategory(), original.getAlertingCategory());
115		assertEquals(copy, original);
116		assertNull(copy.getAlertingLevel());
117		
118		original = new AlertingPatternImpl(AlertingLevel.Level1);
119		// serialize
120		out = new ByteArrayOutputStream();
121		oos = new ObjectOutputStream(out);
122		oos.writeObject(original);
123		oos.close();
124
125		// deserialize
126		pickled = out.toByteArray();
127		in = new ByteArrayInputStream(pickled);
128		ois = new ObjectInputStream(in);
129		o = ois.readObject();
130		copy = (AlertingPatternImpl) o;
131		
132		//test result
133		assertEquals(copy.getAlertingLevel(), original.getAlertingLevel());
134		assertEquals(copy, original);
135		assertNull(copy.getAlertingCategory());
136	}
137}