Changeset 12572

Show
Ignore:
Timestamp:
05/28/08 00:47:36 (5 years ago)
Author:
daisuke_m
Message:

テスト用モックAPI実装。SabotterPlugin#debug? = trueとすると、モックで起動。API制限を気にせずにテスト可能。

Location:
lang/java/sabotter/trunk/src/jp/xet/eclipse/sabotter
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • lang/java/sabotter/trunk/src/jp/xet/eclipse/sabotter/SabotterPlugin.java

    r12484 r12572  
    2424 */ 
    2525public class SabotterPlugin extends AbstractUIPlugin { 
     26         
     27        public static boolean debug = false; 
    2628         
    2729        /** The plug-in ID */ 
  • lang/java/sabotter/trunk/src/jp/xet/eclipse/sabotter/core/mock/SabotterEntryMock.java

    r12547 r12572  
    3535         * @param directMessage  
    3636         */ 
    37         public SabotterEntryMock(int id, SabotterUser user, String statusText, boolean directMessage) { 
     37        public SabotterEntryMock(int id, SabotterUser user, String statusText, boolean directMessage, Date date) { 
    3838                this.id = id; 
    3939                this.user = user; 
    4040                this.statusText = statusText; 
    41                 this.timeStamp = new Date(); 
     41                this.timeStamp = date; 
    4242                this.directMessage = directMessage; 
    4343        } 
  • lang/java/sabotter/trunk/src/jp/xet/eclipse/sabotter/core/mock/SabotterMock.java

    r12547 r12572  
    11package jp.xet.eclipse.sabotter.core.mock; 
    22 
     3import java.util.Date; 
    34import java.util.HashMap; 
    45import java.util.LinkedList; 
     
    67import java.util.Map; 
    78import java.util.Queue; 
     9import java.util.Random; 
    810 
    911import jp.xet.eclipse.sabotter.core.Sabotter; 
     
    1921public class SabotterMock implements Sabotter { 
    2022         
    21         private int nextId = 10000; 
    22          
    23         private SabotterUserMock user; 
     23        private int nextId = 1000; 
     24         
     25        private SabotterUser user; 
    2426         
    2527        private Queue<SabotterEntry> entries = new LinkedList<SabotterEntry>(); 
     
    3032         
    3133        private Map<String, SabotterUser> users = new HashMap<String, SabotterUser>(); 
     34         
     35        private Date prev; 
     36         
     37        private Random rand = new Random(); 
     38         
     39        private String[] usernames = new String[] { 
     40                "Sabotter", 
     41                "daisuke_m", 
     42                "yamashiro", 
     43                "Ewigkeit", 
     44                "ykhr", 
     45                "rairairou", 
     46                // 勝手にID使っちゃってサーセンw 
     47                "bose999", 
     48                "Yoshiori", 
     49                "akr", 
     50                "happy_ryo", 
     51                "t_yano", 
     52                "j5ik2o", 
     53                "isocchi" 
     54        }; 
     55         
     56        private String[] images = new String[] { 
     57                null, 
     58                "http://s3.amazonaws.com/twitter_production/profile_images/33800532/938877_3385895366_normal.jpg", 
     59                "http://s3.amazonaws.com/twitter_production/profile_images/54386600/babakonnga_normal.jpg", 
     60                "http://s3.amazonaws.com/twitter_production/profile_images/52059217/papaicon_normal.png", 
     61                "http://s3.amazonaws.com/twitter_production/profile_images/46283502/NEC_0002_normal.JPG", 
     62                "http://s3.amazonaws.com/twitter_production/profile_images/51556066/DrossHarvester_normal.jpg", 
     63                "http://s3.amazonaws.com/twitter_production/profile_images/52452303/bose999_normal.png", 
     64                "http://s3.amazonaws.com/twitter_production/profile_images/51585673/2_normal.jpg", 
     65                "http://s3.amazonaws.com/twitter_production/profile_images/51761519/yaya3bw_normal.png", 
     66                "http://s3.amazonaws.com/twitter_production/profile_images/52865200/nekobean_normal_s_normal.png", 
     67                "http://s3.amazonaws.com/twitter_production/profile_images/54653845/picture7_normal.jpg", 
     68                "http://s3.amazonaws.com/twitter_production/profile_images/53187335/icon_normal.png", 
     69                "http://s3.amazonaws.com/twitter_production/profile_images/39179002/NEC_0015_normal.jpg" 
     70        }; 
     71         
     72        private String[] msg = new String[] { 
     73                "foo", 
     74                "bar", 
     75                "buz", 
     76                "qux", 
     77                "quux", 
     78                "corge", 
     79                "grault", 
     80                "garply", 
     81                "waldo", 
     82                "fred", 
     83                "plugh", 
     84                "xyzzy", 
     85                "thud" 
     86        }; 
    3287         
    3388 
     
    3893         */ 
    3994        public SabotterMock(String username, String password) { 
    40                 this.user = new SabotterUserMock(username); 
     95                createUsers(username); 
     96                user = users.get(username); 
     97                if (user == null) { 
     98                        user = new SabotterUserMock(username, null); 
     99                        users.put(username, user); 
     100                } 
     101        } 
     102         
     103        private void createUsers(String username) { 
     104                for (int i = 0; i < usernames.length; i++) { 
     105                        String un = usernames[i]; 
     106                        String image = images[i]; 
     107                        if (un.equals(username) == false) { 
     108                                users.put(un, new SabotterUserMock(un, image)); 
     109                        } 
     110                } 
    41111        } 
    42112         
     
    55125        @SuppressWarnings("unchecked") 
    56126        public void getDirectMessagesAsync(SabotterListener listener) { 
     127                createDummyEntries(); 
    57128                sleep(); 
    58129                listener.gotFriendsTimeline((List) directMessages); 
     
    65136        @SuppressWarnings("unchecked") 
    66137        public void getFriendsTimelineAsync(SabotterListener listener) { 
     138                createDummyEntries(); 
    67139                sleep(); 
    68140                listener.gotFriendsTimeline((List) entries); 
     
    75147        @SuppressWarnings("unchecked") 
    76148        public void getRepliesAsync(SabotterListener listener) { 
     149                createDummyEntries(); 
    77150                sleep(); 
    78151                listener.gotFriendsTimeline((List) replies); 
     152        } 
     153         
     154        private void createDummyEntries() { 
     155                Date now = new Date(); 
     156                if (prev == null) { 
     157                        prev = new Date(now.getTime() - (10000 * 20)); 
     158                } 
     159                int num = (int) (now.getTime() - prev.getTime()) / 10000; 
     160                SabotterEntry entry; 
     161                for (int i = 0; i < num; i++) { 
     162                        entry = createDummyEntry(new Date(prev.getTime() + (10000 * i)), false); 
     163                        if (entry.isMessageToMe()) { 
     164                                add(replies, entry); 
     165                        } 
     166                        add(entries, entry); 
     167                } 
     168                if (rand.nextInt() % 10 == 0) { 
     169                        add(directMessages, createDummyEntry(new Date(now.getTime() - 10000), true)); 
     170                } 
     171                prev = now; 
     172        } 
     173         
     174        private SabotterEntry createDummyEntry(Date date, boolean directMessage) { 
     175                return new SabotterEntryMock(nextId++, pickUpUser(), createDummyMessage(directMessage), directMessage, date); 
     176        } 
     177         
     178        private String createDummyMessage(boolean directMessage) { 
     179                StringBuilder sb = new StringBuilder(); 
     180                if (directMessage == false && rand.nextInt() % 3 == 0) { 
     181                        sb.append("@").append(pickUpUser().getScreenName()).append(" "); 
     182                } 
     183                for (int i = 0; i < rand.nextInt(10) + 5; i++) { 
     184                        sb.append(msg[rand.nextInt(msg.length)]).append(" "); 
     185                } 
     186                String message = sb.toString(); 
     187                return message.substring(0, message.length() - 1); 
     188        } 
     189         
     190        private SabotterUser pickUpUser() { 
     191                return users.get(usernames[rand.nextInt(usernames.length)]); 
    79192        } 
    80193         
     
    94207                SabotterUser user = users.get(screenName); 
    95208                if (user == null) { 
    96                         user = new SabotterUserMock(screenName); 
     209                        user = new SabotterUserMock(screenName, null); 
    97210                        users.put(screenName, user); 
    98211                } 
     
    110223                        statusText = statusText.substring(2); 
    111224                } 
    112                 SabotterEntry entry = new SabotterEntryMock(nextId++, user, statusText, directMessage); 
     225                SabotterEntry entry = new SabotterEntryMock(nextId++, user, statusText, directMessage, new Date()); 
    113226                if (directMessage == false && statusText.startsWith("@" + user.getScreenName())) { 
    114227                        add(replies, entry); 
  • lang/java/sabotter/trunk/src/jp/xet/eclipse/sabotter/core/mock/SabotterUserMock.java

    r12547 r12572  
    1717        private String name; 
    1818         
     19        private URL profileImageURL; 
     20         
     21        private static URL defaultImageURL; 
     22         
     23        static { 
     24                try { 
     25                        defaultImageURL = new URL("http://static.twitter.com/images/default_profile_normal.png"); 
     26                } catch (MalformedURLException e) { 
     27                        e.printStackTrace(); 
     28                } 
     29        } 
     30         
    1931 
    2032        /** 
    2133         * コンストラクタ。 
    2234         * @param screenName 
     35         * @param profileImageURL  
    2336         */ 
    24         public SabotterUserMock(String screenName) { 
     37        public SabotterUserMock(String screenName, String profileImageURL) { 
    2538                this.screenName = screenName; 
    2639                this.name = "Name of " + screenName; 
     40                try { 
     41                        this.profileImageURL = new URL(profileImageURL); 
     42                } catch (MalformedURLException e) { 
     43                        this.profileImageURL = defaultImageURL; 
     44                } 
    2745        } 
    2846         
     
    4058         */ 
    4159        public URL getProfileImageURL() { 
    42                 try { 
    43                         return new URL("http://d.hatena.ne.jp/" + screenName); 
    44                 } catch (MalformedURLException e) { 
    45                         e.printStackTrace(); 
    46                 } 
    47                 return null; 
     60                return profileImageURL; 
    4861        } 
    4962         
  • lang/java/sabotter/trunk/src/jp/xet/eclipse/sabotter/manager/TwitterManager.java

    r12547 r12572  
    1313import jp.xet.eclipse.sabotter.core.SabotterListener; 
    1414import jp.xet.eclipse.sabotter.core.impl.SabotterImpl; 
     15import jp.xet.eclipse.sabotter.core.mock.SabotterMock; 
    1516import jp.xet.eclipse.sabotter.exception.SabotterException; 
    1617import jp.xet.eclipse.sabotter.preferences.PreferenceKeys; 
     
    9899                String password = ps.getString(PreferenceKeys.PASSWORD); 
    99100                 
    100                 twitterAPI = new SabotterImpl(username, password); // Twitter4j 
    101 //              twitterAPI = new SabotterMock(username, password); // テスト用Mock 
     101                if (SabotterPlugin.debug) { 
     102                        twitterAPI = new SabotterMock(username, password); // テスト用Mock 
     103                } else { 
     104                        twitterAPI = new SabotterImpl(username, password); // Twitter4j 
     105                } 
    102106        } 
    103107