]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestDirectoryWatch.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestDirectoryWatch.java
1 /*******************************************************************************
2  * Copyright (c) 2010- Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  * 
9  * Contributors:
10  *    VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.tests;
13
14 import java.io.File;
15 import java.io.FileFilter;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Random;
19
20 import junit.framework.TestCase;
21
22 import org.simantics.databoard.accessor.impl.DirectoryWatch;
23 import org.simantics.databoard.accessor.impl.DirectoryWatch.DirectoryEvent;
24 import org.simantics.databoard.accessor.impl.DirectoryWatch.DirectoryListener;
25
26 /**
27  * This test case tests Wathc Servlet.
28  * 
29  * It creates a random named directory under $TMP$
30  * There it 
31  *
32  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
33  */
34 public class TestDirectoryWatch extends TestCase {
35
36         File tmpDir;
37         
38         public static File createTmpDir()
39         {
40                 String tmp = System.getenv("tmp");
41                 if (tmp==null) tmp = "c:/temp";
42                 Random r = new Random();
43                 String randomName = "tmp-"+(r.nextInt(10000)+10000);
44                 File tmpDir = new File(tmp+"/"+randomName);
45                 Boolean ok = tmpDir.mkdirs();
46                 assertTrue( ok );
47                 return tmpDir;
48         }
49         
50         public void setUp() throws Exception {
51                 tmpDir = createTmpDir();
52         }
53         
54         @Override
55         protected void tearDown() throws Exception {
56                 boolean ok = tmpDir.delete();
57                 if (!ok) System.err.println(tmpDir+" was not removed.");
58         }       
59         
60         public void testAll() 
61         throws Exception {
62                 
63                 FileFilter filter = new FileFilter() {
64                         public boolean accept(File pathname) {                          
65                                 return pathname.getAbsolutePath().toLowerCase().endsWith(".dbb");
66                         }};
67                         
68                 DirectoryWatch monitor = new DirectoryWatch(tmpDir, filter);
69                 
70                 final DirectoryEvent result = new DirectoryEvent();
71                 
72                 // Set listener that collects changes
73                 monitor.addListener(new DirectoryListener() {
74                         @Override
75                         public void onWatchEvent(DirectoryEvent e) {
76                                 result.filesAdded.addAll(e.filesAdded);
77                                 result.filesRemoved.addAll(e.filesRemoved);
78                         }});
79
80                 // Create file names
81                 List<File> files = new ArrayList<File>();
82                 for (int i=1; i<10; i++) {
83                         files.add( new File(tmpDir, "file-"+i+".dbb") );
84                 }
85                 
86                 // Create files
87                 for (File f : files)
88                 {
89                         f.createNewFile();
90                 }               
91                 Thread.sleep( DirectoryWatch.POLL_INTERVAL*2 );         
92                 assertTrue( files.containsAll( result.filesAdded ) );
93                 assertTrue( result.filesAdded.containsAll( files ) );
94                 assertTrue( result.filesRemoved.isEmpty() );            
95                 result.filesAdded.clear();
96                 
97                 // Delete files
98                 for (File f : files)
99                         f.delete();
100                 Thread.sleep( DirectoryWatch.POLL_INTERVAL*2 );         
101                 assertTrue( files.containsAll( result.filesRemoved ) );
102                 assertTrue( result.filesRemoved.containsAll( files ) );
103                 assertTrue( result.filesAdded.isEmpty() );              
104                 result.filesRemoved.clear();
105                 
106                 monitor.close();                
107         }
108         
109 }
110