INT-1564 added exception translation logic to TwitterOperationException for better display to the end user, added tests
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.integration.twitter.core;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class TwitterOperationExceptionTests {
|
||||
|
||||
@Test
|
||||
public void test401(){
|
||||
// will result in exception since a,b,c,d are invalid credentials
|
||||
Twitter4jTemplate template = new Twitter4jTemplate("a", "b", "c", "d");
|
||||
try {
|
||||
template.getProfileId();
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof TwitterOperationException);
|
||||
assertEquals(401, ((TwitterOperationException)e).getTwitterStatusCode());
|
||||
assertTrue(e.getMessage().contains("401:Authentication credentials were missing or incorrect"));
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testWithNull(){
|
||||
try {
|
||||
throw new TwitterOperationException();
|
||||
} catch (Exception e) {
|
||||
TwitterOperationException tex = (TwitterOperationException) e;
|
||||
assertFalse(StringUtils.hasText(tex.getMessage()));
|
||||
assertEquals(-1, tex.getTwitterStatusCode());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testWithTwitterException(){
|
||||
try {
|
||||
throw new TwitterOperationException(new TwitterException("foo"));
|
||||
} catch (Exception e) {
|
||||
TwitterOperationException tex = (TwitterOperationException) e;
|
||||
assertTrue(StringUtils.hasText(tex.getMessage()));
|
||||
assertTrue(tex.getMessage().contains("foo"));
|
||||
assertEquals(-1, tex.getTwitterStatusCode());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testWithDescription(){
|
||||
try {
|
||||
throw new TwitterOperationException("foo");
|
||||
} catch (Exception e) {
|
||||
TwitterOperationException tex = (TwitterOperationException) e;
|
||||
assertTrue(StringUtils.hasText(tex.getMessage()));
|
||||
assertTrue(tex.getMessage().contains("foo"));
|
||||
assertEquals(-1, tex.getTwitterStatusCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user