Beispiel #1
0
 public void testSign() throws Exception {
   String baseStr =
       "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal";
   OAuthAuthorization oauth =
       new OAuthAuthorization(
           ConfigurationContext.getInstance(), "dpf43f3p2l4k3l03", "kd94hf93k423kf44");
   trySerializable(oauth);
   // http://wiki.oauth.net/TestCases
   assertEquals(
       "tR3+Ty81lMeYAr/Fid0kMTYa/WM=",
       oauth.generateSignature(baseStr, new RequestToken("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00")));
   assertEquals(
       "egQqG5AJep5sJ7anhXju1unge2I=",
       new OAuthAuthorization(ConfigurationContext.getInstance(), desktopConsumerKey, "cs")
           .generateSignature("bs", new RequestToken("nnch734d00sl2jdk", "")));
   assertEquals(
       "VZVjXceV7JgPq/dOTnNmEfO0Fv8=",
       new OAuthAuthorization(ConfigurationContext.getInstance(), desktopConsumerKey, "cs")
           .generateSignature("bs", new RequestToken("nnch734d00sl2jdk", "ts")));
   assertEquals(
       "tR3+Ty81lMeYAr/Fid0kMTYa/WM=",
       new OAuthAuthorization(
               ConfigurationContext.getInstance(), desktopConsumerKey, "kd94hf93k423kf44")
           .generateSignature(
               "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal",
               new RequestToken("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00")));
 }
Beispiel #2
0
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    desktopConsumerSecret = p.getProperty("desktopConsumerSecret");
    desktopConsumerKey = p.getProperty("desktopConsumerKey");
    HttpClient decktopClient = new HttpClient();
    OAuthSupport desktopClientOAuthAuthentication =
        new OAuthAuthorization(
            ConfigurationContext.getInstance(), desktopConsumerKey, desktopConsumerSecret);

    browserConsumerSecret = p.getProperty("browserConsumerSecret");
    browserConsumerKey = p.getProperty("browserConsumerKey");
    HttpClient browserClient = new HttpClient();
    OAuthSupport browserClientOAuthAuthentication =
        new OAuthAuthorization(
            ConfigurationContext.getInstance(), browserConsumerKey, browserConsumerSecret);

    //        consumerSecret = p.getString("browserConsumerSecret");
    //        consumerKey = p.getString("browserConsumerKey");

    twitterAPI1 = new TwitterFactory().getInstance();
    twitterAPI1.setOAuthConsumer(desktopConsumerKey, desktopConsumerSecret);
    String id1token = p.getProperty("id1.oauth_token");
    String id1tokenSecret = p.getProperty("id1.oauth_token_secret");
    twitterAPI1.setOAuthAccessToken(new AccessToken(id1token, id1tokenSecret));

    twitterAPI2 = new Twitter();
    twitterAPI2.setOAuthConsumer(desktopConsumerKey, desktopConsumerSecret);
    String id2token = p.getProperty("id2.oauth_token");
    String id2tokenSecret = p.getProperty("id2.oauth_token_secret");
    twitterAPI2.setOAuthAccessToken(new AccessToken(id2token, id2tokenSecret));

    unauthenticated = new Twitter();
  }
Beispiel #3
0
/**
 * @author Yusuke Yamamoto - yusuke at mac.com
 * @since Twitter4J 2.1.0
 */
public final class Logger {
  private static final boolean DEBUG = ConfigurationContext.getInstance().isDebugEnabled();
  private static final Logger SINGLETON = new Logger();

  private Logger() {
    // @todo will wrap SLF4J / commons-logging / jul l8er @TFJ-148
  }

  public static Logger getLogger() {
    // @todo detect the class name from the stacktrace @TFJ-148
    return SINGLETON;
  }

  public boolean isDebugEnabled() {
    return DEBUG;
  }

  public void debug(String message) {
    if (DEBUG) {
      // @todo include class name in the message @TFJ-148
      System.out.println("[" + new java.util.Date() + "]" + message);
    }
  }

  public void debug(String message, String message2) {
    if (DEBUG) {
      debug(message + message2);
    }
  }
}
Beispiel #4
0
  static {
    String className = null;
    if (ConfigurationContext.getInstance().isGAE()) {
      final String APP_ENGINE_TWITTER_IMPL = "twitter4j.AppEngineTwitterImpl";
      try {
        Class.forName(APP_ENGINE_TWITTER_IMPL);
        className = APP_ENGINE_TWITTER_IMPL;
      } catch (ClassNotFoundException ignore) {
      }
    }
    if (className == null) {
      className = "twitter4j.TwitterImpl";
    }
    Constructor<Twitter> constructor;
    Class<?> clazz;
    try {
      clazz = Class.forName(className);
      constructor =
          (Constructor<Twitter>)
              clazz.getDeclaredConstructor(Configuration.class, Authorization.class);
    } catch (NoSuchMethodException e) {
      throw new AssertionError(e);
    } catch (ClassNotFoundException e) {
      throw new AssertionError(e);
    }
    TWITTER_CONSTRUCTOR = constructor;

    try {
      SINGLETON =
          TWITTER_CONSTRUCTOR.newInstance(
              ConfigurationContext.getInstance(), DEFAULT_AUTHORIZATION);
    } catch (InstantiationException e) {
      throw new AssertionError(e);
    } catch (IllegalAccessException e) {
      throw new AssertionError(e);
    } catch (InvocationTargetException e) {
      throw new AssertionError(e);
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    orm = QiupuORM.getInstance(mActivity);
    mHandler = new MainHandler();
    asyncQiupu = new AsyncQiupu(ConfigurationContext.getInstance(), null, null);
    QiupuHelper.registerUserListener(getClass().getName(), this);

    width = this.getResources().getDisplayMetrics().widthPixels;
    if (mCallBackListener != null) {
      mUser = mCallBackListener.getUserInfo();
    }
    if (mUser == null) {
      mUser = new QiupuUser();
    }
  }
Beispiel #6
0
  private void executeOauth() {
    // Twitetr4jの設定を読み込む
    Configuration conf = ConfigurationContext.getInstance();

    // Oauth認証オブジェクト作成
    _oauth = new OAuthAuthorization(conf);
    // Oauth認証オブジェクトにconsumerKeyとconsumerSecretを設定
    _oauth.setOAuthConsumer("iy2FEHXmSXNReJ6nYQ8FRg", "KYro4jM8BHlLSMsSdTylnTcm3pYaTCiG2UZrYK1yI4");
    // アプリの認証オブジェクト作成
    try {
      _req = _oauth.getOAuthRequestToken("Callback://CallBackActivity");
    } catch (TwitterException e) {
      e.printStackTrace();
    }
    String _uri;
    _uri = _req.getAuthorizationURL();
    startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse(_uri)), 0);
  }
