Fixed a bug that might cause the wrong episode to be auto-deleted

This commit is contained in:
daniel oeh 2013-10-09 19:52:33 +02:00
parent 09b93a8b4b
commit df9f6661e1
4 changed files with 90 additions and 45 deletions

View File

@ -509,7 +509,7 @@ public final class DBTasks {
for (FeedItem item : delete) { for (FeedItem item : delete) {
try { try {
DBWriter.deleteFeedMediaOfItem(context, item.getId()).get(); DBWriter.deleteFeedMediaOfItem(context, item.getMedia().getId()).get();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} catch (ExecutionException e) { } catch (ExecutionException e) {

View File

@ -4,10 +4,9 @@ import android.content.Context;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.storage.DBReader; import de.danoeh.antennapod.storage.DBReader;
import de.danoeh.antennapod.storage.PodDBAdapter; import de.danoeh.antennapod.storage.PodDBAdapter;
import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator; import static instrumentationTest.de.test.antennapod.storage.DBTestUtils.*;
import java.util.*; import java.util.*;
@ -65,46 +64,11 @@ public class DBReaderTest extends InstrumentationTestCase {
expiredFeedListTestHelper(System.currentTimeMillis() - expirationTime / 2, expirationTime, false); expiredFeedListTestHelper(System.currentTimeMillis() - expirationTime / 2, expirationTime, false);
} }
private List<Feed> 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<Feed> feeds = new ArrayList<Feed>();
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<FeedItem>());
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() { public void testGetFeedList() {
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(10, 0, false); List<Feed> feeds = saveFeedlist(context, 10, 0, false);
List<Feed> savedFeeds = DBReader.getFeedList(context); List<Feed> savedFeeds = DBReader.getFeedList(context);
assertNotNull(savedFeeds); assertNotNull(savedFeeds);
assertTrue(savedFeeds.size() == feeds.size()); assertTrue(savedFeeds.size() == feeds.size());
@ -115,7 +79,7 @@ public class DBReaderTest extends InstrumentationTestCase {
public void testFeedListDownloadUrls() { public void testFeedListDownloadUrls() {
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(10, 0, false); List<Feed> feeds = saveFeedlist(context, 10, 0, false);
List<String> urls = DBReader.getFeedListDownloadUrls(context); List<String> urls = DBReader.getFeedListDownloadUrls(context);
assertNotNull(urls); assertNotNull(urls);
assertTrue(urls.size() == feeds.size()); assertTrue(urls.size() == feeds.size());
@ -128,7 +92,7 @@ public class DBReaderTest extends InstrumentationTestCase {
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
final int numFeeds = 10; final int numFeeds = 10;
final int numItems = 1; final int numItems = 1;
List<Feed> feeds = saveFeedlist(numFeeds, numItems, false); List<Feed> feeds = saveFeedlist(context, numFeeds, numItems, false);
List<FeedItem> items = new ArrayList<FeedItem>(); List<FeedItem> items = new ArrayList<FeedItem>();
for (Feed f : feeds) { for (Feed f : feeds) {
for (FeedItem item : f.getItems()) { for (FeedItem item : f.getItems()) {
@ -152,7 +116,7 @@ public class DBReaderTest extends InstrumentationTestCase {
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
final int numFeeds = 1; final int numFeeds = 1;
final int numItems = 10; final int numItems = 10;
Feed feed = saveFeedlist(numFeeds, numItems, false).get(0); Feed feed = saveFeedlist(context, numFeeds, numItems, false).get(0);
List<FeedItem> items = feed.getItems(); List<FeedItem> items = feed.getItems();
feed.setItems(null); feed.setItems(null);
List<FeedItem> savedItems = DBReader.getFeedItemList(context, feed); List<FeedItem> savedItems = DBReader.getFeedItemList(context, feed);
@ -168,7 +132,7 @@ public class DBReaderTest extends InstrumentationTestCase {
throw new IllegalArgumentException("numItems<=0"); throw new IllegalArgumentException("numItems<=0");
} }
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(numItems, numItems, false); List<Feed> feeds = saveFeedlist(context, numItems, numItems, false);
List<FeedItem> allItems = new ArrayList<FeedItem>(); List<FeedItem> allItems = new ArrayList<FeedItem>();
for (Feed f : feeds) { for (Feed f : feeds) {
allItems.addAll(f.getItems()); allItems.addAll(f.getItems());
@ -220,7 +184,7 @@ public class DBReaderTest extends InstrumentationTestCase {
throw new IllegalArgumentException("numItems<=0"); throw new IllegalArgumentException("numItems<=0");
} }
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(numItems, numItems, true); List<Feed> feeds = saveFeedlist(context, numItems, numItems, true);
List<FeedItem> items = new ArrayList<FeedItem>(); List<FeedItem> items = new ArrayList<FeedItem>();
for (Feed f : feeds) { for (Feed f : feeds) {
items.addAll(f.getItems()); items.addAll(f.getItems());
@ -263,7 +227,7 @@ public class DBReaderTest extends InstrumentationTestCase {
throw new IllegalArgumentException("numItems<=0"); throw new IllegalArgumentException("numItems<=0");
} }
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(numItems, numItems, true); List<Feed> feeds = saveFeedlist(context, numItems, numItems, true);
List<FeedItem> items = new ArrayList<FeedItem>(); List<FeedItem> items = new ArrayList<FeedItem>();
for (Feed f : feeds) { for (Feed f : feeds) {
items.addAll(f.getItems()); items.addAll(f.getItems());

View File

@ -19,6 +19,8 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static instrumentationTest.de.test.antennapod.storage.DBTestUtils.*;
/** /**
* Test class for DBTasks * 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<Feed> 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() { public void testUpdateFeedNewFeed() {
final Context context = getInstrumentation().getTargetContext(); final Context context = getInstrumentation().getTargetContext();
final int NUM_ITEMS = 10; final int NUM_ITEMS = 10;

View File

@ -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<Feed> 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<Feed> feeds = new ArrayList<Feed>();
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<FeedItem>());
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;
}
}