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

@@ -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);
}
}