From c0066b9d64ef0454f9eb984ffe6a8714c278ad7a Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 14 Aug 2013 14:38:24 +0200 Subject: Added test classes for DB*-classes --- src/de/danoeh/antennapod/feed/Feed.java | 677 +++++++++++---------- src/de/danoeh/antennapod/storage/DBReader.java | 43 +- src/de/danoeh/antennapod/storage/DBWriter.java | 4 +- src/de/danoeh/antennapod/storage/PodDBAdapter.java | 18 +- .../de/test/antennapod/AntennaPodTestRunner.java | 1 + .../de/test/antennapod/storage/DBReaderTest.java | 11 + .../de/test/antennapod/storage/DBTasksTest.java | 9 + .../de/test/antennapod/storage/DBWriterTest.java | 473 ++++++++++++++ 8 files changed, 875 insertions(+), 361 deletions(-) create mode 100644 src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java create mode 100644 src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java create mode 100644 src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java (limited to 'src') diff --git a/src/de/danoeh/antennapod/feed/Feed.java b/src/de/danoeh/antennapod/feed/Feed.java index 0f7ebc63a..9cee1a86a 100644 --- a/src/de/danoeh/antennapod/feed/Feed.java +++ b/src/de/danoeh/antennapod/feed/Feed.java @@ -10,330 +10,363 @@ import de.danoeh.antennapod.util.EpisodeFilter; /** * Data Object for a whole feed - * + * * @author daniel - * */ public class Feed extends FeedFile { - public static final int FEEDFILETYPE_FEED = 0; - public static final String TYPE_RSS2 = "rss"; - public static final String TYPE_RSS091 = "rss"; - public static final String TYPE_ATOM1 = "atom"; - - private String title; - /** Contains 'id'-element in Atom feed. */ - private String feedIdentifier; - /** Link to the website. */ - private String link; - private String description; - private String language; - /** Name of the author */ - private String author; - private FeedImage image; - private List items; - /** Date of last refresh. */ - private Date lastUpdate; - private String paymentLink; - /** Feed type, for example RSS 2 or Atom */ - private String type; - - public Feed(Date lastUpdate) { - super(); - items = Collections.synchronizedList(new ArrayList()); - this.lastUpdate = lastUpdate; - } - - /** - * This constructor is used for requesting a feed download. It should NOT be - * used if the title of the feed is already known. - * */ - public Feed(String url, Date lastUpdate) { - this(lastUpdate); - this.download_url = url; - } - - /** - * This constructor is used for requesting a feed download. It should be - * used if the title of the feed is already known. - * */ - public Feed(String url, Date lastUpdate, String title) { - this(url, lastUpdate); - this.title = title; - } - - /** - * Returns the number of FeedItems where 'read' is false. If the 'display - * only episodes' - preference is set to true, this method will only count - * items with episodes. - * */ - public int getNumOfNewItems() { - int count = 0; - for (FeedItem item : items) { - if (item.getState() == FeedItem.State.NEW) { - if (!UserPreferences.isDisplayOnlyEpisodes() - || item.getMedia() != null) { - count++; - } - } - } - return count; - } - - /** - * Returns the number of FeedItems where the media started to play but - * wasn't finished yet. - * */ - public int getNumOfStartedItems() { - int count = 0; - - for (FeedItem item : items) { - FeedItem.State state = item.getState(); - if (state == FeedItem.State.IN_PROGRESS - || state == FeedItem.State.PLAYING) { - count++; - } - } - return count; - } - - /** - * Returns true if at least one item in the itemlist is unread. - * - * @param enableEpisodeFilter - * true if this method should only count items with episodes if - * the 'display only episodes' - preference is set to true by the - * user. - */ - public boolean hasNewItems(boolean enableEpisodeFilter) { - for (FeedItem item : items) { - if (item.getState() == FeedItem.State.NEW) { - if (!(enableEpisodeFilter && UserPreferences - .isDisplayOnlyEpisodes()) || item.getMedia() != null) { - return true; - } - } - } - return false; - } - - /** - * Returns the number of FeedItems. - * - * @param enableEpisodeFilter - * true if this method should only count items with episodes if - * the 'display only episodes' - preference is set to true by the - * user. - * */ - public int getNumOfItems(boolean enableEpisodeFilter) { - if (enableEpisodeFilter && UserPreferences.isDisplayOnlyEpisodes()) { - return EpisodeFilter.countItemsWithEpisodes(items); - } else { - return items.size(); - } - } - - /** - * Returns the item at the specified index. - * - * @param enableEpisodeFilter - * true if this method should ignore items without episdodes if - * the episodes filter has been enabled by the user. - */ - public FeedItem getItemAtIndex(boolean enableEpisodeFilter, int position) { - if (enableEpisodeFilter && UserPreferences.isDisplayOnlyEpisodes()) { - return EpisodeFilter.accessEpisodeByIndex(items, position); - } else { - return items.get(position); - } - } - - /** - * Returns the value that uniquely identifies this Feed. If the - * feedIdentifier attribute is not null, it will be returned. Else it will - * try to return the title. If the title is not given, it will use the link - * of the feed. - * */ - public String getIdentifyingValue() { - if (feedIdentifier != null && !feedIdentifier.isEmpty()) { - return feedIdentifier; - } else if (title != null && !title.isEmpty()) { - return title; - } else { - return link; - } - } - - @Override - public String getHumanReadableIdentifier() { - if (title != null) { - return title; - } else { - return download_url; - } - } - - public void updateFromOther(Feed other) { - super.updateFromOther(other); - if (other.title != null) { - title = other.title; - } - if (other.feedIdentifier != null) { - feedIdentifier = other.feedIdentifier; - } - if (other.link != null) { - link = other.link; - } - if (other.description != null) { - description = other.description; - } - if (other.language != null) { - language = other.language; - } - if (other.author != null) { - author = other.author; - } - if (other.paymentLink != null) { - paymentLink = other.paymentLink; - } - } - - public boolean compareWithOther(Feed other) { - if (super.compareWithOther(other)) { - return true; - } - if (!title.equals(other.title)) { - return true; - } - if (other.feedIdentifier != null) { - if (feedIdentifier == null - || !feedIdentifier.equals(other.feedIdentifier)) { - return true; - } - } - if (other.link != null) { - if (link == null || !link.equals(other.link)) { - return true; - } - } - if (other.description != null) { - if (description == null || !description.equals(other.description)) { - return true; - } - } - if (other.language != null) { - if (language == null || !language.equals(other.language)) { - return true; - } - } - if (other.author != null) { - if (author == null || !author.equals(other.author)) { - return true; - } - } - if (other.paymentLink != null) { - if (paymentLink == null || !paymentLink.equals(other.paymentLink)) { - return true; - } - } - return false; - } - - @Override - public int getTypeAsInt() { - return FEEDFILETYPE_FEED; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getLink() { - return link; - } - - public void setLink(String link) { - this.link = link; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public FeedImage getImage() { - return image; - } - - public void setImage(FeedImage image) { - this.image = image; - } - - public List getItems() { - return items; - } - - public void setItems(List list) { - this.items = Collections.synchronizedList(list); - } - - /** Returns an array that contains all the feeditems of this feed. */ - public FeedItem[] getItemsArray() { - return items.toArray(new FeedItem[items.size()]); - } - - public Date getLastUpdate() { - return lastUpdate; - } - - public void setLastUpdate(Date lastUpdate) { - this.lastUpdate = lastUpdate; - } - - public String getFeedIdentifier() { - return feedIdentifier; - } - - public void setFeedIdentifier(String feedIdentifier) { - this.feedIdentifier = feedIdentifier; - } - - public String getPaymentLink() { - return paymentLink; - } - - public void setPaymentLink(String paymentLink) { - this.paymentLink = paymentLink; - } - - public String getLanguage() { - return language; - } - - public void setLanguage(String language) { - this.language = language; - } - - public String getAuthor() { - return author; - } - - public void setAuthor(String author) { - this.author = author; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } + public static final int FEEDFILETYPE_FEED = 0; + public static final String TYPE_RSS2 = "rss"; + public static final String TYPE_RSS091 = "rss"; + public static final String TYPE_ATOM1 = "atom"; + + private String title; + /** + * Contains 'id'-element in Atom feed. + */ + private String feedIdentifier; + /** + * Link to the website. + */ + private String link; + private String description; + private String language; + /** + * Name of the author + */ + private String author; + private FeedImage image; + private List items; + /** + * Date of last refresh. + */ + private Date lastUpdate; + private String paymentLink; + /** + * Feed type, for example RSS 2 or Atom + */ + private String type; + + /** + * This constructor is used for restoring a feed from the database. + */ + public Feed(long id, Date lastUpdate, String title, String link, String description, String paymentLink, + String author, String language, String type, String feedIdentifier, FeedImage image, String fileUrl, + String downloadUrl, boolean downloaded) { + super(fileUrl, downloadUrl, downloaded); + this.id = id; + this.title = title; + this.lastUpdate = lastUpdate; + this.link = link; + this.description = description; + this.paymentLink = paymentLink; + this.author = author; + this.language = language; + this.type = type; + this.feedIdentifier = feedIdentifier; + this.image = image; + + items = new ArrayList(); + } + + /** + * This constructor can be used when parsing feed data. Only the 'lastUpdate' and 'items' field are initialized. + */ + public Feed() { + super(); + items = new ArrayList(); + lastUpdate = new Date(); + } + + /** + * This constructor is used for requesting a feed download (it must not be used for anything else!). It should NOT be + * used if the title of the feed is already known. + */ + public Feed(String url, Date lastUpdate) { + super(null, url, false); + this.lastUpdate = lastUpdate; + } + + /** + * This constructor is used for requesting a feed download (it must not be used for anything else!). It should be + * used if the title of the feed is already known. + */ + public Feed(String url, Date lastUpdate, String title) { + this(url, lastUpdate); + this.title = title; + } + + /** + * Returns the number of FeedItems where 'read' is false. If the 'display + * only episodes' - preference is set to true, this method will only count + * items with episodes. + */ + public int getNumOfNewItems() { + int count = 0; + for (FeedItem item : items) { + if (item.getState() == FeedItem.State.NEW) { + if (!UserPreferences.isDisplayOnlyEpisodes() + || item.getMedia() != null) { + count++; + } + } + } + return count; + } + + /** + * Returns the number of FeedItems where the media started to play but + * wasn't finished yet. + */ + public int getNumOfStartedItems() { + int count = 0; + + for (FeedItem item : items) { + FeedItem.State state = item.getState(); + if (state == FeedItem.State.IN_PROGRESS + || state == FeedItem.State.PLAYING) { + count++; + } + } + return count; + } + + /** + * Returns true if at least one item in the itemlist is unread. + * + * @param enableEpisodeFilter true if this method should only count items with episodes if + * the 'display only episodes' - preference is set to true by the + * user. + */ + public boolean hasNewItems(boolean enableEpisodeFilter) { + for (FeedItem item : items) { + if (item.getState() == FeedItem.State.NEW) { + if (!(enableEpisodeFilter && UserPreferences + .isDisplayOnlyEpisodes()) || item.getMedia() != null) { + return true; + } + } + } + return false; + } + + /** + * Returns the number of FeedItems. + * + * @param enableEpisodeFilter true if this method should only count items with episodes if + * the 'display only episodes' - preference is set to true by the + * user. + */ + public int getNumOfItems(boolean enableEpisodeFilter) { + if (enableEpisodeFilter && UserPreferences.isDisplayOnlyEpisodes()) { + return EpisodeFilter.countItemsWithEpisodes(items); + } else { + return items.size(); + } + } + + /** + * Returns the item at the specified index. + * + * @param enableEpisodeFilter true if this method should ignore items without episdodes if + * the episodes filter has been enabled by the user. + */ + public FeedItem getItemAtIndex(boolean enableEpisodeFilter, int position) { + if (enableEpisodeFilter && UserPreferences.isDisplayOnlyEpisodes()) { + return EpisodeFilter.accessEpisodeByIndex(items, position); + } else { + return items.get(position); + } + } + + /** + * Returns the value that uniquely identifies this Feed. If the + * feedIdentifier attribute is not null, it will be returned. Else it will + * try to return the title. If the title is not given, it will use the link + * of the feed. + */ + public String getIdentifyingValue() { + if (feedIdentifier != null && !feedIdentifier.isEmpty()) { + return feedIdentifier; + } else if (title != null && !title.isEmpty()) { + return title; + } else { + return link; + } + } + + @Override + public String getHumanReadableIdentifier() { + if (title != null) { + return title; + } else { + return download_url; + } + } + + public void updateFromOther(Feed other) { + super.updateFromOther(other); + if (other.title != null) { + title = other.title; + } + if (other.feedIdentifier != null) { + feedIdentifier = other.feedIdentifier; + } + if (other.link != null) { + link = other.link; + } + if (other.description != null) { + description = other.description; + } + if (other.language != null) { + language = other.language; + } + if (other.author != null) { + author = other.author; + } + if (other.paymentLink != null) { + paymentLink = other.paymentLink; + } + } + + public boolean compareWithOther(Feed other) { + if (super.compareWithOther(other)) { + return true; + } + if (!title.equals(other.title)) { + return true; + } + if (other.feedIdentifier != null) { + if (feedIdentifier == null + || !feedIdentifier.equals(other.feedIdentifier)) { + return true; + } + } + if (other.link != null) { + if (link == null || !link.equals(other.link)) { + return true; + } + } + if (other.description != null) { + if (description == null || !description.equals(other.description)) { + return true; + } + } + if (other.language != null) { + if (language == null || !language.equals(other.language)) { + return true; + } + } + if (other.author != null) { + if (author == null || !author.equals(other.author)) { + return true; + } + } + if (other.paymentLink != null) { + if (paymentLink == null || !paymentLink.equals(other.paymentLink)) { + return true; + } + } + return false; + } + + @Override + public int getTypeAsInt() { + return FEEDFILETYPE_FEED; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public FeedImage getImage() { + return image; + } + + public void setImage(FeedImage image) { + this.image = image; + } + + public List getItems() { + return items; + } + + public void setItems(List list) { + this.items = list; + } + + /** + * Returns an array that contains all the feeditems of this feed. + */ + public FeedItem[] getItemsArray() { + return items.toArray(new FeedItem[items.size()]); + } + + public Date getLastUpdate() { + return lastUpdate; + } + + public void setLastUpdate(Date lastUpdate) { + this.lastUpdate = lastUpdate; + } + + public String getFeedIdentifier() { + return feedIdentifier; + } + + public void setFeedIdentifier(String feedIdentifier) { + this.feedIdentifier = feedIdentifier; + } + + public String getPaymentLink() { + return paymentLink; + } + + public void setPaymentLink(String paymentLink) { + this.paymentLink = paymentLink; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } } diff --git a/src/de/danoeh/antennapod/storage/DBReader.java b/src/de/danoeh/antennapod/storage/DBReader.java index b1ebbc45d..c96051874 100644 --- a/src/de/danoeh/antennapod/storage/DBReader.java +++ b/src/de/danoeh/antennapod/storage/DBReader.java @@ -291,30 +291,32 @@ public final class DBReader { Cursor cursor) { Date lastUpdate = new Date( cursor.getLong(PodDBAdapter.KEY_LAST_UPDATE_INDEX)); - Feed feed = new Feed(lastUpdate); - - feed.setId(cursor.getLong(PodDBAdapter.KEY_ID_INDEX)); - feed.setTitle(cursor.getString(PodDBAdapter.KEY_TITLE_INDEX)); - feed.setLink(cursor.getString(PodDBAdapter.KEY_LINK_INDEX)); - feed.setDescription(cursor - .getString(PodDBAdapter.KEY_DESCRIPTION_INDEX)); - feed.setPaymentLink(cursor - .getString(PodDBAdapter.KEY_PAYMENT_LINK_INDEX)); - feed.setAuthor(cursor.getString(PodDBAdapter.KEY_AUTHOR_INDEX)); - feed.setLanguage(cursor.getString(PodDBAdapter.KEY_LANGUAGE_INDEX)); - feed.setType(cursor.getString(PodDBAdapter.KEY_TYPE_INDEX)); - feed.setFeedIdentifier(cursor - .getString(PodDBAdapter.KEY_FEED_IDENTIFIER_INDEX)); + + final FeedImage image; long imageIndex = cursor.getLong(PodDBAdapter.KEY_IMAGE_INDEX); if (imageIndex != 0) { - feed.setImage(getFeedImage(adapter, imageIndex)); - feed.getImage().setFeed(feed); + image = getFeedImage(adapter, imageIndex); + } else { + image = null; } - feed.setFile_url(cursor.getString(PodDBAdapter.KEY_FILE_URL_INDEX)); - feed.setDownload_url(cursor - .getString(PodDBAdapter.KEY_DOWNLOAD_URL_INDEX)); - feed.setDownloaded(cursor.getInt(PodDBAdapter.KEY_DOWNLOADED_INDEX) > 0); + Feed feed = new Feed(cursor.getLong(PodDBAdapter.KEY_ID_INDEX), + lastUpdate, + cursor.getString(PodDBAdapter.KEY_TITLE_INDEX), + cursor.getString(PodDBAdapter.KEY_LINK_INDEX), + cursor.getString(PodDBAdapter.KEY_DESCRIPTION_INDEX), + cursor.getString(PodDBAdapter.KEY_PAYMENT_LINK_INDEX), + cursor.getString(PodDBAdapter.KEY_AUTHOR_INDEX), + cursor.getString(PodDBAdapter.KEY_LANGUAGE_INDEX), + cursor.getString(PodDBAdapter.KEY_TYPE_INDEX), + cursor.getString(PodDBAdapter.KEY_FEED_IDENTIFIER_INDEX), + image, + cursor.getString(PodDBAdapter.KEY_FILE_URL_INDEX), + cursor.getString(PodDBAdapter.KEY_DOWNLOAD_URL_INDEX), + cursor.getInt(PodDBAdapter.KEY_DOWNLOADED_INDEX) > 0); + if (image != null) { + image.setFeed(feed); + } return feed; } @@ -592,6 +594,7 @@ public final class DBReader { } else { Log.e(TAG, "getFeed could not find feed with id " + feedId); } + feedCursor.close(); adapter.close(); return feed; } diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index 4ad9e9e1f..5cdd6aee4 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -131,8 +131,6 @@ public class DBWriter { editor.commit(); } - PodDBAdapter adapter = new PodDBAdapter(context); - adapter.open(); // delete image file if (feed.getImage() != null) { if (feed.getImage().isDownloaded() @@ -163,6 +161,8 @@ public class DBWriter { requester.cancelDownload(context, item.getMedia()); } } + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); if (queueWasModified) { adapter.setQueue(queue); } diff --git a/src/de/danoeh/antennapod/storage/PodDBAdapter.java b/src/de/danoeh/antennapod/storage/PodDBAdapter.java index ac9309674..edcbe1cf4 100644 --- a/src/de/danoeh/antennapod/storage/PodDBAdapter.java +++ b/src/de/danoeh/antennapod/storage/PodDBAdapter.java @@ -30,7 +30,7 @@ import de.danoeh.antennapod.service.download.DownloadStatus; public class PodDBAdapter { private static final String TAG = "PodDBAdapter"; private static final int DATABASE_VERSION = 9; - private static final String DATABASE_NAME = "Antennapod.db"; + public static final String DATABASE_NAME = "Antennapod.db"; /** * Maximum number of arguments for IN-operator. @@ -629,14 +629,12 @@ public class PodDBAdapter { * @return The cursor of the query */ public final Cursor getAllFeedsCursor() { - open(); Cursor c = db.query(TABLE_NAME_FEEDS, null, null, null, null, null, KEY_TITLE + " ASC"); return c; } public final Cursor getExpiredFeedsCursor(long expirationTime) { - open(); Cursor c = db.query(TABLE_NAME_FEEDS, null, "?= 0"); } - open(); Cursor c = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_PLAYBACK_COMPLETION_DATE + " > 0", null, null, null, KEY_PLAYBACK_COMPLETION_DATE + " DESC LIMIT " + limit); @@ -849,7 +835,6 @@ public class PodDBAdapter { } public final Cursor getFeedCursor(final long id) { - open(); Cursor c = db.query(TABLE_NAME_FEEDS, null, KEY_ID + "=" + id, null, null, null, null); return c; @@ -862,7 +847,6 @@ public class PodDBAdapter { + IN_OPERATOR_MAXIMUM); } - open(); return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_SMALL, KEY_ID + " IN " + buildInOperator(ids.length), ids, null, null, null); diff --git a/src/instrumentationTest/de/test/antennapod/AntennaPodTestRunner.java b/src/instrumentationTest/de/test/antennapod/AntennaPodTestRunner.java index 3eb91deca..7aaa14909 100644 --- a/src/instrumentationTest/de/test/antennapod/AntennaPodTestRunner.java +++ b/src/instrumentationTest/de/test/antennapod/AntennaPodTestRunner.java @@ -13,5 +13,6 @@ public class AntennaPodTestRunner extends InstrumentationTestRunner { @Override public TestSuite getAllTests() { return new TestSuiteBuilder(AntennaPodTestRunner.class).includeAllPackagesUnderHere().build(); + //return new TestSuiteBuilder(AntennaPodTestRunner.class).includeAllPackagesUnderHere().excludePackages("instrumentationTest.de.test.antennapod.syndication.handler").build(); } } diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java new file mode 100644 index 000000000..0fb733b67 --- /dev/null +++ b/src/instrumentationTest/de/test/antennapod/storage/DBReaderTest.java @@ -0,0 +1,11 @@ +package instrumentationTest.de.test.antennapod.storage; + +import android.test.InstrumentationTestCase; + +/** + * Test class for DBReader + */ +public class DBReaderTest extends InstrumentationTestCase { + + +} diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java new file mode 100644 index 000000000..ef48b8d5f --- /dev/null +++ b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java @@ -0,0 +1,9 @@ +package instrumentationTest.de.test.antennapod.storage; + +import android.test.InstrumentationTestCase; + +/** + * Test class for DBTasks + */ +public class DBTasksTest extends InstrumentationTestCase { +} diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java new file mode 100644 index 000000000..dbec84370 --- /dev/null +++ b/src/instrumentationTest/de/test/antennapod/storage/DBWriterTest.java @@ -0,0 +1,473 @@ +package instrumentationTest.de.test.antennapod.storage; + +import android.content.Context; +import android.database.Cursor; +import android.test.InstrumentationTestCase; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedImage; +import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.storage.DBReader; +import de.danoeh.antennapod.storage.DBWriter; +import de.danoeh.antennapod.storage.PodDBAdapter; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +/** + * Test class for DBWriter + */ +public class DBWriterTest extends InstrumentationTestCase { + private static final String TEST_FOLDER = "testDBWriter"; + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + final Context context = getInstrumentation().getTargetContext(); + context.deleteDatabase(PodDBAdapter.DATABASE_NAME); + + File testDir = context.getExternalFilesDir(TEST_FOLDER); + assertNotNull(testDir); + for (File f : testDir.listFiles()) { + f.delete(); + } + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + final Context context = getInstrumentation().getTargetContext(); + context.deleteDatabase(PodDBAdapter.DATABASE_NAME); + // make sure database is created + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + adapter.close(); + } + + public void testDeleteFeedMediaOfItemFileExists() throws IOException, ExecutionException, InterruptedException { + File dest = new File(getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER), "testFile"); + + assertTrue(dest.createNewFile()); + + Feed feed = new Feed("url", new Date(), "title"); + List items = new ArrayList(); + feed.setItems(items); + FeedItem item = new FeedItem(); + item.setTitle("title"); + item.setPubDate(new Date()); + item.setFeed(feed); + + FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", dest.getAbsolutePath(), "download_url", true, null); + item.setMedia(media); + + items.add(item); + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + assertTrue(media.getId() != 0); + assertTrue(item.getId() != 0); + + DBWriter.deleteFeedMediaOfItem(getInstrumentation().getTargetContext(), media.getId()).get(); + media = DBReader.getFeedMedia(getInstrumentation().getTargetContext(), media.getId()); + assertNotNull(media); + assertFalse(dest.exists()); + assertFalse(media.isDownloaded()); + assertNull(media.getFile_url()); + } + + public void testDeleteFeedMediaOfItemFileDoesNotExists() throws IOException, ExecutionException, InterruptedException { + File dest = new File(getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER), "testFile"); + + Feed feed = new Feed("url", new Date(), "title"); + List items = new ArrayList(); + feed.setItems(items); + FeedItem item = new FeedItem(); + item.setTitle("title"); + item.setPubDate(new Date()); + item.setFeed(feed); + + FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", dest.getAbsolutePath(), "download_url", false, null); + item.setMedia(media); + + items.add(item); + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + assertTrue(media.getId() != 0); + assertTrue(item.getId() != 0); + + DBWriter.deleteFeedMediaOfItem(getInstrumentation().getTargetContext(), media.getId()).get(); + media = DBReader.getFeedMedia(getInstrumentation().getTargetContext(), media.getId()); + assertNotNull(media); + assertFalse(dest.exists()); + assertFalse(media.isDownloaded()); + assertNull(media.getFile_url()); + } + + public void testDeleteFeed() throws IOException, ExecutionException, InterruptedException, TimeoutException { + File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER); + assertNotNull(destFolder); + + Feed feed = new Feed ("url", new Date(), "title"); + feed.setItems(new ArrayList()); + + // create Feed image + File imgFile = new File(destFolder, "image"); + assertTrue(imgFile.createNewFile()); + FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true); + image.setFeed(feed); + feed.setImage(image); + + List itemFiles = new ArrayList(); + // create items with downloaded media files + for (int i = 0; i < 10; i++) { + FeedItem item = new FeedItem(); + item.setTitle("Item " + i); + item.setPubDate(new Date(System.currentTimeMillis())); + item.setFeed(feed); + feed.getItems().add(item); + + File enc = new File(destFolder, "file " + i); + assertTrue(enc.createNewFile()); + itemFiles.add(enc); + + FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", enc.getAbsolutePath(), "download_url", true, null); + item.setMedia(media); + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + + assertTrue(feed.getId() != 0); + assertTrue(feed.getImage().getId() != 0); + for (FeedItem item : feed.getItems()) { + assertTrue(item.getId() != 0); + assertTrue(item.getMedia().getId() != 0); + } + + DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(5, TimeUnit.SECONDS); + + // check if files still exist + assertFalse(imgFile.exists()); + for (File f : itemFiles) { + assertFalse(f.exists()); + } + + adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + Cursor c = adapter.getFeedCursor(feed.getId()); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getImageOfFeedCursor(image.getId()); + assertTrue(c.getCount() == 0); + c.close(); + for (FeedItem item : feed.getItems()) { + c = adapter.getFeedItemCursor(String.valueOf(item.getId())); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getSingleFeedMediaCursor(item.getMedia().getId()); + assertTrue(c.getCount() == 0); + c.close(); + } + } + + public void testDeleteFeedNoImage() throws ExecutionException, InterruptedException, IOException, TimeoutException { + File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER); + assertNotNull(destFolder); + + Feed feed = new Feed ("url", new Date(), "title"); + feed.setItems(new ArrayList()); + + feed.setImage(null); + + List itemFiles = new ArrayList(); + // create items with downloaded media files + for (int i = 0; i < 10; i++) { + FeedItem item = new FeedItem(); + item.setTitle("Item " + i); + item.setPubDate(new Date(System.currentTimeMillis())); + item.setFeed(feed); + feed.getItems().add(item); + + File enc = new File(destFolder, "file " + i); + assertTrue(enc.createNewFile()); + itemFiles.add(enc); + + FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", enc.getAbsolutePath(), "download_url", true, null); + item.setMedia(media); + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + + assertTrue(feed.getId() != 0); + for (FeedItem item : feed.getItems()) { + assertTrue(item.getId() != 0); + assertTrue(item.getMedia().getId() != 0); + } + + DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(5, TimeUnit.SECONDS); + + // check if files still exist + for (File f : itemFiles) { + assertFalse(f.exists()); + } + + adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + Cursor c = adapter.getFeedCursor(feed.getId()); + assertTrue(c.getCount() == 0); + c.close(); + for (FeedItem item : feed.getItems()) { + c = adapter.getFeedItemCursor(String.valueOf(item.getId())); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getSingleFeedMediaCursor(item.getMedia().getId()); + assertTrue(c.getCount() == 0); + c.close(); + } + } + + public void testDeleteFeedNoItems() throws IOException, ExecutionException, InterruptedException, TimeoutException { + File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER); + assertNotNull(destFolder); + + Feed feed = new Feed ("url", new Date(), "title"); + feed.setItems(null); + + // create Feed image + File imgFile = new File(destFolder, "image"); + assertTrue(imgFile.createNewFile()); + FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true); + image.setFeed(feed); + feed.setImage(image); + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + + assertTrue(feed.getId() != 0); + assertTrue(feed.getImage().getId() != 0); + + DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(5, TimeUnit.SECONDS); + + // check if files still exist + assertFalse(imgFile.exists()); + + adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + Cursor c = adapter.getFeedCursor(feed.getId()); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getImageOfFeedCursor(image.getId()); + assertTrue(c.getCount() == 0); + c.close(); + } + + public void testDeleteFeedNoFeedMedia() throws IOException, ExecutionException, InterruptedException, TimeoutException { + File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER); + assertNotNull(destFolder); + + Feed feed = new Feed ("url", new Date(), "title"); + feed.setItems(new ArrayList()); + + // create Feed image + File imgFile = new File(destFolder, "image"); + assertTrue(imgFile.createNewFile()); + FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true); + image.setFeed(feed); + feed.setImage(image); + + // create items + for (int i = 0; i < 10; i++) { + FeedItem item = new FeedItem(); + item.setTitle("Item " + i); + item.setPubDate(new Date(System.currentTimeMillis())); + item.setFeed(feed); + feed.getItems().add(item); + + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + + assertTrue(feed.getId() != 0); + assertTrue(feed.getImage().getId() != 0); + for (FeedItem item : feed.getItems()) { + assertTrue(item.getId() != 0); + } + + DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(5, TimeUnit.SECONDS); + + // check if files still exist + assertFalse(imgFile.exists()); + + adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + Cursor c = adapter.getFeedCursor(feed.getId()); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getImageOfFeedCursor(image.getId()); + assertTrue(c.getCount() == 0); + c.close(); + for (FeedItem item : feed.getItems()) { + c = adapter.getFeedItemCursor(String.valueOf(item.getId())); + assertTrue(c.getCount() == 0); + c.close(); + } + } + + public void testDeleteFeedWithQueueItems() throws ExecutionException, InterruptedException, TimeoutException { + File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER); + assertNotNull(destFolder); + + Feed feed = new Feed ("url", new Date(), "title"); + feed.setItems(new ArrayList()); + + // create Feed image + File imgFile = new File(destFolder, "image"); + FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true); + image.setFeed(feed); + feed.setImage(image); + + List itemFiles = new ArrayList(); + // create items with downloaded media files + for (int i = 0; i < 10; i++) { + FeedItem item = new FeedItem(); + item.setTitle("Item " + i); + item.setPubDate(new Date(System.currentTimeMillis())); + item.setFeed(feed); + feed.getItems().add(item); + + File enc = new File(destFolder, "file " + i); + itemFiles.add(enc); + + FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", enc.getAbsolutePath(), "download_url", false, null); + item.setMedia(media); + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + + assertTrue(feed.getId() != 0); + assertTrue(feed.getImage().getId() != 0); + for (FeedItem item : feed.getItems()) { + assertTrue(item.getId() != 0); + assertTrue(item.getMedia().getId() != 0); + } + + + List queue = new ArrayList(); + queue.addAll(feed.getItems()); + adapter.open(); + adapter.setQueue(queue); + + Cursor queueCursor = adapter.getQueueIDCursor(); + assertTrue(queueCursor.getCount() == queue.size()); + queueCursor.close(); + + adapter.close(); + DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(5, TimeUnit.SECONDS); + adapter.open(); + + Cursor c = adapter.getFeedCursor(feed.getId()); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getImageOfFeedCursor(image.getId()); + assertTrue(c.getCount() == 0); + c.close(); + for (FeedItem item : feed.getItems()) { + c = adapter.getFeedItemCursor(String.valueOf(item.getId())); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getSingleFeedMediaCursor(item.getMedia().getId()); + assertTrue(c.getCount() == 0); + c.close(); + } + c = adapter.getQueueCursor(); + assertTrue(c.getCount() == 0); + c.close(); + adapter.close(); + } + + public void testDeleteFeedNoDownloadedFiles() throws ExecutionException, InterruptedException, TimeoutException { + File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER); + assertNotNull(destFolder); + + Feed feed = new Feed ("url", new Date(), "title"); + feed.setItems(new ArrayList()); + + // create Feed image + File imgFile = new File(destFolder, "image"); + FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true); + image.setFeed(feed); + feed.setImage(image); + + List itemFiles = new ArrayList(); + // create items with downloaded media files + for (int i = 0; i < 10; i++) { + FeedItem item = new FeedItem(); + item.setTitle("Item " + i); + item.setPubDate(new Date(System.currentTimeMillis())); + item.setFeed(feed); + feed.getItems().add(item); + + File enc = new File(destFolder, "file " + i); + itemFiles.add(enc); + + FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", enc.getAbsolutePath(), "download_url", false, null); + item.setMedia(media); + } + + PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + adapter.setCompleteFeed(feed); + adapter.close(); + + assertTrue(feed.getId() != 0); + assertTrue(feed.getImage().getId() != 0); + for (FeedItem item : feed.getItems()) { + assertTrue(item.getId() != 0); + assertTrue(item.getMedia().getId() != 0); + } + + DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(5, TimeUnit.SECONDS); + + adapter = new PodDBAdapter(getInstrumentation().getContext()); + adapter.open(); + Cursor c = adapter.getFeedCursor(feed.getId()); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getImageOfFeedCursor(image.getId()); + assertTrue(c.getCount() == 0); + c.close(); + for (FeedItem item : feed.getItems()) { + c = adapter.getFeedItemCursor(String.valueOf(item.getId())); + assertTrue(c.getCount() == 0); + c.close(); + c = adapter.getSingleFeedMediaCursor(item.getMedia().getId()); + assertTrue(c.getCount() == 0); + c.close(); + } + } +} -- cgit v1.2.3