com.datastax.driver.core.ExceptionsTest.java Source code

Java tutorial

Introduction

Here is the source code for com.datastax.driver.core.ExceptionsTest.java

Source

/*
 *      Copyright (C) 2012 DataStax Inc.
 *
 *   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.datastax.driver.core;

import com.datastax.driver.core.exceptions.*;

import java.net.InetAddress;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.testng.annotations.Test;

import static org.testng.Assert.*;

/**
 * Tests Exception classes with seperate clusters per test, when applicable
 */
public class ExceptionsTest {

    /**
     * Tests the AlreadyExistsException.
     * Create a keyspace twice and a table twice.
     * Catch and test all the exception methods.
     */
    @Test(groups = "integration")
    public void alreadyExistsException() throws Throwable {
        Cluster.Builder builder = Cluster.builder();
        CCMBridge.CCMCluster cluster = CCMBridge.buildCluster(1, builder);
        try {
            Session session = cluster.session;
            String keyspace = "TestKeyspace";
            String table = "TestTable";

            String[] cqlCommands = new String[] {
                    String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1), "USE " + keyspace,
                    String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table) };

            // Create the schema once
            session.execute(cqlCommands[0]);
            session.execute(cqlCommands[1]);
            session.execute(cqlCommands[2]);

            // Try creating the keyspace again
            try {
                session.execute(cqlCommands[0]);
            } catch (AlreadyExistsException e) {
                String expected = String.format("Keyspace %s already exists", keyspace.toLowerCase());
                assertEquals(e.getMessage(), expected);
                assertEquals(e.getKeyspace(), keyspace.toLowerCase());
                assertEquals(e.getTable(), null);
                assertEquals(e.wasTableCreation(), false);
            }

            session.execute(cqlCommands[1]);

