Package com.orientechnologies.orient.test.database.auto

Source Code of com.orientechnologies.orient.test.database.auto.TransactionAtomicTest

/*
* Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
*
* 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 com.orientechnologies.orient.test.database.auto;

import java.io.IOException;

import org.testng.Assert;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.orientechnologies.orient.core.db.ODatabase;
import com.orientechnologies.orient.core.db.ODatabaseListener;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.record.ODatabaseFlat;
import com.orientechnologies.orient.core.exception.OConcurrentModificationException;
import com.orientechnologies.orient.core.exception.OTransactionException;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ORecordFlat;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.storage.ORecordDuplicatedException;
import com.orientechnologies.orient.enterprise.channel.binary.OResponseProcessingException;

@Test(groups = "dictionary")
public class TransactionAtomicTest extends DocumentDBBaseTest {
  @Parameters(value = "url")
  public TransactionAtomicTest(@Optional String url) {
    super(url);
  }

  @Test
  public void testTransactionAtomic() throws IOException {
    ODatabaseFlat db1 = new ODatabaseFlat(url);
    db1.open("admin", "admin");

    ODatabaseFlat db2 = new ODatabaseFlat(url);
    db2.open("admin", "admin");

    ORecordFlat record1 = new ORecordFlat(db1);
    record1.value("This is the first version").save();

    // RE-READ THE RECORD
    record1.reload();
    ORecordFlat record2 = db2.load(record1.getIdentity());

    record2.value("This is the second version").save();
    record2.value("This is the third version").save();

    record1.reload(null, true);

    Assert.assertEquals(record1.value(), "This is the third version");

    db1.close();
    db2.close();
  }

  @Test
  public void testMVCC() throws IOException {

    ODocument doc = new ODocument("Account");
    doc.field("version", 0);
    doc.save();

    doc.setDirty();
    doc.field("testmvcc", true);
    doc.getRecordVersion().increment();
    try {
      doc.save();
      Assert.assertTrue(false);
    } catch (OResponseProcessingException e) {
      Assert.assertTrue(e.getCause() instanceof OConcurrentModificationException);
    } catch (OConcurrentModificationException e) {
      Assert.assertTrue(true);
    }
  }

  @Test(expectedExceptions = OTransactionException.class)
  public void testTransactionPreListenerRollback() throws IOException {
    ODatabaseFlat db = new ODatabaseFlat(url);
    db.open("admin", "admin");

    ORecordFlat record1 = new ORecordFlat(db);
    record1.value("This is the first version").save();

    db.registerListener(new ODatabaseListener() {

      @Override
      public void onAfterTxCommit(ODatabase iDatabase) {
      }

      @Override
      public void onAfterTxRollback(ODatabase iDatabase) {
      }

      @Override
      public void onBeforeTxBegin(ODatabase iDatabase) {
      }

      @Override
      public void onBeforeTxCommit(ODatabase iDatabase) {
        throw new RuntimeException("Rollback test");
      }

      @Override
      public void onBeforeTxRollback(ODatabase iDatabase) {
      }

      @Override
      public void onClose(ODatabase iDatabase) {
      }

      @Override
      public void onCreate(ODatabase iDatabase) {
      }

      @Override
      public void onDelete(ODatabase iDatabase) {
      }

      @Override
      public void onOpen(ODatabase iDatabase) {
      }

      @Override
      public boolean onCorruptionRepairDatabase(ODatabase iDatabase, final String iReason, String iWhatWillbeFixed) {
        return true;
      }
    });

    db.begin();
    db.commit();

    db.close();
  }

  @Test
  public void testTransactionWithDuplicateUniqueIndexValues() {
    OClass fruitClass = database.getMetadata().getSchema().getClass("Fruit");

    if (fruitClass == null) {
      fruitClass = database.getMetadata().getSchema().createClass("Fruit");

      fruitClass.createProperty("name", OType.STRING);
      fruitClass.createProperty("color", OType.STRING);

      database.getMetadata().getSchema().getClass("Fruit").getProperty("color").createIndex(OClass.INDEX_TYPE.UNIQUE);
    }

    Assert.assertEquals(database.countClusterElements("Fruit"), 0);

    try {
      database.begin();

      ODocument apple = new ODocument("Fruit").field("name", "Apple").field("color", "Red");
      ODocument orange = new ODocument("Fruit").field("name", "Orange").field("color", "Orange");
      ODocument banana = new ODocument("Fruit").field("name", "Banana").field("color", "Yellow");
      ODocument kumquat = new ODocument("Fruit").field("name", "Kumquat").field("color", "Orange");

      apple.save();
      Assert.assertEquals(apple.getIdentity().getClusterId(), fruitClass.getDefaultClusterId());

      orange.save();
      Assert.assertEquals(orange.getIdentity().getClusterId(), fruitClass.getDefaultClusterId());

      banana.save();
      Assert.assertEquals(banana.getIdentity().getClusterId(), fruitClass.getDefaultClusterId());

      kumquat.save();
      Assert.assertEquals(kumquat.getIdentity().getClusterId(), fruitClass.getDefaultClusterId());

      database.commit();
      Assert.assertTrue(false);

    } catch (OResponseProcessingException e) {
      Assert.assertTrue(e.getCause() instanceof ORecordDuplicatedException);
      database.rollback();
    } catch (ORecordDuplicatedException e) {
      Assert.assertTrue(true);
      database.rollback();
    }

    Assert.assertEquals(database.countClusterElements("Fruit"), 0);
  }

  @Test
  public void testTransactionalSQL() {
    long prev = database.countClusterElements("Account");

    database.command(new OCommandSQL("transactional insert into Account set name = 'txTest1'")).execute();

    Assert.assertEquals(database.countClusterElements("Account"), prev + 1);
  }

  @Test
  public void testTransactionalSQLJoinTx() {
    long prev = database.countClusterElements("Account");

    database.begin();

    database.command(new OCommandSQL("transactional insert into Account set name = 'txTest2'")).execute();

    Assert.assertTrue(database.getTransaction().isActive());

    if (!url.startsWith("remote"))
      Assert.assertEquals(database.countClusterElements("Account"), prev);

    database.commit();

    Assert.assertFalse(database.getTransaction().isActive());
    Assert.assertEquals(database.countClusterElements("Account"), prev + 1);
  }
}
TOP

Related Classes of com.orientechnologies.orient.test.database.auto.TransactionAtomicTest

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.