diff --git a/README.md b/README.md
index 06f0f0a..570e31c 100644
--- a/README.md
+++ b/README.md
@@ -5,34 +5,25 @@ This Java classes can be used to communicate very easily with the Pusher REST AP
Get Started
-----------
-Simply replace the Pusher specific constants in Pusher.java:
+Use the PushApi as seen in ch.mbae.pusher.PushApi.
- private final static String pusherApplicationId = "";
-
- private final static String pusherApplicationKey = "";
-
- private final static String pusherApplicationSecret = "";
-
-Call one of the two available static methods called "triggerPush" and pass channel name, event name and the message body (JSON encoded data) as parameters:
-
- Pusher.triggerPush("test_channel", "my_event", jsonData);
-
-The second "triggerPush" method provides an additional parameter for the socket_id:
+
+ PushApi api = new PushImpl();
- Pusher.triggerPush("test_channel", "my_event", jsonData, socketId);
-
-That's it.
+ api.setGAECredentials(pusherApplicationId, pusherApplicationKey, pusherApplicationSecret);
+
+ then call the following, where all arguments are strings.
+
+ PusherResponse resp = api.pushEvent( channelName, eventName, jsonData, socketId) ;
-Default values
---------------
-Sometimes it can be very convenient to prepulate a PusherRequest instance with default channel and/or event name:
+ The implementation caches your channels, therefore when you have finished, eg a disconnect happens, then
+
+ api.disposeOfChannel(channelName);
- PusherRequest p = new PusherRequest("test_channel","my_event");
-To send a request to the Pusher API just call "triggerPush" on this instance:
+That's it.
- p.triggerPush(jsondata);
License
-------
-Copyright 2010, Stephan Scheuermann. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
\ No newline at end of file
+Copyright 2010, Stephan Scheuermann. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
diff --git a/pom.xml b/pom.xml
index df9077f..5001e17 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,6 +26,7 @@
org.easymock
easymock
3.0
+ test
org.apache.httpcomponents
diff --git a/src/main/java/ch/mbae/pusher/PushApi.java b/src/main/java/ch/mbae/pusher/PushApi.java
new file mode 100644
index 0000000..d59399b
--- /dev/null
+++ b/src/main/java/ch/mbae/pusher/PushApi.java
@@ -0,0 +1,45 @@
+package ch.mbae.pusher;
+
+import java.util.Collection;
+
+/**
+ * Author: wge
+ * Date: 09/02/2013
+ * Time: 18:55
+ */
+
+public interface PushApi
+{
+ /**
+ * The implementation must keep these secret.
+ *
+ * @param pusherApplicationId
+ * @param pusherApplicationKey
+ * @param pusherApplicationSecret
+ */
+ void setGAECredentials(String pusherApplicationId, String pusherApplicationKey, String pusherApplicationSecret);
+
+ /**
+ *
+ * @param channelName the particular channel
+ * @param eventName anything that describes your event.
+ * @param jsonData data to be pushed
+ * @param socketId
+ * @return A response object encapsulating all the headers from PusherApp.com
+ * @throws PusherTransportException
+ */
+ PusherResponse pushEvent( String channelName, String eventName, String jsonData, String socketId) throws PusherTransportException;
+
+ /**
+ * Should be called when we have finished using a channel.
+ * For example, a disconnection has occurred.
+ * @param channelName
+ */
+ void disposeOfChannel(String channelName);
+
+ /**
+ * Show all the channels currently being used
+ * @return
+ */
+ Collection listLiveChannels();
+}
diff --git a/src/main/java/ch/mbae/pusher/impl/CredentialHolder.java b/src/main/java/ch/mbae/pusher/impl/CredentialHolder.java
new file mode 100644
index 0000000..2bea1cd
--- /dev/null
+++ b/src/main/java/ch/mbae/pusher/impl/CredentialHolder.java
@@ -0,0 +1,38 @@
+package ch.mbae.pusher.impl;/**
+ * Author: wge
+ * Date: 15/02/2013
+ * Time: 12:54
+ */
+
+public class CredentialHolder
+{
+ private static final CredentialHolder instance = new CredentialHolder();
+
+ private String APPLICATION_ID;
+ private String APPLICATION_KEY;
+ private String APPLICATION_SECRET;
+
+ public static String getAPPLICATION_ID()
+ {
+ return instance.APPLICATION_ID;
+ }
+
+ public static String getAPPLICATION_KEY()
+ {
+ return instance.APPLICATION_KEY;
+ }
+
+ public static String getAPPLICATION_SECRET()
+ {
+ return instance.APPLICATION_SECRET;
+ }
+
+ public static synchronized void build (String application_id, String application_key, String application_secret)
+ {
+ instance.APPLICATION_ID = application_id;
+ instance.APPLICATION_KEY = application_key;
+ instance.APPLICATION_SECRET = application_secret;
+ }
+
+
+}
diff --git a/src/main/java/ch/mbae/pusher/impl/PushImpl.java b/src/main/java/ch/mbae/pusher/impl/PushImpl.java
new file mode 100644
index 0000000..f92fdef
--- /dev/null
+++ b/src/main/java/ch/mbae/pusher/impl/PushImpl.java
@@ -0,0 +1,68 @@
+package ch.mbae.pusher.impl;/**
+ * Author: wge
+ * Date: 14/02/2013
+ * Time: 22:17
+ */
+
+import ch.mbae.pusher.*;
+import ch.mbae.pusher.transport.HttpClientPusherTransport;
+import org.apache.log4j.Logger;
+
+import java.util.Collection;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class PushImpl implements PushApi
+{
+ private static final Logger log = Logger.getLogger(PushImpl.class);
+ private ConcurrentHashMap channelMap = new ConcurrentHashMap();
+
+ @Override
+ public void setGAECredentials(String pusherApplicationId, String pusherApplicationKey, String pusherApplicationSecret)
+ {
+ CredentialHolder.build(pusherApplicationId,pusherApplicationKey,pusherApplicationSecret);
+ }
+
+ @Override
+ public PusherResponse pushEvent(String channelName, String eventName, String jsonData, String socketId) throws PusherTransportException
+ {
+ PusherChannel channel = findOrCreateHttpChannel(channelName);
+
+ return channel.pushEvent(eventName, jsonData, socketId);
+
+ }
+
+ @Override
+ public void disposeOfChannel(String channelName)
+ {
+ PusherChannel channel = channelMap.get(channelName);
+
+ if(channel != null)
+ {
+ channelMap.remove(channelName);
+ log.info("channel " + channelName + " successfully removed ");
+ }
+ else
+ {
+ log.warn("channel " + channelName + " not found ");
+ }
+ }
+
+ @Override
+ public Collection listLiveChannels()
+ {
+ return channelMap.keySet();
+ }
+
+ private PusherChannel findOrCreateHttpChannel(String channelName)
+ {
+ PusherChannel channel = channelMap.get(channelName);
+ if(channel == null)
+ {
+ channel = new PusherChannel(channelName,new HttpClientPusherTransport());
+ channelMap.putIfAbsent(channelName,channel);
+ }
+ return channel;
+ }
+
+
+}
diff --git a/src/main/java/ch/mbae/pusher/PusherChannel.java b/src/main/java/ch/mbae/pusher/impl/PusherChannel.java
similarity index 65%
rename from src/main/java/ch/mbae/pusher/PusherChannel.java
rename to src/main/java/ch/mbae/pusher/impl/PusherChannel.java
index 93d9fcf..68eef8c 100644
--- a/src/main/java/ch/mbae/pusher/PusherChannel.java
+++ b/src/main/java/ch/mbae/pusher/impl/PusherChannel.java
@@ -2,9 +2,13 @@
* Author: marcbaechinger
* Copyright 2011. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/
-package ch.mbae.pusher;
+package ch.mbae.pusher.impl;
+import ch.mbae.pusher.PusherResponse;
+import ch.mbae.pusher.PusherTransport;
+import ch.mbae.pusher.PusherTransportException;
import ch.mbae.pusher.util.PusherUtil;
+
import java.net.URL;
/**
@@ -15,34 +19,25 @@ public class PusherChannel {
private PusherTransport transport;
private String channelName;
- private String pusherApplicationId;
- private String pusherApplicationSecret;
- private String pusherApplicationKey;
-
- public PusherChannel(String channelName, String pusherApplicationId, String pusherApplicationKey,
- String pusherApplicationSecret, PusherTransport transport) {
-
+
+ public PusherChannel(String channelName, PusherTransport transport) {
this.channelName = channelName;
- this.pusherApplicationKey = pusherApplicationKey;
- this.pusherApplicationId = pusherApplicationId;
- this.pusherApplicationSecret = pusherApplicationSecret;
this.transport = transport;
}
/**
* Delivers a message to the Pusher API without providing a socket_id
- * @param channel
* @param event
* @param jsonData
* @return
*/
- public PusherResponse pushEvent(String event, String jsonData) throws PusherTransportException{
+ public PusherResponse pushEvent(String event, String jsonData) throws PusherTransportException
+ {
return pushEvent(event, jsonData, "");
}
/**
* Delivers a message to the Pusher API
- * @param channel
* @param event
* @param jsonData
* @param socketId
@@ -50,11 +45,11 @@ public PusherResponse pushEvent(String event, String jsonData) throws PusherTran
*/
public PusherResponse pushEvent(String event, String jsonData, String socketId) throws PusherTransportException{
//Build URI path
- String uriPath = PusherUtil.buildURIPath(this.channelName, this.pusherApplicationId);
+ String uriPath = PusherUtil.buildURIPath(this.channelName, CredentialHolder.getAPPLICATION_ID());
//Build query
- String query = PusherUtil.buildQuery(event, jsonData, socketId, this.pusherApplicationKey);
+ String query = PusherUtil.buildQuery(event, jsonData, socketId, CredentialHolder.getAPPLICATION_KEY());
//Generate signature
- String signature = PusherUtil.buildAuthenticationSignature(uriPath, query, this.pusherApplicationSecret);
+ String signature = PusherUtil.buildAuthenticationSignature(uriPath, query, CredentialHolder.getAPPLICATION_SECRET());
//Build URI
URL url = PusherUtil.buildURI(uriPath, query, signature);
diff --git a/Pusher.java b/src/main/java/ch/mbae/pusher/impl/PusherHelper.java
similarity index 88%
rename from Pusher.java
rename to src/main/java/ch/mbae/pusher/impl/PusherHelper.java
index 59246aa..3b68b97 100644
--- a/Pusher.java
+++ b/src/main/java/ch/mbae/pusher/impl/PusherHelper.java
@@ -1,5 +1,10 @@
+package ch.mbae.pusher.impl;
+import com.google.appengine.api.urlfetch.*;
+import org.apache.log4j.Logger;
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
@@ -9,18 +14,6 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
-import javax.crypto.Mac;
-import javax.crypto.spec.SecretKeySpec;
-
-import org.mortbay.log.Log;
-
-import com.google.appengine.api.urlfetch.HTTPHeader;
-import com.google.appengine.api.urlfetch.HTTPMethod;
-import com.google.appengine.api.urlfetch.HTTPRequest;
-import com.google.appengine.api.urlfetch.HTTPResponse;
-import com.google.appengine.api.urlfetch.URLFetchService;
-import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
-
/**
* Static class to send messages to Pusher's REST API.
*
@@ -30,28 +23,15 @@
* @author Stephan Scheuermann
* Copyright 2010. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/
-public class Pusher {
+class PusherHelper
+{
+ private static final Logger log = Logger.getLogger(PusherHelper.class);
/**
* Pusher Host name
*/
private final static String pusherHost = "api.pusherapp.com";
- /**
- * Pusher Application Identifier
- */
- private final static String pusherApplicationId = "";
-
- /**
- * Pusher Application Key
- */
- private final static String pusherApplicationKey = "";
-
- /**
- * Pusher Secret
- */
- private final static String pusherApplicationSecret = "";
-
/**
* Converts a byte array to a string representation
* @param data
@@ -92,10 +72,10 @@ private static String md5Representation(String data) {
* @param data
* @return
*/
- private static String hmacsha256Representation(String data) {
+ public static String hmacsha256Representation(String data) {
try {
// Create the HMAC/SHA256 key from application secret
- final SecretKeySpec signingKey = new SecretKeySpec( pusherApplicationSecret.getBytes(), "HmacSHA256");
+ final SecretKeySpec signingKey = new SecretKeySpec( CredentialHolder.getAPPLICATION_SECRET().getBytes(), "HmacSHA256");
// Create the message authentication code (MAC)
final Mac mac = Mac.getInstance("HmacSHA256");
@@ -128,7 +108,7 @@ private static String buildQuery(String eventName, String jsonData, String socke
StringBuffer buffer = new StringBuffer();
//Auth_Key
buffer.append("auth_key=");
- buffer.append(pusherApplicationKey);
+ buffer.append(CredentialHolder.getAPPLICATION_KEY());
//Timestamp
buffer.append("&auth_timestamp=");
buffer.append(System.currentTimeMillis() / 1000);
@@ -157,7 +137,7 @@ private static String buildURIPath(String channelName){
StringBuffer buffer = new StringBuffer();
//Application ID
buffer.append("/apps/");
- buffer.append(pusherApplicationId);
+ buffer.append(CredentialHolder.getAPPLICATION_ID());
//Channel name
buffer.append("/channels/");
buffer.append(channelName);
@@ -256,7 +236,7 @@ public static HTTPResponse triggerPush(String channel, String event, String json
return urlFetchService.fetch(request);
} catch (IOException e) {
//Log warning
- Log.warn("Pusher request could not be send to the following URI " + url.toString());
+ log.warn("Pusher request could not be send to the following URI " + url.toString());
return null;
}
}
diff --git a/PusherRequest.java b/src/main/java/ch/mbae/pusher/impl/PusherRequest.java
similarity index 88%
rename from PusherRequest.java
rename to src/main/java/ch/mbae/pusher/impl/PusherRequest.java
index 99c06b2..c79908b 100644
--- a/PusherRequest.java
+++ b/src/main/java/ch/mbae/pusher/impl/PusherRequest.java
@@ -1,4 +1,4 @@
-
+package ch.mbae.pusher.impl;
import com.google.appengine.api.urlfetch.HTTPResponse;
@@ -46,7 +46,7 @@ public PusherRequest(String channelName, String eventName) {
* @return
*/
public HTTPResponse triggerPush(String jsonData){
- return Pusher.triggerPush(channelName, eventName, jsonData);
+ return PusherHelper.triggerPush(channelName, eventName, jsonData);
}
/**
@@ -56,7 +56,7 @@ public HTTPResponse triggerPush(String jsonData){
* @return
*/
public HTTPResponse triggerPush(String jsonData, String eventName){
- return Pusher.triggerPush(channelName, eventName, jsonData);
+ return PusherHelper.triggerPush(channelName, eventName, jsonData);
}
/**
@@ -67,7 +67,7 @@ public HTTPResponse triggerPush(String jsonData, String eventName){
* @return
*/
public HTTPResponse triggerPush(String jsonData, String eventName, String socketId){
- return Pusher.triggerPush(channelName, eventName, jsonData, socketId);
+ return PusherHelper.triggerPush(channelName, eventName, jsonData, socketId);
}
public void setEventName(String eventName) {
diff --git a/src/main/java/ch/mbae/pusher/transport/HttpClientPusherTransport.java b/src/main/java/ch/mbae/pusher/transport/HttpClientPusherTransport.java
index fb06579..32f7f29 100644
--- a/src/main/java/ch/mbae/pusher/transport/HttpClientPusherTransport.java
+++ b/src/main/java/ch/mbae/pusher/transport/HttpClientPusherTransport.java
@@ -4,48 +4,41 @@
*/
package ch.mbae.pusher.transport;
-import ch.mbae.pusher.PusherTransport;
import ch.mbae.pusher.PusherResponse;
+import ch.mbae.pusher.PusherTransport;
import ch.mbae.pusher.PusherTransportException;
-import java.io.IOException;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* Implementation of PusherTransport using Jakarta HttpClient
* @author marcbaechinger
*/
public class HttpClientPusherTransport implements PusherTransport {
- DefaultHttpClient httpClient;
+ DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
/**
- * create a http transportn using a single http client
+ * create a http transport using a single http client
* for all requests
*/
public HttpClientPusherTransport() {
- ClientConnectionManager cm = new ThreadSafeClientConnManager();
-
- this.httpClient = new DefaultHttpClient(cm);
- this.httpClient.getParams().setParameter(
- CoreProtocolPNames.PROTOCOL_VERSION,
- HttpVersion.HTTP_1_1); // set default to HTTP 1.1
- this.httpClient.getParams().setParameter(
- CoreProtocolPNames.HTTP_CONTENT_CHARSET,
- "UTF-8");
+ this.httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); // set default to HTTP 1.1
+ this.httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");
}
@Override
diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties
index d947a7e..3073f55 100644
--- a/src/main/resources/log4j.properties
+++ b/src/main/resources/log4j.properties
@@ -6,4 +6,4 @@ log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
-log4j.logger.com.foo=WARN
\ No newline at end of file
+log4j.logger.ch.mbae.pusher=WARN
\ No newline at end of file
diff --git a/src/test/java/ch/mbae/pusher/PusherApiTest.java b/src/test/java/ch/mbae/pusher/PusherApiTest.java
new file mode 100644
index 0000000..ddc8b66
--- /dev/null
+++ b/src/test/java/ch/mbae/pusher/PusherApiTest.java
@@ -0,0 +1,65 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package ch.mbae.pusher;
+
+import ch.mbae.pusher.impl.PushImpl;
+import ch.mbae.pusher.transport.HttpClientPusherTransportTest;
+import org.apache.log4j.Logger;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Collection;
+
+/**
+ *
+ * @author William Edwards
+ */
+public class PusherApiTest
+{
+
+ private static final Logger LOGGER = Logger.getLogger(PusherApiTest.class);
+
+ private static final String JSON_STRING = "{ originator: " + PusherApiTest.class.getName() + "}";
+
+ private PushApi api;
+
+ @Before
+ public void setUp() {
+ api = new PushImpl();
+ }
+
+ @Test
+ public void pushTest()
+ {
+ api.setGAECredentials(TestCredentials.APPLICATION_ID,TestCredentials.APPLICATION_KEY,TestCredentials.APPLICATION_SECRET);
+
+ try
+ {
+ PusherResponse response = api.pushEvent("company-001","create",JSON_STRING,"987");
+
+ assert(response != null);
+
+ HttpClientPusherTransportTest.verifyContent(response);
+
+
+ Collection existingChannels = api.listLiveChannels();
+
+ assert(existingChannels.size() == 1);
+
+ existingChannels.iterator().next().equals("company-001");
+
+ api.disposeOfChannel("company-001");
+
+ assert(api.listLiveChannels().size() == 0);
+
+ }
+ catch (PusherTransportException e)
+ {
+ LOGGER.error(e.getMessage());
+ }
+ }
+
+
+}
diff --git a/src/test/java/ch/mbae/pusher/PusherChannelTest.java b/src/test/java/ch/mbae/pusher/PusherChannelTest.java
index 016ce92..db6daa0 100644
--- a/src/test/java/ch/mbae/pusher/PusherChannelTest.java
+++ b/src/test/java/ch/mbae/pusher/PusherChannelTest.java
@@ -4,20 +4,23 @@
*/
package ch.mbae.pusher;
-import ch.mbae.pusher.transport.HttpClientPusherTransport;
-import java.net.URL;
+import ch.mbae.pusher.impl.CredentialHolder;
+import ch.mbae.pusher.impl.PusherChannel;
import junit.framework.Assert;
import org.apache.log4j.Logger;
import org.easymock.Capture;
-import static org.easymock.EasyMock.*;
import org.junit.Before;
import org.junit.Test;
+import java.net.URL;
+
+import static org.easymock.EasyMock.*;
+
/**
*
* @author marcbaechinger
*/
-public class PusherChannelTest implements PusherCredentials {
+public class PusherChannelTest {
private static final Logger LOGGER = Logger.getLogger(PusherChannelTest.class);
@@ -25,18 +28,24 @@ public class PusherChannelTest implements PusherCredentials {
private PusherChannel testee;
private PusherTransport transportMock;
-
+
+ String CHANNEL = "junit-test-channel";
+ String EVENT = "junit-test-event";
+
@Before
public void setUp() {
+
+ CredentialHolder.build(TestCredentials.APPLICATION_ID, TestCredentials.APPLICATION_KEY, TestCredentials.APPLICATION_SECRET);
+
// create a mock for the transport
this.transportMock = createMock(PusherTransport.class);
// create the channel
- this.testee = new PusherChannel(CHANNEL, APPLICATION_ID, APPLICATION_KEY, APPLICATION_SECRET, transportMock);
+ this.testee = new PusherChannel(CHANNEL, transportMock);
}
/**
- * Test o f pushEvent method, of class PusherChannel.
+ * Test of pushEvent method, of class PusherChannel.
*/
@Test
public void testPushEvent_String_String() throws PusherTransportException {
diff --git a/src/test/java/ch/mbae/pusher/PusherCredentials.java b/src/test/java/ch/mbae/pusher/PusherCredentials.java
deleted file mode 100644
index 4d3561a..0000000
--- a/src/test/java/ch/mbae/pusher/PusherCredentials.java
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-package ch.mbae.pusher;
-
-/**
- *
- * @author marcbaechinger
- */
-public interface PusherCredentials {
-
- static final String APPLICATION_ID = "";
- static final String APPLICATION_KEY = "";
- static final String APPLICATION_SECRET = "";
-
- static final String CHANNEL = "junit-test-channel";
- static final String EVENT = "junit-test-event";
-}
diff --git a/src/test/java/ch/mbae/pusher/TestCredentials.java b/src/test/java/ch/mbae/pusher/TestCredentials.java
new file mode 100644
index 0000000..a684da6
--- /dev/null
+++ b/src/test/java/ch/mbae/pusher/TestCredentials.java
@@ -0,0 +1,12 @@
+package ch.mbae.pusher;/**
+ * Author: wge
+ * Date: 15/02/2013
+ * Time: 14:37
+ * Ensure these are wired to the Test Application, not production.
+ */
+public interface TestCredentials
+{
+ String APPLICATION_ID = "37099";
+ String APPLICATION_KEY = "e543b0af7cea4adfac24";
+ String APPLICATION_SECRET = "cca824ca4792aa6a94b8";
+}
diff --git a/src/test/java/ch/mbae/pusher/transport/GAEPusherTransportTest.java b/src/test/java/ch/mbae/pusher/transport/GAEPusherTransportTest.java
index 697fcae..30a68ea 100644
--- a/src/test/java/ch/mbae/pusher/transport/GAEPusherTransportTest.java
+++ b/src/test/java/ch/mbae/pusher/transport/GAEPusherTransportTest.java
@@ -4,9 +4,10 @@
*/
package ch.mbae.pusher.transport;
-import ch.mbae.pusher.PusherChannel;
-import ch.mbae.pusher.PusherCredentials;
-import org.junit.After;
+import ch.mbae.pusher.impl.PusherChannel;
+import ch.mbae.pusher.PusherResponse;
+import ch.mbae.pusher.PusherTransportException;
+import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@@ -15,29 +16,31 @@
*
* @author marcbaechinger
*/
-public class GAEPusherTransportTest implements PusherCredentials {
+public class GAEPusherTransportTest {
private PusherChannel channel;
-
-
-
-
+ private static final Logger log = Logger.getLogger(GAEPusherTransportTest.class);
+
@Before
public void setUp() {
- this.channel = new PusherChannel("gae", PusherCredentials.APPLICATION_ID,
- PusherCredentials.APPLICATION_KEY, PusherCredentials.APPLICATION_SECRET,
- new GAEPusherTransport());
+ this.channel = new PusherChannel("gae",new GAEPusherTransport());
}
- @After
- public void tearDown() {
- }
/**
* Test of fetch method, of class GAEPusherTransport.
*/
@Test
@Ignore
- public void testFetch() throws Exception {
- channel.pushEvent("gae-event", "{'transport': 'GAE'}");
+ public void testFetch()
+ {
+ try
+ {
+ PusherResponse response = channel.pushEvent("gae-event", "{'transport': 'GAE'}");
+ int x = 2;
+ }
+ catch (PusherTransportException e)
+ {
+ log.error(e.getMessage());
+ }
}
}
diff --git a/src/test/java/ch/mbae/pusher/transport/HttpClientPusherTransportTest.java b/src/test/java/ch/mbae/pusher/transport/HttpClientPusherTransportTest.java
index 2da29e3..3bc7432 100644
--- a/src/test/java/ch/mbae/pusher/transport/HttpClientPusherTransportTest.java
+++ b/src/test/java/ch/mbae/pusher/transport/HttpClientPusherTransportTest.java
@@ -4,11 +4,13 @@
*/
package ch.mbae.pusher.transport;
-import ch.mbae.pusher.PusherChannel;
-import ch.mbae.pusher.PusherCredentials;
import ch.mbae.pusher.PusherResponse;
+import ch.mbae.pusher.TestCredentials;
+import ch.mbae.pusher.impl.CredentialHolder;
+import ch.mbae.pusher.impl.PusherChannel;
import junit.framework.Assert;
import org.apache.log4j.Logger;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -17,7 +19,7 @@
*
* @author marcbaechinger
*/
-public class HttpClientPusherTransportTest implements PusherCredentials {
+public class HttpClientPusherTransportTest {
private static final Logger LOGGER = Logger.getLogger(HttpClientPusherTransportTest.class);
@@ -26,15 +28,24 @@ public class HttpClientPusherTransportTest implements PusherCredentials {
private HttpClientPusherTransport tranportUnderTest;
private PusherChannel channel;
-
+ String CHANNEL = "junit-test-channel";
+ String EVENT = "junit-test-event";
+
@Before
public void setUp() {
+
+ CredentialHolder.build(TestCredentials.APPLICATION_ID, TestCredentials.APPLICATION_KEY, TestCredentials.APPLICATION_SECRET);
// create the transport under test
this.tranportUnderTest = new HttpClientPusherTransport();
// the channel to inject the transport into
- this.channel = new PusherChannel(CHANNEL, APPLICATION_ID, APPLICATION_KEY, APPLICATION_SECRET, this.tranportUnderTest);
+ this.channel = new PusherChannel(CHANNEL,this.tranportUnderTest);
}
+ @After
+ public void tearDown() {
+ this.tranportUnderTest = null;
+ this.channel = null;
+ }
/**
* Test of fetch method, of class HttpClientPusherTransport.
*/
@@ -42,36 +53,49 @@ public void setUp() {
public void testFetch() throws Exception {
// push the event
PusherResponse response = this.channel.pushEvent(EVENT, JSON_STRING);
-
- LOGGER.debug(response.getResponseCode());
- LOGGER.debug(new String(response.getContent()));
- LOGGER.debug(response.getHeaders().get("Content-Length"));
-
- // http status 202
- Assert.assertEquals(202, response.getResponseCode());
- // content as expected ? (hard test; might fail once)
- //Assert.assertEquals("202 ACCEPTED\n", new String(response.getContent()));
- // correct content length in http header?
- Assert.assertEquals(Integer.valueOf(response.getHeaders().get("Content-Length")).intValue(), response.getContent().length);
+ retryAgainIfNecessary(response);
}
/**
* Test of fetch method, of class HttpClientPusherTransport.
*/
@Test
- public void testFetchWidthSessionId() throws Exception {
+ public void testFetchWidthSocketId() throws Exception {
// push the event
PusherResponse response = this.channel.pushEvent(EVENT, JSON_STRING, SOCKET_ID);
- LOGGER.debug(response.getResponseCode());
- LOGGER.debug(new String(response.getContent()));
- LOGGER.debug(response.getHeaders().get("Content-Length"));
-
- // http status 202
- Assert.assertEquals(202, response.getResponseCode());
- // content as expected ? (hard test; might fail once)
- //Assert.assertEquals("202 ACCEPTED\n", new String(response.getContent()));
- // correct content length in http header?
- Assert.assertEquals(Integer.valueOf(response.getHeaders().get("Content-Length")).intValue(), response.getContent().length);
+ retryAgainIfNecessary(response);
}
+
+ private void retryAgainIfNecessary(PusherResponse response) throws Exception
+ {
+ if(!verifyContent(response))
+ {
+ response = this.channel.pushEvent(EVENT, JSON_STRING, SOCKET_ID);
+ verifyContent(response);
+ }
+
+ }
+
+ public static boolean verifyContent(PusherResponse response)
+ {
+ LOGGER.debug(response.getResponseCode());
+ LOGGER.debug(new String(response.getContent()));
+ LOGGER.debug(response.getHeaders().get("Content-Length"));
+
+ Assert.assertEquals(202, response.getResponseCode());
+ Assert.assertEquals(response.getHeaders().size(), 3);
+
+ // content as expected ? (hard test; might fail once)
+ //Assert.assertEquals("202 ACCEPTED\n", new String(response.getContent()));
+ // correct content length in http header?
+ String contentLen = response.getHeaders().get("Content-Length");
+ if(contentLen != null)
+ {
+ Assert.assertEquals(Integer.valueOf(response.getHeaders().get("Content-Length")).intValue(), response.getContent().length);
+ return true;
+ }
+
+ return false;
+ }
}