Use more generic collection instead of list

This commit is contained in:
René Pfeuffer
2018-10-11 09:55:47 +02:00
parent e296812457
commit b4c854ee99
4 changed files with 80 additions and 164 deletions

View File

@@ -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<FileObject> getChildren() {
return children;
public Collection<FileObject> getChildren() {
return unmodifiableCollection(children);
}
public void setChildren(List<FileObject> 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<FileObject> children = new ArrayList<>();
private Collection<FileObject> children = new ArrayList<>();
}

View File

@@ -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<FileObject> foList = root.getChildren();
Collection<FileObject> 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<FileObject> 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<FileObject> foList = root.getChildren();
Collection<FileObject> 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<FileObject> foList = root.getChildren();
List<FileObject> 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<FileObject> foList = root.getChildren();
List<FileObject> 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<FileObject> cChilds = c.getChildren();
assertEquals("d.txt", cChilds.get(0).getName());
assertEquals("e.txt", cChilds.get(1).getName());
Collection<FileObject> cChildren = c.getChildren();
assertThat(cChildren)
.extracting("name")
.containsExactly("d.txt", "e.txt");
}
/**
* Method description
*
*
* @return
*/
private GitBrowseCommand createCommand()
{
private FileObject findFile(Collection<FileObject> 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);
}
}

View File

@@ -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<FileObject> foList = getRootFromTip(new BrowseCommandRequest());
Collection<FileObject> 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<FileObject> foList = c.getChildren();
Collection<FileObject> foList = c.getChildren();
assertNotNull(foList);
assertFalse(foList.isEmpty());
@@ -139,7 +135,7 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase {
request.setDisableLastCommit(true);
List<FileObject> foList = getRootFromTip(request);
Collection<FileObject> 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<FileObject> foList = root.getChildren();
Collection<FileObject> foList = root.getChildren();
assertNotNull(foList);
assertFalse(foList.isEmpty());
@@ -181,33 +177,22 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase {
*
* @return
*/
private FileObject getFileObject(List<FileObject> foList, String name)
private FileObject getFileObject(Collection<FileObject> 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<FileObject> getRootFromTip(BrowseCommandRequest request) throws IOException {
private Collection<FileObject> getRootFromTip(BrowseCommandRequest request) throws IOException {
BrowserResult result = new HgBrowseCommand(cmdContext,
repository).getBrowserResult(request);
assertNotNull(result);
FileObject root = result.getFile();
List<FileObject> foList = root.getChildren();
Collection<FileObject> foList = root.getChildren();
assertNotNull(foList);
assertFalse(foList.isEmpty());

View File

@@ -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<FileObject> foList = getRootFromTip(new BrowseCommandRequest());
Collection<FileObject> 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<FileObject> foList = result.getFile().getChildren();
Collection<FileObject> foList = result.getFile().getChildren();
assertNotNull(foList);
assertFalse(foList.isEmpty());
@@ -145,7 +141,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
request.setDisableLastCommit(true);
List<FileObject> foList = getRootFromTip(request);
Collection<FileObject> foList = getRootFromTip(request);
FileObject a = getFileObject(foList, "a.txt");
@@ -161,7 +157,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
assertNotNull(result);
List<FileObject> foList = result.getFile().getChildren();
Collection<FileObject> foList = result.getFile().getChildren();
assertNotNull(foList);
assertFalse(foList.isEmpty());
@@ -195,31 +191,20 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
*
* @return
*/
private FileObject getFileObject(List<FileObject> foList, String name)
private FileObject getFileObject(Collection<FileObject> 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<FileObject> getRootFromTip(BrowseCommandRequest request) throws RevisionNotFoundException {
private Collection<FileObject> getRootFromTip(BrowseCommandRequest request) throws RevisionNotFoundException {
BrowserResult result = createCommand().getBrowserResult(request);
assertNotNull(result);
List<FileObject> foList = result.getFile().getChildren();
Collection<FileObject> foList = result.getFile().getChildren();
assertNotNull(foList);
assertFalse(foList.isEmpty());