aboutsummaryrefslogtreecommitdiffstats
path: root/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/io/MockFileOp.java
blob: cbe5fdd6820753c34f2850173445ccd3371a03e9 (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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed 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 com.android.sdklib.io;

import com.android.SdkConstants;
import com.android.annotations.NonNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Mock version of {@link FileOp} that wraps some common {@link File}
 * operations on files and folders.
 * <p/>
 * This version does not perform any file operation. Instead it records a textual
 * representation of all the file operations performed.
 * <p/>
 * To avoid cross-platform path issues (e.g. Windows path), the methods here should
 * always use rooted (aka absolute) unix-looking paths, e.g. "/dir1/dir2/file3".
 * When processing {@link File}, you can convert them using {@link #getAgnosticAbsPath(File)}.
 */
public class MockFileOp implements IFileOp {

    private final Set<String> mExistinfFiles = new TreeSet<String>();
    private final Set<String> mExistinfFolders = new TreeSet<String>();
    private final List<StringOutputStream> mOutputStreams = new ArrayList<StringOutputStream>();

    public MockFileOp() {
    }

    /** Resets the internal state, as if the object had been newly created. */
    public void reset() {
        mExistinfFiles.clear();
        mExistinfFolders.clear();
    }

    public String getAgnosticAbsPath(File file) {
        return getAgnosticAbsPath(file.getAbsolutePath());
    }

    public String getAgnosticAbsPath(String path) {
        if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_WINDOWS) {
            // Try to convert the windows-looking path to a unix-looking one
            path = path.replace('\\', '/');
            path = path.replace("C:", "");      //$NON-NLS-1$ //$NON-NLS-2$
        }
        return path;
    }

    /**
     * Records a new absolute file path.
     * Parent folders are not automatically created.
     */
    public void recordExistingFile(File file) {
        mExistinfFiles.add(getAgnosticAbsPath(file));
    }

    /**
     * Records a new absolute file path.
     * Parent folders are not automatically created.
     * <p/>
     * The syntax should always look "unix-like", e.g. "/dir/file".
     * On Windows that means you'll want to use {@link #getAgnosticAbsPath(File)}.
     * @param absFilePath A unix-like file path, e.g. "/dir/file"
     */
    public void recordExistingFile(String absFilePath) {
        mExistinfFiles.add(absFilePath);
    }

    /**
     * Records a new absolute folder path.
     * Parent folders are not automatically created.
     */
    public void recordExistingFolder(File folder) {
        mExistinfFolders.add(getAgnosticAbsPath(folder));
    }

    /**
     * Records a new absolute folder path.
     * Parent folders are not automatically created.
     * <p/>
     * The syntax should always look "unix-like", e.g. "/dir/file".
     * On Windows that means you'll want to use {@link #getAgnosticAbsPath(File)}.
     * @param absFolderPath A unix-like folder path, e.g. "/dir/file"
     */
    public void recordExistingFolder(String absFolderPath) {
        mExistinfFolders.add(absFolderPath);
    }

    /**
     * Returns the list of paths added using {@link #recordExistingFile(String)}
     * and eventually updated by {@link #delete(File)} operations.
     * <p/>
     * The returned list is sorted by alphabetic absolute path string.
     */
    public String[] getExistingFiles() {
        return mExistinfFiles.toArray(new String[mExistinfFiles.size()]);
    }

    /**
     * Returns the list of folder paths added using {@link #recordExistingFolder(String)}
     * and eventually updated {@link #delete(File)} or {@link #mkdirs(File)} operations.
     * <p/>
     * The returned list is sorted by alphabetic absolute path string.
     */
    public String[] getExistingFolders() {
        return mExistinfFolders.toArray(new String[mExistinfFolders.size()]);
    }

    /**
     * Returns the {@link StringOutputStream#toString()} as an array, in creation order.
     * Array can be empty but not null.
     */
    public String[] getOutputStreams() {
        int n = mOutputStreams.size();
        String[] result = new String[n];
        for (int i = 0; i < n; i++) {
            result[i] = mOutputStreams.get(i).toString();
        }
        return result;
    }

    /**
     * Helper to delete a file or a directory.
     * For a directory, recursively deletes all of its content.
     * Files that cannot be deleted right away are marked for deletion on exit.
     * The argument can be null.
     */
    @Override
    public void deleteFileOrFolder(File fileOrFolder) {
        if (fileOrFolder != null) {
            if (isDirectory(fileOrFolder)) {
                // Must delete content recursively first
                for (File item : listFiles(fileOrFolder)) {
                    deleteFileOrFolder(item);
                }
            }
            delete(fileOrFolder);
        }
    }

    /**
     * {@inheritDoc}
     * <p/>
     * <em>Note: this mock version does nothing.</em>
     */
    @Override
    public void setExecutablePermission(File file) throws IOException {
        // pass
    }

    /**
     * {@inheritDoc}
     * <p/>
     * <em>Note: this mock version does nothing.</em>
     */
    @Override
    public void setReadOnly(File file) {
        // pass
    }

    /**
     * {@inheritDoc}
     * <p/>
     * <em>Note: this mock version does nothing.</em>
     */
    @Override
    public void copyFile(File source, File dest) throws IOException {
        // pass
    }

    /**
     * Checks whether 2 binary files are the same.
     *
     * @param source the source file to copy
     * @param destination the destination file to write
     * @throws FileNotFoundException if the source files don't exist.
     * @throws IOException if there's a problem reading the files.
     */
    @Override
    public boolean isSameFile(File source, File destination) throws IOException {
        throw new UnsupportedOperationException("MockFileUtils.isSameFile is not supported."); //$NON-NLS-1$
    }

    /** Invokes {@link File#isFile()} on the given {@code file}. */
    @Override
    public boolean isFile(File file) {
        String path = getAgnosticAbsPath(file);
        return mExistinfFiles.contains(path);
    }

    /** Invokes {@link File#isDirectory()} on the given {@code file}. */
    @Override
    public boolean isDirectory(File file) {
        String path = getAgnosticAbsPath(file);
        if (mExistinfFolders.contains(path)) {
            return true;
        }

        // If we defined a file or folder as a child of the requested file path,
        // then the directory exists implicitely.
        Pattern pathRE = Pattern.compile(
                Pattern.quote(path + (path.endsWith("/") ? "" : '/')) +  //$NON-NLS-1$ //$NON-NLS-2$
                ".*");                                                   //$NON-NLS-1$

        for (String folder : mExistinfFolders) {
            if (pathRE.matcher(folder).matches()) {
                return true;
            }
        }
        for (String filePath : mExistinfFiles) {
            if (pathRE.matcher(filePath).matches()) {
                return true;
            }
        }

        return false;
    }

    /** Invokes {@link File#exists()} on the given {@code file}. */
    @Override
    public boolean exists(File file) {
        return isFile(file) || isDirectory(file);
    }

    /** Invokes {@link File#length()} on the given {@code file}. */
    @Override
    public long length(File file) {
        throw new UnsupportedOperationException("MockFileUtils.length is not supported."); //$NON-NLS-1$
    }

    @Override
    public boolean delete(File file) {
        String path = getAgnosticAbsPath(file);

        if (mExistinfFiles.remove(path)) {
            return true;
        }

        boolean hasSubfiles = false;
        for (String folder : mExistinfFolders) {
            if (folder.startsWith(path) && !folder.equals(path)) {
                // the File.delete operation is not recursive and would fail to remove
                // a root dir that is not empty.
                return false;
            }
        }
        if (!hasSubfiles) {
            for (String filePath : mExistinfFiles) {
                if (filePath.startsWith(path) && !filePath.equals(path)) {
                    // the File.delete operation is not recursive and would fail to remove
                    // a root dir that is not empty.
                    return false;
                }
            }
        }

        return mExistinfFolders.remove(path);
    }

    /** Invokes {@link File#mkdirs()} on the given {@code file}. */
    @Override
    public boolean mkdirs(File file) {
        for (; file != null; file = file.getParentFile()) {
            String path = getAgnosticAbsPath(file);
            mExistinfFolders.add(path);
        }
        return true;
    }

    /**
     * Invokes {@link File#listFiles()} on the given {@code file}.
     * The returned list is sorted by alphabetic absolute path string.
     */
    @Override
    public File[] listFiles(File file) {
        TreeSet<File> files = new TreeSet<File>();

        String path = getAgnosticAbsPath(file);
        Pattern pathRE = Pattern.compile(
                Pattern.quote(path + (path.endsWith("/") ? "" : '/')) +  //$NON-NLS-1$ //$NON-NLS-2$
                ".*");                                                   //$NON-NLS-1$

        for (String folder : mExistinfFolders) {
            if (pathRE.matcher(folder).matches()) {
                files.add(new File(folder));
            }
        }
        for (String filePath : mExistinfFiles) {
            if (pathRE.matcher(filePath).matches()) {
                files.add(new File(filePath));
            }
        }
        return files.toArray(new File[files.size()]);
    }

    /** Invokes {@link File#renameTo(File)} on the given files. */
    @Override
    public boolean renameTo(File oldFile, File newFile) {
        boolean renamed = false;

        String oldPath = getAgnosticAbsPath(oldFile);
        String newPath = getAgnosticAbsPath(newFile);
        Pattern pathRE = Pattern.compile(
                "^(" + Pattern.quote(oldPath) + //$NON-NLS-1$
                ")($|/.*)");                    //$NON-NLS-1$

        Set<String> newStrings = new HashSet<String>();
        for (Iterator<String> it = mExistinfFolders.iterator(); it.hasNext(); ) {
            String folder = it.next();
            Matcher m = pathRE.matcher(folder);
            if (m.matches()) {
                it.remove();
                String newFolder = newPath + m.group(2);
                newStrings.add(newFolder);
                renamed = true;
            }
        }
        mExistinfFolders.addAll(newStrings);
        newStrings.clear();

        for (Iterator<String> it = mExistinfFiles.iterator(); it.hasNext(); ) {
            String filePath = it.next();
            Matcher m = pathRE.matcher(filePath);
            if (m.matches()) {
                it.remove();
                String newFilePath = newPath + m.group(2);
                newStrings.add(newFilePath);
                renamed = true;
            }
        }
        mExistinfFiles.addAll(newStrings);

        return renamed;
    }

    /**
     * {@inheritDoc}
     * <p/>
     * <em>TODO: we might want to overload this to read mock properties instead of a real file.</em>
     */
    @Override
    public @NonNull Properties loadProperties(@NonNull File file) {
        Properties props = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            props.load(fis);
        } catch (IOException ignore) {
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception ignore) {}
            }
        }
        return props;
    }

    /**
     * {@inheritDoc}
     * <p/>
     * <em>Note that this uses the mock version of {@link #newFileOutputStream(File)} and thus
     * records the write rather than actually performing it.</em>
     */
    @Override
    public boolean saveProperties(@NonNull File file, @NonNull Properties props,
            @NonNull String comments) {
        OutputStream fos = null;
        try {
            fos = newFileOutputStream(file);

            props.store(fos, comments);
            return true;
        } catch (IOException ignore) {
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }

        return false;
    }

    /**
     * Returns an OutputStream that will capture the bytes written and associate
     * them with the given file.
     */
    @Override
    public OutputStream newFileOutputStream(File file) throws FileNotFoundException {
        StringOutputStream os = new StringOutputStream(file);
        mOutputStreams.add(os);
        return os;
    }

    /**
     * An {@link OutputStream} that will capture the stream as an UTF-8 string once properly closed
     * and associate it to the given {@link File}.
     */
    public class StringOutputStream extends ByteArrayOutputStream {
        private String mData;
        private final File mFile;

        public StringOutputStream(File file) {
            mFile = file;
            recordExistingFile(file);
        }

        public File getFile() {
            return mFile;
        }

        /** Can be null if the stream has never been properly closed. */
        public String getData() {
            return mData;
        }

        /** Once the stream is properly closed, convert the byte array to an UTF-8 string */
        @Override
        public void close() throws IOException {
            super.close();
            mData = new String(toByteArray(), "UTF-8");                         //$NON-NLS-1$
        }

        /** Returns a string representation suitable for unit tests validation. */
        @Override
        public synchronized String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append('<').append(getAgnosticAbsPath(mFile)).append(": ");      //$NON-NLS-1$
            if (mData == null) {
                sb.append("(stream not closed properly)>");                     //$NON-NLS-1$
            } else {
                sb.append('\'').append(mData).append("'>");                     //$NON-NLS-1$
            }
            return sb.toString();
        }
    }
}