Package javango.contrib.hibernate.tests

Source Code of javango.contrib.hibernate.tests.HibernateManagerTest

package javango.contrib.hibernate.tests;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import javango.contrib.hibernate.HibernateManager;
import javango.contrib.hibernate.HibernateModule;
import javango.contrib.hibernate.HibernateUtil;
import javango.db.Manager;
import javango.db.Managers;
import javango.db.QuerySet;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

import junit.framework.TestCase;

public class HibernateManagerTest extends TestCase {

  public void testManagerFactory() throws Exception {
      Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
      assertEquals(HibernateManager.class, manager.getClass());
     
      Poll poll = new Poll();
      poll.setPubDate(new Date());
     
      manager.save(poll);
     
      poll = manager.get(1L);
     
      assertNotNull(poll);
  }
 
  public void testPk() throws Exception {
      Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
      assertEquals(HibernateManager.class, manager.getClass());
     
      Poll poll = new Poll();
      poll.setPubDate(new Date());
      manager.save(poll);
     
      Class[] keyClasses = manager.getPkClass();
      assertEquals(1, keyClasses.length);
      assertEquals(Long.class, keyClasses[0]);
      assertEquals(1L, manager.getPk(poll));
  }
 
  public void testSearch() throws Exception {
      Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
     
      Poll poll = new Poll();
      poll.setQuestion("What is this");
      poll.setPubDate(new Date());
      manager.save(poll);
     
      Manager<Choice> choiceManager = managers.forClass(Choice.class);
      Choice choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My cool choice");
      choiceManager.save(choice);
     
      choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My other cool choice");
      choiceManager.save(choice);

      assertNull(manager.filter("id", 2L).get());
      assertNotNull(manager.filter("id", 1L).get());
      assertNotNull(manager.filter("id__eq", 1L).get());
     
      assertNull(choiceManager.filter("poll__id__eq", 2L).get());
      assertEquals(2, choiceManager.filter("poll__id__eq", 1L).count());
      assertEquals(2, choiceManager.filter("poll__id", 1L).count());
     
      assertEquals(2, choiceManager.filter("poll__question", "What is this").count());

  }
 
  public void testPropertySearch() throws Exception {
    Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
     
      Poll poll = new Poll();
      poll.setPubDate(new Date());
      manager.save(poll);
     
      Manager<Choice> choiceManager = managers.forClass(Choice.class);
      Choice choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My cool choice");
      choice.setVotes(1L); // should match the id for the property test below...
      choiceManager.save(choice);
     
      choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My other cool choice");
      choice.setVotes(1L); // should not match the id for the property test below...
      choiceManager.save(choice);

      choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("Yet other cool choice");
      choice.setVotes(1L); // should not match the id for the property test below...
      choiceManager.save(choice);

      assertEquals(3, choiceManager.all().count());
      assertEquals(1, choiceManager.filterByProperty("id", "votes").count());
      assertEquals(1, choiceManager.filterByProperty("id__eq", "votes").count());
      assertEquals(2, choiceManager.filterByProperty("id__ne", "votes").count());
      assertEquals(2, choiceManager.filterByProperty("id__gt", "votes").count());
  }
 
  public void testMultipleJoinSearches() throws Exception {
      Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
     
      Poll poll = new Poll();
      poll.setPubDate(new Date());
      manager.save(poll);
     
      Manager<Choice> choiceManager = managers.forClass(Choice.class);
      Choice choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My cool choice");
      choiceManager.save(choice);
     
      choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My other cool choice");
      choiceManager.save(choice);

      QuerySet<Choice> qs = choiceManager.filter("poll__id__eq", 1L);
      qs.filter("poll__id__eq", 2L); // should not affect qs because a new qs would be returned.
      assertEquals(2, qs.count());
     
      qs = qs.filter("poll__id__eq", 2L);
      assertEquals(0, qs.count());
  }
 
