OSDN Git Service

Configクラスをパッケージ移動
[coroid/NicoBrowser.git] / test / nicobrowser / config / ConfigTest.java
1 /*$Id$*/
2 package nicobrowser.config;
3
4 import java.io.File;
5 import java.io.IOException;
6 import java.lang.reflect.Field;
7 import java.util.Arrays;
8 import java.util.List;
9 import nicobrowser.config.Config.NicoFeed;
10 import org.apache.commons.io.FileUtils;
11 import org.junit.After;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import static org.junit.Assert.*;
15
16 public class ConfigTest {
17
18     private static File APP_HOME;
19     private static File CONFIG_FILE;
20     private static File FEEDURL_FILE;
21     private final File TEST_PROPERTY_FILE;
22     private final File TEST_PROPERTY_FILE_WAIT;
23     private final File TEST_FEED_FILE;
24
25     public ConfigTest() {
26         TEST_PROPERTY_FILE = new File("test/testdata/nicobrowser.properties");
27         TEST_PROPERTY_FILE_WAIT = new File("testdata/waittimeconfig/nicobrowser.properties");
28         TEST_FEED_FILE = new File("test/testdata/feedurl.txt");
29     }
30
31     @BeforeClass
32     public static void setUpClass() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
33         try {
34             Field f;
35             f = Config.class.getDeclaredField("APP_HOME");
36             f.setAccessible(true);
37             APP_HOME = (File) f.get(null);
38
39             f = Config.class.getDeclaredField("CONFIG_FILE");
40             f.setAccessible(true);
41             CONFIG_FILE = (File) f.get(null);
42
43             f = Config.class.getDeclaredField("FEEDURL_FILE");
44             f.setAccessible(true);
45             FEEDURL_FILE = (File) f.get(null);
46
47             if (APP_HOME.exists()) {
48                 String message = "ディレクトリを削除/移動してから再実行してください: " + APP_HOME;
49                 System.err.println(message);
50                 throw new Error(message);
51             }
52         } catch (Throwable th) {
53             throw new Error(th);
54         }
55     }
56
57     @After
58     public void tearDown() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
59         // コンフィグディレクトリを削除
60         FileUtils.deleteQuietly(APP_HOME);
61
62         // コンフィグインスタンスを削除.
63         Field f = Config.class.getDeclaredField("instance");
64         f.setAccessible(true);
65         f.set(null, null);
66     }
67
68     /**
69      * Test of getInstance method, of class Config.
70      */
71     @Test
72     public void testGetInstance() throws IOException {
73         System.out.println("getInstance");
74
75         try {
76             Config.getInstance();
77             fail("コンフィグファイル未作成の場合は例外発生");
78         } catch (Exception ex) {
79         }
80
81         Config.createNewConfigFiles();
82         assertNotNull(Config.getInstance());
83
84     }
85
86     /**
87      * Test of createNewConfigFiles method, of class Config.
88      */
89     @Test
90     public void testCreateNewConfigFiles() throws Exception {
91         System.out.println("createNewConfigFiles");
92         boolean result;
93
94         result = Config.createNewConfigFiles();
95         assertTrue("コンフィグが無いので新規作成される", result);
96         assertTrue(CONFIG_FILE.isFile());
97         assertTrue(FEEDURL_FILE.isFile());
98
99         result = Config.createNewConfigFiles();
100         assertFalse("作成済みなので作成されない", result);
101
102         CONFIG_FILE.delete();
103         assertFalse(CONFIG_FILE.exists());
104         result = Config.createNewConfigFiles();
105         assertTrue("コンフィグファイルだけ無くてもtrue", result);
106         assertTrue(CONFIG_FILE.isFile());
107
108         FEEDURL_FILE.delete();
109         assertFalse(FEEDURL_FILE.exists());
110         result = Config.createNewConfigFiles();
111         assertTrue("フィードファイルだけ無くてもtrue", result);
112         assertTrue(FEEDURL_FILE.isFile());
113     }
114
115     /**
116      * 初期作成コンフィグの妥当性をテストする.
117      */
118     @Test
119     public void testConfigInitialMake() throws IOException {
120         System.out.println("testConfigInitialMake");
121         Config.createNewConfigFiles();
122
123         Config conf = Config.getInstance();
124         assertEquals(new File(APP_HOME, "db/nicodb").getAbsolutePath(), conf.getDbFile());
125         assertEquals(new File(APP_HOME, "flv").getAbsolutePath(), conf.getSrcSaveDir());
126     }
127
128     /**
129      * コンフィグ初期生成を行うためのヘルパーメソッド.
130      */
131     private void initConfig() {
132         initConfig(TEST_PROPERTY_FILE);
133     }
134
135     private void initConfig(File config) {
136         try {
137             Config.createNewConfigFiles();
138             FileUtils.copyFile(config, CONFIG_FILE);
139             FileUtils.copyFile(TEST_FEED_FILE, FEEDURL_FILE);
140         } catch (Exception ex) {
141             throw new RuntimeException(ex);
142         }
143     }
144
145     /**
146      * Test of getNicoMail method, of class Config.
147      */
148     @Test
149     public void testGetNicoMail() {
150         System.out.println("getNicoMail");
151         initConfig();
152
153         String result = Config.getInstance().getNicoMail();
154         assertEquals("my@mail.address", result);
155     }
156
157     /**
158      * Test of getNicoPassword method, of class Config.
159      */
160     @Test
161     public void testGetNicoPassword() {
162         System.out.println("getNicoPassword");
163         initConfig();
164
165         String result = Config.getInstance().getNicoPassword();
166         assertEquals("my_password", result);
167     }
168
169     /**
170      * Test of getDbFile method, of class Config.
171      */
172     @Test
173     public void testGetDbFile() {
174         System.out.println("getDbFile");
175         initConfig();
176
177         String result = Config.getInstance().getDbFile();
178         assertEquals("C:\\Documents and Settings\\test\\.nicobrowser\\db\\nicodb", result);
179     }
180
181     /**
182      * Test of getSrcSaveDir method, of class Config.
183      */
184     @Test
185     public void testGetSrcSaveDir() {
186         System.out.println("getSrcSaveDir");
187         initConfig();
188
189         String result = Config.getInstance().getSrcSaveDir();
190         assertEquals("d:\\test", result);
191     }
192
193     /**
194      * Test of getEncoding method, of class Config.
195      */
196     @Test
197     public void testGetEncoding() {
198         System.out.println("getEncoding");
199         initConfig();
200
201         String result = Config.getInstance().getEncoding();
202         assertEquals("UTF-8", result);
203     }
204
205     /**
206      * Test of getMaxRetry method, of class Config.
207      */
208     @Test
209     public void testGetMaxRetry() {
210         System.out.println("getMaxRetry");
211         initConfig();
212
213         int result = Config.getInstance().getMaxRetry();
214         assertEquals(3, result);
215     }
216
217     /**
218      * Test of getDownLoadMyList method, of class Config.
219      */
220     @Test
221     public void testGetDownLoadMyList() {
222         System.out.println("getDownLoadMyList");
223         initConfig();
224
225         List<String> result = Config.getInstance().getDownLoadMyList();
226         List expected = Arrays.asList(new String[]{"100", "200", "300"});
227         assertEquals(expected, result);
228     }
229
230     @Test
231     public void testGetFeeds() {
232         System.out.println("testGetFeeds");
233         initConfig();
234
235         List<Config.NicoFeed> feeds = Config.getInstance().getNicoFeeds();
236         assertEquals(5, feeds.size());
237
238         NicoFeed feed = feeds.get(2);
239         assertEquals("http://www.nicovideo.jp/tag/科学?sort=f&rss=2.0", feed.getUrl());
240         assertEquals(3, feed.getNumber());
241
242         for (Config.NicoFeed nf : feeds) {
243             System.out.println(nf);
244         }
245     }
246
247     /**
248      * Test of getWaitTime method, of class Config.
249      */
250     @Test
251     public void testGetWaitTime() {
252         System.out.println("getWaitTime");
253         initConfig();
254
255         Config config = Config.getInstance();
256         int res = config.getWaitTime();
257         assertEquals(15, res);
258     }
259
260     @Test
261     public void testGetWaitTimeWithSetting() {
262         System.out.println("getWaitTimeWithSetting");
263         initConfig(TEST_PROPERTY_FILE_WAIT);
264
265         Config config = Config.getInstance();
266         int res = config.getWaitTime();
267         assertEquals(20, res);
268     }
269 }