This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
274 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...itter/src/test/java/org/springframework/social/twitter/api/impl/StreamDispatcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2011 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.social.twitter.api.impl; | ||
|
||
import static org.mockito.Matchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
import org.junit.Test; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.social.twitter.api.StreamDeleteEvent; | ||
import org.springframework.social.twitter.api.StreamListener; | ||
import org.springframework.social.twitter.api.Tweet; | ||
|
||
public class StreamDispatcherTest { | ||
|
||
@Test | ||
public void activeWithItemsInQueue() throws Exception { | ||
StreamListener mockListener = mock(StreamListener.class); | ||
Queue<String> queue = createQueueWithItems(); | ||
StreamDispatcher dispatcher = new StreamDispatcher(queue, Arrays.asList(mockListener)); | ||
runAndAssert(mockListener, dispatcher, 4, 2, 1); | ||
dispatcher.stop(); | ||
} | ||
|
||
@Test | ||
public void activeWithEmptyQueue() throws Exception { | ||
StreamListener mockListener = mock(StreamListener.class); | ||
Queue<String> queue = new ConcurrentLinkedQueue<String>(); | ||
StreamDispatcher dispatcher = new StreamDispatcher(queue, Arrays.asList(mockListener)); | ||
runAndAssert(mockListener, dispatcher, 0, 0, 0); | ||
dispatcher.stop(); | ||
} | ||
|
||
@Test | ||
public void stoppedWithItemsInQueue() throws Exception { | ||
StreamListener mockListener = mock(StreamListener.class); | ||
Queue<String> queue = createQueueWithItems(); | ||
StreamDispatcher dispatcher = new StreamDispatcher(queue, Arrays.asList(mockListener)); | ||
dispatcher.stop(); | ||
runAndAssert(mockListener, dispatcher, 4, 2, 1); | ||
} | ||
|
||
@Test | ||
public void stoppedWithEmptyQueue() throws Exception { | ||
StreamListener mockListener = mock(StreamListener.class); | ||
Queue<String> queue = new ConcurrentLinkedQueue<String>(); | ||
StreamDispatcher dispatcher = new StreamDispatcher(queue, Arrays.asList(mockListener)); | ||
dispatcher.stop(); | ||
runAndAssert(mockListener, dispatcher, 0, 0, 0); | ||
} | ||
|
||
@Test | ||
public void ignoreUnrecognizedEvent() throws Exception { | ||
StreamListener mockListener = mock(StreamListener.class); | ||
Queue<String> queue = new ConcurrentLinkedQueue<String>(); | ||
queue.add("BOGUS LINE"); | ||
queue.add("{\"unrecognized\":\"event\"}"); | ||
StreamDispatcher dispatcher = new StreamDispatcher(queue, Arrays.asList(mockListener)); | ||
runAndAssert(mockListener, dispatcher, 0, 0, 0); | ||
dispatcher.stop(); | ||
} | ||
|
||
private void runAndAssert(StreamListener mockListener, StreamDispatcher dispatcher, int tweetEvents, int deleteEvents, int limitEvents) throws Exception { | ||
new Thread(dispatcher).start(); | ||
Thread.sleep(100); // pause to give thread opportunity to do its job | ||
verify(mockListener, times(tweetEvents)).onTweet(any(Tweet.class)); | ||
verify(mockListener, times(deleteEvents)).onDelete(any(StreamDeleteEvent.class)); | ||
verify(mockListener, times(limitEvents)).onLimit(369); | ||
} | ||
|
||
private Queue<String> createQueueWithItems() { | ||
InputStream inputStream = null; | ||
Queue<String> queue = new ConcurrentLinkedQueue<String>(); | ||
try { | ||
inputStream = new ClassPathResource("filter-stream-track.json", getClass()).getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
while (reader.ready()) { | ||
queue.add(reader.readLine()); | ||
} | ||
} catch (IOException e) { | ||
try { | ||
inputStream.close(); | ||
} catch (IOException ignore) {} | ||
} | ||
return queue; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ial-twitter/src/test/java/org/springframework/social/twitter/api/impl/StreamImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2011 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.social.twitter.api.impl; | ||
|
||
import static java.util.Arrays.*; | ||
import static org.mockito.Matchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.io.InputStream; | ||
|
||
import org.junit.Test; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.social.twitter.api.StreamListener; | ||
import org.springframework.social.twitter.api.StreamingException; | ||
import org.springframework.social.twitter.api.Tweet; | ||
|
||
|
||
public class StreamImplTest { | ||
|
||
@Test | ||
public void next() throws Exception { | ||
StreamListener mockListener = mock(StreamListener.class); | ||
InputStream inputStream = new ClassPathResource("filter-stream-track.json", getClass()).getInputStream(); | ||
StreamImpl stream = new StreamImpl(inputStream, asList(mockListener)); | ||
try { | ||
while(true) { | ||
stream.next(); | ||
} | ||
} catch(StreamingException e) {} | ||
Thread.sleep(100); | ||
verify(mockListener, times(4)).onTweet(any(Tweet.class)); | ||
inputStream.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ocial-twitter/src/test/java/org/springframework/social/twitter/api/impl/delete-event.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"delete":{"status":{"id":1234,"id_str":"1234","user_id":3,"user_id_str":"3"}}} |
1 change: 1 addition & 0 deletions
1
...social-twitter/src/test/java/org/springframework/social/twitter/api/impl/limit-event.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"limit":{"track":369}} |
Oops, something went wrong.