diff --git a/src/de/danoeh/antennapod/storage/DBTasks.java b/src/de/danoeh/antennapod/storage/DBTasks.java index 6b611bbbf..26d5c712a 100644 --- a/src/de/danoeh/antennapod/storage/DBTasks.java +++ b/src/de/danoeh/antennapod/storage/DBTasks.java @@ -509,7 +509,7 @@ public final class DBTasks { for (FeedItem item : delete) { try { - DBWriter.deleteFeedMediaOfItem(context, item.getId()).get(); + DBWriter.deleteFeedMediaOfItem(context, item.getMedia().getId()).get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java index 445092ebc..91ac61867 100644 --- a/src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java +++ b/src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java @@ -4,10 +4,9 @@ import android.content.Context; import android.test.InstrumentationTestCase; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.storage.DBReader; import de.danoeh.antennapod.storage.PodDBAdapter; -import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator; +import static instrumentationTest.de.test.antennapod.storage.DBTestUtils.*; import java.util.*; @@ -65,46 +64,11 @@ public class DBReaderTest extends InstrumentationTestCase { expiredFeedListTestHelper(System.currentTimeMillis() - expirationTime / 2, expirationTime, false); } - private List saveFeedlist(int numFeeds, int numItems, boolean withMedia) { - if (numFeeds <= 0) { - throw new IllegalArgumentException("numFeeds<=0"); - } - if (numItems < 0) { - throw new IllegalArgumentException("numItems<0"); - } - final Context context = getInstrumentation().getTargetContext(); - List feeds = new ArrayList(); - PodDBAdapter adapter = new PodDBAdapter(context); - adapter.open(); - for (int i = 0; i < numFeeds; i++) { - Feed f = new Feed(0, new Date(), "feed " + i, "link" + i, "descr", null, null, - null, null, "id" + i, null, null, "url" + i, false); - f.setItems(new ArrayList()); - for (int j = 0; j < numItems; j++) { - FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(), - true, f); - if (withMedia) { - FeedMedia media = new FeedMedia(item, "url" + j, 1, "audio/mp3"); - item.setMedia(media); - } - f.getItems().add(item); - } - Collections.sort(f.getItems(), new FeedItemPubdateComparator()); - adapter.setCompleteFeed(f); - assertTrue(f.getId() != 0); - for (FeedItem item : f.getItems()) { - assertTrue(item.getId() != 0); - } - feeds.add(f); - } - adapter.close(); - return feeds; - } public void testGetFeedList() { final Context context = getInstrumentation().getTargetContext(); - List feeds = saveFeedlist(10, 0, false); + List feeds = saveFeedlist(context, 10, 0, false); List savedFeeds = DBReader.getFeedList(context); assertNotNull(savedFeeds); assertTrue(savedFeeds.size() == feeds.size()); @@ -115,7 +79,7 @@ public class DBReaderTest extends InstrumentationTestCase { public void testFeedListDownloadUrls() { final Context context = getInstrumentation().getTargetContext(); - List feeds = saveFeedlist(10, 0, false); + List feeds = saveFeedlist(context, 10, 0, false); List urls = DBReader.getFeedListDownloadUrls(context); assertNotNull(urls); assertTrue(urls.size() == feeds.size()); @@ -128,7 +92,7 @@ public class DBReaderTest extends InstrumentationTestCase { final Context context = getInstrumentation().getTargetContext(); final int numFeeds = 10; final int numItems = 1; - List feeds = saveFeedlist(numFeeds, numItems, false); + List feeds = saveFeedlist(context, numFeeds, numItems, false); List items = new ArrayList(); for (Feed f : feeds) { for (FeedItem item : f.getItems()) { @@ -152,7 +116,7 @@ public class DBReaderTest extends InstrumentationTestCase { final Context context = getInstrumentation().getTargetContext(); final int numFeeds = 1; final int numItems = 10; - Feed feed = saveFeedlist(numFeeds, numItems, false).get(0); + Feed feed = saveFeedlist(context, numFeeds, numItems, false).get(0); List items = feed.getItems(); feed.setItems(null); List savedItems = DBReader.getFeedItemList(context, feed); @@ -168,7 +132,7 @@ public class DBReaderTest extends InstrumentationTestCase { throw new IllegalArgumentException("numItems<=0"); } final Context context = getInstrumentation().getTargetContext(); - List feeds = saveFeedlist(numItems, numItems, false); + List feeds = saveFeedlist(context, numItems, numItems, false); List allItems = new ArrayList(); for (Feed f : feeds) { allItems.addAll(f.getItems()); @@ -220,7 +184,7 @@ public class DBReaderTest extends InstrumentationTestCase { throw new IllegalArgumentException("numItems<=0"); } final Context context = getInstrumentation().getTargetContext(); - List feeds = saveFeedlist(numItems, numItems, true); + List feeds = saveFeedlist(context, numItems, numItems, true); List items = new ArrayList(); for (Feed f : feeds) { items.addAll(f.getItems()); @@ -263,7 +227,7 @@ public class DBReaderTest extends InstrumentationTestCase { throw new IllegalArgumentException("numItems<=0"); } final Context context = getInstrumentation().getTargetContext(); - List feeds = saveFeedlist(numItems, numItems, true); + List feeds = saveFeedlist(context, numItems, numItems, true); List items = new ArrayList(); for (Feed f : feeds) { items.addAll(f.getItems()); diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java index 3f928bd9b..e9b871867 100644 --- a/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java +++ b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java @@ -19,6 +19,8 @@ import java.util.Collections; import java.util.Date; import java.util.List; +import static instrumentationTest.de.test.antennapod.storage.DBTestUtils.*; + /** * Test class for DBTasks */ @@ -167,6 +169,29 @@ public class DBTasksTest extends InstrumentationTestCase { } } + /** + * Reproduces a bug where DBTasks.performAutoCleanup(android.content.Context) would use the ID of the FeedItem in the + * call to DBWriter.deleteFeedMediaOfItem instead of the ID of the FeedMedia. This would cause the wrong item to be deleted. + * @throws IOException + */ + public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue_withFeedsWithNoMedia() throws IOException { + final Context context = getInstrumentation().getTargetContext(); + // add feed with no enclosures so that item ID != media ID + saveFeedlist(context, 1, 10, false); + + // add candidate for performAutoCleanup + List feeds = saveFeedlist(getInstrumentation().getTargetContext(), 1, 1, true); + FeedMedia m = feeds.get(0).getItems().get(0).getMedia(); + m.setDownloaded(true); + m.setFile_url("file"); + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + adapter.setMedia(m); + adapter.close(); + + testPerformAutoCleanupShouldNotDeleteBecauseInQueue(); + } + public void testUpdateFeedNewFeed() { final Context context = getInstrumentation().getTargetContext(); final int NUM_ITEMS = 10; diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBTestUtils.java b/src/instrumentationTest/de/test/antennapod/storage/DBTestUtils.java new file mode 100644 index 000000000..fbb7b0386 --- /dev/null +++ b/src/instrumentationTest/de/test/antennapod/storage/DBTestUtils.java @@ -0,0 +1,56 @@ +package instrumentationTest.de.test.antennapod.storage; + +import android.content.Context; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.storage.PodDBAdapter; +import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator; +import junit.framework.Assert; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +/** + * Utility methods for DB* tests. + */ +public class DBTestUtils { + + public static List saveFeedlist(Context context, int numFeeds, int numItems, boolean withMedia) { + if (numFeeds <= 0) { + throw new IllegalArgumentException("numFeeds<=0"); + } + if (numItems < 0) { + throw new IllegalArgumentException("numItems<0"); + } + + List feeds = new ArrayList(); + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + for (int i = 0; i < numFeeds; i++) { + Feed f = new Feed(0, new Date(), "feed " + i, "link" + i, "descr", null, null, + null, null, "id" + i, null, null, "url" + i, false); + f.setItems(new ArrayList()); + for (int j = 0; j < numItems; j++) { + FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(), + true, f); + if (withMedia) { + FeedMedia media = new FeedMedia(item, "url" + j, 1, "audio/mp3"); + item.setMedia(media); + } + f.getItems().add(item); + } + Collections.sort(f.getItems(), new FeedItemPubdateComparator()); + adapter.setCompleteFeed(f); + Assert.assertTrue(f.getId() != 0); + for (FeedItem item : f.getItems()) { + Assert.assertTrue(item.getId() != 0); + } + feeds.add(f); + } + adapter.close(); + return feeds; + } +}