Package de.chris_soft.database

Source Code of de.chris_soft.database.DbTest

package de.chris_soft.database;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Test;
import de.chris_soft.nanoarchive.DB;
import de.chris_soft.utilities.FileUtils;
import de.chris_soft.utilities.Pair;

/**
* Unit tests for database class.
* @author Christian Packenius.
*/
public class DbTest extends TestCase {
  /**
   * Sample document.
   */
  private static final File SAMPLE_DOCUMENT_FILE = new File("libs/derby.jar");

  private static final String DB_PATH = "dummy";

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   */
  @Test
  public void testCreateNewDatabase() throws SQLException, ClassNotFoundException {
    FileUtils.deleteAll(new File(DB_PATH));
    DB db = new DB(DB_PATH);
    db.close();
  }

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   */
  // @Test
  public void _testListAllTables() throws SQLException, ClassNotFoundException {
    DB db = new DB(DB_PATH);
    db.printSelection("select * from SYS.SYSTABLES");
    db.printSelection("select * from SYS.SYSCOLUMNS");
    db.printSelection("select * from SYS.SYSCONGLOMERATES");
    db.close();
  }

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   * @throws IOException
   */
  @Test
  public void testCreateDocument() throws SQLException, ClassNotFoundException, IOException {
    DB db = new DB(DB_PATH);
    db.addDocument(333, 4711, SAMPLE_DOCUMENT_FILE);
    db.addDocument(334, 4712, SAMPLE_DOCUMENT_FILE);
    db.close();
  }

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   * @throws IOException
   */
  @Test
  public void testCreateDocumentTwice() throws SQLException, ClassNotFoundException, IOException {
    DB db = new DB(DB_PATH);
    long documentID = db.addDocument(-1, 4711, SAMPLE_DOCUMENT_FILE);
    try {
      db.addDocument(documentID, 4711, SAMPLE_DOCUMENT_FILE);
      throw new RuntimeException("Documents with identical documentID created!");
    } catch (SQLException e) {
      // Okay.
    }
    db.close();
  }

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   * @throws IOException
   */
  @Test
  public void testRetrieveDocument() throws SQLException, ClassNotFoundException, IOException {
    DB db = new DB(DB_PATH);
    byte[] ba = db.readDocument(333);
    assertEquals(ba.length, SAMPLE_DOCUMENT_FILE.length());
    db.close();
  }

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   */
  @Test
  public void testGetDocumentsFromPath() throws ClassNotFoundException, SQLException {
    DB db = new DB(DB_PATH);
    List<Long> docs = db.getDocumentsFromPath(4712);
    assertEquals(docs.size(), 1);
    assertEquals(docs.get(0).longValue(), 334);
    docs = db.getDocumentsFromPath(4711);
    Collections.sort(docs);
    assertEquals(docs.size(), 2);
    assertEquals(docs.get(0).longValue(), 333);
    db.close();
  }

  /**
   * @throws ClassNotFoundException
   * @throws SQLException
   */
  public void testDeleteDocument() throws ClassNotFoundException, SQLException {
    DB db = new DB(DB_PATH);
    db.deleteDocument(333);
    List<Long> docs = db.getDocumentsFromPath(4711);
    Collections.sort(docs);
    assertEquals(docs.size(), 1);
    assertNotSame(docs.get(0).longValue(), 333);
    db.close();
  }

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   */
  @Test
  public void testCountDocuments() throws SQLException, ClassNotFoundException {
    DB db = new DB(DB_PATH);
    assertEquals(2, db.getDocumentCount());
    db.close();
  }

  /**
   * @throws ClassNotFoundException
   * @throws SQLException
   */
  @Test
  public void testUpdateDocumentPath() throws ClassNotFoundException, SQLException {
    DB db = new DB(DB_PATH);
    List<Long> docs = db.getDocumentsFromPath(4712);
    assertEquals(docs.size(), 1);
    docs = db.getDocumentsFromPath(4711);
    assertEquals(docs.size(), 1);
    db.changeDocumentPath(docs.get(0), 4712);
    docs = db.getDocumentsFromPath(4712);
    assertEquals(docs.size(), 2);
    assertEquals(2, db.getDocumentCount());
    db.close();
  }

