Sebastian Sdorra avatar Sebastian Sdorra committed eb2b456

added unit tests for repository listener

Comments (0)

Files changed (1)

scm-test/src/main/java/sonia/scm/repository/RepositoryManagerTestBase.java

 
 import org.junit.Test;
 
+import sonia.scm.HandlerEvent;
 import sonia.scm.Manager;
 import sonia.scm.ManagerTestBase;
 
    * @throws RepositoryException
    */
   @Test
+  public void testListener() throws RepositoryException, IOException
+  {
+    RepositoryManager repoManager = createRepositoryManager(false);
+    TestListener listener = new TestListener();
+
+    repoManager.addListener(listener);
+
+    Repository repository = RepositoryTestData.create42Puzzle();
+
+    repoManager.create(repository);
+    assertRepositoriesEquals(repository, listener.preRepository);
+    assertSame(HandlerEvent.BEFORE_CREATE, listener.preEvent);
+    assertRepositoriesEquals(repository, listener.postRepository);
+    assertSame(HandlerEvent.CREATE, listener.postEvent);
+
+    repository.setDescription("changed description");
+    repoManager.modify(repository);
+    assertRepositoriesEquals(repository, listener.preRepository);
+    assertSame(HandlerEvent.BEFORE_MODIFY, listener.preEvent);
+    assertRepositoriesEquals(repository, listener.postRepository);
+    assertSame(HandlerEvent.MODIFY, listener.postEvent);
+
+    repoManager.delete(repository);
+
+    assertRepositoriesEquals(repository, listener.preRepository);
+    assertSame(HandlerEvent.BEFORE_DELETE, listener.preEvent);
+    assertRepositoriesEquals(repository, listener.postRepository);
+    assertSame(HandlerEvent.DELETE, listener.postEvent);
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @throws IOException
+   * @throws RepositoryException
+   */
+  @Test
   public void testModify() throws RepositoryException, IOException
   {
     Repository heartOfGold = createTestRepository();
    *
    *
    * @version        Enter version here..., 13/01/29
-   * @author         Enter your name here...    
+   * @author         Enter your name here...
    */
   private static class CountingReceiveHook extends PreReceiveRepositoryHook
   {
    *
    *
    * @version        Enter version here..., 13/01/29
-   * @author         Enter your name here...    
+   * @author         Enter your name here...
+   */
+  private static class TestListener implements RepositoryListener
+  {
+
+    /**
+     * Method description
+     *
+     *
+     * @param repository
+     * @param event
+     */
+    @Override
+    public void onEvent(Repository repository, HandlerEvent event)
+    {
+      if (event.isPost())
+      {
+        this.postRepository = repository;
+        this.postEvent = event;
+      }
+      else if (event.isPre())
+      {
+        this.preRepository = repository;
+        this.preEvent = event;
+      }
+    }
+
+    //~--- fields -------------------------------------------------------------
+
+    /** Field description */
+    private HandlerEvent postEvent;
+
+    /** Field description */
+    private Repository postRepository;
+
+    /** Field description */
+    private HandlerEvent preEvent;
+
+    /** Field description */
+    private Repository preRepository;
+  }
+
+
+  /**
+   * Class description
+   *
+   *
+   * @version        Enter version here..., 13/01/29
+   * @author         Enter your name here...
    */
   private static class TestRepositoryHookEvent implements RepositoryHookEvent
   {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.