34
34
import org .exist .test .ExistEmbeddedServer ;
35
35
import org .exist .test .TestConstants ;
36
36
import org .exist .util .DatabaseConfigurationException ;
37
+ import org .exist .util .FileInputSource ;
37
38
import org .exist .util .FileUtils ;
38
39
import org .exist .util .LockException ;
39
40
import org .exist .xmldb .XmldbURI ;
41
+ import org .junit .After ;
40
42
import org .junit .Ignore ;
41
43
import org .junit .Rule ;
42
44
import org .junit .Test ;
45
+ import org .xml .sax .InputSource ;
43
46
44
47
import java .io .IOException ;
45
48
import java .nio .file .Files ;
46
49
import java .nio .file .Path ;
47
50
import java .nio .file .Paths ;
48
51
import java .util .Optional ;
49
- import java .util .concurrent .Executors ;
50
52
51
53
import static org .junit .Assert .*;
52
54
55
57
*/
56
58
public abstract class AbstractRecoverTest {
57
59
58
- private static final boolean COMMIT = true ;
59
- private static final boolean NO_COMMIT = false ;
60
+ protected static final boolean COMMIT = true ;
61
+ protected static final boolean NO_COMMIT = false ;
60
62
61
- private static final boolean MUST_EXIST = true ;
62
- private static final boolean MUST_NOT_EXIST = false ;
63
+ protected static final boolean MUST_EXIST = true ;
64
+ protected static final boolean MUST_NOT_EXIST = false ;
63
65
64
66
/**
65
67
* We set useTemporaryStorage=true for ExistEmbeddedServer
@@ -69,6 +71,11 @@ public abstract class AbstractRecoverTest {
69
71
public final ExistEmbeddedServer existEmbeddedServer =
70
72
new ExistEmbeddedServer (true , true );
71
73
74
+ @ After
75
+ public void tearDown () {
76
+ BrokerPool .FORCE_CORRUPTION = false ;
77
+ }
78
+
72
79
@ Test
73
80
public void storeAndLoad () throws LockException , TriggerException , PermissionDeniedException , EXistException ,
74
81
IOException , DatabaseConfigurationException , InterruptedException {
@@ -603,7 +610,7 @@ public void replaceWithoutCommitThenDeleteWithoutCommitAndLoad_isRepeatable() th
603
610
* unfinished (i.e. neither committed, aborted, or closed)
604
611
* @param file The file that to store
605
612
*/
606
- private void store (final boolean commitAndClose , final Path file ) throws EXistException , PermissionDeniedException ,
613
+ protected void store (final boolean commitAndClose , final Path file ) throws EXistException , PermissionDeniedException ,
607
614
IOException , TriggerException , LockException , InterruptedException {
608
615
store (commitAndClose , file , FileUtils .fileName (file ));
609
616
}
@@ -618,13 +625,27 @@ private void store(final boolean commitAndClose, final Path file) throws EXistEx
618
625
*/
619
626
private void store (final boolean commitAndClose , final Path file , final String dbFilename ) throws EXistException ,
620
627
PermissionDeniedException , IOException , TriggerException , LockException , InterruptedException {
628
+ store (commitAndClose , new FileInputSource (file ), dbFilename );
629
+ }
630
+
631
+ /**
632
+ * Store a document into the database.
633
+ *
634
+ * @param commitAndClose true if the transaction should be committed. false will leave the transaction
635
+ * unfinished (i.e. neither committed, aborted, or closed)
636
+ * @param data The data to store in the document
637
+ * @param dbFilename the name to use when storing the file in the database
638
+ */
639
+ protected void store (final boolean commitAndClose , final InputSource data , final String dbFilename ) throws EXistException ,
640
+ PermissionDeniedException , IOException , TriggerException , LockException , InterruptedException {
641
+
621
642
622
643
runSync (new BrokerTask (existEmbeddedServer .getBrokerPool (), (broker , transaction ) -> {
623
644
final Collection root = broker .getOrCreateCollection (transaction , TestConstants .TEST_COLLECTION_URI );
624
645
assertNotNull (root );
625
646
broker .saveCollection (transaction , root );
626
647
627
- storeAndVerify (broker , transaction , root , file , dbFilename );
648
+ storeAndVerify (broker , transaction , root , data , dbFilename );
628
649
629
650
if (commitAndClose ) {
630
651
transaction .commit ();
@@ -641,11 +662,11 @@ private void store(final boolean commitAndClose, final Path file, final String d
641
662
* @param broker The database broker
642
663
* @param transaction The database transaction
643
664
* @param collection The Collection into which the document should be stored
644
- * @param file The file which holds the content for the document to store in the database
665
+ * @param data The content for the document to store in the database
645
666
* @param dbFilename The name to store the document as in the database
646
667
*/
647
668
protected abstract void storeAndVerify (final DBBroker broker , final Txn transaction , final Collection collection ,
648
- final Path file , final String dbFilename ) throws EXistException , PermissionDeniedException ,
669
+ final InputSource data , final String dbFilename ) throws EXistException , PermissionDeniedException ,
649
670
IOException , TriggerException , LockException ;
650
671
651
672
/**
@@ -668,6 +689,18 @@ private void read(final boolean shouldExist, final Path file)
668
689
*/
669
690
private void read (final boolean shouldExist , final Path file , final String dbFilename )
670
691
throws EXistException , PermissionDeniedException , IOException {
692
+ read (shouldExist , new FileInputSource (file ), dbFilename );
693
+ }
694
+
695
+ /**
696
+ * Read a document from the database.
697
+ *
698
+ * @param shouldExist true if the document should exist in the database, false if the document should not exist
699
+ * @param data The data that was previously stored
700
+ * @param dbFilename The name of the file to read from the database
701
+ */
702
+ protected void read (final boolean shouldExist , final InputSource data , final String dbFilename )
703
+ throws EXistException , PermissionDeniedException , IOException {
671
704
final BrokerPool pool = existEmbeddedServer .getBrokerPool ();
672
705
try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()))) {
673
706
final XmldbURI uri = TestConstants .TEST_COLLECTION_URI .append (dbFilename );
@@ -679,7 +712,7 @@ private void read(final boolean shouldExist, final Path file, final String dbFil
679
712
} else {
680
713
assertNotNull ("Document does not exist in the database: " + uri , doc );
681
714
682
- readAndVerify (broker , doc .getDocument (), file , dbFilename );
715
+ readAndVerify (broker , doc .getDocument (), data , dbFilename );
683
716
}
684
717
}
685
718
}
@@ -690,11 +723,11 @@ private void read(final boolean shouldExist, final Path file, final String dbFil
690
723
*
691
724
* @param broker The database broker.
692
725
* @param doc The document from the database.
693
- * @param file The file that was previously stored
726
+ * @param data The data that was previously stored
694
727
* @param dbFilename The name of the file read from the database
695
728
*/
696
729
protected abstract void readAndVerify (final DBBroker broker , final DocumentImpl doc ,
697
- final Path file , final String dbFilename ) throws EXistException , PermissionDeniedException , IOException ;
730
+ final InputSource data , final String dbFilename ) throws EXistException , PermissionDeniedException , IOException ;
698
731
699
732
/**
700
733
* Delete a document from the database.
@@ -737,7 +770,7 @@ private void delete(final boolean commitAndClose, final String dbFilename)
737
770
}));
738
771
}
739
772
740
- private void flushJournal () {
773
+ protected void flushJournal () {
741
774
final BrokerPool pool = existEmbeddedServer .getBrokerPool ();
742
775
pool .getJournalManager ().get ().flush (true , false );
743
776
}
0 commit comments