  /**
   * @throws ClassNotFoundException
   * @throws SQLException
   */
  @Test
  public void testDocumentProperties() throws ClassNotFoundException, SQLException {
    DB db = new DB(DB_PATH);
    assertNull(db.getDocumentProperty(334, "name"));
    db.setDocumentProperty(334, "name", "Wumpel Putz");
    assertEquals("Wumpel Putz", db.getDocumentProperty(334, "name"));
    db.deleteDocumentProperty(334, "name");
    assertNull(db.getDocumentProperty(334, "name"));
    db.close();
  }

  /**
   * @throws ClassNotFoundException
   * @throws SQLException
   * @throws IOException
   */
  @Test
  public void testPaths() throws ClassNotFoundException, SQLException, IOException {
    DB db = new DB(DB_PATH);
    db.addDocument(3312, 0, SAMPLE_DOCUMENT_FILE);
    long pathID = db.addPath("neuer Pfad", -1);
    List<Pair<Long, String>> paths = db.getChildPaths(0);
    assertEquals(1, paths.size());
    assertEquals(pathID, paths.get(0).obj1.longValue());
    assertEquals("neuer Pfad", paths.get(0).obj2);
    assertEquals(1, db.getDocumentsFromPath(-1).size());
    db.changeDocumentPath(3312, pathID);
    assertEquals(0, db.getDocumentsFromPath(0).size());
    assertEquals(1, db.getDocumentsFromPath(pathID).size());
    db.deletePath(pathID);
    assertEquals(1, db.getDocumentsFromPath(0).size());
    assertEquals(0, db.getDocumentsFromPath(pathID).size());
    db.close();
  }

  /**
   * @throws ClassNotFoundException
   * @throws SQLException
   * @throws IOException
   */
  @Test
  public void testPaths2() throws ClassNotFoundException, SQLException, IOException {
    DB db = new DB(DB_PATH);
    long pathID1 = db.addPath("neuer Pfad 1", -1);
    long pathID2 = db.addPath("neuer Pfad 2", -1);
    long pathID3 = db.addPath("neuer Pfad 3", pathID2);
    db.addDocument(-1, 0, SAMPLE_DOCUMENT_FILE);
    db.addDocument(-1, pathID1, SAMPLE_DOCUMENT_FILE);
    db.addDocument(-1, pathID2, SAMPLE_DOCUMENT_FILE);
    db.addDocument(-1, pathID3, SAMPLE_DOCUMENT_FILE);
    assertEquals(2, db.getDocumentsFromPath(0).size());
    assertEquals(1, db.getDocumentsFromPath(pathID1).size());
    assertEquals(1, db.getDocumentsFromPath(pathID2).size());
    assertEquals(1, db.getDocumentsFromPath(pathID3).size());
    db.deletePath(pathID2);
    assertEquals(4, db.getDocumentsFromPath(0).size());
    assertEquals(1, db.getDocumentsFromPath(pathID1).size());
    db.close();
  }

  /**
   * @throws ClassNotFoundException
   * @throws SQLException
   * @throws IOException
   */
  @Test
  public void testLabels() throws ClassNotFoundException, SQLException, IOException {
    DB db = new DB(DB_PATH);
    long labelID = db.createLabel("lucas");
    assertEquals("lucas", db.getLabelName(labelID));
    db.addLabelToDocument(3312L, labelID);
    List<Long> list = db.getDocumentsFromLabel(labelID);
    assertEquals(1, list.size());
    assertEquals(3312L, list.get(0).longValue());
    db.deleteLabel(labelID);
    list = db.getDocumentsFromLabel(labelID);
    assertEquals(0, list.size());
    labelID = db.createLabel("luisa");
    db.addLabelToDocument(3312L, labelID);
    list = db.getDocumentsFromLabel(labelID);
    assertEquals(1, list.size());
    assertEquals(3312L, list.get(0).longValue());
    list = db.getLabelsFromDocument(3312L);
    assertEquals(1, list.size());
    assertEquals(labelID, list.get(0).longValue());
    db.removeLabelFromDocument(3312L, labelID);
    list = db.getLabelsFromDocument(3312L);
    assertEquals(0, list.size());
    db.close();
  }

  /**
   * @throws SQLException
   * @throws ClassNotFoundException
   */
  // @Test
  public void _testListDocuments() throws SQLException, ClassNotFoundException {
    DB db = new DB(DB_PATH);
    db.printSelection("select * from DOCUMENTS");
    db.close();
  }
}
TOP

Related Classes of de.chris_soft.database.DbTest

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.