Package net.sf.collabreview.hibernate

Source Code of net.sf.collabreview.hibernate.BasicPersistenceTest

/*
   Copyright 2012 Christian Prause and Fraunhofer FIT

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package net.sf.collabreview.hibernate;

import net.sf.collabreview.core.Artifact;
import net.sf.collabreview.core.ArtifactIdentifier;
import net.sf.collabreview.core.CollabReviewSingleton;
import net.sf.collabreview.core.configuration.AutoConfigurator;
import net.sf.collabreview.core.markup.MarkupElement;
import net.sf.collabreview.core.markup.TrivialLineMarkup;
import net.sf.collabreview.core.users.Author;
import net.sf.collabreview.repository.Review;
import junit.framework.TestCase;
import prause.toolbox.hibernate.HibernateUtil;

import java.util.Date;

/**
* Tests specific to the Hibernate repository, e.g. if saving persistent objects are saved correctly.
* <p/>
* This test class is here because there seem to be some incompatibilities between MySQL and HSQLDB which became
* visible when I tried to run all tests with HSQLDB instead of my own memory-only implementation of MemoryRepository.
*
* @author Christian Prause (chris)
* @date 2009-07-31 10:29:41
*/
public class BasicPersistenceTest extends TestCase {
  private ArtifactIdentifier createIdentifier(int num) {
    return new ArtifactIdentifier("a", num, "");
  }

  private Author author(int i) {
    Author author = new HibernateAuthorManager().createAuthor("" + i);
    HibernateUtil.getCurrentSession().save(author);
    HibernateUtil.closeSession();
    return author;
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    CollabReviewSingleton.get();
    HibernateUtil.closeSession();
    HibernateRepository.clearTables();
    HibernateAuthorManager.clearTables();
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();
    HibernateUtil.closeSession();
  }

  public void testStoreAuthors() {
    Author author = author(1);
    Author loaded = (Author) HibernateUtil.getCurrentSession().load(Author.class, "1");
    assertEquals(author, loaded);
    Author author2 = author(2);
    assertEquals(author2, HibernateUtil.getCurrentSession().load(Author.class, "2"));
  }

  public void testSaveArtifact() {
    Artifact a = new HibernateArtifact(createIdentifier(1), null, new Date(), "bula bula");
    HibernateUtil.save(a);
  }

  /**
   * Save an ArtifactIdentifier to the database and then load it back avoiding Hibernate's session cache
   * by using a new session so that the database has to be accessed).
   */
  public void testLoadArtifactIdentifierInNewSession() {
    ArtifactIdentifier aid = createIdentifier(1);
    HibernateUtil.save(aid);
    HibernateUtil.closeSession();
    ArtifactIdentifier aid2 = (ArtifactIdentifier) HibernateUtil.getCurrentSession().get(ArtifactIdentifier.class, aid.getPk());
    assertEquals(aid, aid2);
  }

  public void testLoadArtifactInSameSession() {
    Artifact a = new HibernateArtifact(createIdentifier(1), null, new Date(), "bula bula");
    MarkupElement me = new TrivialLineMarkup(0, 5);
    a.getMarkup().add(me);
    HibernateUtil.save(a);
    HibernateUtil.commit();
    Artifact loaded = (Artifact) HibernateUtil.getCurrentSession().get(HibernateArtifact.class, a.getId().getPk());
    assertNotNull(loaded);
    assertEquals("bula bula", loaded.getContent());
    assertEquals(me, loaded.getMarkup().get(0));
  }

  public void testLoadArtifactInNewSession() {
    Artifact a = new HibernateArtifact(createIdentifier(1), null, new Date(), "bula bula");
    MarkupElement me = new TrivialLineMarkup(0, 5);
    a.getMarkup().add(me);
    HibernateUtil.save(a);
    HibernateUtil.closeSession();
    Artifact loaded = (Artifact) HibernateUtil.getCurrentSession().get(HibernateArtifact.class, a.getId().getPk());
    assertNotNull(loaded);
    assertEquals("bula bula", loaded.getContent());
    assertEquals(me, loaded.getMarkup().get(0));
  }

  public void testLoadArtifactWithRetrieveMethod() throws Exception {
    Artifact a = new HibernateArtifact(createIdentifier(1), null, new Date(), "bula bula");
    HibernateUtil.save(a);
    HibernateUtil.closeSession();
    HibernateRepository repo = (HibernateRepository) AutoConfigurator.newConfiguredApplication().getRepository();
    Artifact loaded = repo.retrieve(createIdentifier(1));
    assertNotNull(loaded);
    assertEquals("bula bula", loaded.getContent());
  }

  public void testStoreReview() {
    ArtifactIdentifier aid = createIdentifier(1);
    HibernateUtil.save(new HibernateArtifact(aid, null, new Date(), "bula bula"));
    HibernateUtil.closeSession();
    Author author = author(5);
    System.out.println("\n\n\n\n\n\n\n\n****\n\n\n\n");
    Review review = new HibernateReview(
        (ArtifactIdentifier) HibernateUtil.getCurrentSession().get(ArtifactIdentifier.class, aid.getPk()),
        author, "möhb", 2, true);
    HibernateUtil.getCurrentSession().save(review);
  }
}
TOP

Related Classes of net.sf.collabreview.hibernate.BasicPersistenceTest

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.