I have a unit test that populates the datastore with 26 entities. I have a DAO that tries to retrieve these entities and page through them but somehow I can't get it to work entirely. If I do query.setRange(0,10) it works but not for query.setRange(10,20). The query returns 0 results even if the unit test shows that I have 26 entit开发者_C百科ies.
public void testGetModulesWithPagination(){
String current_user="currentUser";
String tags[] = {"football","soccer"};
for (int i=0;i<=25;i++){
Module m = new Module();
m.setOwner("myName");
m.setObjective("m"+i+" objective");
m.setTitle("m"+i);
m.setTags(tags);
dao.insert(m);
}
Query query = new Query(Module.class.getSimpleName());
List<Module> modules_page_one = dao.getModules(current_user,"football", "1");
List<Module> modules_page_two = dao.getModules(current_user,"football","2");
assertEquals(26, DatastoreServiceFactory.getDatastoreService().prepare(query).countEntities());
assertNotNull(modules_page_one);
assertEquals(10,modules_page_one.size());
assertNotNull(modules_page_two);
assertEquals(10,modules_page_two.size()); //FAILS here
assertFalse(modules_page_one.equals(modules_page_two));
}
This is my dao implementation
public List<Module> getModules(String owner,String tag,String page){
getPm();
String queryString = "select from "+Module.class.getName()+" where owner!='"+owner
+"'";
Query query = pm.newQuery(queryString);
Long pageNumber = Long.parseLong(page);
Long lower_bound = pageNumber*10-10;
Long upper_bound = pageNumber*10;
log.info("pageNumber "+pageNumber);
log.info("lower_bound "+lower_bound);
log.info("upper_bound "+upper_bound);
query.setRange(lower_bound, upper_bound);
List<Module> modules = (List<Module>) pm.detachCopyAll((List<Module>) query.execute());
log.info("modules results size "+modules.size());
log.info(modules+"");
List<Module> search_results = new ArrayList<Module>();
for (Module module:modules){
String module_tags[] = module.getTags();
for (int i=0;i<module_tags.length;i++){
if (module_tags[i].equals(tag)){
search_results.add(module);
continue;
}
}
}
closePm();
return search_results;
}
sorry but
String queryString = "select from "+Module.class.getName()+" where owner!='"+owner+"'";
should't be
String queryString = "select from "+Module.class.getName()+" where owner != '"+owner+"'";
?
just checking.
精彩评论