Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #13 from darrinholst/exception-handling

log errors instead of rethrowing them
  • Loading branch information...
commit 4d938b611f9e951de09db06da6fe0889c53b4b5e 2 parents 07de2d2 + ed89760
@jlewallen authored
View
6 pom.xml
@@ -49,6 +49,12 @@
<artifactId>htmlunit</artifactId>
<version>2.2-hudson-10</version>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
View
13 src/main/java/jenkins/plugins/hipchat/HipChatService.java
@@ -1,15 +1,6 @@
package jenkins.plugins.hipchat;
public interface HipChatService {
-
- public void publish(String message);
-
- public void publish(String message, String color);
-
- public void rooms();
-
- public enum MessageColor {
- YELLOW, RED, GREEN, PURPLE, RANDOM
- }
-
+ void publish(String message);
+ void publish(String message, String color);
}
View
39 src/main/java/jenkins/plugins/hipchat/StandardHipChatService.java
@@ -1,17 +1,16 @@
package jenkins.plugins.hipchat;
-import java.io.IOException;
-import java.util.logging.Logger;
-
import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
public class StandardHipChatService implements HipChatService {
private static final Logger logger = Logger.getLogger(StandardHipChatService.class.getName());
+ private String host = "api.hipchat.com";
private String token;
private String[] roomIds;
private String from;
@@ -31,8 +30,9 @@ public void publish(String message, String color) {
for(String roomId : roomIds) {
logger.info("Posting: " + from + " to " + roomId + ": " + message + " " + color);
HttpClient client = new HttpClient();
- String url = "https://api.hipchat.com/v1/rooms/message?auth_token=" + token;
+ String url = "https://" + host + "/v1/rooms/message?auth_token=" + token;
PostMethod post = new PostMethod(url);
+
try {
post.addParameter("from", from);
post.addParameter("room_id", roomId);
@@ -41,11 +41,8 @@ public void publish(String message, String color) {
post.addParameter("notify", shouldNotify(color));
client.executeMethod(post);
}
- catch(HttpException e) {
- throw new RuntimeException("Error posting to HipChat", e);
- }
- catch(IOException e) {
- throw new RuntimeException("Error posting to HipChat", e);
+ catch(Exception e) {
+ logger.log(Level.WARNING, "Error posting to HipChat", e);
}
finally {
post.releaseConnection();
@@ -57,23 +54,7 @@ private String shouldNotify(String color) {
return color.equalsIgnoreCase("green") ? "0" : "1";
}
- public void rooms() {
- HttpClient client = new HttpClient();
- String url = "https://api.hipchat.com/v1/rooms/list?format=json&auth_token=" + token;
- GetMethod get = new GetMethod(url);
- try {
- client.executeMethod(get);
- logger.info(get.getResponseBodyAsString());
- }
- catch(HttpException e) {
- throw new RuntimeException("Error posting to HipChat", e);
- }
- catch(IOException e) {
- throw new RuntimeException("Error posting to HipChat", e);
- }
- finally {
- get.releaseConnection();
- }
+ void setHost(String host) {
+ this.host = host;
}
-
}
View
19 src/test/java/jenkins/plugins/hipchat/StandardHipChatServiceTest.java
@@ -0,0 +1,19 @@
+package jenkins.plugins.hipchat;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class StandardHipChatServiceTest {
+ private StandardHipChatService service;
+
+ @Before
+ public void setUp() throws Exception {
+ service = new StandardHipChatService("token", "room", "from");
+ service.setHost("localhost");
+ }
+
+ @Test
+ public void publishShouldNotRethrowExceptions() {
+ service.publish("message");
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.