/modeshape-jcr/src/test/java/org/modeshape/jcr/RepositoryBackupTest.java

https://github.com/kbachl/modeshape · Java · 127 lines · 94 code · 14 blank · 19 comment · 2 complexity · 4d5d91f4f79df9b7f4d52b3aad1cb89d MD5 · raw file

  1. /*
  2. * ModeShape (http://www.modeshape.org)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.modeshape.jcr;
  17. import static org.hamcrest.core.Is.is;
  18. import static org.junit.Assert.assertThat;
  19. import java.io.File;
  20. import java.util.concurrent.CountDownLatch;
  21. import java.util.concurrent.TimeUnit;
  22. import java.util.concurrent.atomic.AtomicReference;
  23. import javax.jcr.RepositoryException;
  24. import org.junit.After;
  25. import org.junit.AfterClass;
  26. import org.junit.Before;
  27. import org.junit.BeforeClass;
  28. import org.junit.Ignore;
  29. import org.junit.Test;
  30. import org.modeshape.common.statistic.Stopwatch;
  31. import org.modeshape.common.util.FileUtil;
  32. import org.modeshape.jcr.api.Problems;
  33. public class RepositoryBackupTest extends MultiUseAbstractTest {
  34. @BeforeClass
  35. public static void beforeAll() throws Exception {
  36. startRepository();
  37. // Initialize the repository data
  38. initializeContent(session);
  39. }
  40. @AfterClass
  41. public static final void afterAll() throws Exception {
  42. stopRepository();
  43. }
  44. protected static void initializeContent( JcrSession session ) throws Exception {
  45. int breadth = 10;
  46. int depth = 3;
  47. int properties = 7;
  48. boolean print = true;
  49. createSubgraph(session, "/", depth, breadth, properties, false, new Stopwatch(), print ? System.out : null, null);
  50. // session.save();
  51. }
  52. private File testDirectory;
  53. @Before
  54. public void setUp() throws Exception {
  55. testDirectory = new File("target/backupArea/backupTests");
  56. FileUtil.delete(testDirectory);
  57. }
  58. @After
  59. public void tearDown() throws Exception {
  60. try {
  61. // We want to be able to see the result of each test in 'target' directory, so don't do this ...
  62. // FileUtil.delete(testDirectory);
  63. } finally {
  64. testDirectory = null;
  65. }
  66. }
  67. @Test
  68. public void shouldPerformOneBackup() throws Exception {
  69. Stopwatch sw = new Stopwatch();
  70. sw.start();
  71. Problems problems = session().getWorkspace().getRepositoryManager().backupRepository(testDirectory);
  72. sw.stop();
  73. assertThat(problems.hasProblems(), is(false));
  74. printMessage("Time to perform backup: " + sw.getMaximumDuration());
  75. }
  76. @Test
  77. public void shouldPerformMultipleBackups() throws Exception {
  78. for (int i = 0; i != 3; ++i) {
  79. File file = new File(testDirectory, "test" + i);
  80. Stopwatch sw = new Stopwatch();
  81. sw.start();
  82. Problems problems = session().getWorkspace().getRepositoryManager().backupRepository(file);
  83. sw.stop();
  84. assertThat(problems.hasProblems(), is(false));
  85. printMessage("Time to perform backup: " + sw.getMaximumDuration());
  86. }
  87. }
  88. @Ignore
  89. @Test
  90. public void shouldPerformOneBackupWhileChangesAreMade() throws Exception {
  91. JcrSession session = session();
  92. session.getRootNode().addNode("extras");
  93. final File testDirectory = this.testDirectory;
  94. final Stopwatch sw = new Stopwatch();
  95. final CountDownLatch latch = new CountDownLatch(1);
  96. final AtomicReference<Problems> problems = new AtomicReference<Problems>();
  97. new Thread(() -> {
  98. sw.start();
  99. try {
  100. Problems backupProblems = session().getWorkspace().getRepositoryManager().backupRepository(testDirectory);
  101. problems.set(backupProblems);
  102. } catch (RepositoryException e) {
  103. throw new RuntimeException(e);
  104. }
  105. sw.stop();
  106. latch.countDown();
  107. }).start();
  108. createSubgraph(session, "/extras", 1, 2, 2, false, new Stopwatch(), print ? System.out : null, null);
  109. latch.await(10, TimeUnit.SECONDS);
  110. assertThat(problems.get().hasProblems(), is(false));
  111. printMessage("Time to perform backup: " + sw.getTotalDuration());
  112. }
  113. }