@Test public void getAttributeNames() { this.simpAttributes.setAttribute("name1", "value1"); this.simpAttributes.setAttribute("name2", "value1"); this.simpAttributes.setAttribute("name3", "value1"); assertThat(this.simpAttributes.getAttributeNames(), arrayContainingInAnyOrder("name1", "name2", "name3")); }
@Test public void get() { this.simpAttributes.setAttribute("name", "value"); Object actual = this.scope.get("name", this.objectFactory); assertThat(actual, is("value")); }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); Object scopedObject = simpAttributes.getAttribute(name); if (scopedObject != null) { return scopedObject; } synchronized (simpAttributes.getSessionMutex()) { scopedObject = simpAttributes.getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); simpAttributes.setAttribute(name, scopedObject); } return scopedObject; } }
@Test public void getAttribute() { this.simpAttributes.setAttribute("name1", "value1"); assertThat(this.simpAttributes.getAttribute("name1"), is("value1")); assertThat(this.simpAttributes.getAttribute("name2"), nullValue()); }
@Test public void getSessionMutexExplicit() { Object mutex = new Object(); this.simpAttributes.setAttribute(SimpAttributes.SESSION_MUTEX_NAME, mutex); assertThat(this.simpAttributes.getSessionMutex(), sameInstance(mutex)); }
@Test public void setAttributesFromMessage() { String sessionId = "session1"; ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>(); SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(); headerAccessor.setSessionId(sessionId); headerAccessor.setSessionAttributes(map); Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders()); SimpAttributesContextHolder.setAttributesFromMessage(message); SimpAttributes attrs = SimpAttributesContextHolder.getAttributes(); assertThat(attrs, notNullValue()); assertThat(attrs.getSessionId(), is(sessionId)); attrs.setAttribute("name1", "value1"); assertThat(map.get("name1"), is("value1")); }
@Test public void remove() { this.simpAttributes.setAttribute("name", "value"); Object removed = this.scope.remove("name"); assertThat(removed, is("value")); assertThat(this.simpAttributes.getAttribute("name"), nullValue()); removed = this.scope.remove("name"); assertThat(removed, nullValue()); }
@Test public void webSocketScope() { Runnable runnable = Mockito.mock(Runnable.class); SimpAttributes simpAttributes = new SimpAttributes(this.session.getId(), this.session.getAttributes()); simpAttributes.setAttribute("name", "value"); simpAttributes.registerDestructionCallback("name", runnable); MessageChannel testChannel = new MessageChannel() { @Override public boolean send(Message<?> message) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); assertThat(simpAttributes.getAttribute("name"), is("value")); return true; } @Override public boolean send(Message<?> message, long timeout) { return false; } }; this.protocolHandler.afterSessionStarted(this.session, this.channel); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); TextMessage textMessage = new TextMessage(new StompEncoder().encode(message)); this.protocolHandler.handleMessageFromClient(this.session, textMessage, testChannel); assertEquals(Collections.<WebSocketMessage<?>>emptyList(), session.getSentMessages()); this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, testChannel); assertEquals(Collections.<WebSocketMessage<?>>emptyList(), this.session.getSentMessages()); verify(runnable, times(1)).run(); }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); Object scopedObject = simpAttributes.getAttribute(name); if (scopedObject != null) { return scopedObject; } synchronized (simpAttributes.getSessionMutex()) { scopedObject = simpAttributes.getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); simpAttributes.setAttribute(name, scopedObject); } return scopedObject; } }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); Object scopedObject = simpAttributes.getAttribute(name); if (scopedObject != null) { return scopedObject; } synchronized (simpAttributes.getSessionMutex()) { scopedObject = simpAttributes.getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); simpAttributes.setAttribute(name, scopedObject); } return scopedObject; } }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); Object scopedObject = simpAttributes.getAttribute(name); if (scopedObject != null) { return scopedObject; } synchronized (simpAttributes.getSessionMutex()) { scopedObject = simpAttributes.getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); simpAttributes.setAttribute(name, scopedObject); } return scopedObject; } }