View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.messaging;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  /**
23   * A stub MessageChannel that saves all sent messages.
24   *
25   * @author Rossen Stoyanchev
26   */
27  public class StubMessageChannel implements SubscribableChannel {
28  
29  	private final List<Message<byte[]>> messages = new ArrayList<>();
30  
31  	private final List<MessageHandler> handlers = new ArrayList<>();
32  
33  
34  	public List<Message<byte[]>> getMessages() {
35  		return this.messages;
36  	}
37  
38  	@Override
39  	@SuppressWarnings("unchecked")
40  	public boolean send(Message<?> message) {
41  		this.messages.add((Message<byte[]>) message);
42  		return true;
43  	}
44  
45  	@Override
46  	@SuppressWarnings("unchecked")
47  	public boolean send(Message<?> message, long timeout) {
48  		this.messages.add((Message<byte[]>) message);
49  		return true;
50  	}
51  
52  	@Override
53  	public boolean subscribe(MessageHandler handler) {
54  		this.handlers.add(handler);
55  		return true;
56  	}
57  
58  	@Override
59  	public boolean unsubscribe(MessageHandler handler) {
60  		this.handlers.remove(handler);
61  		return true;
62  	}
63  }