@Test
public void testNewsService() throws Exception {
INewsDAO newsDao = Owl.getPersistenceService().getDAOService().getNewsDAO();
NewsService service = Controller.getDefault().getNewsService();
IFeed feed = new Feed(new URI("http://www.rssowl.org/rssowl2dg/tests/manager/rss_2_0.xml")); //$NON-NLS-1$
feed = DynamicDAO.save(feed);
assertEquals(0, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(0, getStickyCount(feed));
Owl.getModelFactory().createNews(null, feed, new Date()); //$NON-NLS-1$
feed = DynamicDAO.save(feed);
assertEquals(1, getUnreadCount(feed));
assertEquals(1, getNewCount(feed));
assertEquals(0, getStickyCount(feed));
newsDao.setState(feed.getNews(), INews.State.READ, true, false);
assertEquals(0, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(0, getStickyCount(feed));
newsDao.setState(feed.getNews(), INews.State.UNREAD, true, false);
feed.getNews().get(0).setFlagged(true);
DynamicDAO.save(feed.getNews().get(0));
assertEquals(1, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(1, getStickyCount(feed));
newsDao.setState(feed.getNews(), INews.State.READ, true, false);
assertEquals(0, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(1, getStickyCount(feed));
newsDao.setState(feed.getNews(), INews.State.UPDATED, true, false);
assertEquals(1, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(1, getStickyCount(feed));
feed.getNews().get(0).setFlagged(false);
DynamicDAO.save(feed.getNews().get(0));
newsDao.setState(feed.getNews(), INews.State.READ, true, false);
assertEquals(0, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(0, getStickyCount(feed));
/* Simulate Shutdown */
service.stopService();
Owl.getModelFactory().createNews(null, feed, new Date()); //$NON-NLS-1$
feed = DynamicDAO.save(feed);
Owl.getModelFactory().createNews(null, feed, new Date()); //$NON-NLS-1$
feed = DynamicDAO.save(feed);
assertEquals(2, getUnreadCount(feed));
assertEquals(2, getNewCount(feed));
assertEquals(0, getStickyCount(feed));
newsDao.setState(feed.getNews(), INews.State.READ, true, false);
feed.getNews().get(0).setFlagged(true);
feed.getNews().get(1).setFlagged(true);
DynamicDAO.save(feed);
assertEquals(0, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(2, getStickyCount(feed));
newsDao.setState(feed.getNews(), INews.State.UNREAD, true, false);
assertEquals(3, getUnreadCount(feed));
assertEquals(0, getNewCount(feed));
assertEquals(2, getStickyCount(feed));
/* Simulate Dirty Shutdown */
Owl.getPersistenceService().recreateSchema();
feed = new Feed(new URI("http://www.rssowl.org/rssowl2dg/tests/manager/rss_2_0.xml")); //$NON-NLS-1$
feed = DynamicDAO.save(feed);
Owl.getModelFactory().createNews(null, feed, new Date()); //$NON-NLS-1$
feed = DynamicDAO.save(feed);
Owl.getModelFactory().createNews(null, feed, new Date()); //$NON-NLS-1$
feed = DynamicDAO.save(feed);
feed.getNews().get(0).setFlagged(true);
feed.getNews().get(1).setFlagged(true);
DynamicDAO.save(feed);
service.testDirtyShutdown();
assertEquals(2, getUnreadCount(feed));