/* ********************************************************************** **
** Copyright notice **
** **
** (c) 2005-2006 RSSOwl Development Team **
** http://www.rssowl.org/ **
** **
** All rights reserved **
** **
** This program and the accompanying materials are made available under **
** the terms of the Eclipse Public License v1.0 which accompanies this **
** distribution, and is available at: **
** http://www.rssowl.org/legal/epl-v10.html **
** **
** A copy is found in the file epl-v10.html and important notices to the **
** license from the team is found in the textfile LICENSE.txt distributed **
** in this package. **
** **
** This copyright notice MUST APPEAR in all copies of the file! **
** **
** Contributors: **
** RSSOwl Development Team - initial API and implementation **
** **
** ********************************************************************** */
package org.rssowl.core.tests.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.rssowl.core.model.NewsModel;
import org.rssowl.core.model.dao.IModelDAO;
import org.rssowl.core.model.dao.PersistenceException;
import org.rssowl.core.model.reference.NewsReference;
import org.rssowl.core.model.search.IModelSearch;
import org.rssowl.core.model.search.ISearchCondition;
import org.rssowl.core.model.search.ISearchField;
import org.rssowl.core.model.search.ISearchHit;
import org.rssowl.core.model.search.SearchSpecifier;
import org.rssowl.core.model.types.IAttachment;
import org.rssowl.core.model.types.IFeed;
import org.rssowl.core.model.types.IModelTypesFactory;
import org.rssowl.core.model.types.INews;
import org.rssowl.core.model.types.INews.State;
import org.rssowl.core.tests.TestUtils;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Test searching types from the persistence layer.
*
* @author bpasero
*/
public class ModelSearchTest {
private IModelDAO fDao;
private IModelTypesFactory fTypesFactory;
private IModelSearch fModelSearch;
/**
* @throws Exception
*/
@Before
public void setUp() throws Exception {
NewsModel.getDefault().getPersistenceLayer().getModelSearch().startIndexer();
NewsModel.getDefault().getPersistenceLayer().recreateSchema();
fDao = NewsModel.getDefault().getPersistenceLayer().getModelDAO();
fModelSearch = NewsModel.getDefault().getPersistenceLayer().getModelSearch();
fTypesFactory = NewsModel.getDefault().getTypesFactory();
}
/**
* @throws Exception
*/
@After
public void tearDown() throws Exception {
NewsModel.getDefault().getPersistenceLayer().getModelSearch().clearIndex();
NewsModel.getDefault().getPersistenceLayer().getModelSearch().shutdown();
}
/**
* Tests whether searching news in the UNREAD, READ or NEW returns the correct
* result.
*
* @throws Exception
*/
@Test
@SuppressWarnings("nls")
public void testSearchUnreadNewUpdatedNews() throws Exception {
try {
/* First add some Types */
IFeed feed = fTypesFactory.createFeed(null, new URL("http://www.feed.com/feed.xml"));
INews news1 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news1.setState(State.READ);
news1.setLink(new URI("http://www.news.com/news1.html"));
INews news2 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news2.setState(State.UNREAD);
news2.setLink(new URI("http://www.news.com/news2.html"));
INews news3 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news3.setState(State.NEW);
news3.setLink(new URI("http://www.news.com/news3.html"));
INews news4 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news4.setState(State.NEW);
news4.setLink(new URI("http://www.news.com/news4.html"));
INews news5 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news5.setState(State.UPDATED);
news5.setLink(new URI("http://www.news.com/news5.html"));
INews news6 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news6.setState(State.DELETED);
news6.setLink(new URI("http://www.news.com/news6.html"));
INews news7 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news7.setState(State.HIDDEN);
news7.setLink(new URI("http://www.news.com/news7.html"));
fDao.saveFeed(feed);
// TODO Replace this by a event-based solution if that becomes available
Thread.sleep(500);
ISearchField field1 = fTypesFactory.createSearchField(INews.STATE, INews.class);
ISearchCondition cond1 = fTypesFactory.createSearchCondition(field1, SearchSpecifier.IS, State.NEW.toString(), false);
ISearchField field2 = fTypesFactory.createSearchField(INews.STATE, INews.class);
ISearchCondition cond2 = fTypesFactory.createSearchCondition(field2, SearchSpecifier.IS, State.UPDATED.toString(), false);
ISearchField field3 = fTypesFactory.createSearchField(INews.STATE, INews.class);
ISearchCondition cond3 = fTypesFactory.createSearchCondition(field3, SearchSpecifier.IS, State.UNREAD.toString(), false);
List<ISearchCondition> conditions = new ArrayList<ISearchCondition>();
conditions.add(cond1);
conditions.add(cond2);
conditions.add(cond3);
List<ISearchHit<NewsReference>> results = fModelSearch.searchNews(conditions);
assertEquals(4, results.size());
for (ISearchHit<NewsReference> result : results) {
INews news = result.getResult().resolve();
assertTrue((news.getState() == State.NEW) || (news.getState() == State.UNREAD) || (news.getState() == State.UPDATED));
}
} catch (PersistenceException e) {
TestUtils.fail(e);
}
}
/**
* Tests whether searching a news by the state READ returns the correct
* result.
*
* @throws Exception
*/
@Test
@SuppressWarnings("nls")
public void testSearchNewsByState() throws Exception {
try {
List<ISearchCondition> conditions = new ArrayList<ISearchCondition>();
/* First add some Types */
IFeed feed = fTypesFactory.createFeed(null, new URL("http://www.feed.com/feed.xml"));
INews news1 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news1.setState(State.READ);
news1.setLink(new URI("http://www.news.com/news1.html"));
INews news2 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news2.setState(State.UNREAD);
news2.setLink(new URI("http://www.news.com/news2.html"));
fDao.saveFeed(feed);
// TODO Replace this by a event-based solution if that becomes available
Thread.sleep(500);
/* SearchCondition: News in state READ */
ISearchField field1 = fTypesFactory.createSearchField(INews.STATE, INews.class);
ISearchCondition cond2 = fTypesFactory.createSearchCondition(field1, SearchSpecifier.CONTAINS, State.READ.toString(), false);
conditions.add(cond2);
List<ISearchHit<NewsReference>> results = fModelSearch.searchNews(conditions);
assertEquals(1, results.size());
assertEquals(news1.getLink(), results.get(0).getResult().resolve().getLink());
} catch (PersistenceException e) {
TestUtils.fail(e);
}
}
/**
* This Test searches for News containing "News" in their title or which are
* not marked Read.
*
* @throws Exception
*/
@Test
@SuppressWarnings("nls")
public void testSearchNews1() throws Exception {
try {
List<ISearchCondition> conditions = new ArrayList<ISearchCondition>();
/* First add some Types */
IFeed feed = fTypesFactory.createFeed(null, new URL("http://www.feed.com/feed.xml"));
INews news1 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news1.setState(State.READ);
news1.setLink(new URI("http://www.news.com/news1.html"));
INews news2 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news2.setLink(new URI("http://www.news.com/news2.html"));
INews news3 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news3.setLink(new URI("http://www.news.com/news3.html"));
fDao.saveFeed(feed);
// TODO Replace this by a event-based solution if that becomes available
Thread.sleep(500);
/* 1. SearchCondition: Title contains "News" */
ISearchField field1 = fTypesFactory.createSearchField(INews.TITLE, INews.class);
ISearchCondition cond1 = fTypesFactory.createSearchCondition(field1, SearchSpecifier.CONTAINS, "News", false);
conditions.add(cond1);
/* 2. SearchCondition: News is not marked read */
ISearchField field2 = fTypesFactory.createSearchField(INews.STATE, INews.class);
ISearchCondition cond2 = fTypesFactory.createSearchCondition(field2, SearchSpecifier.IS_NOT, State.READ.name(), false);
conditions.add(cond2);
List<ISearchHit<NewsReference>> results = fModelSearch.searchNews(conditions);
assertEquals(3, results.size());
boolean resultsMatch[] = new boolean[3];
for (ISearchHit<NewsReference> searchHit : results) {
INews news = searchHit.getResult().resolve();
if (news.getTitle().equals(news1.getTitle()))
resultsMatch[0] = true;
else if (news.getTitle().equals(news2.getTitle()))
resultsMatch[1] = true;
else if (news.getTitle().equals(news3.getTitle()))
resultsMatch[2] = true;
}
for (boolean element : resultsMatch)
assertTrue("Missing a result match", element);
} catch (PersistenceException e) {
TestUtils.fail(e);
}
}
/**
* This Test searches for News that are NEW, come from a Feed that contains
* "feed2.xml" in the Link and are not containing "Foo" in the description.
*
* @throws Exception
*/
@Test
@SuppressWarnings("nls")
public void testSearchNews2() throws Exception {
try {
List<ISearchCondition> conditions = new ArrayList<ISearchCondition>();
/* First add some Types */
IFeed feed1 = fTypesFactory.createFeed(null, new URL("http://www.feed.com/feed1.xml"));
IFeed feed2 = fTypesFactory.createFeed(null, new URL("http://www.feed.com/feed2.xml"));
INews news1 = fTypesFactory.createNews(null, feed1, new Date(System.currentTimeMillis()));
news1.setDescription("News #1 Description");
news1.setLink(new URI("http://www.news.com/news1.html"));
INews news2 = fTypesFactory.createNews(null, feed1, new Date(System.currentTimeMillis()));
news2.setDescription("News #2 Description");
news2.setLink(new URI("http://www.news.com/news2.html"));
INews news3 = fTypesFactory.createNews(null, feed1, new Date(System.currentTimeMillis()));
news3.setDescription("News #3 Description");
news3.setLink(new URI("http://www.news.com/news3.html"));
INews news4 = fTypesFactory.createNews(null, feed2, new Date(System.currentTimeMillis()));
news4.setDescription("News #4 Description");
news4.setLink(new URI("http://www.news.com/news4.html"));
INews news5 = fTypesFactory.createNews(null, feed2, new Date(System.currentTimeMillis()));
news5.setDescription("News #5 Description Foo Bar");
news5.setLink(new URI("http://www.news.com/news5.html"));
INews news6 = fTypesFactory.createNews(null, feed2, new Date(System.currentTimeMillis()));
news6.setLink(new URI("http://www.news.com/news6.html"));
fDao.saveFeed(feed1);
fDao.saveFeed(feed2);
// TODO Replace this by a event-based solution if that becomes available
Thread.sleep(500);
/* 1. SearchCondition: News coming from Feed that contains "feed2.xml" */
ISearchField field1 = fTypesFactory.createSearchField(IFeed.LINK, IFeed.class);
ISearchCondition cond1 = fTypesFactory.createSearchCondition(field1, SearchSpecifier.CONTAINS, "feed2.xml", true);
conditions.add(cond1);
/* 2. SearchCondition: News Description does not contain "Foo" */
ISearchField field2 = fTypesFactory.createSearchField(INews.DESCRIPTION, INews.class);
ISearchCondition cond2 = fTypesFactory.createSearchCondition(field2, SearchSpecifier.CONTAINS_NOT, "Foo", true);
conditions.add(cond2);
/* 3. SearchCondition: News State is NEW */
ISearchField field3 = fTypesFactory.createSearchField(INews.STATE, INews.class);
ISearchCondition cond3 = fTypesFactory.createSearchCondition(field3, SearchSpecifier.IS, State.NEW.name(), true);
conditions.add(cond3);
List<ISearchHit<NewsReference>> results = fModelSearch.searchNews(conditions);
assertEquals(2, results.size());
boolean resultsMatch[] = new boolean[2];
for (ISearchHit<NewsReference> searchHit : results) {
INews news = searchHit.getResult().resolve();
if (news.getTitle().equals(news4.getTitle()))
resultsMatch[0] = true;
else if (news.getTitle().equals(news6.getTitle()))
resultsMatch[1] = true;
}
for (boolean element : resultsMatch)
assertTrue("Missing a result match", element);
} catch (PersistenceException e) {
TestUtils.fail(e);
}
}
/**
* This Test searches for News that contain Attachments of the type "mp3".
*
* @throws Exception
*/
/**
* @throws Exception
*/
@Test
@SuppressWarnings("nls")
public void testSearchNews3() throws Exception {
try {
List<ISearchCondition> conditions = new ArrayList<ISearchCondition>();
/* First add some Types */
IFeed feed = fTypesFactory.createFeed(null, new URL("http://www.feed.com/feed.xml"));
INews news1 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news1.setLink(new URI("http://www.news.com/news1.html"));
INews news2 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news2.setLink(new URI("http://www.news.com/news2.html"));
INews news3 = fTypesFactory.createNews(null, feed, new Date(System.currentTimeMillis()));
news3.setLink(new URI("http://www.news.com/news3.html"));
fTypesFactory.createAttachment(null, news1).setType("text/html");
fTypesFactory.createAttachment(null, news2).setType("text/xml");
fTypesFactory.createAttachment(null, news3).setType("audio/mp3");
fDao.saveFeed(feed);
// TODO Replace this by a event-based solution if that becomes available
Thread.sleep(500);
/* 1. SearchCondition: News has Attachment with Type "audio/mp3" */
ISearchField field1 = fTypesFactory.createSearchField(IAttachment.TYPE, IAttachment.class);
ISearchCondition cond1 = fTypesFactory.createSearchCondition(field1, SearchSpecifier.IS, "audio/mp3", false);
conditions.add(cond1);
List<ISearchHit<NewsReference>> results = fModelSearch.searchNews(conditions);
assertEquals(1, results.size());
boolean resultsMatch[] = new boolean[1];
for (ISearchHit<NewsReference> searchHit : results) {
INews news = searchHit.getResult().resolve();
if (news.getTitle().equals(news3.getTitle()))
resultsMatch[0] = true;
}
for (boolean element : resultsMatch)
assertTrue("Missing a result match", element);
} catch (PersistenceException e) {
TestUtils.fail(e);
}
}
}