From b4c854ee9994d923e3396325e6887fcbcda78314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Thu, 11 Oct 2018 09:55:47 +0200 Subject: [PATCH] Use more generic collection instead of list --- .../java/sonia/scm/repository/FileObject.java | 13 +- .../repository/spi/GitBrowseCommandTest.java | 153 ++++++------------ .../repository/spi/HgBrowseCommandTest.java | 39 ++--- .../repository/spi/SvnBrowseCommandTest.java | 39 ++--- 4 files changed, 80 insertions(+), 164 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/repository/FileObject.java b/scm-core/src/main/java/sonia/scm/repository/FileObject.java index 135cefcc7b..7dedebb13a 100644 --- a/scm-core/src/main/java/sonia/scm/repository/FileObject.java +++ b/scm-core/src/main/java/sonia/scm/repository/FileObject.java @@ -33,8 +33,6 @@ package sonia.scm.repository; -//~--- non-JDK imports -------------------------------------------------------- - import com.google.common.base.MoreObjects; import com.google.common.base.Objects; import com.google.common.base.Strings; @@ -46,9 +44,10 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; import java.util.List; -//~--- JDK imports ------------------------------------------------------------ +import static java.util.Collections.unmodifiableCollection; /** * The FileObject represents a file or a directory in a repository. @@ -303,12 +302,12 @@ public class FileObject implements LastModifiedAware, Serializable this.subRepository = subRepository; } - public List getChildren() { - return children; + public Collection getChildren() { + return unmodifiableCollection(children); } public void setChildren(List children) { - this.children = children; + this.children = new ArrayList<>(children); } public void addChild(FileObject child) { @@ -343,5 +342,5 @@ public class FileObject implements LastModifiedAware, Serializable @XmlElement(name = "subrepository") private SubRepository subRepository; - private List children = new ArrayList<>(); + private Collection children = new ArrayList<>(); } diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java index 7b00419a43..92b7ff69a9 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java @@ -6,13 +6,13 @@ * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. + * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. * 3. Neither the name of SCM-Manager; nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -26,15 +26,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * http://bitbucket.org/sdorra/scm-manager - * */ - package sonia.scm.repository.spi; -//~--- non-JDK imports -------------------------------------------------------- - import org.junit.Test; import sonia.scm.NotFoundException; import sonia.scm.repository.BrowserResult; @@ -42,22 +38,20 @@ import sonia.scm.repository.FileObject; import sonia.scm.repository.GitConstants; import java.io.IOException; -import java.util.List; +import java.util.Collection; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -//~--- JDK imports ------------------------------------------------------------ - /** * Unit tests for {@link GitBrowseCommand}. - * + * * @author Sebastian Sdorra */ -public class GitBrowseCommandTest extends AbstractGitCommandTestBase -{ +public class GitBrowseCommandTest extends AbstractGitCommandTestBase { @Test public void testGetFile() throws IOException, NotFoundException { @@ -67,40 +61,33 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase FileObject fileObject = result.getFile(); assertEquals("a.txt", fileObject.getName()); } - - /** - * Test browse command with default branch. - */ + @Test - public void testDefaultBranch() throws IOException, NotFoundException { + public void testDefaultDefaultBranch() throws IOException, NotFoundException { // without default branch, the repository head should be used FileObject root = createCommand().getBrowserResult(new BrowseCommandRequest()).getFile(); assertNotNull(root); - List foList = root.getChildren(); + Collection foList = root.getChildren(); assertNotNull(foList); assertFalse(foList.isEmpty()); - assertEquals(4, foList.size()); - - assertEquals("a.txt", foList.get(0).getName()); - assertEquals("b.txt", foList.get(1).getName()); - assertEquals("c", foList.get(2).getName()); - assertEquals("f.txt", foList.get(3).getName()); - - // set default branch and fetch again + assertThat(foList) + .extracting("name") + .containsExactly("a.txt", "b.txt", "c", "f.txt"); + } + + @Test + public void testExplicitDefaultBranch() throws IOException, NotFoundException { repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch"); - root = createCommand().getBrowserResult(new BrowseCommandRequest()).getFile(); + FileObject root = createCommand().getBrowserResult(new BrowseCommandRequest()).getFile(); assertNotNull(root); - foList = root.getChildren(); - assertNotNull(foList); - assertFalse(foList.isEmpty()); - assertEquals(2, foList.size()); - - assertEquals("a.txt", foList.get(0).getName()); - assertEquals("c", foList.get(1).getName()); + Collection foList = root.getChildren(); + assertThat(foList) + .extracting("name") + .containsExactly("a.txt", "c"); } @Test @@ -108,35 +95,18 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase FileObject root = createCommand().getBrowserResult(new BrowseCommandRequest()).getFile(); assertNotNull(root); - List foList = root.getChildren(); + Collection foList = root.getChildren(); - assertNotNull(foList); - assertFalse(foList.isEmpty()); - assertEquals(4, foList.size()); + FileObject a = findFile(foList, "a.txt"); + FileObject c = findFile(foList, "c"); - FileObject a = null; - FileObject c = null; - - for (FileObject f : foList) - { - if ("a.txt".equals(f.getName())) - { - a = f; - } - else if ("c".equals(f.getName())) - { - c = f; - } - } - - assertNotNull(a); assertFalse(a.isDirectory()); assertEquals("a.txt", a.getName()); assertEquals("a.txt", a.getPath()); assertEquals("added new line for blame", a.getDescription()); assertTrue(a.getLength() > 0); checkDate(a.getLastModified()); - assertNotNull(c); + assertTrue(c.isDirectory()); assertEquals("c", c.getName()); assertEquals("c", c.getPath()); @@ -149,38 +119,21 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase request.setPath("c"); FileObject root = createCommand().getBrowserResult(request).getFile(); - assertNotNull(root); + Collection foList = root.getChildren(); - List foList = root.getChildren(); + assertThat(foList).hasSize(2); - assertNotNull(foList); - assertFalse(foList.isEmpty()); - assertEquals(2, foList.size()); + FileObject d = findFile(foList, "d.txt"); + FileObject e = findFile(foList, "e.txt"); - FileObject d = null; - FileObject e = null; - - for (FileObject f : foList) - { - if ("d.txt".equals(f.getName())) - { - d = f; - } - else if ("e.txt".equals(f.getName())) - { - e = f; - } - } - - assertNotNull(d); assertFalse(d.isDirectory()); assertEquals("d.txt", d.getName()); assertEquals("c/d.txt", d.getPath()); assertEquals("added file d and e in folder c", d.getDescription()); assertTrue(d.getLength() > 0); checkDate(d.getLastModified()); - assertNotNull(e); + assertFalse(e.isDirectory()); assertEquals("e.txt", e.getName()); assertEquals("c/e.txt", e.getPath()); @@ -196,35 +149,29 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase request.setRecursive(true); FileObject root = createCommand().getBrowserResult(request).getFile(); - assertNotNull(root); + Collection foList = root.getChildren(); - List foList = root.getChildren(); + assertThat(foList) + .extracting("name") + .containsExactly("a.txt", "b.txt", "c", "f.txt"); - assertNotNull(foList); - assertFalse(foList.isEmpty()); + FileObject c = findFile(foList, "c"); - assertEquals(4, foList.size()); - - assertEquals("a.txt", foList.get(0).getName()); - assertEquals("b.txt", foList.get(1).getName()); - FileObject c = foList.get(2); - assertEquals("c", c.getName()); - assertEquals("f.txt", foList.get(3).getName()); - - List cChilds = c.getChildren(); - assertEquals("d.txt", cChilds.get(0).getName()); - assertEquals("e.txt", cChilds.get(1).getName()); + Collection cChildren = c.getChildren(); + assertThat(cChildren) + .extracting("name") + .containsExactly("d.txt", "e.txt"); } - /** - * Method description - * - * - * @return - */ - private GitBrowseCommand createCommand() - { + private FileObject findFile(Collection foList, String name) { + return foList.stream() + .filter(f -> name.equals(f.getName())) + .findFirst() + .orElseThrow(() -> new AssertionError("file " + name + " not found")); + } + + private GitBrowseCommand createCommand() { return new GitBrowseCommand(createContext(), repository); } } diff --git a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java index 4b52705613..32b536e69d 100644 --- a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java +++ b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java @@ -33,14 +33,12 @@ package sonia.scm.repository.spi; -//~--- non-JDK imports -------------------------------------------------------- - import org.junit.Test; import sonia.scm.repository.BrowserResult; import sonia.scm.repository.FileObject; import java.io.IOException; -import java.util.List; +import java.util.Collection; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -48,8 +46,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -//~--- JDK imports ------------------------------------------------------------ - /** * * @author Sebastian Sdorra @@ -68,7 +64,7 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase { @Test public void testBrowse() throws IOException { - List foList = getRootFromTip(new BrowseCommandRequest()); + Collection foList = getRootFromTip(new BrowseCommandRequest()); FileObject a = getFileObject(foList, "a.txt"); FileObject c = getFileObject(foList, "c"); @@ -96,7 +92,7 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase { FileObject c = result.getFile(); assertEquals("c", c.getName()); - List foList = c.getChildren(); + Collection foList = c.getChildren(); assertNotNull(foList); assertFalse(foList.isEmpty()); @@ -139,7 +135,7 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase { request.setDisableLastCommit(true); - List foList = getRootFromTip(request); + Collection foList = getRootFromTip(request); FileObject a = getFileObject(foList, "a.txt"); @@ -159,7 +155,7 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase { assertNotNull(result); FileObject root = result.getFile(); - List foList = root.getChildren(); + Collection foList = root.getChildren(); assertNotNull(foList); assertFalse(foList.isEmpty()); @@ -181,33 +177,22 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase { * * @return */ - private FileObject getFileObject(List foList, String name) + private FileObject getFileObject(Collection foList, String name) { - FileObject a = null; - - for (FileObject f : foList) - { - if (name.equals(f.getName())) - { - a = f; - - break; - } - } - - assertNotNull(a); - - return a; + return foList.stream() + .filter(f -> name.equals(f.getName())) + .findFirst() + .orElseThrow(() -> new AssertionError("file " + name + " not found")); } - private List getRootFromTip(BrowseCommandRequest request) throws IOException { + private Collection getRootFromTip(BrowseCommandRequest request) throws IOException { BrowserResult result = new HgBrowseCommand(cmdContext, repository).getBrowserResult(request); assertNotNull(result); FileObject root = result.getFile(); - List foList = root.getChildren(); + Collection foList = root.getChildren(); assertNotNull(foList); assertFalse(foList.isEmpty()); diff --git a/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java b/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java index 014f02bdbe..bcf5d2ec55 100644 --- a/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java +++ b/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java @@ -33,15 +33,13 @@ package sonia.scm.repository.spi; -//~--- non-JDK imports -------------------------------------------------------- - import org.junit.Test; import sonia.scm.repository.BrowserResult; import sonia.scm.repository.FileObject; import sonia.scm.repository.RevisionNotFoundException; import java.io.IOException; -import java.util.List; +import java.util.Collection; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -49,8 +47,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -//~--- JDK imports ------------------------------------------------------------ - /** * * @author Sebastian Sdorra @@ -70,7 +66,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase @Test public void testBrowse() throws RevisionNotFoundException { - List foList = getRootFromTip(new BrowseCommandRequest()); + Collection foList = getRootFromTip(new BrowseCommandRequest()); FileObject a = getFileObject(foList, "a.txt"); FileObject c = getFileObject(foList, "c"); @@ -102,7 +98,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase assertNotNull(result); - List foList = result.getFile().getChildren(); + Collection foList = result.getFile().getChildren(); assertNotNull(foList); assertFalse(foList.isEmpty()); @@ -145,7 +141,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase request.setDisableLastCommit(true); - List foList = getRootFromTip(request); + Collection foList = getRootFromTip(request); FileObject a = getFileObject(foList, "a.txt"); @@ -161,7 +157,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase assertNotNull(result); - List foList = result.getFile().getChildren(); + Collection foList = result.getFile().getChildren(); assertNotNull(foList); assertFalse(foList.isEmpty()); @@ -195,31 +191,20 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase * * @return */ - private FileObject getFileObject(List foList, String name) + private FileObject getFileObject(Collection foList, String name) { - FileObject a = null; - - for (FileObject f : foList) - { - if (name.equals(f.getName())) - { - a = f; - - break; - } - } - - assertNotNull(a); - - return a; + return foList.stream() + .filter(f -> name.equals(f.getName())) + .findFirst() + .orElseThrow(() -> new AssertionError("file " + name + " not found")); } - private List getRootFromTip(BrowseCommandRequest request) throws RevisionNotFoundException { + private Collection getRootFromTip(BrowseCommandRequest request) throws RevisionNotFoundException { BrowserResult result = createCommand().getBrowserResult(request); assertNotNull(result); - List foList = result.getFile().getChildren(); + Collection foList = result.getFile().getChildren(); assertNotNull(foList); assertFalse(foList.isEmpty());