Skip to content

Commit

Permalink
Use consistent variable names & default values
Browse files Browse the repository at this point in the history
  • Loading branch information
aldaris committed Oct 20, 2014
1 parent 02ebce9 commit 95ace56
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
7 changes: 3 additions & 4 deletions src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ public BuildStepMonitor getRequiredMonitorService() {
}

public HipChatService newHipChatService() {
return new StandardHipChatService(getServer(), getAuthToken(), getRoom(),
StringUtils.isBlank(getSendAs()) ? "Build Server" : getSendAs());
return new StandardHipChatService(getServer(), getAuthToken(), getRoom(), getSendAs());
}

@Override
Expand All @@ -166,10 +165,10 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {

private String server;
private String server = "api.hipchat.com";
private String token;
private String room;
private String sendAs;
private String sendAs = "Jenkins";
private static int testNotificationCount = 0;

public DescriptorImpl() {
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/jenkins/plugins/hipchat/StandardHipChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@
public class StandardHipChatService implements HipChatService {

private static final Logger logger = Logger.getLogger(StandardHipChatService.class.getName());
private static final String DEFAULT_HOST = "api.hipchat.com";
private static final String[] DEFAULT_ROOMS = new String[0];
private static final String DEFAULT_FROM = "Build Server";

private final String host;
private final String server;
private final String token;
private final String[] roomIds;
private final String from;
private final String sendAs;

public StandardHipChatService(String host, String token, String roomIds, String from) {
public StandardHipChatService(String server, String token, String roomIds, String sendAs) {
super();
this.host = host == null ? DEFAULT_HOST : host;
this.server = server;
this.token = token;
this.roomIds = roomIds == null ? DEFAULT_ROOMS : roomIds.split("\\s*,\\s*");
this.from = from == null ? DEFAULT_FROM : from;
this.sendAs = sendAs;
}

public void publish(String message) {
Expand All @@ -35,13 +33,13 @@ public void publish(String message) {

public void publish(String message, String color) {
for (String roomId : roomIds) {
logger.log(Level.INFO, "Posting: {0} to {1}: {2} {3}", new Object[]{from, roomId, message, color});
logger.log(Level.INFO, "Posting: {0} to {1}: {2} {3}", new Object[]{sendAs, roomId, message, color});
HttpClient client = getHttpClient();
String url = "https://" + host + "/v1/rooms/message?auth_token=" + token;
String url = "https://" + server + "/v1/rooms/message?auth_token=" + token;
PostMethod post = new PostMethod(url);

try {
post.addParameter("from", from);
post.addParameter("from", sendAs);
post.addParameter("room_id", roomId);
post.addParameter("message", message);
post.addParameter("color", color);
Expand Down Expand Up @@ -78,15 +76,15 @@ private String shouldNotify(String color) {
return color.equalsIgnoreCase("green") ? "0" : "1";
}

public String getHost() {
return host;
public String getServer() {
return server;
}

public String[] getRoomIds() {
return roomIds;
}

public String getFrom() {
return from;
public String getSendAs() {
return sendAs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<f:textbox name="hipchat.room" value="${descriptor.room}" />
</f:entry>
<f:entry title="${%Send As}" help="${rootURL}/plugin/hipchat/help-globalConfig-hipChatSendAs.html">
<f:textbox name="hipchat.sendAs" default="Build User" value="${descriptor.sendAs}" />
<f:textbox name="hipchat.sendAs" default="Jenkins" value="${descriptor.sendAs}" />
</f:entry>
<f:validateButton method="sendTestNotification" title="${%Test configuration}" progress="${%Testing...}"
with="hipchat.server,hipchat.token,hipchat.room,hipchat.sendAs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ public void publishWithBadHostShouldNotRethrowExceptions() {
service.publish("message");
}

@Test
public void shouldSetADefaultHost() {
StandardHipChatService service = new StandardHipChatService(null, "token", "room", "from");
assertEquals("api.hipchat.com", service.getHost());
}

@Test
public void shouldBeAbleToOverrideHost() {
StandardHipChatService service = new StandardHipChatService("some.other.host", "token", "room", "from");
assertEquals("some.other.host", service.getHost());
assertEquals("some.other.host", service.getServer());
}

@Test
Expand All @@ -42,15 +36,9 @@ public void shouldNotSplitTheRoomsIfNullIsPassed() {
assertArrayEquals(new String[0], service.getRoomIds());
}

@Test
public void shouldProvideADefaultFrom() {
StandardHipChatService service = new StandardHipChatService(null, "token", "room", null);
assertEquals("Build Server", service.getFrom());
}

@Test
public void shouldBeAbleToOverrideFrom() {
StandardHipChatService service = new StandardHipChatService(null, "token", "room", "from");
assertEquals("from", service.getFrom());
assertEquals("from", service.getSendAs());
}
}

0 comments on commit 95ace56

Please sign in to comment.