From 9f1e0057c9666d39bd48654a5ed322e15d02f546 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 24 Feb 2016 23:10:04 +0100 Subject: [PATCH] Code cleanup --- .../java/ch/dissem/bitmessage/BitmessageContext.java | 5 +++-- .../ch/dissem/bitmessage/DefaultMessageListener.java | 2 +- .../java/ch/dissem/bitmessage/entity/CustomMessage.java | 7 ++++--- .../main/java/ch/dissem/bitmessage/entity/Plaintext.java | 9 +++++---- .../dissem/bitmessage/entity/valueobject/PrivateKey.java | 3 ++- .../bitmessage/exception/ApplicationException.java | 4 ++++ .../java/ch/dissem/bitmessage/ports/SimplePOWEngine.java | 3 ++- .../ch/dissem/bitmessage/ProofOfWorkServiceTest.java | 3 +-- .../bitmessage/extensions/CryptoCustomMessage.java | 5 +++-- .../java/ch/dissem/bitmessage/networking/Connection.java | 1 + .../bitmessage/repository/JdbcMessageRepository.java | 9 +++++---- .../main/java/ch/dissem/bitmessage/wif/WifExporter.java | 3 ++- 12 files changed, 33 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index 94cf3c3..f54a8f9 100644 --- a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -25,6 +25,7 @@ import ch.dissem.bitmessage.entity.payload.Pubkey.Feature; import ch.dissem.bitmessage.entity.valueobject.InventoryVector; import ch.dissem.bitmessage.entity.valueobject.Label; import ch.dissem.bitmessage.entity.valueobject.PrivateKey; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.ports.*; @@ -119,7 +120,7 @@ public class BitmessageContext { public void addDistributedMailingList(String address, String alias) { // TODO - throw new RuntimeException("not implemented"); + throw new ApplicationException("not implemented"); } public void broadcast(final BitmessageAddress from, final String subject, final String message) { @@ -187,7 +188,7 @@ public class BitmessageContext { case BROADCAST: return Factory.getBroadcast(msg); default: - throw new RuntimeException("Unknown message type " + msg.getType()); + throw new ApplicationException("Unknown message type " + msg.getType()); } } diff --git a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java index dcc62b9..d209974 100644 --- a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java +++ b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java @@ -103,7 +103,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener { address.setPubkey(pubkey); LOG.info("Got pubkey for contact " + address); ctx.getAddressRepository().save(address); - List messages = ctx.getMessageRepository().findMessages(Plaintext.Status.PUBKEY_REQUESTED, address); + List<Plaintext> messages = ctx.getMessageRepository().findMessages(PUBKEY_REQUESTED, address); LOG.info("Sending " + messages.size() + " messages for contact " + address); for (Plaintext msg : messages) { msg.setStatus(DOING_PROOF_OF_WORK); diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java b/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java index 5702b6e..ffdb9b2 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java @@ -16,6 +16,7 @@ package ch.dissem.bitmessage.entity; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.utils.AccessCounter; import ch.dissem.bitmessage.utils.Encode; @@ -66,7 +67,7 @@ public class CustomMessage implements MessagePayload { write(out); return out.toByteArray(); } catch (IOException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } } @@ -77,7 +78,7 @@ public class CustomMessage implements MessagePayload { Encode.varString(command, out); out.write(data); } else { - throw new RuntimeException("Tried to write custom message without data. " + + throw new ApplicationException("Tried to write custom message without data. " + "Programmer: did you forget to override #write()?"); } } @@ -90,7 +91,7 @@ public class CustomMessage implements MessagePayload { try { return new CustomMessage(COMMAND_ERROR, message.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } } diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java index fc38c65..fe8b1d3 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java @@ -18,6 +18,7 @@ package ch.dissem.bitmessage.entity; import ch.dissem.bitmessage.entity.valueobject.InventoryVector; import ch.dissem.bitmessage.entity.valueobject.Label; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.utils.Decode; import ch.dissem.bitmessage.utils.Encode; @@ -111,9 +112,9 @@ public class Plaintext implements Streamable { public void setTo(BitmessageAddress to) { if (this.to.getVersion() != 0) - throw new RuntimeException("Correct address already set"); + throw new IllegalStateException("Correct address already set"); if (!Arrays.equals(this.to.getRipe(), to.getRipe())) { - throw new RuntimeException("RIPEs don't match"); + throw new IllegalArgumentException("RIPEs don't match"); } this.to = to; } @@ -223,7 +224,7 @@ public class Plaintext implements Streamable { } return text; } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } @@ -402,7 +403,7 @@ public class Plaintext implements Streamable { this.encoding = Encoding.SIMPLE.getCode(); this.message = ("Subject:" + subject + '\n' + "Body:" + message).getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } return this; } diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java index d07c859..e058308 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java @@ -18,6 +18,7 @@ package ch.dissem.bitmessage.entity.valueobject; import ch.dissem.bitmessage.entity.Streamable; import ch.dissem.bitmessage.entity.payload.Pubkey; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.utils.Bytes; import ch.dissem.bitmessage.utils.Decode; @@ -71,7 +72,7 @@ public class PrivateKey implements Streamable { this.pubkey = security().createPubkey(version, stream, privateSigningKey, privateEncryptionKey, nonceTrialsPerByte, extraBytes, features); } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } diff --git a/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java b/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java index 998284d..c381410 100644 --- a/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java +++ b/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java @@ -23,4 +23,8 @@ public class ApplicationException extends RuntimeException { public ApplicationException(Throwable cause) { super(cause); } + + public ApplicationException(String message) { + super(message); + } } diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java b/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java index e8d649b..4fa1116 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java @@ -16,6 +16,7 @@ package ch.dissem.bitmessage.ports; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.utils.Bytes; import java.security.MessageDigest; @@ -37,7 +38,7 @@ public class SimplePOWEngine implements ProofOfWorkEngine { try { mda = MessageDigest.getInstance("SHA-512"); } catch (Exception e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } do { inc(nonce); diff --git a/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java b/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java index eb5d368..e489e35 100644 --- a/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java @@ -20,12 +20,10 @@ import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.Plaintext; -import ch.dissem.bitmessage.entity.payload.GetPubkey; import ch.dissem.bitmessage.entity.payload.Msg; import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.utils.Singleton; import ch.dissem.bitmessage.utils.TestUtils; -import org.hamcrest.CoreMatchers; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -39,6 +37,7 @@ import static org.junit.Assert.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.*; /** diff --git a/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java b/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java index 49c6f1b..eb2c4e7 100644 --- a/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java +++ b/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java @@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.CustomMessage; import ch.dissem.bitmessage.entity.Streamable; import ch.dissem.bitmessage.entity.payload.CryptoBox; import ch.dissem.bitmessage.entity.payload.Pubkey; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.utils.Encode; @@ -134,9 +135,9 @@ public class CryptoCustomMessage<T extends Streamable> extends CustomMessage { return read; } - public void checkSignature(Pubkey pubkey) throws IOException, RuntimeException { + public void checkSignature(Pubkey pubkey) throws IOException, IllegalStateException { if (!security().isSignatureValid(out.toByteArray(), varBytes(wrapped), pubkey)) { - throw new RuntimeException("Signature check failed"); + throw new IllegalStateException("Signature check failed"); } } } diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index fef1d32..f24736f 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -252,6 +252,7 @@ class Connection { case CUSTOM: case VERACK: case VERSION: + default: throw new RuntimeException("Unexpectedly received '" + messagePayload.getCommand() + "' command"); } } diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java index 69890c3..c01e147 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java @@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.entity.valueobject.InventoryVector; import ch.dissem.bitmessage.entity.valueobject.Label; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.ports.MessageRepository; import ch.dissem.bitmessage.utils.Strings; import org.slf4j.Logger; @@ -53,7 +54,7 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito result.add(getLabel(rs)); } } catch (SQLException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } return result; } @@ -118,7 +119,7 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito case 1: return plaintexts.get(0); default: - throw new RuntimeException("This shouldn't happen, found " + plaintexts.size() + + throw new ApplicationException("This shouldn't happen, found " + plaintexts.size() + " messages, one or none was expected"); } } @@ -225,10 +226,10 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito } catch (SQLException e1) { LOG.debug(e1.getMessage(), e); } - throw new RuntimeException(e); + throw new ApplicationException(e); } } catch (SQLException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } diff --git a/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java b/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java index b41d645..00f2790 100644 --- a/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java +++ b/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java @@ -18,6 +18,7 @@ package ch.dissem.bitmessage.wif; import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.entity.BitmessageAddress; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.utils.Base58; import org.ini4j.Ini; import org.ini4j.Profile; @@ -95,7 +96,7 @@ public class WifExporter { try { ini.store(writer); } catch (IOException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } return writer.toString(); }