File: VirtualFileTest.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (365 lines) | stat: -rw-r--r-- 13,562 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*

   Derby - Class org.apache.derbyTesting.unitTests.junit.VirtualFileTest

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

package org.apache.derbyTesting.unitTests.junit;

import java.io.FileNotFoundException;
import java.io.IOException;
import junit.framework.Test;
import org.apache.derby.impl.io.vfmem.DataStore;
import org.apache.derby.impl.io.vfmem.VirtualFile;
import org.apache.derby.io.StorageFile;
import org.apache.derby.io.StorageRandomAccessFile;
import org.apache.derbyTesting.junit.BaseTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;

/**
 * Basic tests of the class {@code VirtualFile}.
 */
public class VirtualFileTest
        extends BaseTestCase {

    private final String[] NON_EXISTING_DIRS = new String[] {
                "this", "dir", "does", "not", "exist"};

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

    public void testCreateFileInRoot() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile("service.properties", store);
        assertFalse(new VirtualFile("service.properties", store).exists());
        assertFalse(vFile.exists());
    }

    public void testCreateDirInRoot() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile("seg0", store);
        assertFalse(vFile.exists());
        vFile.mkdir();
        assertTrue(vFile.exists());
        assertTrue(vFile.isDirectory());
    }

    public void testCreateInvalidDir() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile(
                PathUtilTest.join(NON_EXISTING_DIRS),
                store);
        assertFalse(vFile.exists());
        VirtualFile tmp = new VirtualFile("", store);
        assertTrue(tmp.mkdir());
        assertFalse("Dir creation should have failed", vFile.mkdir());
    }

    public void testMkdirsValidRelative() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile(
                PathUtilTest.join(NON_EXISTING_DIRS),
                store);
        assertTrue(vFile.mkdirs());
    }

    public void testMkdirsValidAbsolute() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile(
                PathUtilTest.joinAbs(NON_EXISTING_DIRS),
                store);
        assertTrue(vFile.mkdirs());
    }

    public void testMkdirsInvalidAbsolute()
            throws IOException {
        DataStore store = getStore();
        VirtualFile tmp = new VirtualFile(PathUtilTest.abs("directory"), store);
        assertTrue(tmp.mkdir());
        tmp = new VirtualFile(
                PathUtilTest.joinAbs("directory", "afile"),
                store);
        assertTrue(tmp.createNewFile());
        assertTrue(tmp.exists());
        assertFalse(tmp.isDirectory());
        VirtualFile vFile = new VirtualFile(
                PathUtilTest.joinAbs("directory", "afile", "anotherdir"),
                store);
        assertFalse(vFile.mkdir());
        assertFalse(vFile.mkdirs());
    }

    public void testMkdirsInvalidRelative()
            throws IOException {
        DataStore store = getStore();
        VirtualFile tmp = new VirtualFile("seg0", store);
        assertTrue(tmp.mkdir());
        tmp = new VirtualFile(PathUtilTest.join("seg0", "datafile"), store);
        assertTrue(tmp.createNewFile());
        assertTrue(tmp.exists());
        assertFalse(tmp.isDirectory());
        VirtualFile vFile = new VirtualFile(
                PathUtilTest.join("seg0", "datafile", "anotherdir"), store);
        assertFalse(vFile.mkdir());
        assertFalse(vFile.mkdirs());
    }

    public void testGetParentRelative() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile(
                PathUtilTest.join(NON_EXISTING_DIRS), store);
        int count = 0;
        StorageFile parent = vFile.getParentDir();
        while (parent != null) {
            count++;
            parent = parent.getParentDir();
        }
        assertEquals(4, count);
    }

    public void testGetParentAbsolute() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile(
                PathUtilTest.joinAbs(NON_EXISTING_DIRS), store);
        int count = 0;
        StorageFile parent = vFile.getParentDir();
        while (parent != null) {
            count++;
            parent = parent.getParentDir();
        }
        assertEquals(5, count);
    }

    public void testDeleteAll()
            throws IOException {
        DataStore store = getStore();
        String[] dirs = new String[] {
            "seg0", PathUtilTest.join("seg0", "dir1"),
            "seg1", PathUtilTest.join("seg0", "dir2")};
        for (int i=0; i < dirs.length; i++) {
            assertTrue(new VirtualFile(dirs[i], store).mkdir());
        }
        String[] files = new String[] {
            PathUtilTest.join("seg0", "f1"),
            PathUtilTest.join("seg0", "dir1", "f1"),
            PathUtilTest.join("seg1", "f1"), PathUtilTest.join("seg0","f5")};
        for (int i=0; i < files.length; i++) {
            assertTrue(new VirtualFile(files[i], store).createNewFile());
        }
        String root = "seg0";
        VirtualFile rootToDelete = new VirtualFile(root, store);
        assertTrue(rootToDelete.deleteAll());
        for (int i=0; i < dirs.length; i++) {
            assertEquals(!dirs[i].startsWith(root),
                         new VirtualFile(dirs[i], store).exists());
        }
        for (int i=0; i < files.length; i++) {
            assertEquals(!files[i].startsWith(root),
                         new VirtualFile(files[i], store).exists());
        }
    }

    public void testRenameToSimple() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile("originalFile", store);
        assertFalse(vFile.canWrite());
        vFile.createNewFile();
        assertTrue(vFile.canWrite());
        VirtualFile newFile = new VirtualFile("newFile", store);
        assertFalse(newFile.exists());
        assertTrue(vFile.renameTo(newFile));
        assertFalse(vFile.exists());
        assertFalse(vFile.canWrite());
        assertTrue(newFile.exists());
    }

    /**
     * Getting a random access file in write mode for a non-existing file
     * should cause the file to be created.
     */
    public void testGetRAFNonExisting()
            throws FileNotFoundException {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile("aNewFile.txt", store);
        assertFalse(vFile.exists());
        StorageRandomAccessFile vRAF = vFile.getRandomAccessFile("rw");
        assertNotNull(vRAF);
        assertTrue(vFile.exists());
    }

    /**
     * Getting a random access file in read mode for a non-existing file
     * should fail, and the file shouldn't be created.
     */
    public void testGetRAFNonExistingReadMode()
            throws FileNotFoundException {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile("aNewFile.txt", store);
        assertFalse(vFile.exists());
        try {
            vFile.getRandomAccessFile("r");
            fail("Cannot read from a non-exsiting file");
        } catch (FileNotFoundException fnfe) {
            // Expected.
        }
        assertFalse(vFile.exists());
    }

    /**
     * Opens a random access file for a file which has been marked as read-only.
     * <p>
     * Opening for reading only should work, opening for writing should fail.
     */
    public void testGetRAExistingReadOnly()
            throws FileNotFoundException {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile("aNewFile.txt", store);
        assertFalse(vFile.exists());
        assertTrue(vFile.createNewFile());
        assertTrue(vFile.exists());
        assertTrue(vFile.setReadOnly());
        assertNotNull(vFile.getRandomAccessFile("r"));
        // Try opening in write mode, which should fail.
        try {
            vFile.getRandomAccessFile("rw");
            fail("Should not be able to open a read-only file in write mode");
        } catch (FileNotFoundException fnfe) {
            // Expected.
        }
    }

    /**
     * Opening a random access file on a directory should fail.
     */
    public void testGetRAFOnDirectory() {
        DataStore store = getStore();
        VirtualFile vFile = new VirtualFile("mydir", store);
        assertTrue(vFile.mkdir());
        assertTrue(vFile.exists());
        assertTrue(vFile.isDirectory());
        // Try opening in read mode.
        try {
            vFile.getRandomAccessFile("r");
            fail("Opening a RAF on a directory should have failed");
        } catch (FileNotFoundException fnfe) {
            // Expected.
        }
        // Try opening in write mode.
        try {
            vFile.getRandomAccessFile("r");
            fail("Opening a RAF on a directory should have failed");
        } catch (FileNotFoundException fnfe) {
            // Expected.
        }
        // A few sanity checks.
        assertTrue(vFile.exists());
        assertTrue(vFile.isDirectory());
    }

    /**
     * Tests that {@code listChildren} doesn't include too many entries.
     */
    public void testListChilderen() {
        DataStore store = getStore();
        VirtualFile dir1 = new VirtualFile(PathUtilTest.abs("mydir"), store);
        VirtualFile dir2 = new VirtualFile(
                PathUtilTest.abs("mydirectory"), store);
        VirtualFile file1 = new VirtualFile(
                PathUtilTest.joinAbs("mydir", "file1.txt"), store);
        VirtualFile file2 = new VirtualFile(
                PathUtilTest.joinAbs("mydirectory", "file2.txt"), store);
        assertTrue(dir1.mkdirs());
        assertTrue(dir1.exists());
        assertTrue(dir1.isDirectory());
        assertTrue(dir2.mkdirs());
        assertTrue(dir2.exists());
        assertTrue(dir2.isDirectory());
        assertTrue(file1.createNewFile());
        assertTrue(file1.exists());
        assertFalse(file1.isDirectory());
        assertTrue(file2.createNewFile());
        assertTrue(file2.exists());
        assertFalse(file2.isDirectory());
        // We should only get one child; file1.txt
        String[] children = dir1.list();
        assertEquals(1, children.length);
        assertEquals(file1.getName(), children[0]);
        // Test that the same path ending with the separator results in the
        // same list being returned.
        VirtualFile dir1abs = new VirtualFile(
                PathUtilTest.joinAbs("mydir", ""), store);
        assertFalse(dir1.getName().equals(dir1abs.getName()));
        String[] childrenAbs = dir1abs.list();
        assertEquals(1, childrenAbs.length);
        assertEquals(children[0], childrenAbs[0]);
        // The deleteAll below shouldn't delete "mydirectory" and "file2.txt"..
        assertFalse(dir1.delete());
        assertTrue(dir1.deleteAll());
        assertTrue(dir2.exists());
        assertTrue(file2.exists());
    }

    /**
     * Makes sure that the root can be created.
     */
    public void testCreateRoot() {
        DataStore store = new DataStore("testCreateRootStore");
        String path = PathUtilTest.joinAbs("these", "are", "directories");
        assertTrue(store.createAllParents(path));
        assertNotNull(store.createEntry(path, true));
        VirtualFile vf = new VirtualFile(path, store);
        assertTrue(vf.exists());
        assertTrue(vf.isDirectory());

        // Also test one Windows specific root.
        path = PathUtilTest.join("c:", "Documents and Settings", "directories");
        assertTrue(store.createAllParents(path));
        assertNotNull(store.createEntry(path, true));
        vf = new VirtualFile(path, store);
        assertTrue(vf.exists());
        assertTrue(vf.isDirectory());
    }

    /**
     * Verify that the close() method of VirtualRandomAccessFile can be
     * called more than once.
     */
    public void testCloseIdempotent() throws IOException {
        DataStore store = getStore();
        VirtualFile f = new VirtualFile("afile", store);
        StorageRandomAccessFile raf = f.getRandomAccessFile("rw");
        raf.close();
        // The second close() used to throw NullPointerException (DERBY-5960)
        raf.close();
    }

    public static Test suite() {
        return new BaseTestSuite(VirtualFileTest.class);
    }

    /** A counter used to obtain unique data store names. */
    private static int dbStoreIndex = 0;
    /** Utility method returning a fresh data store. */
    private static synchronized DataStore getStore() {
        DataStore store = new DataStore("testVFMemDB-" + dbStoreIndex++);
        // We need the root to exist.
        assertNotNull(store.createEntry(java.io.File.separator, true));
        return store;
    }
}