            // Try creating the table again
            try {
                session.execute(cqlCommands[2]);
            } catch (AlreadyExistsException e) {
                // TODO: Pending CASSANDRA-5362 this won't work. So let's re-enable this once C* 1.2.4
                // is released
                //assertEquals(e.getKeyspace(), keyspace.toLowerCase());
                //assertEquals(e.getTable(), table.toLowerCase());
                assertEquals(e.wasTableCreation(), true);
            }
        } catch (Throwable e) {
            throw e;
        } finally {
            cluster.discard();
        }
    }

    /**
     * Placeholder test for the AuthenticationException.
     * Testing pending CCM authenticated sessions integration.
     */
    public void authenticationException() throws Exception {
        // TODO: Modify CCM to accept authenticated sessions
    }

    /**
     * Tests DriverInternalError.
     * Tests basic message, rethrow, and copy abilities.
     */
    @Test(groups = "integration")
    public void driverInternalError() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new DriverInternalError(errorMessage);
        } catch (DriverInternalError e1) {
            try {
                throw new DriverInternalError(e1);
            } catch (DriverInternalError e2) {
                assertTrue(StringUtils.contains(e2.getMessage(), errorMessage));

                DriverInternalError copy = (DriverInternalError) e2.copy();
                assertEquals(copy.getMessage(), e2.getMessage());
            }
        }
    }

    /**
     * Tests InvalidConfigurationInQueryException.
     * Tests basic message abilities.
     */
    @Test(groups = "integration")
    public void invalidConfigurationInQueryException() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new InvalidConfigurationInQueryException(errorMessage);
        } catch (InvalidConfigurationInQueryException e) {
            assertEquals(e.getMessage(), errorMessage);
        }
    }

    /**
     * Tests InvalidQueryException.
     * Tests basic message and copy abilities.
     */
    @Test(groups = "integration")
    public void invalidQueryException() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new InvalidQueryException(errorMessage);
        } catch (InvalidQueryException e) {
            assertEquals(e.getMessage(), errorMessage);

            InvalidQueryException copy = (InvalidQueryException) e.copy();
            assertEquals(copy.getMessage(), e.getMessage());
        }
    }

    /**
     * Tests InvalidTypeException.
     * Tests basic message and copy abilities.
     */
    @Test(groups = "integration")
    public void invalidTypeException() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new InvalidTypeException(errorMessage);
        } catch (InvalidTypeException e) {
            assertEquals(e.getMessage(), errorMessage);

            InvalidTypeException copy = (InvalidTypeException) e.copy();
            assertEquals(copy.getMessage(), e.getMessage());
        }
    }

    /**
     * Tests the NoHostAvailableException.
     * by attempting to build a cluster using the IP address "255.255.255.255"
     * and test all available exception methods.
     */
    @Test(groups = "integration")
    public void noHostAvailableException() throws Exception {
        String ipAddress = "255.255.255.255";
        HashMap<InetAddress, String> errorsHashMap = new HashMap<InetAddress, String>();
        errorsHashMap.put(InetAddress.getByName(ipAddress), "[/255.255.255.255] Cannot connect");

        try {
            Cluster cluster = Cluster.builder().addContactPoints("255.255.255.255").build();
        } catch (NoHostAvailableException e) {
            assertEquals(e.getMessage(),
                    String.format("All host(s) tried for query failed (tried: [/%s])", ipAddress));
            assertEquals(e.getErrors(), errorsHashMap);

            NoHostAvailableException copy = (NoHostAvailableException) e.copy();
            assertEquals(copy.getMessage(), e.getMessage());
            assertEquals(copy.getErrors(), e.getErrors());
        }
    }

    /**
     * Tests the ReadTimeoutException.
     * Create a 3 node cluster and write out a single key at CL.ALL.
     * Then forcibly kill single node and attempt a read of the key at CL.ALL.
     * Catch and test all available exception methods.
     */
    @Test(groups = "integration")
    public void readTimeoutException() throws Throwable {
        Cluster.Builder builder = Cluster.builder();
        CCMBridge.CCMCluster cluster = CCMBridge.buildCluster(3, builder);
        try {
            Session session = cluster.session;
            CCMBridge bridge = cluster.cassandraCluster;

            String keyspace = "TestKeyspace";
            String table = "TestTable";
            int replicationFactor = 3;
            String key = "1";

            session.execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor));
            session.execute("USE " + keyspace);
            session.execute(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table));

            session.execute(
                    new SimpleStatement(String.format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f))
                            .setConsistencyLevel(ConsistencyLevel.ALL));
            session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table))
                    .setConsistencyLevel(ConsistencyLevel.ALL));

            bridge.forceStop(2);
            try {
                session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table))
                        .setConsistencyLevel(ConsistencyLevel.ALL));
            } catch (ReadTimeoutException e) {
                assertEquals(e.getConsistencyLevel(), ConsistencyLevel.ALL);
                assertEquals(e.getReceivedAcknowledgements(), 2);
                assertEquals(e.getRequiredAcknowledgements(), 3);
                assertEquals(e.wasDataRetrieved(), true);

                ReadTimeoutException copy = (ReadTimeoutException) e.copy();
                assertEquals(copy.getMessage(), e.getMessage());
                assertEquals(copy.wasDataRetrieved(), e.wasDataRetrieved());
            }
        } catch (Throwable e) {
            throw e;
        } finally {
            cluster.discard();
        }
    }

    /**
     * Tests SyntaxError.
     * Tests basic message and copy abilities.
     */
    @Test(groups = "integration")
    public void syntaxError() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new SyntaxError(errorMessage);
        } catch (SyntaxError e) {
            assertEquals(e.getMessage(), errorMessage);

            SyntaxError copy = (SyntaxError) e.copy();
            assertEquals(copy.getMessage(), e.getMessage());
        }
    }

    /**
     * Tests TraceRetrievalException.
     * Tests basic message and copy abilities.
     */
    @Test(groups = "integration")
    public void traceRetrievalException() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new TraceRetrievalException(errorMessage);
        } catch (TraceRetrievalException e) {
            assertEquals(e.getMessage(), errorMessage);

            TraceRetrievalException copy = (TraceRetrievalException) e.copy();
            assertEquals(copy.getMessage(), e.getMessage());
        }
    }

    /**
     * Tests TruncateException.
     * Tests basic message and copy abilities.
     */
    @Test(groups = "integration")
    public void truncateException() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new TruncateException(errorMessage);
        } catch (TruncateException e) {
            assertEquals(e.getMessage(), errorMessage);

            TruncateException copy = (TruncateException) e.copy();
            assertEquals(copy.getMessage(), e.getMessage());
        }
    }

    /**
     * Tests UnauthorizedException.
     * Tests basic message and copy abilities.
     */
    @Test(groups = "integration")
    public void unauthorizedException() throws Exception {
        String errorMessage = "Test Message";

        try {
            throw new UnauthorizedException(errorMessage);
        } catch (UnauthorizedException e) {
            assertEquals(e.getMessage(), errorMessage);

            UnauthorizedException copy = (UnauthorizedException) e.copy();
            assertEquals(copy.getMessage(), e.getMessage());
        }
    }

    /**
     * Tests the UnavailableException.
     * Create a 3 node cluster and write out a single key at CL.ALL.
     * Then kill single node, wait for gossip to propogate the new state,
     * and attempt to read and write the same key at CL.ALL.
     * Catch and test all available exception methods.
     */
    @Test(groups = "integration")
    public void unavailableException() throws Throwable {
        Cluster.Builder builder = Cluster.builder();
        CCMBridge.CCMCluster cluster = CCMBridge.buildCluster(3, builder);
        try {
            Session session = cluster.session;
            CCMBridge bridge = cluster.cassandraCluster;

            String keyspace = "TestKeyspace";
            String table = "TestTable";
            int replicationFactor = 3;
            String key = "1";

            session.execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor));
            session.execute("USE " + keyspace);
            session.execute(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table));

            session.execute(
                    new SimpleStatement(String.format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f))
                            .setConsistencyLevel(ConsistencyLevel.ALL));
            session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table))
                    .setConsistencyLevel(ConsistencyLevel.ALL));

            bridge.stop(2);
            // Ensure that gossip has reported the node as down.
            Thread.sleep(1000);

            try {
                session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table))
                        .setConsistencyLevel(ConsistencyLevel.ALL));
            } catch (UnavailableException e) {
                String expectedError = String.format(
                        "Not enough replica available for query at consistency %s (%d required but only %d alive)",
                        "ALL", 3, 2);
                assertEquals(e.getMessage(), expectedError);
                assertEquals(e.getConsistency(), ConsistencyLevel.ALL);
                assertEquals(e.getRequiredReplicas(), replicationFactor);
                assertEquals(e.getAliveReplicas(), replicationFactor - 1);
            }

            try {
                session.execute(
                        new SimpleStatement(String.format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f))
                                .setConsistencyLevel(ConsistencyLevel.ALL));
            } catch (UnavailableException e) {
                String expectedError = String.format(
                        "Not enough replica available for query at consistency %s (%d required but only %d alive)",
                        "ALL", 3, 2);
                assertEquals(e.getMessage(), expectedError);
                assertEquals(e.getConsistency(), ConsistencyLevel.ALL);
                assertEquals(e.getRequiredReplicas(), replicationFactor);
                assertEquals(e.getAliveReplicas(), replicationFactor - 1);
            }
        } catch (Throwable e) {
            throw e;
        } finally {
            cluster.discard();
        }
    }

    /**
     * Tests the WriteTimeoutException.
     * Create a 3 node cluster and write out a single key at CL.ALL.
     * Then forcibly kill single node and attempt to write the same key at CL.ALL.
     * Catch and test all available exception methods.
     */
    @Test(groups = "integration")
    public void writeTimeoutException() throws Throwable {
        Cluster.Builder builder = Cluster.builder();
        CCMBridge.CCMCluster cluster = CCMBridge.buildCluster(3, builder);
        try {
            Session session = cluster.session;
            CCMBridge bridge = cluster.cassandraCluster;

            String keyspace = "TestKeyspace";
            String table = "TestTable";
            int replicationFactor = 3;
            String key = "1";

            session.execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor));
            session.execute("USE " + keyspace);
            session.execute(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table));

            session.execute(
                    new SimpleStatement(String.format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f))
                            .setConsistencyLevel(ConsistencyLevel.ALL));
            session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table))
                    .setConsistencyLevel(ConsistencyLevel.ALL));

            bridge.forceStop(2);
            try {
                session.execute(
                        new SimpleStatement(String.format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f))
                                .setConsistencyLevel(ConsistencyLevel.ALL));
            } catch (WriteTimeoutException e) {
                assertEquals(e.getConsistencyLevel(), ConsistencyLevel.ALL);
                assertEquals(e.getReceivedAcknowledgements(), 2);
                assertEquals(e.getRequiredAcknowledgements(), 3);
                assertEquals(e.getWriteType(), WriteType.SIMPLE);
            }
        } catch (Throwable e) {
            throw e;
        } finally {
            cluster.discard();
        }
    }
}