Migrate net.i2p.data tests in the router to JUnit 4

This commit is contained in:
str4d
2017-12-02 20:08:44 +00:00
parent b6298dc091
commit 10d5a17422
3 changed files with 74 additions and 66 deletions

View File

@ -8,7 +8,11 @@ package net.i2p.data.i2np;
*
*/
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import net.i2p.I2PAppContext;
import net.i2p.data.DataFormatException;
@ -24,6 +28,10 @@ import net.i2p.util.Clock;
* @author jrandom
*/
public class DatabaseStoreMessageTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException {
DatabaseStoreMessage msg = new DatabaseStoreMessage(I2PAppContext.getGlobalContext());
RouterInfo info = (RouterInfo)new RouterInfoTest().createDataStructure();
@ -40,9 +48,7 @@ public class DatabaseStoreMessageTest extends StructureTest {
@Override
@Test
public void testStructure() throws Exception {
try {
super.testStructure();
fail("should not be supported");
} catch (UnsupportedOperationException expected){}
exception.expect(UnsupportedOperationException.class);
super.testStructure();
}
}

View File

@ -8,9 +8,15 @@ package net.i2p.data.router;
*
*/
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.util.Properties;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.StructureTest;
@ -22,6 +28,10 @@ import net.i2p.util.OrderedProperties;
* @author jrandom
*/
public class RouterAddressTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException {
//addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970
OrderedProperties options = new OrderedProperties();
@ -33,44 +43,37 @@ public class RouterAddressTest extends StructureTest {
public DataStructure createStructureToRead() { return new RouterAddress(); }
@SuppressWarnings("deprecation")
@Test
public void testSetNullOptions(){
RouterAddress addr = new RouterAddress();
boolean error = false;
try{
addr.setOptions(null);
}catch(NullPointerException dfe){
error = true;
}
assertTrue(error);
exception.expect(NullPointerException.class);
addr.setOptions(null);
}
@SuppressWarnings("deprecation")
@Test
public void testSetOptionsAgain(){
OrderedProperties options = new OrderedProperties();
options.setProperty("hostname", "localhost");
options.setProperty("portnum", "1234");
RouterAddress addr = new RouterAddress("Blah", options, 42);
options.setProperty("portnum", "2345");
boolean error = false;
try{
addr.setOptions(options);
}catch(IllegalStateException dfe){
error = true;
}
assertTrue(error);
exception.expect(IllegalStateException.class);
addr.setOptions(options);
}
@Test
public void testBadWrite() throws Exception{
RouterAddress addr = new RouterAddress();
boolean error = false;
try{
addr.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
exception.expect(DataFormatException.class);
exception.expectMessage("uninitialized");
addr.writeBytes(new ByteArrayOutputStream());
}
@Test
public void testNullEquals(){
//addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970
OrderedProperties options = new OrderedProperties();
@ -81,6 +84,7 @@ public class RouterAddressTest extends StructureTest {
assertFalse(addr.equals(""));
}
@Test
public void testToString(){
//addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970
OrderedProperties options = new OrderedProperties();

View File

@ -8,8 +8,14 @@ package net.i2p.data.router;
*
*/
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import net.i2p.data.Certificate;
import net.i2p.data.CertificateTest;
import net.i2p.data.DataFormatException;
@ -26,6 +32,10 @@ import net.i2p.data.StructureTest;
* @author jrandom
*/
public class RouterIdentityTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException {
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
@ -37,7 +47,8 @@ public class RouterIdentityTest extends StructureTest {
return ident;
}
public DataStructure createStructureToRead() { return new RouterIdentity(); }
@Test
public void testNullCert() throws Exception{
RouterIdentity ident = new RouterIdentity();
ident.setCertificate(null);
@ -45,16 +56,13 @@ public class RouterIdentityTest extends StructureTest {
ident.setPublicKey(pk);
SigningPublicKey k = (SigningPublicKey)(new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
boolean error = false;
try{
ident.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
exception.expect(DataFormatException.class);
exception.expectMessage("Not enough data to format the router identity");
ident.writeBytes(new ByteArrayOutputStream());
}
@Test
public void testNullPublicKey() throws Exception{
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
@ -62,17 +70,13 @@ public class RouterIdentityTest extends StructureTest {
ident.setPublicKey(null);
SigningPublicKey k = (SigningPublicKey)(new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
boolean error = false;
try{
ident.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
exception.expect(DataFormatException.class);
exception.expectMessage("Not enough data to format the router identity");
ident.writeBytes(new ByteArrayOutputStream());
}
@Test
public void testNullSigningKey() throws Exception{
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
@ -80,21 +84,19 @@ public class RouterIdentityTest extends StructureTest {
PublicKey pk = (PublicKey)(new PublicKeyTest()).createDataStructure();
ident.setPublicKey(pk);
ident.setSigningPublicKey(null);
boolean error = false;
try{
ident.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
exception.expect(DataFormatException.class);
exception.expectMessage("Not enough data to format the router identity");
ident.writeBytes(new ByteArrayOutputStream());
}
@Test
public void testNullEquals() throws Exception{
RouterIdentity ident = new RouterIdentity();
assertFalse(ident.equals(null));
}
@Test
public void testCalculatedHash() throws Exception{
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
@ -103,24 +105,20 @@ public class RouterIdentityTest extends StructureTest {
ident.setPublicKey(pk);
SigningPublicKey k = (SigningPublicKey)(new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
ident.calculateHash();
ident.calculateHash();
ident.calculateHash();
ident.calculateHash();
ident.calculateHash();
}
@Test
public void testBadHash() throws Exception {
RouterIdentity ident = new RouterIdentity();
boolean error = false;
try {
ident.getHash();
} catch (IllegalStateException ise) {
error = true;
}
assertTrue(error);
exception.expect(IllegalStateException.class);
exception.expectMessage("KAC hash error");
ident.getHash();
}
}