Beispiel #7
0
 public void testHeader() throws Exception {
   HttpParameter[] params = new HttpParameter[2];
   params[0] = new HttpParameter("file", "vacation.jpg");
   params[1] = new HttpParameter("size", "original");
   OAuthAuthorization oauth =
       new OAuthAuthorization(
           ConfigurationContext.getInstance(), "dpf43f3p2l4k3l03", "kd94hf93k423kf44");
   String expected =
       "OAuth oauth_consumer_key=\"dpf43f3p2l4k3l03\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1191242096\",oauth_nonce=\"kllo9940pd9333jh\",oauth_version=\"1.0\",oauth_token=\"nnch734d00sl2jdk\",oauth_signature=\"tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D\"";
   assertEquals(
       expected,
       oauth.generateAuthorizationHeader(
           "GET",
           "http://photos.example.net/photos",
           params,
           "kllo9940pd9333jh",
           "1191242096",
           new RequestToken("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00")));
 }
 public HttpClientImpl()
 {
     super(ConfigurationContext.getInstance());
 }
/** @author Yusuke Yamamoto - yusuke at mac.com */
public class DAOTest extends TwitterTestBase {
  Configuration conf = ConfigurationContext.getInstance();

  public DAOTest(String name) {
    super(name);
  }

  protected void setUp() throws Exception {
    super.setUp();
  }

  protected void tearDown() throws Exception {
    super.tearDown();
  }

  public void testEmptyJSON() throws Exception {
    HttpClientImpl http = new HttpClientImpl();

    // empty User list
    List<User> users =
        UserJSONImpl.createUserList(
            http.get("http://twitter4j.org/en/testcases/statuses/friends/T4J_hudson.json"), conf);
    Assert.assertTrue(users.size() == 0);
    assertDeserializedFormIsEqual(users);

    // empty Status list
    List<Status> statuses =
        StatusJSONImpl.createStatusList(
            http.get("http://twitter4j.org/en/testcases/statuses/friends/T4J_hudson.json"), conf);
    Assert.assertTrue(statuses.size() == 0);
    assertDeserializedFormIsEqual(statuses);

    // empty DirectMessages list
    List<DirectMessage> directMessages =
        DirectMessageJSONImpl.createDirectMessageList(
            http.get("http://twitter4j.org/en/testcases/statuses/friends/T4J_hudson.json"), conf);
    Assert.assertTrue(directMessages.size() == 0);
    assertDeserializedFormIsEqual(directMessages);

    // empty Trends list
    List<Trends> trends =
        TrendsJSONImpl.createTrendsList(
            http.get("http://twitter4j.org/en/testcases/trends/daily-empty.json"),
            conf.isJSONStoreEnabled());
    Assert.assertTrue(trends.size() == 0);
    assertDeserializedFormIsEqual(trends);
  }

  public void testLocation() throws Exception {
    JSONArray array = getJSONArrayFromClassPath("/dao/trends-available.json");
    ResponseList<Location> locations =
        LocationJSONImpl.createLocationList(array, conf.isJSONStoreEnabled());
    Assert.assertEquals(23, locations.size());
    Location location = locations.get(0);
    Assert.assertEquals("GB", location.getCountryCode());
    Assert.assertEquals("United Kingdom", location.getCountryName());
    Assert.assertEquals("United Kingdom", location.getName());
    Assert.assertEquals(12, location.getPlaceCode());
    Assert.assertEquals("Country", location.getPlaceName());
    Assert.assertEquals("http://where.yahooapis.com/v1/place/23424975", location.getURL());
    Assert.assertEquals(23424975, location.getWoeid());
  }

