Add unit test for DeleteTrashFolders Change-Id: I1873f5e9e748b33edd42bd40d31a9ca8e7cf6d48
diff --git a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java index 14214c4..d80a677 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java +++ b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java
@@ -56,6 +56,7 @@ } private Set<Path> repoFolders; + private Thread thread; @Inject public DeleteTrashFolders( @@ -113,7 +114,8 @@ @Override public void start() { - new Thread( + thread = + new Thread( () -> { for (Path folder : repoFolders) { if (!folder.toFile().exists()) { @@ -127,8 +129,13 @@ } } }, - "DeleteTrashFolders") - .start(); + "DeleteTrashFolders"); + thread.start(); + } + + @VisibleForTesting + Thread getWorkerThread() { + return thread; } @Override
diff --git a/src/test/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFoldersTest.java b/src/test/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFoldersTest.java new file mode 100644 index 0000000..acb7a03 --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFoldersTest.java
@@ -0,0 +1,73 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.deleteproject.fs; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableList; +import com.google.gerrit.server.config.RepositoryConfig; +import com.google.gerrit.server.config.SitePaths; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.Config; +import org.eclipse.jgit.lib.Repository; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class DeleteTrashFoldersTest { + + @Mock private RepositoryConfig repositoryCfg; + + @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); + + private Path basePath; + private DeleteTrashFolders trashFolders; + + @Before + public void setUp() throws Exception { + SitePaths sitePaths = new SitePaths(tempFolder.newFolder("gerrit_site").toPath()); + basePath = sitePaths.resolve("base"); + Config cfg = new Config(); + cfg.setString("gerrit", null, "basePath", basePath.toString()); + when(repositoryCfg.getAllBasePaths()).thenReturn(ImmutableList.of()); + trashFolders = new DeleteTrashFolders(sitePaths, cfg, repositoryCfg); + } + + @Test + public void testStart() throws Exception { + FileRepository repoToDelete = createRepository("repo.1234567890123.deleted"); + FileRepository repoToKeep = createRepository("anotherRepo.git"); + trashFolders.start(); + trashFolders.getWorkerThread().join(); + assertThat(repoToDelete.getDirectory().exists()).isFalse(); + assertThat(repoToKeep.getDirectory().exists()).isTrue(); + } + + private FileRepository createRepository(String repoName) throws IOException { + Path repoPath = Files.createDirectories(basePath.resolve(repoName)); + Repository repository = new FileRepository(repoPath.toFile()); + repository.create(true); + return (FileRepository) repository; + } +}