improve exception handling

This commit is contained in:
Sebastian Sdorra
2013-05-10 13:02:35 +02:00
parent f4a3bc57c3
commit fb59c5cd56
6 changed files with 111 additions and 7 deletions

View File

@@ -33,16 +33,20 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.aragost.javahg.commands.ExecutionException;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.spi.javahg.HgIncomingChangesetCommand;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.util.Collections;
import java.util.List;
/**
@@ -53,6 +57,11 @@ public class HgIncomingCommand extends AbstractCommand
implements IncomingCommand
{
/** Field description */
private static final int NO_INCOMING_CHANGESETS = 1;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
@@ -77,10 +86,13 @@ public class HgIncomingCommand extends AbstractCommand
* @param request
*
* @return
*
* @throws RepositoryException
*/
@Override
public ChangesetPagingResult getIncomingChangesets(
IncomingCommandRequest request)
throws RepositoryException
{
File remoteRepository = handler.getDirectory(request.getRemoteRepository());
@@ -88,8 +100,25 @@ public class HgIncomingCommand extends AbstractCommand
// TODO implement paging
List<Changeset> changesets =
on(repository).execute(remoteRepository.getAbsolutePath());
List<Changeset> changesets;
try
{
changesets = on(repository).execute(remoteRepository.getAbsolutePath());
}
catch (ExecutionException ex)
{
if (ex.getCommand().getReturnCode() == NO_INCOMING_CHANGESETS)
{
changesets = Collections.EMPTY_LIST;
}
else
{
throw new RepositoryException("could not execute incoming command", ex);
}
}
return new ChangesetPagingResult(changesets.size(), changesets);
}

View File

@@ -38,6 +38,7 @@ import com.aragost.javahg.Changeset;
import org.junit.Test;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.RepositoryException;
import static org.junit.Assert.*;
@@ -57,9 +58,11 @@ public class HgIncomingCommandTest extends IncomingOutgoingTestBase
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testGetIncomingChangesets() throws IOException
public void testGetIncomingChangesets()
throws IOException, RepositoryException
{
writeNewFile(outgoing, outgoingDirectory, "a.txt", "Content of file a.txt");
writeNewFile(outgoing, outgoingDirectory, "b.txt", "Content of file b.txt");
@@ -84,6 +87,56 @@ public class HgIncomingCommandTest extends IncomingOutgoingTestBase
assertChangesetsEqual(c2, cpr.getChangesets().get(1));
}
/**
* Method description
*
*
* @throws RepositoryException
*/
@Test
public void testGetIncomingChangesetsWithEmptyRepository()
throws RepositoryException
{
HgIncomingCommand cmd = createIncomingCommand();
IncomingCommandRequest request = new IncomingCommandRequest();
request.setRemoteRepository(outgoingRepository);
ChangesetPagingResult cpr = cmd.getIncomingChangesets(request);
assertNotNull(cpr);
assertEquals(0, cpr.getTotal());
}
/**
* Method description
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test(expected = RepositoryException.class)
public void testGetIncomingChangesetsWithUnrelatedRepository()
throws IOException, RepositoryException
{
writeNewFile(outgoing, outgoingDirectory, "a.txt", "Content of file a.txt");
writeNewFile(outgoing, outgoingDirectory, "b.txt", "Content of file b.txt");
commit(outgoing, "added a and b");
writeNewFile(incoming, incomingDirectory, "c.txt", "Content of file c.txt");
writeNewFile(incoming, incomingDirectory, "d.txt", "Content of file d.txt");
commit(incoming, "added c and d");
HgIncomingCommand cmd = createIncomingCommand();
IncomingCommandRequest request = new IncomingCommandRequest();
request.setRemoteRepository(outgoingRepository);
cmd.getIncomingChangesets(request);
}
/**
* Method description
*