Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BatchSessionTest.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db;
  21. import org.junit.Test;
  22. import static org.mockito.ArgumentMatchers.anyBoolean;
  23. import static org.mockito.Mockito.mock;
  24. import static org.mockito.Mockito.never;
  25. import static org.mockito.Mockito.times;
  26. import static org.mockito.Mockito.verify;
  27. public class BatchSessionTest {
  28. @Test
  29. public void shouldCommitWhenReachingBatchSize() {
  30. DbSession mybatisSession = mock(DbSession.class);
  31. BatchSession session = new BatchSession(mybatisSession, 10);
  32. for (int i = 0; i < 9; i++) {
  33. session.insert("id" + i);
  34. verify(mybatisSession).insert("id" + i);
  35. verify(mybatisSession, never()).commit();
  36. verify(mybatisSession, never()).commit(anyBoolean());
  37. }
  38. session.insert("id9");
  39. verify(mybatisSession).commit();
  40. session.close();
  41. }
  42. @Test
  43. public void shouldCommitWhenReachingBatchSizeWithoutCommits() {
  44. DbSession mybatisSession = mock(DbSession.class);
  45. BatchSession session = new BatchSession(mybatisSession, 10);
  46. for (int i = 0; i < 9; i++) {
  47. session.delete("delete something");
  48. verify(mybatisSession, never()).commit();
  49. verify(mybatisSession, never()).commit(anyBoolean());
  50. }
  51. session.delete("delete something");
  52. verify(mybatisSession).commit();
  53. session.close();
  54. }
  55. @Test
  56. public void shouldResetCounterAfterCommit() {
  57. DbSession mybatisSession = mock(DbSession.class);
  58. BatchSession session = new BatchSession(mybatisSession, 10);
  59. for (int i = 0; i < 35; i++) {
  60. session.insert("id" + i);
  61. }
  62. verify(mybatisSession, times(3)).commit();
  63. session.close();
  64. }
  65. }