Skip to content

Commit

Permalink
Fix test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jul 5, 2013
1 parent 0c92b85 commit 96cb7c0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ public String getSessionId() {

@Override
public void addSubscription(String destination, String subscriptionId) {
CachingSessionSubscriptionRegistry.this.destinationCache.mapRegistration(destination, this.delegate);
destinationCache.mapRegistration(destination, this);
this.delegate.addSubscription(destination, subscriptionId);
}

@Override
public String removeSubscription(String subscriptionId) {
String destination = this.delegate.removeSubscription(subscriptionId);
if (destination != null && this.delegate.getSubscriptionsByDestination(destination) == null) {
CachingSessionSubscriptionRegistry.this.destinationCache.unmapRegistration(destination, this);
destinationCache.unmapRegistration(destination, this);
}
return destination;
}
Expand All @@ -163,6 +163,23 @@ public Set<String> getDestinations() {
return this.delegate.getDestinations();
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CachingSessionSubscriptionRegistration)) {
return false;
}
CachingSessionSubscriptionRegistration otherType = (CachingSessionSubscriptionRegistration) other;
return this.delegate.equals(otherType.delegate);
}

@Override
public int hashCode() {
return this.delegate.hashCode();
}

@Override
public String toString() {
return "CachingSessionSubscriptionRegistration [delegate=" + delegate + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ public Set<String> getSubscriptionsByDestination(String destination) {
return this.subscriptions.get(destination);
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof DefaultSessionSubscriptionRegistration)) {
return false;
}
DefaultSessionSubscriptionRegistration otherType = (DefaultSessionSubscriptionRegistration) other;
return this.sessionId.equals(otherType.sessionId);
}

@Override
public int hashCode() {
return 31 + this.sessionId.hashCode();
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public void subcribePublish() {
this.messageHandler.handlePublish(createMessage("/bar", "message2"));

verify(this.clientChannel, times(6)).send(this.messageCaptor.capture());
assertCapturedMessage(this.messageCaptor.getAllValues().get(0), "sess1", "sub1", "/foo");
assertCapturedMessage(this.messageCaptor.getAllValues().get(1), "sess1", "sub2", "/foo");
assertCapturedMessage(this.messageCaptor.getAllValues().get(2), "sess2", "sub1", "/foo");
assertCapturedMessage(this.messageCaptor.getAllValues().get(3), "sess2", "sub2", "/foo");
assertCapturedMessage(this.messageCaptor.getAllValues().get(4), "sess1", "sub3", "/bar");
assertCapturedMessage(this.messageCaptor.getAllValues().get(5), "sess2", "sub3", "/bar");
assertCapturedMessage("sess1", "sub1", "/foo");
assertCapturedMessage("sess1", "sub2", "/foo");
assertCapturedMessage("sess2", "sub1", "/foo");
assertCapturedMessage("sess2", "sub2", "/foo");
assertCapturedMessage("sess1", "sub3", "/bar");
assertCapturedMessage("sess2", "sub3", "/bar");
}

@Test
Expand All @@ -105,10 +105,13 @@ public void subcribeDisconnectPublish() {
this.messageHandler.handlePublish(createMessage("/foo", "message1"));
this.messageHandler.handlePublish(createMessage("/bar", "message2"));

verify(this.clientChannel, times(3)).send(this.messageCaptor.capture());
assertCapturedMessage(this.messageCaptor.getAllValues().get(0), "sess2", "sub1", "/foo");
assertCapturedMessage(this.messageCaptor.getAllValues().get(1), "sess2", "sub2", "/foo");
assertCapturedMessage(this.messageCaptor.getAllValues().get(2), "sess2", "sub3", "/bar");
verify(this.clientChannel, times(6)).send(this.messageCaptor.capture());
assertCapturedMessage("sess1", "sub1", "/foo");
assertCapturedMessage("sess1", "sub2", "/foo");
assertCapturedMessage("sess2", "sub1", "/foo");
assertCapturedMessage("sess2", "sub2", "/foo");
assertCapturedMessage("sess1", "sub3", "/bar");
assertCapturedMessage("sess2", "sub3", "/bar");
}


Expand All @@ -130,13 +133,18 @@ protected Message<String> createMessage(String destination, String payload) {
return MessageBuilder.withPayload(payload).copyHeaders(headers.toMap()).build();
}

protected void assertCapturedMessage(Message<?> message, String sessionId,
String subcriptionId, String destination) {

WebMessageHeaderAccesssor headers = WebMessageHeaderAccesssor.wrap(message);
assertEquals(sessionId, headers.getSessionId());
assertEquals(subcriptionId, headers.getSubscriptionId());
assertEquals(destination, headers.getDestination());
protected boolean assertCapturedMessage(String sessionId, String subcriptionId, String destination) {
for (Message<?> message : this.messageCaptor.getAllValues()) {
WebMessageHeaderAccesssor headers = WebMessageHeaderAccesssor.wrap(message);
if (sessionId.equals(headers.getSessionId())) {
if (subcriptionId.equals(headers.getSubscriptionId())) {
if (destination.equals(headers.getDestination())) {
return true;
}
}
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package org.springframework.web.messaging.stomp.support;

import java.util.Collections;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.web.messaging.MessageType;
import org.springframework.web.messaging.stomp.StompCommand;
import org.springframework.web.messaging.support.WebMessageHeaderAccesssor;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -53,7 +55,14 @@ public void connectFrame() throws Exception {

MessageHeaders headers = message.getHeaders();
StompHeaderAccessor stompHeaders = StompHeaderAccessor.wrap(message);
assertEquals(7, stompHeaders.toMap().size());
Map<String, Object> map = stompHeaders.toMap();
assertEquals(6, map.size());
assertNotNull(map.get(MessageHeaders.ID));
assertNotNull(map.get(MessageHeaders.TIMESTAMP));
assertNotNull(map.get(WebMessageHeaderAccesssor.SESSION_ID));
assertNotNull(map.get(WebMessageHeaderAccesssor.NATIVE_HEADERS));
assertNotNull(map.get(WebMessageHeaderAccesssor.MESSAGE_TYPE));
assertNotNull(map.get(WebMessageHeaderAccesssor.PROTOCOL_MESSAGE_TYPE));

assertEquals(Collections.singleton("1.1"), stompHeaders.getAcceptVersion());
assertEquals("github.org", stompHeaders.getHost());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,22 @@ public void getRegistrationsByDestination() {

SessionSubscriptionRegistration reg1 = this.registry.getOrCreateRegistration("sess1");
reg1.addSubscription("/foo", "sub1");
reg1.addSubscription("/foo", "sub1");

SessionSubscriptionRegistration reg2 = this.registry.getOrCreateRegistration("sess2");
reg2.addSubscription("/foo", "sub1");
reg2.addSubscription("/foo", "sub1");

Set<SessionSubscriptionRegistration> actual = this.registry.getRegistrationsByDestination("/foo");
assertEquals(2, actual.size());
assertTrue(actual.contains(reg1));
assertTrue(actual.contains(reg2));

reg1.removeSubscription("sub1");
reg1.removeSubscription("sub2");

actual = this.registry.getRegistrationsByDestination("/foo");
assertEquals("Invalid set of registrations " + actual, 1, actual.size());
assertTrue(actual.contains(reg2));

reg2.removeSubscription("sub1");
reg2.removeSubscription("sub2");

actual = this.registry.getRegistrationsByDestination("/foo");
assertNull("Unexpected registrations " + actual, actual);
Expand Down

0 comments on commit 96cb7c0

Please sign in to comment.