mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Clean up hg cat command
This commit is contained in:
@@ -33,8 +33,7 @@
|
|||||||
|
|
||||||
package sonia.scm.repository.spi;
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
import com.aragost.javahg.commands.ExecutionException;
|
||||||
|
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
import com.google.common.io.Closeables;
|
import com.google.common.io.Closeables;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
@@ -45,50 +44,18 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
public class HgCatCommand extends AbstractCommand implements CatCommand {
|
||||||
|
|
||||||
/**
|
HgCatCommand(HgCommandContext context, Repository repository) {
|
||||||
*
|
|
||||||
* @author Sebastian Sdorra
|
|
||||||
*/
|
|
||||||
public class HgCatCommand extends AbstractCommand implements CatCommand
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs ...
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param context
|
|
||||||
* @param repository
|
|
||||||
*/
|
|
||||||
HgCatCommand(HgCommandContext context, Repository repository)
|
|
||||||
{
|
|
||||||
super(context, repository);
|
super(context, repository);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
* @param output
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void getCatResult(CatCommandRequest request, OutputStream output)
|
public void getCatResult(CatCommandRequest request, OutputStream output) throws IOException, RepositoryException {
|
||||||
throws IOException, RepositoryException
|
|
||||||
{
|
|
||||||
InputStream input = getCatResultStream(request);
|
InputStream input = getCatResultStream(request);
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
ByteStreams.copy(input, output);
|
ByteStreams.copy(input, output);
|
||||||
}
|
} finally {
|
||||||
finally
|
|
||||||
{
|
|
||||||
Closeables.close(input, true);
|
Closeables.close(input, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,6 +67,10 @@ public class HgCatCommand extends AbstractCommand implements CatCommand
|
|||||||
|
|
||||||
cmd.rev(HgUtil.getRevision(request.getRevision()));
|
cmd.rev(HgUtil.getRevision(request.getRevision()));
|
||||||
|
|
||||||
return cmd.execute(request.getPath());
|
try {
|
||||||
|
return cmd.execute(request.getPath());
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
throw new RepositoryException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,36 +33,21 @@
|
|||||||
|
|
||||||
package sonia.scm.repository.spi;
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import sonia.scm.repository.RepositoryException;
|
import sonia.scm.repository.RepositoryException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
/**
|
import static org.junit.Assert.assertEquals;
|
||||||
*
|
|
||||||
* @author Sebastian Sdorra
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
*/
|
|
||||||
public class HgCatCommandTest extends AbstractHgCommandTestBase
|
public class HgCatCommandTest extends AbstractHgCommandTestBase {
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testCat() throws IOException, RepositoryException
|
public void testCat() throws IOException, RepositoryException {
|
||||||
{
|
|
||||||
CatCommandRequest request = new CatCommandRequest();
|
CatCommandRequest request = new CatCommandRequest();
|
||||||
|
|
||||||
request.setPath("a.txt");
|
request.setPath("a.txt");
|
||||||
@@ -70,48 +55,39 @@ public class HgCatCommandTest extends AbstractHgCommandTestBase
|
|||||||
assertEquals("a", execute(request));
|
assertEquals("a", execute(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleCat() throws IOException, RepositoryException
|
public void testSimpleCat() throws IOException, RepositoryException {
|
||||||
{
|
|
||||||
CatCommandRequest request = new CatCommandRequest();
|
CatCommandRequest request = new CatCommandRequest();
|
||||||
|
|
||||||
request.setPath("b.txt");
|
request.setPath("b.txt");
|
||||||
assertEquals("b", execute(request));
|
assertEquals("b", execute(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Test(expected = RepositoryException.class)
|
||||||
* Method description
|
public void testUnknownFile() throws IOException, RepositoryException {
|
||||||
*
|
CatCommandRequest request = new CatCommandRequest();
|
||||||
*
|
|
||||||
* @param request
|
request.setPath("unknown");
|
||||||
*
|
execute(request);
|
||||||
* @return
|
}
|
||||||
*
|
|
||||||
* @throws IOException
|
@Test
|
||||||
* @throws RepositoryException
|
public void testSimpleStream() throws IOException, RepositoryException {
|
||||||
*/
|
CatCommandRequest request = new CatCommandRequest();
|
||||||
private String execute(CatCommandRequest request)
|
request.setPath("b.txt");
|
||||||
throws IOException, RepositoryException
|
|
||||||
{
|
InputStream catResultStream = new HgCatCommand(cmdContext, repository).getCatResultStream(request);
|
||||||
String content = null;
|
|
||||||
|
assertEquals('b', catResultStream.read());
|
||||||
|
assertEquals('\n', catResultStream.read());
|
||||||
|
assertEquals(-1, catResultStream.read());
|
||||||
|
|
||||||
|
catResultStream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String execute(CatCommandRequest request) throws IOException, RepositoryException {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
new HgCatCommand(cmdContext, repository).getCatResult(request, baos);
|
||||||
try
|
return baos.toString().trim();
|
||||||
{
|
|
||||||
new HgCatCommand(cmdContext, repository).getCatResult(request, baos);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
content = baos.toString().trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
return content;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user