PageRenderTime 28ms CodeModel.GetById 7ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/isup/isup-impl/src/test/java/org/mobicents/protocols/ss7/isup/impl/message/parameter/ParameterHarness.java

http://mobicents.googlecode.com/
Java | 266 lines | 177 code | 46 blank | 43 comment | 34 complexity | f8efd86fe21780c01ca7073bbe4be12f 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
 23/**
 24 * Start time:09:16:42 2009-04-22<br>
 25 * Project: mobicents-isup-stack<br>
 26 * 
 27 * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski
 28 *         </a>
 29 * 
 30 */
 31package org.mobicents.protocols.ss7.isup.impl.message.parameter;
 32
 33import java.io.IOException;
 34import java.lang.reflect.Method;
 35import java.util.ArrayList;
 36import java.util.Arrays;
 37import java.util.List;
 38
 39import static org.testng.Assert.*;
 40import org.testng.*;
 41import org.testng.annotations.*;
 42
 43import org.mobicents.protocols.ss7.isup.ParameterException;
 44
 45/**
 46 * Start time:09:16:42 2009-04-22<br>
 47 * Project: mobicents-isup-stack<br>
 48 * 
 49 * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski
 50 *         </a>
 51 */
 52public abstract class ParameterHarness  {
 53
 54	// 21 10000011 Address....................... 83
 55	// 22 01100000 Address....................... 60
 56	// 23 00111000 Address....................... 38
 57	// NOTE: now see how nice digits swap can come out with conversion, lol
 58	private final static byte[] sixDigits = new byte[] { (byte) 0x83, 0x60, 0x38 };
 59	private final static byte[] fiveDigits = new byte[] { (byte) 0x83, 0x60, 0x08 };
 60	private final static byte[] sevenDigits  = new byte[] { (byte) 0x83, 0x60,0x33, 0x08 };
 61	private final static byte[] eightDigits  = new byte[] { (byte) 0x83, 0x60,0x33, 0x48 };
 62	private final static byte[] threeDigits = new byte[] { (byte) 0x83, 0x0 };;
 63	private final static String sixDigitsString = "380683";
 64	private final static String fiveDigitsString = "38068";
 65	private final static String sevenDigitsString = "3806338";
 66	private final static String eightDigitsString = "38063384";
 67	private final static String threeDigitsString = "380";
 68
 69	// FIXME: add code to check values :)
 70
 71	protected List<byte[]> goodBodies = new ArrayList<byte[]>();
 72
 73	protected List<byte[]> badBodies = new ArrayList<byte[]>();
 74
 75	protected String makeCompare(byte[] hardcodedBody, byte[] elementEncoded) {
 76		int totalLength = 0;
 77		if (hardcodedBody == null || elementEncoded == null) {
 78			return "One arg is null";
 79		}
 80		if (hardcodedBody.length >= elementEncoded.length) {
 81			totalLength = hardcodedBody.length;
 82		} else {
 83			totalLength = elementEncoded.length;
 84		}
 85
 86		String out = "";
 87
 88		for (int index = 0; index < totalLength; index++) {
 89			if (hardcodedBody.length > index) {
 90				out += "hardcodedBody[" + Integer.toHexString(hardcodedBody[index]) + "]";
 91			} else {
 92				out += "hardcodedBody[NOP]";
 93			}
 94
 95			if (elementEncoded.length > index) {
 96				out += "elementEncoded[" + Integer.toHexString(elementEncoded[index]) + "]";
 97			} else {
 98				out += "elementEncoded[NOP]";
 99			}
100			out += "\n";
101		}
102
103		return out;
104	}
105	public String makeCompare(int[] hardcodedBody, int[] elementEncoded) {
106
107		int totalLength = 0;
108		if (hardcodedBody == null || elementEncoded == null) {
109			return "One arg is null";
110		}
111		if (hardcodedBody.length >= elementEncoded.length) {
112			totalLength = hardcodedBody.length;
113		} else {
114			totalLength = elementEncoded.length;
115		}
116
117		String out = "";
118
119		for (int index = 0; index < totalLength; index++) {
120			if (hardcodedBody.length > index) {
121				out += "hardcodedBody[" + Integer.toHexString(hardcodedBody[index]) + "]";
122			} else {
123				out += "hardcodedBody[NOP]";
124			}
125
126			if (elementEncoded.length > index) {
127				out += "elementEncoded[" + Integer.toHexString(elementEncoded[index]) + "]";
128			} else {
129				out += "elementEncoded[NOP]";
130			}
131			out += "\n";
132		}
133
134		return out;
135	}
136	
137	@Test(groups = { "functional.encode","functional.decode","parameter"})
138	public void testDecodeEncode() throws IOException, ParameterException {
139
140		for (int index = 0; index < this.goodBodies.size(); index++) {
141			byte[] goodBody = this.goodBodies.get(index);
142			AbstractISUPParameter component = this.getTestedComponent();
143			doTestDecode(goodBody, true, component, index);
144			byte[] encodedBody = component.encode();
145			boolean equal = Arrays.equals(goodBody, encodedBody);
146			assertTrue(equal,"Body index: " + index + "\n" + makeCompare(goodBody, encodedBody));
147
148		}
149		for (int index = 0; index < this.badBodies.size(); index++) {
150
151			byte[] badBody = this.badBodies.get(index);
152			AbstractISUPParameter component = this.getTestedComponent();
153			doTestDecode(badBody, false, component, index);
154			byte[] encodedBody = component.encode();
155			//TODO: make some tests here?
156		}
157
158	}
159
160	public abstract AbstractISUPParameter getTestedComponent() throws ParameterException;
161
162	protected void doTestDecode(byte[] presumableBody, boolean shouldPass, AbstractISUPParameter component, int index) throws ParameterException {
163		try {
164			component.decode(presumableBody);
165			if (!shouldPass) {
166				fail("Decoded[" + index + "] parameter[" + component.getClass() + "], should not pass. Passed data: " + dumpData(presumableBody));
167			}
168
169		} catch (IllegalArgumentException iae) {
170			if (shouldPass) {
171				fail("Failed to decode[" + index + "] parameter[" + component.getClass() + "], should not happen. " + iae + ".Passed data: " + dumpData(presumableBody));
172				iae.printStackTrace();
173			}
174		} catch (ParameterException iae) {
175			if (shouldPass) {
176				fail("Failed to decode[" + index + "] parameter[" + component.getClass() + "], should not happen. " + iae + ".Passed data: " + dumpData(presumableBody));
177				throw iae;
178			}
179		}
180		catch (Exception e) {
181			fail("Failed to decode[" + index + "] parameter[" + component.getClass() + "]." + e + ". Passed data: " + dumpData(presumableBody));
182			e.printStackTrace();
183		}
184	}
185
186	protected String dumpData(byte[] b) {
187		String s = "\n";
188		for (byte bb : b) {
189			s += Integer.toHexString(bb);
190		}
191
192		return s;
193	}
194
195	public void testValues(AbstractISUPParameter component, String[] getterMethodNames, Object[] expectedValues) {
196		try {
197			Class cClass = component.getClass();
198			for (int index = 0; index < getterMethodNames.length; index++) {
199				Method m = cClass.getMethod(getterMethodNames[index], null);
200				// Should not be null by now
201				Object v = m.invoke(component, null);
202				if (v == null && expectedValues != null) {
203					fail("Failed to validate values in component: " + component.getClass().getName() + ". Value of: " + getterMethodNames[index] + " is null, but test values is not.");
204				}
205				if(expectedValues[index].getClass().isArray() )
206				{
207					assertTrue(Arrays.deepEquals(new Object[]{expectedValues[index]},new Object[]{ v}),"Failed to validate values in component: " + component.getClass().getName() + ". Value of: " + getterMethodNames[index]);
208				}else
209				{
210					assertEquals(  v,expectedValues[index],"Failed to validate values in component: " + component.getClass().getName() + ". Value of: " + getterMethodNames[index]);
211				}
212
213			}
214
215		} catch (Exception e) {
216			fail("Failed to check values on component: " + component.getClass().getName() + ", due to: " + e);
217			e.printStackTrace();
218		}
219	}
220
221	public static byte[] getSixDigits() {
222		return sixDigits;
223	}
224
225	public static byte[] getFiveDigits() {
226		return fiveDigits;
227	}
228
229	public static byte[] getThreeDigits() {
230		return threeDigits;
231	}
232	public static byte[] getSevenDigits()
233	{
234		return sevenDigits;
235	}
236	public static byte[] getEightDigits()
237	{
238		return eightDigits;
239	}
240	
241	public static String getSixDigitsString() {
242		return sixDigitsString;
243	}
244
245	public static String getFiveDigitsString() {
246		return fiveDigitsString;
247	}
248
249	public static String getThreeDigitsString() {
250		return threeDigitsString;
251	}
252	
253
254	public static String getSevenDigitsString() {
255		return sevenDigitsString;
256	}
257	
258
259	public static String getEightDigitsString() {
260		return eightDigitsString;
261	}
262
263	
264	
265
266}