KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > store > LocalHistoryStoreTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.localhistory.store;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.DataInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.modules.localhistory.utils.FileUtils;
35
36 /**
37 */

38 public class LocalHistoryStoreTest extends NbTestCase {
39     
40     private static int DELETED = 0;
41     private static int TOUCHED = 1;
42     
43     public LocalHistoryStoreTest(String JavaDoc testName) {
44         super(testName);
45         System.setProperty("netbeans.user", getDataDir().getAbsolutePath());
46     }
47
48     public void testFileCreate() throws Exception JavaDoc {
49         cleanUpStore();
50         
51         LocalHistoryStore store = LocalHistoryStoreFactory.getInstance().createLocalHistoryStorage();
52         long ts = System.currentTimeMillis();
53         
54         // create file1 in store
55
File JavaDoc file = new File JavaDoc(getDataDir(), "file1");
56         createFile(store, file, ts, "data");
57         // is it there?
58
checkFile(file, ts, -1, 2, 1, "data", TOUCHED);
59         
60         File JavaDoc storefile = getStoreFile(file, ts);
61         // try again
62
// if(store.lastModified(file) != ts) {
63
// createFile(store, file, ts, "data");
64
// }
65
// no changes ?
66
checkFile(file, ts, storefile.lastModified(), 2, 1, "data", TOUCHED);
67
68         // create file2 in store
69
file = new File JavaDoc(getDataDir(), "file2");
70         createFile(store, file, ts, "data");
71         // is it there?
72
checkFile(file, ts, -1, 2, 1, "data", TOUCHED);
73         
74         // check the whole repository
75
// File mainDir = getStoreMainFolder();
76
// File[] files = mainDir.listFiles();
77
// assertTrue(files != null && files.length == 2);
78
// files = files[0].listFiles();
79
// assertTrue(files != null && files.length == 1);
80
// files = files[1].listFiles();
81
// assertTrue(files != null && files.length == 1);
82

83         // create file in store
84
File JavaDoc folder = new File JavaDoc(getDataDir(), "folder");
85         ts = System.currentTimeMillis();
86         // create folder
87
createFile(store, folder, ts, null);
88         // is it there?
89
checkFile(folder, ts, -1, 1, 1, null, TOUCHED);
90         
91         file = new File JavaDoc(folder, "file2");
92         createFile(store, file, ts, "data");
93         // is it there?
94
checkFile(file, ts, -1, 2, 2, "data", TOUCHED);
95         //File parentFile = file.getParentFile();
96
//checkParent(parentFile, ts, 2);
97

98         // one more file in folder
99
file = new File JavaDoc(folder, "file3");
100         createFile(store, file, ts, "data");
101         checkFile(file, ts, -1, 2, 2, "data", TOUCHED);
102         //checkParent(parentFile, ts, 3);
103

104         // XXX check parent journal
105
}
106
107     public void testFileChange() throws Exception JavaDoc {
108         cleanUpStore();
109         
110         LocalHistoryStore store = LocalHistoryStoreFactory.getInstance().createLocalHistoryStorage();
111         long ts = System.currentTimeMillis();
112         
113         // create file in store
114
File JavaDoc file = new File JavaDoc(getDataDir(), "file1");
115         createFile(store, file, ts, "data");
116         
117         File JavaDoc storefile = getStoreFile(file, ts);
118         // change file with same ts
119
// XXX
120
// if(store.lastModified(file) != ts) {
121
// changeFile(store, file, ts, "data2");
122
// }
123
// check that nothing changed
124
checkFile(file, ts, storefile.lastModified(), 2, 1, "data", TOUCHED);
125         
126         // change file with new ts
127
ts = System.currentTimeMillis();
128         changeFile(store, file, ts, "data2");
129         // check the change
130
checkFile(file, ts, -1, 3, 1, "data2", TOUCHED);
131     }
132
133     public void testFileDelete() throws Exception JavaDoc {
134         cleanUpStore();
135         
136         LocalHistoryStore store = LocalHistoryStoreFactory.getInstance().createLocalHistoryStorage();
137         long ts = System.currentTimeMillis();
138         
139         // create file in store
140
File JavaDoc file = new File JavaDoc(getDataDir(), "file1");
141         createFile(store, file, ts, "data");
142         
143         store.fileDelete(file, ts);
144         // check
145
File JavaDoc storefile = getStoreFile(file, ts);
146         checkFile(file, ts, storefile.lastModified(), 2, 1, "data", DELETED);
147         
148         file = new File JavaDoc(getDataDir(), "file2");
149         createFile(store, file, ts, "data");
150         
151         store.fileDelete(file, ts);
152         // check
153
storefile = getStoreFile(file, ts);
154         checkFile(file, ts, storefile.lastModified(), 2, 1, "data", DELETED);
155     }
156     
157     public void testRevertTo() throws Exception JavaDoc {
158         cleanUpStore();
159         cleanUpDataFolder();
160         
161         LocalHistoryStore store = LocalHistoryStoreFactory.getInstance().createLocalHistoryStorage();
162
163         // check for deleted root folder
164
File JavaDoc folder = new File JavaDoc(getDataDir(), "datafolder");
165         setupFirstFolderToRevert(store, folder);
166         
167         File JavaDoc[] files = folder.listFiles();
168         assertEquals(files.length, 7); // fileNotInStorage
169
// fileUnchanged
170
// fileChangedAfterRevert
171
// X fileDeletedAfterRevert
172
// fileDeletedBeforeRevert
173
// fileUndeletedBeforeRevert
174
// fileCreatedToLate
175
// folderCreatedAfterRevert
176

177         store.fileDelete(folder, System.currentTimeMillis());
178         Thread.sleep(1000); // give me some time
179
long revertToTS = System.currentTimeMillis();
180         
181         StoreEntry[] entries = store.getFolderState(folder, files , revertToTS);
182         assertEquals(entries.length, 0); // all are deleted
183

184         
185         cleanUpStore();
186         cleanUpDataFolder();
187         folder = new File JavaDoc(getDataDir(), "datafolder");
188         revertToTS = setupFirstFolderToRevert(store, folder);
189         files = folder.listFiles();
190         files = folder.listFiles();
191         assertEquals(files.length, 7); // fileNotInStorage
192
// fileUnchanged
193
// fileChangedAfterRevert
194
// X fileDeletedAfterRevert
195
// fileDeletedBeforeRevert
196
// fileUndeletedBeforeRevert
197
// fileCreatedToLate
198

199                                         // X folderDeletedAfterRevert
200
// folderCreatedAfterRevert
201

202        
203         entries = store.getFolderState(folder, files , revertToTS);
204
205         for(StoreEntry se : entries) {
206             System.out.println(se.getFile().getName());
207         }
208         
209         assertEquals(entries.length, 8);
210         // * returned, X as to be deleted
211
// fileNotInStorage -
212
//* fileUnchanged - *
213
//* fileChangedAfterRevert - * previous revision
214
//* fileDeletedAfterRevert - *
215
//* fileUndeletedBeforeRevert - *
216
// X fileCreatedAfterRevert - * X
217
//* X fileDeletedBeforeRevert - * X
218

219         //* folderDeletedAfterRevert - *
220
// folderCreatedAfterRevert - * X
221

222         Map JavaDoc<String JavaDoc, StoreEntry> entriesMap = new HashMap JavaDoc<String JavaDoc, StoreEntry>();
223         for(StoreEntry se : entries) {
224             entriesMap.put(se.getFile().getName(), se);
225         }
226         assertNull(entriesMap.get("fileNotInStorage"));
227         
228         assertNotNull(entriesMap.get("fileUnchanged"));
229         assertNotNull(entriesMap.get("fileChangedAfterRevert"));
230         assertNotNull(entriesMap.get("fileDeletedAfterRevert"));
231         assertNotNull(entriesMap.get("fileUndeletedBeforeRevert"));
232         assertNotNull(entriesMap.get("fileCreatedAfterRevert"));
233         assertNotNull(entriesMap.get("fileDeletedBeforeRevert"));
234         assertNotNull(entriesMap.get("folderDeletedAfterRevert"));
235         assertNotNull(entriesMap.get("folderCreatedAfterRevert"));
236         
237         assertNotNull(entriesMap.get("fileUnchanged").getStoreFile());
238         assertNotNull(entriesMap.get("fileChangedAfterRevert").getStoreFile());
239         assertNotNull(entriesMap.get("fileDeletedAfterRevert").getStoreFile());
240         assertNotNull(entriesMap.get("fileUndeletedBeforeRevert").getStoreFile());
241         assertNotNull(entriesMap.get("folderDeletedAfterRevert").getStoreFile());
242         assertNull(entriesMap.get("fileCreatedAfterRevert").getStoreFile());
243         assertNull(entriesMap.get("fileDeletedBeforeRevert").getStoreFile());
244         assertNull(entriesMap.get("folderCreatedAfterRevert").getStoreFile());
245         
246         String JavaDoc strStore = read(entriesMap.get("fileChangedAfterRevert").getStoreFileInputStream(), 1024);
247         String JavaDoc strFile = read(new FileInputStream JavaDoc(entriesMap.get("fileChangedAfterRevert").getFile()), 1024);
248         assertNotSame(strFile, strStore);
249     }
250     
251     private long setupFirstFolderToRevert(LocalHistoryStore store, File JavaDoc folder) throws Exception JavaDoc {
252         
253         File JavaDoc fileNotInStorage = new File JavaDoc(folder, "fileNotInStorage");
254         File JavaDoc fileUnchanged = new File JavaDoc(folder, "fileUnchanged");
255         File JavaDoc fileChangedAfterRevert = new File JavaDoc(folder, "fileChangedAfterRevert");
256         File JavaDoc fileDeletedAfterRevert = new File JavaDoc(folder, "fileDeletedAfterRevert");
257         File JavaDoc fileDeletedBeforeRevert = new File JavaDoc(folder, "fileDeletedBeforeRevert");
258         File JavaDoc fileUndeletedBeforeRevert = new File JavaDoc(folder, "fileUndeletedBeforeRevert");
259         File JavaDoc fileCreatedAfterRevert = new File JavaDoc(folder, "fileCreatedAfterRevert");
260         
261         File JavaDoc folderDeletedAfterRevert = new File JavaDoc(folder, "folderDeletedAfterRevert");
262         File JavaDoc folderCreatedAfterRevert = new File JavaDoc(folder, "folderCreatedAfterRevert");
263         
264         createFile(store, folder, System.currentTimeMillis(), null);
265         write(fileNotInStorage, "fileNotInStorage".getBytes());
266         createFile(store, fileUnchanged, System.currentTimeMillis(), "fileUnchanged");
267         createFile(store, fileChangedAfterRevert, System.currentTimeMillis(), "fileChangedAfterRevert BEFORE change");
268         createFile(store, fileDeletedAfterRevert, System.currentTimeMillis(), "fileDeletedAfterRevert BEFORE delete");
269         createFile(store, fileDeletedBeforeRevert, System.currentTimeMillis(), "fileDeletedBeforeRevert BEFORE delete");
270         createFile(store, fileUndeletedBeforeRevert, System.currentTimeMillis(), "fileUndeletedBeforeRevert");
271         
272         createFile(store, folderDeletedAfterRevert, System.currentTimeMillis(), null);
273         
274                 
275         
276         fileDeletedBeforeRevert.delete();
277         store.fileDelete(fileDeletedBeforeRevert, System.currentTimeMillis());
278         
279         fileUndeletedBeforeRevert.delete();
280         store.fileDelete(fileUndeletedBeforeRevert, System.currentTimeMillis());
281         createFile(store, fileUndeletedBeforeRevert, System.currentTimeMillis(), "fileUndeletedBeforeRevert BEFORE revert");
282         
283         // REVERT
284
Thread.sleep(1000); // give me some time
285
long revertToTS = System.currentTimeMillis();
286         Thread.sleep(1000); // give me some time
287
// REVERT
288

289         changeFile(store, fileChangedAfterRevert, System.currentTimeMillis(), "fileChanged AFTER change");
290                 
291         fileDeletedAfterRevert.delete();
292         store.fileDelete(fileDeletedAfterRevert, System.currentTimeMillis());
293         
294         createFile(store, fileDeletedBeforeRevert, System.currentTimeMillis(), "fileDeletedBeforeRevert after delete");
295         
296         createFile(store, fileCreatedAfterRevert, System.currentTimeMillis(), "fileCreatedAfterRevert");
297         
298         folderDeletedAfterRevert.delete();
299         store.fileDelete(folderDeletedAfterRevert, System.currentTimeMillis());
300         
301         createFile(store, folderCreatedAfterRevert, System.currentTimeMillis(), null);
302         
303                 
304         // check datadir
305
assertTrue(folder.exists());
306         assertTrue(fileNotInStorage.exists());
307         assertTrue(fileUnchanged.exists());
308         assertTrue(fileChangedAfterRevert.exists());
309         assertTrue(!fileDeletedAfterRevert.exists());
310         assertTrue(fileDeletedBeforeRevert.exists());
311         assertTrue(fileCreatedAfterRevert.exists());
312         assertTrue(!folderDeletedAfterRevert.exists());
313         assertTrue(folderCreatedAfterRevert.exists());
314         
315         File JavaDoc[] files = folder.listFiles();
316         assertEquals(files.length, 7); // fileNotInStorage
317
// fileUnchanged
318
// fileChangedAfterRevert
319
// X fileDeletedAfterRevert
320
// fileDeletedBeforeRevert
321
// fileUndeletedBeforeRevert
322
// fileCreatedAfterRevert
323
// folderCreatedAfterRevert
324

325         return revertToTS;
326     }
327     
328     private void checkFile(File JavaDoc file, long ts, long storeFileLastModified, int siblings, int parentChildren, String JavaDoc data, int action) throws Exception JavaDoc {
329         
330         File JavaDoc storeFolder = getStoreFolder(file);
331         String JavaDoc[] files = storeFolder.list();
332         assertTrue(files != null && files.length == siblings);
333         
334         File JavaDoc storeParent = getStoreFolder(file.getParentFile());
335         files = storeParent.list();
336         assertTrue(files != null && files.length == parentChildren);
337         
338         if(file.isFile()) {
339             File JavaDoc storefile = getStoreFile(file, ts);
340             assertTrue(storefile.exists());
341             if(data != null) assertTrue(containsData(storefile, data.getBytes()));
342         }
343         
344         File JavaDoc dataFle = getDataFile(file);
345         assertTrue(dataFle.exists());
346         assertTrue(contains(dataFle, file.isFile(), action, ts, file.getAbsolutePath()));
347         if(storeFileLastModified!=-1) assertTrue(storeFileLastModified == storeFolder.lastModified());
348     }
349
350 // private void checkParent(File file, long ts, int children) throws Exception {
351
// File storeFolder = getStoreFolder(file);
352
// assertTrue(storeFolder.exists());
353
//
354
// String[] files = storeFolder.list();
355
// assertTrue(files != null && files.length == children);
356
// }
357

358     private void createFile(LocalHistoryStore store, File JavaDoc file, long ts, String JavaDoc data) throws Exception JavaDoc {
359         if(data != null) {
360             write(file, data.getBytes());
361         } else {
362             file.mkdirs();
363         }
364         store.fileCreate(file, ts);
365     }
366     
367     private void changeFile(LocalHistoryStore store, File JavaDoc file, long ts, String JavaDoc data) throws Exception JavaDoc {
368         write(file, data.getBytes());
369         store.fileChange(file, ts);
370     }
371         
372     private void write(File JavaDoc file, byte[] data) throws Exception JavaDoc {
373         BufferedOutputStream JavaDoc bos = null;
374         try {
375             bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
376             bos.write(data);
377         } catch(Exception JavaDoc e) {
378             throw e;
379         } finally {
380             if(bos != null) { bos.close(); };
381         }
382     }
383
384     private String JavaDoc read(InputStream JavaDoc is, int length) throws Exception JavaDoc {
385         try {
386             byte[] buffer = new byte[length];
387             int len = is.read(buffer);
388             return new String JavaDoc(java.util.Arrays.copyOfRange(buffer, 0, len));
389         } catch(Exception JavaDoc e) {
390             throw e;
391         } finally {
392             if(is != null) { is.close(); };
393         }
394     }
395     
396     private boolean contains(File JavaDoc file, boolean isFile, int action, long modified, String JavaDoc filePath) throws Exception JavaDoc {
397         DataInputStream JavaDoc dis = null;
398         try {
399             dis = new DataInputStream JavaDoc(new FileInputStream JavaDoc(file));
400             boolean f = dis.readBoolean();
401             int i = dis.readInt();
402             long l = dis.readLong();
403             int len = dis.readInt();
404             StringBuffer JavaDoc fileName = new StringBuffer JavaDoc();
405             while( len-- > 0 ) {
406                 char c = dis.readChar();
407                 fileName.append(c);
408             }
409             return f == isFile && modified == l && action == i && fileName.toString().equals(filePath);
410         } catch(Exception JavaDoc e) {
411             throw e;
412         } finally {
413             if(dis != null) { dis.close(); };
414         }
415     }
416     
417     private boolean containsData(File JavaDoc file, byte[] data) throws Exception JavaDoc {
418         BufferedInputStream JavaDoc bis = null;
419         try {
420             bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
421             byte[] contents = new byte[data.length];
422             int l = bis.read(contents);
423             if(l != data.length) return false;
424             for (int i = 0; i < contents.length; i++) {
425                 if(contents[i] != data[i]) return false;
426             }
427             return true;
428         } catch(Exception JavaDoc e) {
429             throw e;
430         } finally {
431             if(bis != null) { bis.close(); };
432         }
433     }
434     
435     private Method JavaDoc getStoreFolderMethod;
436     private Method JavaDoc getDataDileMethod;
437     private Method JavaDoc getStoreFileMethod;
438     private Method JavaDoc cleanUpImplMethod;
439     private Object JavaDoc storage;
440     
441     private File JavaDoc getStoreFolder(File JavaDoc file) throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
442         if(getStoreFolderMethod == null) {
443             getStoreFolderMethod = getStorage().getClass().getDeclaredMethod("getStoreFolder", new Class JavaDoc[] {File JavaDoc.class});
444             getStoreFolderMethod.setAccessible(true);
445         }
446         return (File JavaDoc) getStoreFolderMethod.invoke(storage, new Object JavaDoc[]{file});
447     }
448
449     private File JavaDoc getDataFile(File JavaDoc file) throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
450         if(getDataDileMethod == null) {
451             getDataDileMethod = getStorage().getClass().getDeclaredMethod("getDataFile", new Class JavaDoc[] {File JavaDoc.class});
452             getDataDileMethod.setAccessible(true);
453         }
454         return (File JavaDoc) getDataDileMethod.invoke(storage, new Object JavaDoc[]{file});
455     }
456     
457     private File JavaDoc getStoreFile(File JavaDoc file, long ts) throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
458         if(getStoreFileMethod == null) {
459             getStoreFileMethod = getStorage().getClass().getDeclaredMethod("getStoreFile", new Class JavaDoc[] {File JavaDoc.class, String JavaDoc.class, boolean.class});
460             getStoreFileMethod.setAccessible(true);
461         }
462         return (File JavaDoc) getStoreFileMethod.invoke(storage, new Object JavaDoc[]{file, Long.toString(ts), true});
463     }
464     
465     private Object JavaDoc getStorage() throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
466         if(storage == null) {
467             storage = LocalHistoryStoreFactory.getInstance().createLocalHistoryStorage();
468 // Class c = Class.forName("org.netbeans.modules.localhistory.store.LocalHistoryStoreImpl");
469
// Class impl = store.getClass().asSubclass(c);
470
// storage = impl.newInstance();
471
}
472         return storage;
473     }
474     
475     private File JavaDoc getStoreMainFolder() {
476         return new File JavaDoc(getDataDir(), "localhistory");
477     }
478
479     private void cleanUpDataFolder() {
480         FileUtils.deleteRecursively(getDataDir());
481     }
482     
483     private void cleanUpStore() throws Exception JavaDoc {
484         if(cleanUpImplMethod == null) {
485             cleanUpImplMethod = getStorage().getClass().getDeclaredMethod("cleanUpImpl", new Class JavaDoc[] {long.class});
486             cleanUpImplMethod.setAccessible(true);
487         }
488         cleanUpImplMethod.invoke(getStorage(), new Object JavaDoc[]{1});
489     }
490 }
491
Popular Tags