  public void testMultipleJoins() throws Exception {
      Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      User user = new User();
      user.setName("joe smith");
      managers.forClass(User.class).save(user);
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
     
      Poll poll = new Poll();
      poll.setPubDate(new Date());
      poll.setUser(user);
      manager.save(poll);
     
      Manager<Choice> choiceManager = managers.forClass(Choice.class);
      Choice choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My cool choice");
      choiceManager.save(choice);
     
      choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My other cool choice");
      choiceManager.save(choice);

      QuerySet<Choice> qs = choiceManager.filter("poll__user__name__like", "john%");
      assertEquals(0, qs.count());
     
      qs = choiceManager.filter("poll__user__name__like", "joe%");
      assertEquals(2, qs.count());
     
      qs = qs.filter("poll__user__name__ilike", "JOE%");
      assertEquals(2, qs.count());
  }
 
  public void testJoinedOrderBy() throws Exception {
      Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      User user = new User();
      user.setName("joe smith");
      managers.forClass(User.class).save(user);
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
     
      Poll poll = new Poll();
      poll.setPubDate(new Date());
      poll.setUser(user);
      manager.save(poll);
     
      Manager<Choice> choiceManager = managers.forClass(Choice.class);
      Choice choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My cool choice");
      choiceManager.save(choice);
     
      choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My other cool choice");
      choiceManager.save(choice);

      QuerySet<Choice> qs = choiceManager.all().orderBy("-poll__user__name", "poll__pubDate", "choice");
      qs.list(); // TODO for now just tesitng the sql output ..  make the test complete...
  }
 
  public void testDistinctEntry() throws Exception {
      Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
      Managers managers = injector.getInstance(Managers.class);     
     
      User user = new User();
      user.setName("joe smith");
      managers.forClass(User.class).save(user);
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
     
      Poll poll = new Poll();
      poll.setPubDate(new Date());
      poll.setUser(user);
      manager.save(poll);
     
      Manager<Choice> choiceManager = managers.forClass(Choice.class);
      Choice choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My cool choice");
      choiceManager.save(choice);
     
      choice = new Choice();
      choice.setPoll(poll);
      choice.setChoice("My other cool choice");
      choiceManager.save(choice);

      assertEquals(1, manager.all().count());
      QuerySet<Poll> qs = manager.filter("choices__choice__like" , "%cool choice");
      List<?> l = qs.list();
      assertEquals(1, l.size());
      // humm,  qs.count() does not work..  bug somewhere (probably limitation of hibernate).
  }

  public void testInSet() throws Exception {
    Injector injector = Guice.createInjector(new HibernateModule());
    AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();   
    new SchemaExport(cfg).drop(false, true);
    new SchemaExport(cfg).create(false, true);
   
    Managers managers = injector.getInstance(Managers.class);     
     
      User user = new User();
      user.setName("joe smith");
      managers.forClass(User.class).save(user);
     
      Manager<Poll> manager = managers.forClass(Poll.class);     
     
      Poll poll1 = new Poll();
      poll1.setPubDate(new Date());
      poll1.setUser(user);
      manager.save(poll1);
     
      Manager<Choice> choiceManager = managers.forClass(Choice.class);
      Choice choice1 = new Choice();
      choice1.setPoll(poll1);
      choice1.setChoice("My cool choice");
      choiceManager.save(choice1);
     
      Choice choice2 = new Choice();
      choice2.setPoll(poll1);
      choice2.setChoice("My other cool choice");
      choiceManager.save(choice2);
     
      Poll poll2 = new Poll();
      poll2.setPubDate(new Date());
      poll2.setUser(user);
      manager.save(poll2);
     
      Choice choice3 = new Choice();
      choice3.setPoll(poll2);
      choice3.setChoice("Yet another choice");
      choiceManager.save(choice3);
     
      assertEquals(3, choiceManager.all().count());
      QuerySet<Choice> qs = choiceManager.filter("poll__in" , new Poll[] { poll1 });
      assertEquals(2, qs.count());
     
      qs = choiceManager.filter("poll__in" , new Poll[] { poll1, poll2 });
      assertEquals(3, qs.count());
     
      Set<Poll> pollSet = new HashSet<Poll>();
      pollSet.add(poll1);
      qs = choiceManager.filter("poll__in" , pollSet);
      assertEquals(2, qs.count());

      pollSet.add(poll2);
      qs = choiceManager.filter("poll__in" , pollSet);
      assertEquals(3, qs.count());
  }
}
TOP

Related Classes of javango.contrib.hibernate.tests.HibernateManagerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.