28
28
import org .exist .security .MessageDigester ;
29
29
import org .exist .security .SecurityManager ;
30
30
import org .exist .test .ExistWebServer ;
31
- import org .exist .util .MimeTable ;
31
+ import org .exist .util .MimeType ;
32
32
import org .exist .xmldb .*;
33
33
import org .junit .*;
34
34
import org .junit .rules .TemporaryFolder ;
35
35
import org .junit .runner .RunWith ;
36
36
import org .junit .runners .Parameterized ;
37
37
import org .xmldb .api .DatabaseManager ;
38
38
import org .xmldb .api .base .Collection ;
39
+ import org .xmldb .api .base .Resource ;
39
40
import org .xmldb .api .base .XMLDBException ;
41
+ import org .xmldb .api .modules .BinaryResource ;
42
+ import org .xmldb .api .modules .XMLResource ;
40
43
41
44
import javax .annotation .Nullable ;
42
45
import java .io .File ;
@@ -62,6 +65,17 @@ public class XMLDBRestoreTest {
62
65
63
66
private static final String PORT_PLACEHOLDER = "${PORT}" ;
64
67
68
+ private static final String COLLECTION1_NAME = "col1" ;
69
+ private static final DocInfo [] BACKUP_DOCS = {
70
+ new DocInfo ("doc1.xml" , MimeType .XML , "application/xml" , "<doc1/>" ),
71
+ new DocInfo ("doc2.xml" , MimeType .XML , "application/xml" , "<doc2/>" ),
72
+ new DocInfo ("doc3.svg" , MimeType .XML , "image/svg+xml" , "<svg height=\" 100\" width=\" 100\" ><circle cx=\" 50\" cy=\" 50\" r=\" 40\" stroke=\" black\" stroke-width=\" 3\" fill=\" red\" />Sorry, your browser does not support inline SVG.</svg>" ),
73
+ new DocInfo ("doc4.html" , MimeType .BINARY , "text/html" , "<html><body><h1>BinaryResource</h1></body></html>" ),
74
+ new DocInfo ("doc5.html" , MimeType .XML , "text/html" , "<html><body><h1>XMLResource</h1></body></html>" ),
75
+ new DocInfo ("doc6.xml" , MimeType .XML , "<doc6/>" ),
76
+ new DocInfo ("doc7.bin" , MimeType .BINARY , "1234567" )
77
+ };
78
+
65
79
@ ClassRule
66
80
public static final TemporaryFolder tempFolder = new TemporaryFolder ();
67
81
@@ -83,20 +97,6 @@ private String getBaseUri() {
83
97
return baseUri .replace (PORT_PLACEHOLDER , Integer .toString (existWebServer .getPort ()));
84
98
}
85
99
86
- @ BeforeClass
87
- public static void prepare () throws IOException {
88
- Path mimeTypesDefinition = tempFolder .getRoot ().toPath ().resolve ("mime-types.xml" );
89
- String mimeTypesContent =
90
- "<mime-types default-mime-type=\" foo/bar\" default-resource-type=\" binary\" xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\" schema/mime-types.xsd\" >\n " +
91
- "<mime-type name=\" text/html\" type=\" xml\" >\n " +
92
- "<description>HTML document</description>\n " +
93
- "<extensions>.html</extensions>\n " +
94
- "</mime-type>\n " +
95
- "</mime-types>" ;
96
- Files .write (mimeTypesDefinition , mimeTypesContent .getBytes (UTF_8 ));
97
- MimeTable .getInstance (mimeTypesDefinition );
98
- }
99
-
100
100
@ Test
101
101
public void restoreValidZipBackup () throws IOException , XMLDBException {
102
102
final Path zipFile = createZipBackupWithValidContent ();
@@ -105,9 +105,13 @@ public void restoreValidZipBackup() throws IOException, XMLDBException {
105
105
106
106
restoreBackup (rootUri , zipFile , null , listener );
107
107
108
- assertEquals (7 , listener .restored .size ());
109
- assertEquals (1 , listener .warnings .size ());
108
+ assertEquals (9 , listener .restored .size ());
109
+ assertEquals (2 , listener .warnings .size ());
110
110
assertEquals (0 , listener .errors .size ());
111
+
112
+ for (final DocInfo backupDocInfo : BACKUP_DOCS ) {
113
+ checkMediaType (XmldbURI .ROOT_COLLECTION_URI .append (COLLECTION1_NAME ), backupDocInfo );
114
+ }
111
115
}
112
116
113
117
@ Test
@@ -118,9 +122,13 @@ public void restoreValidDirBackup() throws IOException, XMLDBException {
118
122
119
123
restoreBackup (rootUri , contentsFile , null , listener );
120
124
121
- assertEquals (7 , listener .restored .size ());
122
- assertEquals (1 , listener .warnings .size ());
125
+ assertEquals (9 , listener .restored .size ());
126
+ assertEquals (2 , listener .warnings .size ());
123
127
assertEquals (0 , listener .errors .size ());
128
+
129
+ for (final DocInfo backupDocInfo : BACKUP_DOCS ) {
130
+ checkMediaType (XmldbURI .ROOT_COLLECTION_URI .append (COLLECTION1_NAME ), backupDocInfo );
131
+ }
124
132
}
125
133
126
134
@ Test
@@ -290,57 +298,55 @@ private static void restoreBackup(final XmldbURI uri, final Path backup, @Nullab
290
298
restoreService .restore (backup .normalize ().toAbsolutePath ().toString (), backupPassword , listener , false );
291
299
}
292
300
301
+ private void checkMediaType (final XmldbURI collectionUri , final DocInfo backupDocInfo ) throws XMLDBException {
302
+ final Collection collection = DatabaseManager .getCollection (XmldbURI .create (getBaseUri ()).append (collectionUri ).toString (), TestUtils .ADMIN_DB_USER , TestUtils .ADMIN_DB_PWD );
303
+ final Resource resource = collection .getResource (backupDocInfo .name );
304
+ if (backupDocInfo .type == MimeType .XML ) {
305
+ assertTrue (resource instanceof XMLResource );
306
+ } else {
307
+ assertTrue (resource instanceof BinaryResource );
308
+ }
309
+ if (backupDocInfo .mediaType != null ) {
310
+ assertEquals (backupDocInfo .mediaType , ((EXistResource ) resource ).getMimeType ());
311
+ } else {
312
+ assertEquals (backupDocInfo .type == MimeType .XML ? MimeType .XML_TYPE .getName () : MimeType .BINARY_TYPE .getName (), ((EXistResource ) resource ).getMimeType ());
313
+ }
314
+ }
315
+
293
316
private static Path createZipBackupWithValidContent () throws IOException {
294
317
final Path dbContentsFile = createBackupWithValidContent ();
295
318
final Path dbDir = dbContentsFile .getParent ();
296
319
return zipDirectory (dbDir );
297
320
}
298
321
299
322
private static Path createBackupWithValidContent () throws IOException {
323
+
300
324
final Path backupDir = tempFolder .newFolder ().toPath ();
301
325
final Path db = Files .createDirectories (backupDir .resolve ("db" ));
302
- final Path col1 = Files .createDirectories (db .resolve ("col1" ));
326
+ final Path col1 = Files .createDirectories (db .resolve (COLLECTION1_NAME ));
303
327
304
328
final String dbContents =
305
329
"<collection xmlns=\" http://exist.sourceforge.net/NS/exist\" name=\" /db\" owner=\" SYSTEM\" group=\" dba\" mode=\" 755\" created=\" 2019-05-15T15:58:39.385+04:00\" version=\" 1\" >\n " +
306
330
" <acl entries=\" 0\" version=\" 1\" />\n " +
307
- " <subcollection name=\" col1 \" filename=\" col1 \" />\n " +
331
+ " <subcollection name=\" " + COLLECTION1_NAME + " \" filename=\" " + COLLECTION1_NAME + " \" />\n " +
308
332
"</collection>" ;
309
333
310
334
311
- final String col1Contents =
312
- "<collection xmlns=\" http://exist.sourceforge.net/NS/exist\" name=\" /db/col1\" owner=\" admin\" group=\" dba\" mode=\" 755\" created=\" 2019-05-15T15:58:39.385+04:00\" deduplicate-blobs=\" false\" version=\" 2\" >\n " +
313
- " <acl entries=\" 0\" version=\" 1\" />\n " +
314
- " <resource type=\" XMLResource\" name=\" doc1.xml\" owner=\" admin\" group=\" dba\" mode=\" 644\" created=\" 2019-05-15T15:58:48.638+04:00\" modified=\" 2019-05-15T15:58:48.638+04:00\" filename=\" doc1.xml\" mimetype=\" application/xml\" >\n " +
315
- " <acl entries=\" 0\" version=\" 1\" />\n " +
316
- " </resource>\n " +
317
- " <resource type=\" XMLResource\" name=\" doc2.xml\" owner=\" admin\" group=\" dba\" mode=\" 644\" created=\" 2019-05-15T15:58:48.638+04:00\" modified=\" 2019-05-15T15:58:48.638+04:00\" filename=\" doc2.xml\" mimetype=\" application/xml\" >\n " +
318
- " <acl entries=\" 0\" version=\" 1\" />\n " +
319
- " </resource>\n " +
320
- " <resource type=\" XMLResource\" name=\" doc3.xml\" owner=\" admin\" group=\" dba\" mode=\" 644\" created=\" 2019-05-15T15:58:49.618+04:00\" modified=\" 2019-05-15T15:58:49.618+04:00\" filename=\" doc3.xml\" mimetype=\" application/special-xml\" >\n " +
321
- " <acl entries=\" 0\" version=\" 1\" />\n " +
322
- " </resource>\n " +
323
- " <resource type=\" BinaryResource\" name=\" doc4.html\" owner=\" admin\" group=\" dba\" mode=\" 644\" created=\" 2019-05-15T15:58:49.618+04:00\" modified=\" 2019-05-15T15:58:49.618+04:00\" filename=\" doc4.html\" mimetype=\" text/xhtml\" >\n " +
324
- " <acl entries=\" 0\" version=\" 1\" />\n " +
325
- " </resource>\n " +
326
- " <resource type=\" BinaryResource\" name=\" doc5.html\" owner=\" admin\" group=\" dba\" mode=\" 644\" created=\" 2019-05-15T15:58:49.618+04:00\" modified=\" 2019-05-15T15:58:49.618+04:00\" filename=\" doc5.html\" mimetype=\" text/html\" >\n " +
327
- " <acl entries=\" 0\" version=\" 1\" />\n " +
328
- " </resource>\n " +
329
- "</collection>" ;
330
-
331
- final String doc1 = "<doc1/>" ;
332
- final String doc2 = "<doc2/>" ;
333
- final String doc3 = "<doc3/>" ;
334
- final String doc4 = "<html><body><hr/></body></html>" ;
335
- final String doc5 = "<html><body><hr></body></html>" ;
335
+ final StringBuilder col1Contents = new StringBuilder ();
336
+ col1Contents .append ("<collection xmlns=\" http://exist.sourceforge.net/NS/exist\" name=\" /db/" ).append (COLLECTION1_NAME ).append ("\" owner=\" admin\" group=\" dba\" mode=\" 755\" created=\" 2019-05-15T15:58:39.385+04:00\" deduplicate-blobs=\" false\" version=\" 2\" >\n " );
337
+ col1Contents .append (" <acl entries=\" 0\" version=\" 1\" />\n " );
338
+ for (final DocInfo backupDocInfo : BACKUP_DOCS ) {
339
+ col1Contents .append (" <resource type=\" " ).append (backupDocInfo .type == MimeType .XML ? "XMLResource" : "BinaryResource" ).append ("\" name=\" " ).append (backupDocInfo .name ).append ("\" owner=\" admin\" group=\" dba\" mode=\" 644\" created=\" 2019-05-15T15:58:48.638+04:00\" modified=\" 2019-05-15T15:58:48.638+04:00\" filename=\" " ).append (backupDocInfo .name ).append (backupDocInfo .mediaType != null ? "\" mimetype=\" " + backupDocInfo .mediaType + "\" >\n " : "\" >\n " );
340
+ col1Contents .append (" <acl entries=\" 0\" version=\" 1\" />\n " );
341
+ col1Contents .append (" </resource>\n " );
342
+ }
343
+ col1Contents .append ("</collection>" );
336
344
337
345
final Path dbContentsFile = Files .write (db .resolve (BackupDescriptor .COLLECTION_DESCRIPTOR ), dbContents .getBytes (UTF_8 ));
338
- final Path col1ContentsFile = Files .write (col1 .resolve (BackupDescriptor .COLLECTION_DESCRIPTOR ), col1Contents .getBytes (UTF_8 ));
339
- Files .write (col1 .resolve ("doc1.xml" ), doc1 .getBytes (UTF_8 ));
340
- Files .write (col1 .resolve ("doc2.xml" ), doc2 .getBytes (UTF_8 ));
341
- Files .write (col1 .resolve ("doc3.xml" ), doc3 .getBytes (UTF_8 ));
342
- Files .write (col1 .resolve ("doc4.html" ), doc4 .getBytes (UTF_8 ));
343
- Files .write (col1 .resolve ("doc5.html" ), doc5 .getBytes (UTF_8 ));
346
+ final Path col1ContentsFile = Files .write (col1 .resolve (BackupDescriptor .COLLECTION_DESCRIPTOR ), col1Contents .toString ().getBytes (UTF_8 ));
347
+ for (final DocInfo backupDocInfo : BACKUP_DOCS ) {
348
+ Files .write (col1 .resolve (backupDocInfo .name ), backupDocInfo .content .getBytes (UTF_8 ));
349
+ }
344
350
345
351
return dbContentsFile ;
346
352
}
@@ -633,4 +639,22 @@ public void error(final String message) {
633
639
errors .add (message );
634
640
}
635
641
}
642
+
643
+ private static class DocInfo {
644
+ final String name ;
645
+ final int type ;
646
+ @ Nullable final String mediaType ;
647
+ final String content ;
648
+
649
+ private DocInfo (final String name , final int type , final String content ) {
650
+ this (name , type , null , content );
651
+ }
652
+
653
+ private DocInfo (final String name , final int type , final String mediaType , final String content ) {
654
+ this .name = name ;
655
+ this .type = type ;
656
+ this .mediaType = mediaType ;
657
+ this .content = content ;
658
+ }
659
+ }
636
660
}
0 commit comments