From 4868740ed7f40ce0cf162f17a8224856ae10a4b0 Mon Sep 17 00:00:00 2001 From: Kevin Walter Date: Tue, 15 Apr 2014 02:00:42 +0200 Subject: fix NullPointerException in ITicketService and add TestCase --- .../java/com/gitblit/tickets/ITicketService.java | 37 ++++++++++++---------- .../java/com/gitblit/tests/TicketServiceTest.java | 4 +++ 2 files changed, 24 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/main/java/com/gitblit/tickets/ITicketService.java b/src/main/java/com/gitblit/tickets/ITicketService.java index 9522e420..7d699757 100644 --- a/src/main/java/com/gitblit/tickets/ITicketService.java +++ b/src/main/java/com/gitblit/tickets/ITicketService.java @@ -713,27 +713,30 @@ public abstract class ITicketService { public final TicketModel getTicket(RepositoryModel repository, long ticketId) { TicketKey key = new TicketKey(repository, ticketId); TicketModel ticket = ticketsCache.getIfPresent(key); - + + // if ticket not cached if (ticket == null) { - // load & cache ticket + //load ticket ticket = getTicketImpl(repository, ticketId); - if (ticket.hasPatchsets()) { - Repository r = repositoryManager.getRepository(repository.name); - try { - Patchset patchset = ticket.getCurrentPatchset(); - DiffStat diffStat = DiffUtils.getDiffStat(r, patchset.base, patchset.tip); - // diffstat could be null if we have ticket data without the - // commit objects. e.g. ticket replication without repo - // mirroring - if (diffStat != null) { - ticket.insertions = diffStat.getInsertions(); - ticket.deletions = diffStat.getDeletions(); + // if ticket exists + if (ticket != null) { + if (ticket.hasPatchsets()) { + Repository r = repositoryManager.getRepository(repository.name); + try { + Patchset patchset = ticket.getCurrentPatchset(); + DiffStat diffStat = DiffUtils.getDiffStat(r, patchset.base, patchset.tip); + // diffstat could be null if we have ticket data without the + // commit objects. e.g. ticket replication without repo + // mirroring + if (diffStat != null) { + ticket.insertions = diffStat.getInsertions(); + ticket.deletions = diffStat.getDeletions(); + } + } finally { + r.close(); } - } finally { - r.close(); } - } - if (ticket != null) { + //cache ticket ticketsCache.put(key, ticket); } } diff --git a/src/test/java/com/gitblit/tests/TicketServiceTest.java b/src/test/java/com/gitblit/tests/TicketServiceTest.java index 5f94a46e..d91ce533 100644 --- a/src/test/java/com/gitblit/tests/TicketServiceTest.java +++ b/src/test/java/com/gitblit/tests/TicketServiceTest.java @@ -92,6 +92,10 @@ public abstract class TicketServiceTest extends GitblitUnitTest { @Test public void testLifecycle() throws Exception { + // query non-existent ticket + TicketModel nonExistent = service.getTicket(getRepository(), 0); + assertNull(nonExistent); + // create and insert a ticket Change c1 = newChange("testCreation() " + Long.toHexString(System.currentTimeMillis())); TicketModel ticket = service.createTicket(getRepository(), c1); -- cgit v1.2.3