Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomli committed Feb 27, 2013
1 parent fbdfe8a commit 2a1dfa1
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
135 changes: 135 additions & 0 deletions core/src/test/java/org/smpp/pdu/ByteDataTest.java
@@ -0,0 +1,135 @@
package org.smpp.pdu;

import static org.junit.Assert.*;
import static org.powermock.reflect.Whitebox.*;
import static org.smpp.pdu.Matchers.*;

import java.io.UnsupportedEncodingException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ByteDataTest {
private static Class<?> CLAZZ = ByteData.class;

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testCheckStringEqualZero() throws Exception {
checkString("", 0);
}
@Test
public void testCheckStringNonZero() throws Exception {
checkString(" ", 1);
}
@Test
public void testCheckStringOneZero() throws Exception {
thrown.expect(stringLengthException(0, 0, 1));
checkString(" ", 0);
}

@Test
public void testCheckStringUTF16() throws Exception {
thrown.expect(stringLengthException(0, 1, 4));
checkString(" ", 1, "UTF-16");
}
@Test
public void testCheckStringInvalidEncoding() throws Exception {
thrown.expect(UnsupportedEncodingException.class);
checkString(" ", 1, "UTF-17");
}

@Test
public void testCheckCString() throws Exception {
thrown.expect(stringLengthException(0, 0, 1));
invokeMethod(CLAZZ, "checkCString", (String) null, 0, 0);
}

@Test
public void testCheckRange() throws Exception {
checkRange(0, 0, 0);
checkRange(0, 10, 100);
}
@Test
public void testCheckRangeBelow() throws Exception {
thrown.expect(integerOutOfRange(5, 10, 1));
checkRange(5, 1, 10);
}
@Test
public void testCheckRangeAbove() throws Exception {
thrown.expect(integerOutOfRange(5, 10, 11));
checkRange(5, 11, 10);
}

@Test
public void testDecodeUnsignedByte() throws Exception {
assertEquals(0, decodeUnsigned((byte) 0x00));
assertEquals(127, decodeUnsigned((byte) 0x7f));
assertEquals(255, decodeUnsigned((byte) 0xff));
}
@Test
public void testDecodeUnsignedShort() throws Exception {
assertEquals(0, decodeUnsigned((short) 0));
assertEquals(32768, decodeUnsigned((short) 32768));
}
@Test
public void testEncodeUnsignedShort() throws Exception {
assertEquals((byte) 0x00, encodeUnsigned((short) 0));
assertEquals((byte) 0xff, encodeUnsigned((short) 255));
}
@Test
public void testEncodeUnsignedInt() throws Exception {
assertEquals((short) 0, encodeUnsigned((int) 0));
assertEquals((short) 32768, encodeUnsigned((int) 32768));
}

// FIXME: plenty more tests to write here

// maps to ByteData static methods

private void checkString(String string, int max) throws Exception {
invokeMethod(CLAZZ, "checkString", string, max);
}
private void checkString(String string, int min, int max) throws Exception {
invokeMethod(CLAZZ, "checkString", string, min, max);
}
private void checkString(String string, int max, String encoding) throws Exception {
invokeMethod(CLAZZ, "checkString", string, max, encoding);
}
private void checkString(String string, int min, int max, String encoding) throws Exception {
invokeMethod(CLAZZ, "checkString", string, min, max, encoding);
}
private void checkString(int min, int length, int max) throws Exception {
invokeMethod(CLAZZ, "checkString", min, length, max);
}

private void checkCString(String string, int max) throws Exception {
invokeMethod(CLAZZ, "checkCString", string, max);
}
private void checkCString(String string, int min, int max) throws Exception {
invokeMethod(CLAZZ, "checkCString", string, min, max);
}

private void checkDate(String string) throws Exception {
invokeMethod(CLAZZ, "checkDate", string);
}

private void checkRange(int min, int value, int max) throws Exception {
invokeMethod(CLAZZ, "checkRange", min, value, max);
}

private short decodeUnsigned(byte bite) throws Exception {
return invokeMethod(CLAZZ, "decodeUnsigned", bite);
}
private int decodeUnsigned(short value) throws Exception {
return invokeMethod(CLAZZ, "decodeUnsigned", value);
}
private byte encodeUnsigned(short value) throws Exception {
return invokeMethod(CLAZZ, "encodeUnsigned", value);
}
private short encodeUnsigned(int positive) throws Exception {
return invokeMethod(CLAZZ, "encodeUnsigned", positive);
}
}
67 changes: 67 additions & 0 deletions core/src/test/java/org/smpp/pdu/Matchers.java
@@ -0,0 +1,67 @@
package org.smpp.pdu;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

public class Matchers {
public static Matcher<WrongLengthOfStringException> stringLengthException(int min, int max, int actual) {
return new WrongLengthOfStringExceptionMatcher(min, max, actual);
}

public static Matcher<IntegerOutOfRangeException> integerOutOfRange(int min, int max, int actual) {
return new IntegerOutOfRangeExceptionMatcher(min, max, actual);
}

private static class WrongLengthOfStringExceptionMatcher extends BaseMatcher<WrongLengthOfStringException> {
private static final String FORMAT = "org.smpp.pdu.WrongLengthOfStringException: The string is shorter or longer than required: min=%d max=%d actual=%d.";

private final int min;
private final int max;
private final int actual;

private WrongLengthOfStringExceptionMatcher(int min, int max, int actual) {
this.min = min;
this.max = max;
this.actual = actual;
}

public boolean matches(Object item) {
if (! (item instanceof WrongLengthOfStringException)) {
return false;
}

return String.format(FORMAT, min, max, actual).equals(((WrongLengthOfStringException) item).toString());
}

public void describeTo(Description description) {
description.appendText("Wrong length of string");
}
}

public static class IntegerOutOfRangeExceptionMatcher extends BaseMatcher<IntegerOutOfRangeException> {
private static final String FORMAT = "org.smpp.pdu.IntegerOutOfRangeException: The integer is lower or greater than required: min=%d max=%d actual=%d.";

private final int min;
private final int max;
private final int actual;

private IntegerOutOfRangeExceptionMatcher(int min, int max, int actual) {
this.min = min;
this.max = max;
this.actual = actual;
}

public boolean matches(Object item) {
if (! (item instanceof IntegerOutOfRangeException)) {
return false;
}

return String.format(FORMAT, min, max, actual).equals(((IntegerOutOfRangeException) item).toString());
}

public void describeTo(Description description) {
description.appendText("Integer out of range");
}
}
}

0 comments on commit 2a1dfa1

Please sign in to comment.