|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + * info@exist-db.org |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.backup; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.io.TempDir; |
| 26 | + |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | + |
| 30 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | + |
| 33 | +class FileSystemBackupDescriptorTest { |
| 34 | + |
| 35 | + private static final String CONTENTS_XML = """ |
| 36 | + <?xml version="1.0" encoding="UTF-8"?> |
| 37 | + <exist:collection xmlns:exist="http://exist.sourceforge.net/NS/exist"> |
| 38 | + <exist:resource name="doc1.xml" type="XMLResource" filename="doc1.xml"/> |
| 39 | + <exist:resource name="test.binary" type="BinaryResource" filename="test.binary"/> |
| 40 | + </exist:collection> |
| 41 | + """; |
| 42 | + |
| 43 | + private static final String NESTED_CONTENTS_XML = """ |
| 44 | + <?xml version="1.0" encoding="UTF-8"?> |
| 45 | + <exist:collection xmlns:exist="http://exist.sourceforge.net/NS/exist"> |
| 46 | + <exist:resource name="nested.xml" type="XMLResource" filename="nested.xml"/> |
| 47 | + </exist:collection> |
| 48 | + """; |
| 49 | + |
| 50 | + @Test |
| 51 | + void countsResourcesFromContentsXmlOnly(@TempDir final Path tempDir) throws Exception { |
| 52 | + final Path backupRoot = tempDir.resolve("backup"); |
| 53 | + final Path dbDir = backupRoot.resolve("db"); |
| 54 | + Files.createDirectories(dbDir); |
| 55 | + |
| 56 | + Files.writeString(dbDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR), CONTENTS_XML, UTF_8); |
| 57 | + Files.writeString(dbDir.resolve("doc1.xml"), "<test/>", UTF_8); |
| 58 | + Files.write(dbDir.resolve("test.binary"), "test".getBytes(UTF_8)); |
| 59 | + |
| 60 | + final FileSystemBackupDescriptor descriptor = new FileSystemBackupDescriptor( |
| 61 | + backupRoot, |
| 62 | + dbDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR)); |
| 63 | + |
| 64 | + assertEquals(2, descriptor.getNumberOfFiles()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void countsNestedCollectionDescriptorsInSubtree(@TempDir final Path tempDir) throws Exception { |
| 69 | + final Path backupRoot = tempDir.resolve("backup"); |
| 70 | + final Path dbDir = backupRoot.resolve("db"); |
| 71 | + final Path nestedDir = dbDir.resolve("nested"); |
| 72 | + Files.createDirectories(nestedDir); |
| 73 | + |
| 74 | + Files.writeString(dbDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR), CONTENTS_XML, UTF_8); |
| 75 | + Files.writeString(nestedDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR), NESTED_CONTENTS_XML, UTF_8); |
| 76 | + Files.writeString(dbDir.resolve("doc1.xml"), "<test/>", UTF_8); |
| 77 | + Files.write(dbDir.resolve("test.binary"), "test".getBytes(UTF_8)); |
| 78 | + Files.writeString(nestedDir.resolve("nested.xml"), "<nested/>", UTF_8); |
| 79 | + |
| 80 | + final FileSystemBackupDescriptor descriptor = new FileSystemBackupDescriptor( |
| 81 | + backupRoot, |
| 82 | + dbDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR)); |
| 83 | + |
| 84 | + assertEquals(3, descriptor.getNumberOfFiles()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void collectionDescriptorCountsOwnSubtreeOnly(@TempDir final Path tempDir) throws Exception { |
| 89 | + final Path backupRoot = tempDir.resolve("backup"); |
| 90 | + final Path dbDir = backupRoot.resolve("db"); |
| 91 | + final Path nestedDir = dbDir.resolve("nested"); |
| 92 | + Files.createDirectories(nestedDir); |
| 93 | + |
| 94 | + Files.writeString(dbDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR), CONTENTS_XML, UTF_8); |
| 95 | + Files.writeString(nestedDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR), NESTED_CONTENTS_XML, UTF_8); |
| 96 | + |
| 97 | + final FileSystemBackupDescriptor nestedDescriptor = new FileSystemBackupDescriptor( |
| 98 | + backupRoot, |
| 99 | + nestedDir.resolve(BackupDescriptor.COLLECTION_DESCRIPTOR)); |
| 100 | + |
| 101 | + assertEquals(1, nestedDescriptor.getNumberOfFiles()); |
| 102 | + } |
| 103 | +} |
0 commit comments