  public void testUnparsable() throws Exception {
    String str;
    str = "";
    try {
      DataObjectFactory.createStatus(str);
      fail("should fail");
    } catch (TwitterException expected) {
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
    try {
      DataObjectFactory.createStatus(str);
      fail("should fail");
    } catch (TwitterException expected) {
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
    str =
        "{\"in_reply_to_status_id_str\":null,\"place\":null,\"in_reply_to_user_id\":null,\"text\":\"working\",\"contributors\":null,\"retweet_count\":0,\"in_reply_to_user_id_str\":null,\"retweeted\":false,\"id_str\":\"794626207\",\"source\":\"\\u003Ca href=\\\"http:\\/\\/twitterhelp.blogspot.com\\/2008\\/05\\/twitter-via-mobile-web-mtwittercom.html\\\" rel=\\\"nofollow\\\"\\u003Emobile web\\u003C\\/a\\u003E\",\"truncated\":false,\"geo\":null,\"in_reply_to_status_id\":null,\"favorited\":false,\"user\":{\"show_all_inline_media\":false,\"geo_enabled\":false,\"profile_background_tile\":false,\"time_zone\":null,\"favourites_count\":0,\"description\":null,\"friends_count\":0,\"profile_link_color\":\"0084B4\",\"location\":null,\"profile_sidebar_border_color\":\"C0DEED\",\"id_str\":\"14481043\",\"url\":null,\"follow_request_sent\":false,\"statuses_count\":1,\"profile_use_background_image\":true,\"lang\":\"en\",\"profile_background_color\":\"C0DEED\",\"profile_image_url\":\"http:\\/\\/a3.twimg.com\\/a\\/1292975674\\/images\\/default_profile_3_normal.png\",\"profile_background_image_url\":\"http:\\/\\/a3.twimg.com\\/a\\/1292975674\\/images\\/themes\\/theme1\\/bg.png\",\"followers_count\":44,\"protected\":false,\"contributors_enabled\":false,\"notifications\":false,\"screen_name\":\"Yusuke\",\"name\":\"Yusuke\",\"is_translator\":false,\"listed_count\":1,\"following\":false,\"verified\":false,\"profile_text_color\":\"333333\",\"id\":14481043,\"utc_offset\":null,\"created_at\":\"Tue Apr 22 21:49:13 +0000 2008\",\"profile_sidebar_fill_color\":\"DDEEF6\"},\"id\":794626207,\"coordinates\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Tue Apr 2200 21:49:34 +0000 2008\"";

    try {
      DataObjectFactory.createCategory(str);
      fail("should fail");
    } catch (TwitterException expected) {
      expected.printStackTrace();
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
    try {
      DataObjectFactory.createCategory(str);
      fail("should fail");
    } catch (TwitterException expected) {
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
  }

  public void testSchema() throws Exception {
    String[] schema;
    String url;

    //        JSONObject json = new
    // JSONObject("{\"a\":\"avalue\",\"b\":\"bvalue\",\"c\":{\"c-1\":12,\"c-2\":\"c-2value\"}}");
    //        schema = new String[]{"a", "b", "c/c-1", "c/c-2"};
    //        validateJSONObjectSchema(json, schema);
    //        try {
    //            schema = new String[]{"a", "b", "c/c-1"};
    //            validateJSONObjectSchema(json, schema);
    //            fail("c/c-2 is missing. expecting an AssertionFailedError.");
    //        } catch (AssertionFailedError ignore) {
    ////            ignore.printStackTrace();
    //        }
    //        try {
    //            schema = new String[]{"a", "b"};
    //            validateJSONObjectSchema(json, schema);
    //            fail("c is missing. expecting an AssertionFailedError.");
    //        } catch (AssertionFailedError ignore) {
    ////            ignore.printStackTrace();
    //        }
    //        try {
    //            schema = new String[]{"a", "b","c"};
    //            validateJSONObjectSchema(json, schema);
    //            fail("c/* is missing. expecting an AssertionFailedError.");
    //        } catch (AssertionFailedError ignore) {
    ////            ignore.printStackTrace();
    //        }
    //        schema = new String[]{"a", "b", "c/*"};
    //        validateJSONObjectSchema(json, schema);
    //
    //        JSONArray array = new JSONArray("[{\"slug\":\"art-design\",\"name\":\"Art &
    // Design\"},{\"slug\":\"books\",\"name\":\"Books\"}]");
    //        schema = new String[]{"slug", "name"};
    //        validateJSONArraySchema(array, schema);

    // Location

    //        schema = new
    // String[]{"url","country","woeid","placeType/name","placeType/code","name","countryCode"};
    //        url = "http://api.twitter.com/1.1/trends/available.json";
    //        validateJSONArraySchema(url, schema);
    // Place
    if (!Boolean.valueOf(System.getProperties().getProperty("twitter4j.test.schema"))) {
      // skipping schema validation
      return;
    }

    schema =
        new String[] {
          "slug", "name", "size",
        };
    url = "http://api.twitter.com/1.1/users/suggestions.json";
    List categories =
        CategoryJSONImpl.createCategoriesList(validateJSONArraySchema(url, schema), null, conf);
    Assert.assertEquals(20, categories.size());

    schema = new String[] {"slug", "name", "size", "categories/*", "users/*"};
    url = "http://api.twitter.com/1.1/users/suggestions/art-design.json";
    validateJSONObjectSchema(url, schema);

    schema =
        new String[] {
          "result/places/name",
          "result/places/street_address",
          "result/places/attributes/*",
          "result/places/country_code",
          "result/places/id",
          "result/places/country",
          "result/places/place_type",
          "result/places/url",
          "result/places/full_name",
          "result/places/bounding_box/*",
          "result/places/contained_within/place_type",
          "result/places/contained_within/attributes/*",
          "result/places/contained_within/street_address",
          "result/places/contained_within/url",
          "result/places/contained_within/bounding_box/type",
          "result/places/contained_within/bounding_box/coordinates/*",
          "result/places/contained_within/full_name",
          "result/places/contained_within/country_code",
          "result/places/contained_within/name",
          "result/places/contained_within/id",
          "result/places/contained_within/country",
          "query",
          "query/type",
          "query/url",
          "query/params",
          "query/params/granularity",
          "query/params/coordinates",
          "query/params/coordinates/type",
          "query/params/coordinates/coordinates",
          "query/params/coordinates/coordinates/*",
          "query/params/accuracy",
        };
    url = "http://api.twitter.com/1.1/geo/reverse_geocode.json?lat=37.78215&long=-122.40060";
    validateJSONObjectSchema(url, schema);

    schema =
        new String[] {
          "next_cursor",
          "next_cursor_str",
          "previous_cursor",
          "previous_cursor_str",
          "lists/id",
          "lists/id_str",
          "lists/member_count",
          "lists/description",
          "lists/name",
          "lists/subscriber_count",
          "lists/slug",
          "lists/user/*",
          "lists/uri",
          "lists/full_name",
          "lists/mode",
          "lists/following",
        };
    url = "http://api.twitter.com/1.1/twit4j2/lists.json";
    validateJSONObjectSchema(url, schema);

    schema =
        new String[] {
          "id",
          "id_str",
          "member_count",
          "description",
          "name",
          "subscriber_count",
          "slug",
          "user/*",
          "uri",
          "full_name",
          "mode",
          "following",
        };
    url = "http://api.twitter.com/1.1/twit4j2/lists/9499823.json";
    UserList userList = new UserListJSONImpl(validateJSONObjectSchema(url, schema));
    Assert.assertEquals("", userList.getDescription());
    Assert.assertEquals("@twit4j2/test", userList.getFullName());
    Assert.assertEquals(9499823, userList.getId());
    Assert.assertTrue(1 < userList.getMemberCount());
    Assert.assertEquals("test", userList.getName());
    Assert.assertEquals("test", userList.getSlug());
    Assert.assertEquals(0, userList.getSubscriberCount());
    Assert.assertEquals("/twit4j2/test", userList.getURI().toString());
    Assert.assertNotNull(userList.getUser());
    Assert.assertTrue(userList.isPublic());
    Assert.assertFalse(userList.isFollowing());

    schema =
        new String[] {
          "favorited",
          "in_reply_to_status_id",
          "in_reply_to_status_id_str",
          "created_at",
          "geo",
          "place",
          "source",
          "in_reply_to_screen_name",
          "in_reply_to_user_id",
          "in_reply_to_user_id_str",
          "coordinates",
          "truncated",
          "contributors",
          "id",
          "id_str",
          "text",
          "user/*",
          "retweeted",
          "retweet_count"
        };
    url = "http://api.twitter.com/1.1/statuses/show/2245071380.json";
    Status status = new StatusJSONImpl(validateJSONObjectSchema(url, schema));

    schema =
        new String[] {
          "profile_background_image_url",
          "created_at",
          "friends_count",
          "profile_link_color",
          "description",
          "contributors_enabled",
          "status/*",
          "following",
          "profile_background_tile",
          "favourites_count",
          "profile_sidebar_fill_color",
          "url",
          "profile_image_url",
          "geo_enabled",
          "notifications",
          "profile_sidebar_border_color",
          "location",
          "screen_name",
          "verified",
          "time_zone",
          "profile_background_color",
          "profile_use_background_image",
          "protected",
          "name",
          "profile_text_color",
          "followers_count",
          "id",
          "id_str",
          "lang",
          "statuses_count",
          "follow_request_sent",
          "utc_offset",
          "listed_count",
          "is_translator",
          "show_all_inline_media"
        };

    url = "http://api.twitter.com/1.1/users/show/yusukey.json";
    User user = new UserJSONImpl(validateJSONObjectSchema(url, schema));
  }

  private JSONObject validateJSONObjectSchema(String url, String[] knownNames) throws Exception {
    JSONObject json = getJSONObjectFromGetURL(url);
    validateJSONObjectSchema(json, knownNames);
    return json;
  }

  private static JSONObject validateJSONObjectSchema(JSONObject json, String[] knownNames)
      throws JSONException {
    boolean debug = false;
    Map<String, String[]> schemaMap = new HashMap<String, String[]>();
    List<String> names = new ArrayList<String>();
    if (debug) {
      System.out.println("validating:" + json);
    }
    for (int i = 0; i < knownNames.length; i++) {
      if (debug) {
        System.out.println("knownName[" + i + "]:" + knownNames[i]);
      }
      String knownName = knownNames[i];
      int index;
      if (-1 != (index = knownName.indexOf("/"))) {
        String parent = knownName.substring(0, index);
        String child = knownName.substring(index + 1);
        String[] array = schemaMap.get(parent);
        if (null == array) {
          schemaMap.put(parent, new String[] {child});
        } else {
          String[] newArray = new String[array.length + 1];
          System.arraycopy(array, 0, newArray, 0, array.length);
          newArray[newArray.length - 1] = child;
          schemaMap.put(parent, newArray);
        }
        names.add(parent);
      } else {
        names.add(knownName);
      }
    }

    Iterator ite = json.keys();
    while (ite.hasNext()) {
      String name = (String) ite.next();
      boolean found = false;
      if (debug) {
        System.out.println("name:" + name);
      }
      for (String elementName : names) {
        if (debug) {
          System.out.println("elementname:" + elementName);
        }
        Object obj = json.get(name);
        if (obj instanceof JSONObject || obj instanceof JSONArray) {
          String[] children = schemaMap.get(name);
          if (null == children) {
            Assert.fail(
                elementName + ":" + name + " is not supposed to have any child but has:" + obj);
          } else if (!children[0].equals("*")) {
            validateJSONSchema(obj, children);
          }
        }
        if (elementName.equals(name)) {
          found = true;
          break;
        }
      }
      if (!found) {
        Assert.fail("unknown element:[" + name + "] in " + json);
      }
    }
    return json;
  }

  private JSONArray validateJSONArraySchema(String url, String[] knownNames) throws Exception {
    return validateJSONArraySchema(getJSONArrayFromGetURL(url), knownNames);
  }

  private static void validateJSONSchema(Object json, String[] knownNames) throws JSONException {
    if (json instanceof JSONArray) {
      validateJSONArraySchema((JSONArray) json, knownNames);
    } else if (json instanceof JSONObject) {
      validateJSONObjectSchema((JSONObject) json, knownNames);
    } else {
      Assert.fail(
          "expecting either JSONArray or JSONObject here. Passed:" + json.getClass().getName());
    }
  }

  private static JSONArray validateJSONArraySchema(JSONArray array, String[] knownNames)
      throws JSONException {
    for (int i = 0; i < array.length(); i++) {
      Object obj = array.get(i);
      if (obj instanceof JSONObject) {
        JSONObject json = array.getJSONObject(i);
        validateJSONObjectSchema(json, knownNames);
      }
    }
    return array;
  }

  private static JSONArray getJSONArrayFromClassPath(String path) throws Exception {
    return new JSONArray(getStringFromClassPath(path));
  }

  private static JSONObject getJSONObjectFromClassPath(String path) throws Exception {
    return new JSONObject(getStringFromClassPath(path));
  }

  private JSONObject getJSONObjectFromGetURL(String url) throws Exception {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setUser(id1.screenName);
    builder.setPassword(id1.password);
    return getJSONObjectFromGetURL(url, builder.build());
  }

  private static JSONObject getJSONObjectFromPostURL(String url, Configuration conf)
      throws Exception {
    HttpClientWrapper http = new HttpClientWrapper(conf);
    return http.post(url).asJSONObject();
  }

  private JSONObject getJSONObjectFromPostURL(String url) throws Exception {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setUser(id1.screenName);
    builder.setPassword(id1.password);
    return getJSONObjectFromPostURL(url, builder.build());
  }

  private JSONObject getJSONObjectFromGetURL(String url, Configuration conf) throws Exception {
    HttpClientWrapper http = new HttpClientWrapper(conf);
    return http.get(url, getOAuthOuthorization(conf)).asJSONObject();
  }

  private JSONArray getJSONArrayFromGetURL(String url) throws Exception {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setUser(id1.screenName);
    builder.setPassword(id1.password);
    return getJSONArrayFromGetURL(url, builder.build());
  }

  private JSONArray getJSONArrayFromGetURL(String url, Configuration conf) throws Exception {
    HttpClientWrapper http = new HttpClientWrapper(conf);
    return http.get(url, getOAuthOuthorization(conf)).asJSONArray();
  }

  private OAuthAuthorization getOAuthOuthorization(Configuration conf) {
    OAuthAuthorization oauth = new OAuthAuthorization(conf);
    oauth.setOAuthConsumer(desktopConsumerKey, desktopConsumerSecret);
    oauth.setOAuthAccessToken(new AccessToken(id1.accessToken, id1.accessTokenSecret));
    return oauth;
  }

  private static String getStringFromClassPath(String path) throws Exception {
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    try {
      is = DAOTest.class.getResourceAsStream(path);
      if (is == null) {
        throw new IllegalStateException(path + " not found.");
      }
      isr = new InputStreamReader(is);
      br = new BufferedReader(isr);
      StringBuilder buf = new StringBuilder();
      String line;
      while ((line = br.readLine()) != null) {
        buf.append(line);
      }
      return buf.toString();
    } finally {
      if (is != null) {
        is.close();
        isr.close();
        br.close();
      }
    }
  }

  public void testUserAsJSON() throws Exception {
    // single User
    HttpClientWrapper http = new HttpClientWrapper();
    JSONObject json = getJSONObjectFromClassPath("/dao/user.json");
    User user = new UserJSONImpl(json);
    Assert.assertTrue(user.isGeoEnabled());
    Assert.assertFalse(user.isVerified());
    Assert.assertEquals(id1.screenName, user.getName());
    Assert.assertEquals(id1.screenName, user.getScreenName());
    Assert.assertNotNull(user.getLocation());
    Assert.assertNotNull(user.getDescription());
    Assert.assertNotNull(user.getProfileImageURL());
    Assert.assertNotNull(user.getURL());
    Assert.assertFalse(user.isProtected());

    Assert.assertTrue(0 <= user.getFavouritesCount());
    Assert.assertTrue(0 <= user.getFollowersCount());
    Assert.assertTrue(0 <= user.getFriendsCount());
    Assert.assertNotNull(user.getCreatedAt());
    Assert.assertNotNull(user.getTimeZone());
    Assert.assertNotNull(user.getProfileBackgroundImageURL());

    Assert.assertTrue(0 <= user.getStatusesCount());
    Assert.assertNotNull(user.getProfileBackgroundColor());
    Assert.assertNotNull(user.getProfileTextColor());
    Assert.assertNotNull(user.getProfileLinkColor());
    Assert.assertNotNull(user.getProfileSidebarBorderColor());
    Assert.assertNotNull(user.getProfileSidebarFillColor());
    Assert.assertNotNull(user.getProfileTextColor());

    Assert.assertTrue(1 < user.getFollowersCount());
    Assert.assertNotNull(user.getStatus().getCreatedAt());
    Assert.assertNotNull(user.getStatus().getText());
    Assert.assertNotNull(user.getStatus().getSource());
    Assert.assertFalse(user.getStatus().isFavorited());
    Assert.assertEquals(-1, user.getStatus().getInReplyToStatusId());
    Assert.assertEquals(-1, user.getStatus().getInReplyToUserId());
    Assert.assertFalse(user.getStatus().isFavorited());
    Assert.assertNull(user.getStatus().getInReplyToScreenName());
    assertDeserializedFormIsEqual(user);
    Assert.assertTrue(0 <= user.getListedCount());
    List<User> users;

    // User list
    users =
        UserJSONImpl.createUserList(
            http.get("http://twitter4j.org/en/testcases/statuses/followers/T4J_hudson.json"), conf);
    Assert.assertTrue(users.size() > 0);
    assertDeserializedFormIsEqual(users);
  }

  public void testUserListAsJSON() throws Exception {}

  public void testStatusAsJSON() throws Exception {
    // single Status
    HttpClientImpl http = new HttpClientImpl();
    List<Status> statuses =
        StatusJSONImpl.createStatusList(
            http.get("http://twitter4j.org/en/testcases/statuses/public_timeline.json"), conf);
    Status status = statuses.get(0);
    Assert.assertEquals(new Date(1259041785000l), status.getCreatedAt());
    Assert.assertEquals(6000554383l, status.getId());
    Assert.assertEquals("G_Shock22", status.getInReplyToScreenName());
    Assert.assertEquals(6000444309l, status.getInReplyToStatusId());
    Assert.assertEquals(20159829, status.getInReplyToUserId());
    Assert.assertNull(status.getGeoLocation());
    Assert.assertEquals("web", status.getSource());
    Assert.assertEquals(
        "@G_Shock22 I smelled a roast session coming when yu said that shyt about @2koolNicia lol....",
        status.getText());
    Assert.assertEquals(23459577, status.getUser().getId());
    Assert.assertFalse(status.isRetweet());
    assertDeserializedFormIsEqual(statuses);
  }

  public void testRetweetStatusAsJSON() throws Exception {
    // single Status
    HttpClientImpl http = new HttpClientImpl();
    Status status =
        new StatusJSONImpl(
            http.get("http://twitter4j.org/en/testcases/statuses/retweet/6010814202.json"), conf);
    Assert.assertEquals(new Date(1259078050000l), status.getCreatedAt());
    Assert.assertEquals(6011259778l, status.getId());
    Assert.assertEquals(null, status.getInReplyToScreenName());
    Assert.assertEquals(-1l, status.getInReplyToStatusId());
    Assert.assertEquals(-1, status.getInReplyToUserId());
    Assert.assertNull(status.getGeoLocation());
    Assert.assertEquals(
        "<a href=\"http://apiwiki.twitter.com/\" rel=\"nofollow\">API</a>", status.getSource());
    Assert.assertEquals(
        "RT @yusukey: この前取材受けた奴 -> 次世代のシステム環境を見据えたアプリケーションサーバー製品の選択 ITpro: http://special.nikkeibp.co.jp/ts/article/0iaa/104388/",
        status.getText());
    Assert.assertEquals(6358482, status.getUser().getId());
    Assert.assertTrue(status.isRetweet());
    assertDeserializedFormIsEqual(status);
  }

  public void testCategoryAsJSON() throws Exception {
    List<Category> categories =
        CategoryJSONImpl.createCategoriesList(
            getJSONArrayFromClassPath("/dao/suggestions.json"), null, conf);
    Assert.assertEquals(20, categories.size());
    Assert.assertEquals("art-design", categories.get(0).getSlug());
    Assert.assertEquals("Art & Design", categories.get(0).getName());
    Assert.assertTrue(0 < categories.get(0).getSize());
  }

  public void testPlaceAsJSON() throws Exception {
    List<Place> places =
        PlaceJSONImpl.createPlaceList(
            getJSONObjectFromClassPath("/dao/reverse-geocode.json")
                .getJSONObject("result")
                .getJSONArray("places"),
            null,
            conf);
    Place place = places.get(0);
    Assert.assertEquals("SoMa", place.getName());
    Assert.assertEquals("US", place.getCountryCode());
    Assert.assertEquals("2b6ff8c22edd9576", place.getId());
    Assert.assertEquals("", place.getCountry());
    Assert.assertEquals("neighborhood", place.getPlaceType());
    Assert.assertEquals("http://api.twitter.com/1/geo/id/2b6ff8c22edd9576.json", place.getURL());
    Assert.assertEquals("SoMa, San Francisco", place.getFullName());
    Assert.assertEquals("Polygon", place.getBoundingBoxType());
    GeoLocation[][] boundingBox = place.getBoundingBoxCoordinates();
    Assert.assertEquals(1, boundingBox.length);
    Assert.assertEquals(4, boundingBox[0].length);
    Assert.assertEquals(37.76893497, boundingBox[0][0].getLatitude());
    Assert.assertEquals(-122.42284884, boundingBox[0][0].getLongitude());
    Assert.assertEquals(37.76893497, boundingBox[0][1].getLatitude());
    Assert.assertEquals(-122.3964, boundingBox[0][1].getLongitude());
    Assert.assertEquals(37.78752897, boundingBox[0][2].getLatitude());
    Assert.assertEquals(-122.3964, boundingBox[0][2].getLongitude());
    Assert.assertEquals(37.78752897, boundingBox[0][3].getLatitude());
    Assert.assertEquals(-122.42284884, boundingBox[0][3].getLongitude());
    Assert.assertNull(place.getGeometryType());
    Assert.assertNull(place.getGeometryCoordinates());

    Place[] containedWithinArray = place.getContainedWithIn();
    Assert.assertEquals(1, containedWithinArray.length);
    Place containedWithin = containedWithinArray[0];
    Assert.assertNull(containedWithin.getContainedWithIn());
    Assert.assertEquals("San Francisco", containedWithin.getName());
    Assert.assertEquals("US", containedWithin.getCountryCode());
    Assert.assertEquals("5a110d312052166f", containedWithin.getId());
    Assert.assertEquals("", containedWithin.getCountry());
    Assert.assertEquals("city", containedWithin.getPlaceType());
    Assert.assertEquals(
        "http://api.twitter.com/1/geo/id/5a110d312052166f.json", containedWithin.getURL());
    Assert.assertEquals("San Francisco", containedWithin.getFullName());
    boundingBox = containedWithin.getBoundingBoxCoordinates();
    Assert.assertEquals("Polygon", place.getBoundingBoxType());
    Assert.assertEquals(1, boundingBox.length);
    Assert.assertEquals(4, boundingBox[0].length);
    Assert.assertEquals(37.70813196, boundingBox[0][0].getLatitude());
    Assert.assertEquals(-122.51368188, boundingBox[0][0].getLongitude());
    Assert.assertEquals(37.70813196, boundingBox[0][1].getLatitude());
    Assert.assertEquals(-122.35845384, boundingBox[0][1].getLongitude());
    Assert.assertEquals(37.83245301, boundingBox[0][2].getLatitude());
    Assert.assertEquals(-122.35845384, boundingBox[0][2].getLongitude());
    Assert.assertEquals(37.83245301, boundingBox[0][3].getLatitude());
    Assert.assertEquals(-122.51368188, boundingBox[0][3].getLongitude());

    Assert.assertNull(place.getGeometryType());
    Assert.assertNull(place.getGeometryCoordinates());

    place = new PlaceJSONImpl(getJSONObjectFromClassPath("/dao/5a110d312052166f.json"), null);
    Assert.assertNotNull(place.getGeometryType());
    Assert.assertNotNull(place.getGeometryCoordinates());

    // Test that a geo object with geometry type "Point" works.
    place = new PlaceJSONImpl(getJSONObjectFromClassPath("/dao/3c6797665e2d42eb.json"), null);
    Assert.assertEquals(place.getGeometryType(), "Point");
    Assert.assertNotNull(place.getGeometryCoordinates());

    place = new PlaceJSONImpl(getJSONObjectFromClassPath("/dao/c3f37afa9efcf94b.json"), null);
    // MultiPolygon is not supported by twitter4j yet, so we set geometryType to null
    Assert.assertNull(place.getGeometryType());
    Assert.assertNull(place.getGeometryCoordinates());
  }

  public void testDirectMessagesAsJSON() throws Exception {
    HttpClientImpl http = new HttpClientImpl();
    List<DirectMessage> directMessages =
        DirectMessageJSONImpl.createDirectMessageList(
            http.get("http://twitter4j.org/en/testcases/direct_messages.json"), conf);
    DirectMessage dm = directMessages.get(0);
    Assert.assertEquals(new java.util.Date(1248177356000l), dm.getCreatedAt());
    Assert.assertEquals(6358482, dm.getRecipient().getId());
    Assert.assertEquals(246928323, dm.getId());
    Assert.assertEquals(6358482, dm.getRecipientId());
    Assert.assertEquals("twit4j", dm.getRecipientScreenName());
    Assert.assertEquals(6377362, dm.getSender().getId());
    Assert.assertEquals(6377362, dm.getSenderId());
    Assert.assertEquals("twit4j2", dm.getSenderScreenName());
    Assert.assertEquals("Tue Jul 21 20:55:39 KST 2009:directmessage test", dm.getText());
    assertDeserializedFormIsEqual(directMessages);
  }

  public void testTwitterMethod() throws Exception {
    assertDeserializedFormIsSingleton(TwitterMethod.CREATE_LIST_MEMBER);
    assertDeserializedFormIsSingleton(TwitterMethod.BLOCK_LIST);
  }

  /**
   * @param obj the object to be asserted
   * @return the deserialized object
   * @throws Exception in the case the object is not (de)serializable
   */
  public static Object assertDeserializedFormIsEqual(Object obj) throws Exception {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(byteOutputStream);
    oos.writeObject(obj);
    byteOutputStream.close();
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(byteInputStream);
    Object that = ois.readObject();
    byteInputStream.close();
    ois.close();
    Assert.assertEquals(obj, that);
    return that;
  }

  /**
   * @param obj the object to be asserted
   * @return the deserialized object
   * @throws Exception in the case the object is not (de)serializable
   */
  public static Object assertDeserializedFormIsSingleton(Object obj) throws Exception {
    Object that = assertDeserializedFormIsEqual(obj);
    Assert.assertTrue(obj == that);
    return that;
  }

  public void testStatusJSONImplSupportsMoreThan100RetweetedStatus() throws Exception {
    UserJSONImpl user =
        new UserJSONImpl(new JSONObject(getStringFromClassPath("/dao/24696018620.json")));
    Assert.assertNotNull(user.getStatus());
    Assert.assertNotNull(user.getStatus().getRetweetCount());
  }
}
Beispiel #10
0
 /** Creates a TwitterFactory with the root configuration. */
 public TwitterFactory() {
   this(ConfigurationContext.getInstance());
 }
Beispiel #11
0
/**
 * A factory class for Twitter. <br>
 * An instance of this class is completely thread safe and can be re-used and used concurrently.
 *
 * @author Yusuke Yamamoto - yusuke at mac.com
 * @since Twitter4J 2.1.0
 */
@SuppressWarnings("unchecked")
public final class TwitterFactory implements java.io.Serializable {
  private static final Constructor<Twitter> TWITTER_CONSTRUCTOR;
  /*AsyncTwitterFactory and TWitterStream will access this field*/
  static final Authorization DEFAULT_AUTHORIZATION =
      AuthorizationFactory.getInstance(ConfigurationContext.getInstance());
  private static final Twitter SINGLETON;
  private static final long serialVersionUID = 5193900138477709155L;
  private final Configuration conf;

  static {
    String className = null;
    if (ConfigurationContext.getInstance().isGAE()) {
      final String APP_ENGINE_TWITTER_IMPL = "twitter4j.AppEngineTwitterImpl";
      try {
        Class.forName(APP_ENGINE_TWITTER_IMPL);
        className = APP_ENGINE_TWITTER_IMPL;
      } catch (ClassNotFoundException ignore) {
      }
    }
    if (className == null) {
      className = "twitter4j.TwitterImpl";
    }
    Constructor<Twitter> constructor;
    Class<?> clazz;
    try {
      clazz = Class.forName(className);
      constructor =
          (Constructor<Twitter>)
              clazz.getDeclaredConstructor(Configuration.class, Authorization.class);
    } catch (NoSuchMethodException e) {
      throw new AssertionError(e);
    } catch (ClassNotFoundException e) {
      throw new AssertionError(e);
    }
    TWITTER_CONSTRUCTOR = constructor;

    try {
      SINGLETON =
          TWITTER_CONSTRUCTOR.newInstance(
              ConfigurationContext.getInstance(), DEFAULT_AUTHORIZATION);
    } catch (InstantiationException e) {
      throw new AssertionError(e);
    } catch (IllegalAccessException e) {
      throw new AssertionError(e);
    } catch (InvocationTargetException e) {
      throw new AssertionError(e);
    }
  }

  /** Creates a TwitterFactory with the root configuration. */
  public TwitterFactory() {
    this(ConfigurationContext.getInstance());
  }

  /**
   * Creates a TwitterFactory with the given configuration.
   *
   * @param conf the configuration to use
   * @since Twitter4J 2.1.1
   */
  public TwitterFactory(Configuration conf) {
    if (conf == null) {
      throw new NullPointerException("configuration cannot be null");
    }
    this.conf = conf;
  }

  /**
   * Creates a TwitterFactory with a specified config tree
   *
   * @param configTreePath the path
   */
  public TwitterFactory(String configTreePath) {
    this(ConfigurationContext.getInstance(configTreePath));
  }

  /**
   * Returns a instance associated with the configuration bound to this factory.
   *
   * @return default singleton instance
   */
  public Twitter getInstance() {
    return getInstance(AuthorizationFactory.getInstance(conf));
  }

  /**
   * Returns a OAuth Authenticated instance.<br>
   * consumer key and consumer Secret must be provided by twitter4j.properties, or system
   * properties.<br>
   * Unlike {@link Twitter#setOAuthAccessToken(twitter4j.auth.AccessToken)}, this factory method
   * potentially returns a cached instance.
   *
   * @param accessToken access token
   * @return an instance
   * @since Twitter4J 2.1.9
   */
  public Twitter getInstance(AccessToken accessToken) {
    String consumerKey = conf.getOAuthConsumerKey();
    String consumerSecret = conf.getOAuthConsumerSecret();
    if (null == consumerKey && null == consumerSecret) {
      throw new IllegalStateException("Consumer key and Consumer secret not supplied.");
    }
    OAuthAuthorization oauth = new OAuthAuthorization(conf);
    oauth.setOAuthAccessToken(accessToken);
    return getInstance(oauth);
  }

  public Twitter getInstance(Authorization auth) {
    try {
      return TWITTER_CONSTRUCTOR.newInstance(conf, auth);
    } catch (InstantiationException e) {
      throw new AssertionError(e);
    } catch (IllegalAccessException e) {
      throw new AssertionError(e);
    } catch (InvocationTargetException e) {
      throw new AssertionError(e);
    }
  }

  /**
   * Returns default singleton Twitter instance.
   *
   * @return default singleton Twitter instance
   * @since Twitter4J 2.2.4
   */
  public static Twitter getSingleton() {
    return SINGLETON;
  }
}
Beispiel #12
0
 /**
  * Creates a TwitterFactory with a specified config tree
  *
  * @param configTreePath the path
  */
 public TwitterFactory(String configTreePath) {
   this(ConfigurationContext.getInstance(configTreePath));
 }
 RequestToken(HttpResponse httpresponse, OAuthSupport oauthsupport) {
   super(httpresponse);
   conf = ConfigurationContext.getInstance();
   oauth = oauthsupport;
 }
 RequestToken(String s, String s1, OAuthSupport oauthsupport) {
   super(s, s1);
   conf = ConfigurationContext.getInstance();
   oauth = oauthsupport;
 }
 public RequestToken(String s, String s1) {
   super(s, s1);
   conf = ConfigurationContext.getInstance();
 }