summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/common/io/FileSystemUtilsTests.java
blob: 39d24a0e792ee235b34b41de0e58566b070e9827 (plain)
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
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.common.io;

import org.apache.lucene.util.LuceneTestCase.SuppressFileSystems;
import org.elasticsearch.test.ESTestCase;
import org.junit.Assert;
import org.junit.Before;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFileNotExists;
import static org.elasticsearch.common.io.FileTestUtils.assertFileContent;

/**
 * Unit tests for {@link org.elasticsearch.common.io.FileSystemUtils}.
 */
@SuppressFileSystems("WindowsFS") // tries to move away open file handles
public class FileSystemUtilsTests extends ESTestCase {

    private Path src;
    private Path dst;

    @Before
    public void copySourceFilesToTarget() throws IOException, URISyntaxException {
        src = createTempDir();
        dst = createTempDir();
        Files.createDirectories(src);
        Files.createDirectories(dst);

        // We first copy sources test files from src/test/resources
        // Because after when the test runs, src files are moved to their destination
        final Path path = getDataPath("/org/elasticsearch/common/io/copyappend");
        FileSystemUtils.copyDirectoryRecursively(path, src);
    }

    public void testMoveOverExistingFileAndAppend() throws IOException {

        FileSystemUtils.moveFilesWithoutOverwriting(src.resolve("v1"), dst, ".new");
        assertFileContent(dst, "file1.txt", "version1");
        assertFileContent(dst, "dir/file2.txt", "version1");

        FileSystemUtils.moveFilesWithoutOverwriting(src.resolve("v2"), dst, ".new");
        assertFileContent(dst, "file1.txt", "version1");
        assertFileContent(dst, "dir/file2.txt", "version1");
        assertFileContent(dst, "file1.txt.new", "version2");
        assertFileContent(dst, "dir/file2.txt.new", "version2");
        assertFileContent(dst, "file3.txt", "version1");
        assertFileContent(dst, "dir/subdir/file4.txt", "version1");

        FileSystemUtils.moveFilesWithoutOverwriting(src.resolve("v3"), dst, ".new");
        assertFileContent(dst, "file1.txt", "version1");
        assertFileContent(dst, "dir/file2.txt", "version1");
        assertFileContent(dst, "file1.txt.new", "version3");
        assertFileContent(dst, "dir/file2.txt.new", "version3");
        assertFileContent(dst, "file3.txt", "version1");
        assertFileContent(dst, "dir/subdir/file4.txt", "version1");
        assertFileContent(dst, "file3.txt.new", "version2");
        assertFileContent(dst, "dir/subdir/file4.txt.new", "version2");
        assertFileContent(dst, "dir/subdir/file5.txt", "version1");
    }

    public void testMoveOverExistingFileAndIgnore() throws IOException {
        Path dest = createTempDir();

        FileSystemUtils.moveFilesWithoutOverwriting(src.resolve("v1"), dest, null);
        assertFileContent(dest, "file1.txt", "version1");
        assertFileContent(dest, "dir/file2.txt", "version1");

        FileSystemUtils.moveFilesWithoutOverwriting(src.resolve("v2"), dest, null);
        assertFileContent(dest, "file1.txt", "version1");
        assertFileContent(dest, "dir/file2.txt", "version1");
        assertFileContent(dest, "file1.txt.new", null);
        assertFileContent(dest, "dir/file2.txt.new", null);
        assertFileContent(dest, "file3.txt", "version1");
        assertFileContent(dest, "dir/subdir/file4.txt", "version1");

        FileSystemUtils.moveFilesWithoutOverwriting(src.resolve("v3"), dest, null);
        assertFileContent(dest, "file1.txt", "version1");
        assertFileContent(dest, "dir/file2.txt", "version1");
        assertFileContent(dest, "file1.txt.new", null);
        assertFileContent(dest, "dir/file2.txt.new", null);
        assertFileContent(dest, "file3.txt", "version1");
        assertFileContent(dest, "dir/subdir/file4.txt", "version1");
        assertFileContent(dest, "file3.txt.new", null);
        assertFileContent(dest, "dir/subdir/file4.txt.new", null);
        assertFileContent(dest, "dir/subdir/file5.txt", "version1");
    }

    public void testMoveFilesDoesNotCreateSameFileWithSuffix() throws Exception {
        Path[] dirs = new Path[] { createTempDir(), createTempDir(), createTempDir()};
        for (Path dir : dirs) {
            Files.write(dir.resolve("file1.txt"), "file1".getBytes(StandardCharsets.UTF_8));
            Files.createDirectory(dir.resolve("dir"));
            Files.write(dir.resolve("dir").resolve("file2.txt"), "file2".getBytes(StandardCharsets.UTF_8));
        }

        FileSystemUtils.moveFilesWithoutOverwriting(dirs[0], dst, ".new");
        assertFileContent(dst, "file1.txt", "file1");
        assertFileContent(dst, "dir/file2.txt", "file2");

        // do the same operation again, make sure, no .new files have been added
        FileSystemUtils.moveFilesWithoutOverwriting(dirs[1], dst, ".new");
        assertFileContent(dst, "file1.txt", "file1");
        assertFileContent(dst, "dir/file2.txt", "file2");
        assertFileNotExists(dst.resolve("file1.txt.new"));
        assertFileNotExists(dst.resolve("dir").resolve("file2.txt.new"));

        // change file content, make sure it gets updated
        Files.write(dirs[2].resolve("dir").resolve("file2.txt"), "UPDATED".getBytes(StandardCharsets.UTF_8));
        FileSystemUtils.moveFilesWithoutOverwriting(dirs[2], dst, ".new");
        assertFileContent(dst, "file1.txt", "file1");
        assertFileContent(dst, "dir/file2.txt", "file2");
        assertFileContent(dst, "dir/file2.txt.new", "UPDATED");
    }

    public void testAppend() {
        assertEquals(FileSystemUtils.append(PathUtils.get("/foo/bar"), PathUtils.get("/hello/world/this_is/awesome"), 0),
            PathUtils.get("/foo/bar/hello/world/this_is/awesome"));

        assertEquals(FileSystemUtils.append(PathUtils.get("/foo/bar"), PathUtils.get("/hello/world/this_is/awesome"), 2),
                PathUtils.get("/foo/bar/this_is/awesome"));

        assertEquals(FileSystemUtils.append(PathUtils.get("/foo/bar"), PathUtils.get("/hello/world/this_is/awesome"), 1),
                PathUtils.get("/foo/bar/world/this_is/awesome"));
    }

    public void testIsHidden() {
        for (String p : Arrays.asList(
                "/",
                "foo",
                "/foo",
                "foo.bar",
                "/foo.bar",
                "foo/bar",
                "foo/./bar",
                "foo/../bar",
                "/foo/./bar",
                "/foo/../bar"
                )) {
            Path path = PathUtils.get(p);
            assertFalse(FileSystemUtils.isHidden(path));
        }
        for (String p : Arrays.asList(
                ".hidden",
                ".hidden.ext",
                "/.hidden",
                "/.hidden.ext",
                "foo/.hidden",
                "foo/.hidden.ext",
                "/foo/.hidden",
                "/foo/.hidden.ext",
                ".",
                "..",
                "foo/.",
                "foo/.."
                )) {
            Path path = PathUtils.get(p);
            assertTrue(FileSystemUtils.isHidden(path));
        }
    }
}