Skip to content
Browse files

Deleted from types directory and move into its our test package.

  • Loading branch information...
1 parent 9da280c commit 5993beb127162baf8b6a828bb5fe9a5f81816cb2 @ccorsi committed Sep 3, 2010
Showing with 0 additions and 364 deletions.
  1. +0 −364 persistence/books/orientdb/src/books/persistence/types/orientdb/BooksDBTest.java
View
364 persistence/books/orientdb/src/books/persistence/types/orientdb/BooksDBTest.java
@@ -1,364 +0,0 @@
-/**
-
- BSD License
-
- Copyright (c) 2010- , Claudio Corsi
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
- in the documentation and/or other materials provided with the distribution.
- * Neither the name of Claudio Corsi nor the names of its contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- */
-package books.persistence.types.orientdb;
-
-//import static org.junit.Assert.*;
-
-import java.io.File;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Properties;
-
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.valhalla.services.Service;
-import org.valhalla.services.ServiceException;
-import org.valhalla.services.ServiceFactory;
-
-import books.persistence.manager.DatabaseManager;
-import books.persistence.manager.orientdb.OrientDBDatabaseManager;
-import books.persistence.types.Book;
-import books.persistence.types.Gender;
-import books.persistence.types.Person;
-import books.persistence.types.PersonType;
-
-import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
-import com.orientechnologies.orient.core.hook.ORecordHook;
-import com.orientechnologies.orient.core.iterator.ORecordIteratorMultiCluster;
-import com.orientechnologies.orient.core.record.ORecord;
-import com.orientechnologies.orient.core.record.impl.ODocument;
-import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
-
-/**
- * @author ccorsi
- *
- */
-public class BooksDBTest {
-
- private static ODatabaseDocumentTx db;
-
- // @BeforeClass
- public static void setUpBeforeClass() throws Exception {
-
- File dbFile = new File("/orientdb/databases", "books");
-
- // Get a reference to a database document transaction instance
- db = new ODatabaseDocumentTx("local:"
- + dbFile.getAbsolutePath().replace('\\', '/'));
-
- // Create the books database.
- // TODO: Replace this call and/or the previous call with a manager type
- // call.
- try {
- db.open("admin", "admin");
- } catch (Exception e) {
- // e.printStackTrace();
- db.create();
- }
-
- db.registerHook(new ORecordHook() {
-
- @Override
- public void onTrigger(TYPE type, ORecord<?> rec) {
- ODocument doc;
- if( (doc = ODocument.class.cast(rec)) != null ) {
- String name = doc.getClassName();
- System.out.println("Processing hook callback " + type + " for record " + name);
- if("Author".equals(name)) {
- // get the middle names collection and print the type...
- Collection<String> middleNames;
- if( (middleNames = doc.field("middle-names")) != null) {
- System.out.println("Middle names collection is type " + middleNames.getClass());
- if(List.class.isInstance(middleNames)) {
- System.out.println("#################### THIS IS WRONG!!! #########################");
- System.out.println("Record id: " + doc.getIdentity());
- System.out.println(doc.toJSON());
- System.out.println("Status: " + doc.getInternalStatus());
- } else {
- System.out.println("THIS IS THE RIGHT TYPE");
- }
- }
- }
- }
- }
-
- });
- }
-
- // @AfterClass
- public static void tearDownAfterClass() throws Exception {
- db.close();
- if(db != null)
- return;
- // Delete all Books, Authors, etc... references within the database...
- List<ODocument> children = new LinkedList<ODocument>();
- // This method should return an empty collection but does that make
- // sense....
- List<ODocument> books = db.query(new OSQLSynchQuery<ODocument>("select * from books"));
- ODocument root = books.iterator().next();
- books = root.field("books");
- // ORecordIteratorMultiCluster<ODocument> books = db.browseClass("Book");
- if (books != null) {
- db.begin();
- for (ODocument book : books) {
- // Get references to Person and Gender instances and place them
- // on a list of ODocument...
- List<ODocument> list = book.field(PersonType.Author + "s");
- if (list != null && !list.isEmpty()) {
- for(ODocument author : list) {
- author.setVersion(-1);
- db.save(author);
- }
- }
- list = book.field(PersonType.Illustrator + "s");
- if (list != null && !list.isEmpty()) {
- for(ODocument illustrator : list) {
- illustrator.setVersion(-1);
- db.save(illustrator);
- }
- }
- if (book.getIdentity().isValid()) {
- // Let use see if this will resolve the current problem...
- // If this does the trick then we know that this is the bug...
- book.setVersion(-1);
- db.save(book);
- }
- }
- db.commit();
- for (ODocument book : books) {
- // Get references to Person and Gender instances and place them
- // on a list of ODocument...
- List<ODocument> list = book.field(PersonType.Author + "s");
- if (list != null && !list.isEmpty()) {
- children.addAll(list);
- }
- list = book.field(PersonType.Illustrator + "s");
- if (list != null && !list.isEmpty()) {
- children.addAll(list);
- }
- if (book.getIdentity().isValid())
- db.delete(book);
- }
- for (ODocument child : children) {
- if (child.getIdentity().isValid())
- db.delete(child);
- }
- db.commit();
- }
- db.close();
- }
-
- @Test
- public void simulateBookFramework() throws ServiceException {
- System.setProperty("books.persistence.manager.databasemanager",
- "books.persistence.manager.orientdb.OrientDBDatabaseManager");
-
- Service<DatabaseManager> service = ServiceFactory.createService(
- "books.persistence.manager.databasemanager",
- "books.persistence.manager.databasemanager",
- DatabaseManager.class);
-
- DatabaseManager manager = service.create();
-
- Properties properties = new Properties();
- properties.setProperty("dbPath", "/orientdb/databases/books");
- properties.setProperty("dbName", "MoreBooks");
-
- manager.init(properties);
-
- ODatabaseDocumentTx db = OrientDBDatabaseManager.class.cast(manager)
- .getDatabase();
-
- try {
- System.out.println("Number of books: " + db.countClass("Book"));
- System.out.println("Number of authors: " + db.countClass("Author"));
- System.out.println("Number of illustrators: "
- + db.countClass("Illustrator"));
- } catch (IllegalArgumentException e) {
- e.printStackTrace(System.out);
- }
-
- manager.beginTx(); // Start a new transaction...
-
- Book book = manager.createBook();
-
- book.setCopyRight(2008);
- book.setPages(182);
- book.setTitle("Year of the Rat");
- book.setDateFinishReading(Calendar.getInstance().getTime());
-
- Person author = manager.createPerson(PersonType.Author);
- author.setFirstName("Grace");
- author.addLastName("Lin");
-
- System.out.println("After creating Author: " + author.toString());
-
- Person illustrator = manager.createPerson(PersonType.Illustrator);
- illustrator.setFirstName("Grace");
- illustrator.addLastName("Lin");
-
- Gender gender = manager.createGender();
- gender.setName("Fiction");
-
- System.out.println("Before adding Author: " + author.toString());
- book.addPerson(author);
- book.addPerson(illustrator);
- book.addGender(gender);
-
- manager.save(book);
-
- manager.commitTx(); // Commit the changes...
-
- System.out.println("Saved book identity " + manager.getIdentifier(book));
-
- manager.beginTx(); // Start a new transaction...
-
- book = manager.createBook();
-
- book.setCopyRight(1996);
- book.setPages(133);
- book.setSeries("The Boxcar Children");
- book.setTitle("The Mystery of the Black Raven #12");
- Calendar cal = Calendar.getInstance();
- book.setDateFinishReading(cal.getTime());
-
- author = manager.createPerson(PersonType.Author);
- author.setFirstName("Gertrude");
- author.addLastName("Warner");
- author.addMiddleName("Chandler");
- author.setType(PersonType.Author);
- book.addPerson(author);
-
- illustrator = manager.createPerson(PersonType.Illustrator);
- illustrator.setType(PersonType.Illustrator);
- illustrator.setFirstName("Charles");
- illustrator.addLastName("Tang");
- book.addPerson(illustrator);
-
- manager.save(book);
-
- manager.commitTx(); // Commit the new changes...
-
- System.out.println("Listing Books in database");
-
- for (Book abook : manager.getBooks()) {
- System.out.println("Book: " + abook);
- }
-
- System.out.println("Listing Persons in database");
- for (PersonType type : PersonType.values()) {
- for (Person person : manager.getPersons(type)) {
- System.out.println("Person: " + person);
- System.out.println("Middle names: " + person.getMiddleNames());
- }
- }
-
- System.out.println("Listing Genders in database");
- for (Gender agender : manager.getGenders()) {
- System.out.println("Gender: " + agender);
- }
-
- manager.close();
- }
-
- public void testBookOrientDB() {
-
- ODocument doc = db.newInstance("Book");
- BookOrientDB book = new BookOrientDB(doc);
-
- book.setCopyRight(1996);
- book.setPages(133);
- book.setSeries("The Boxcar Children");
- book.setTitle("The Mystery of the Black Raven #12");
- Calendar cal = Calendar.getInstance();
- // cal.set(2010, 7, 3);
- book.setDateFinishReading(cal.getTime());
-
- PersonOrientDB author = new PersonOrientDB(db.newInstance("Author"));
- author.setFirstName("Gertrude");
- author.addLastName("Warner");
- author.addMiddleName("Chandler");
- author.setType(PersonType.Author);
- book.addPerson(author);
-
- PersonOrientDB illustrator = new PersonOrientDB(db
- .newInstance("Illustrator"));
- illustrator.setType(PersonType.Illustrator);
- illustrator.setFirstName("Charles");
- illustrator.addLastName("Tang");
- book.addPerson(illustrator);
-
- System.out.println("Object id before saving: " + doc.getIdentity());
- db.save(doc);
- System.out.println("Object id after saving: " + doc.getIdentity());
-
- System.out.println("Saved document id: " + doc.getIdentity());
- System.out.println(doc.field(BookOrientDB.DATEREAD));
- printBook(doc);
-
- ORecordIteratorMultiCluster<ODocument> books = db.browseClass("Book");
-
- System.out.println("Printing all Book instances in the database");
- for (ODocument abook : books) {
- // TODO: Only process books that are valid...
- // Ask the developers of orientdb if deleted records are suppose to
- // still exist within the database?
- // Yes, they still exist but it seems that their version number is not set to -1
- // but to the last time it was updated...
- System.out.println(abook.getIdentity() + " : "
- + abook.getIdentity().isValid());
- if (abook.getIdentity().isValid() == false)
- continue;
- System.out.println(abook.field(BookOrientDB.DATEREAD));
- System.out.println(abook.toJSON());
- // printBook(abook);
- }
- System.out
- .println("Completed listing all Book instances in the database");
- }
-
- private void printBook(ODocument book) {
- BookOrientDB theBook = new BookOrientDB(book);
- for (Gender gender : theBook.getGenders()) {
- System.out.println("Gender: " + gender.getName());
- }
- for (Person person : theBook.getPersons(PersonType.Author)) {
- // TODO: Need to create a ticket for this issue....
- PersonOrientDB.class.cast(person).getDocument().toJSON();
- StringBuilder name = new StringBuilder(person.getFirstName());
- for (String middleName : person.getMiddleNames()) {
- name.append(' ').append(middleName);
- }
- for (String lastName : person.getLastNames()) {
- name.append(' ').append(lastName);
- }
- System.out.println("Name: " + name.toString());
- }
- return;
- }
-
-}

0 comments on commit 5993beb

Please sign in to comment.
Something went wrong with that request. Please try again.