Java Code Examples for org.cassandraunit.utils.EmbeddedCassandraServerHelper#startEmbeddedCassandra()

The following examples show how to use org.cassandraunit.utils.EmbeddedCassandraServerHelper#startEmbeddedCassandra() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColumnFamilyApp.java    From tutorials with MIT License 6 votes vote down vote up
public static void main(String... args) throws Exception {

        EmbeddedCassandraServerHelper.startEmbeddedCassandra();

        ColumnConfiguration configuration = new CassandraConfiguration();
        try(ColumnFamilyManagerFactory entityManagerFactory = configuration.get()) {
            ColumnFamilyManager entityManager = entityManagerFactory.get(KEY_SPACE);
            ColumnEntity columnEntity = ColumnEntity.of(COLUMN_FAMILY);
            Column key = Columns.of("id", 10L);
            Column name = Columns.of("name", "JNoSQL in Acion");
            columnEntity.add(key);
            columnEntity.add(name);
            ColumnEntity saved = entityManager.insert(columnEntity);
            System.out.println(saved);
        }

        EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
        EmbeddedCassandraServerHelper.stopEmbeddedCassandra();
    }
 
Example 2
Source File: Cassandra.java    From spring-data-examples with Apache License 2.0 6 votes vote down vote up
@Override
protected void before() throws Throwable {

	if (runtimeMode == RuntimeMode.REQUIRE_RUNNING_INSTANCE) {
		if (!CassandraSocket.isConnectable(getHost(), getPort())) {
			throw new AssumptionViolatedException(
					String.format("Cassandra is not reachable at %s:%s.", getHost(), getPort()));
		}
	}

	if (runtimeMode == RuntimeMode.EMBEDDED_IF_NOT_RUNNING) {
		if (CassandraSocket.isConnectable(getHost(), getPort())) {
			return;
		}
	}

	EmbeddedCassandraServerHelper.startEmbeddedCassandra("embedded-cassandra.yaml", "target/embeddedCassandra",
			TimeUnit.SECONDS.toMillis(60));
	super.before();
}
 
Example 3
Source File: CassandraClientTestBase.java    From vertx-cassandra-client with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startEmbeddedCassandra() throws Exception {
  String version = System.getProperty("java.version");
  // this statement can be removed only when this issue[https://github.com/jsevellec/cassandra-unit/issues/249] will be resolved
  if (!version.startsWith("1.8")) {
    throw new IllegalStateException("Only Java 8 is allowed for running tests. Your java version: " + version);
  }
  EmbeddedCassandraServerHelper.startEmbeddedCassandra();
}
 
Example 4
Source File: EmbeddedCassandraStoreFactory.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private Session createEmbeddedCassandra() {
    // Disable fsync for a massive speedup on old platters. This improves boot time by a few seconds.
    System.setProperty("cassandra.unsafesystem", "true");

    try {
        File cassandraTmpDir = Files.createTempDir();
        EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG, cassandraTmpDir.getAbsolutePath(), STARTUP_TIMEOUT);
    } catch (Exception e) {
        throw new IllegalStateException("Cannot initialize the embedded Cassandra", e);
    }

    Session session = EmbeddedCassandraServerHelper.getSession();
    session.execute("CREATE KEYSPACE " + CASSANDRA_KEYSPACE + " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 }");
    session.execute("USE " + CASSANDRA_KEYSPACE);

    CQLDataLoader dataLoader = new CQLDataLoader(session);
    dataLoader.load(new ClassPathCQLDataSet(CASSANDRA_SCHEMA, "Titus"));

    return session;
}
 
Example 5
Source File: _AbstractCassandraTest.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
@BeforeClass
public static void startServer() throws InterruptedException, TTransportException, ConfigurationException, IOException {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    Cluster cluster = new Cluster.Builder().addContactPoints("127.0.0.1").withPort(9142).build();
    Session session = cluster.connect();
    CQLDataLoader dataLoader = new CQLDataLoader(session);
    dataLoader.load(new ClassPathCQLDataSet("config/cql/create-tables.cql", true, "cassandra_unit_keyspace"));
}
 
Example 6
Source File: SpringContextTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeClass
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
    final Session session = cluster.connect();
    session.execute(KEYSPACE_CREATION_QUERY);
    session.execute(KEYSPACE_ACTIVATE_QUERY);
    Thread.sleep(5000);
}
 
Example 7
Source File: ExampleCassandraAsyncEndpoint.java    From riposte-microservice-template with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("UnusedReturnValue")
private static Session startEmbeddedCassandra(boolean disableCassandra) {
    if (disableCassandra) {
        logger.warn("Embedded cassandra is NOT starting up because your app configuration explicitly requests "
                    + "that it be disabled.");
        return null;
    }

    if (cassandraSession == null) {
        File cassandraWorkDir = new File(embeddedClusterWorkDirectory);
        String cassandraWorkDirAbsolutePath = cassandraWorkDir.getAbsolutePath();
        if (!cassandraWorkDir.exists()) {
            logger.info("Creating the  embedded Cassandra folders...{}", cassandraWorkDirAbsolutePath);
            if (!cassandraWorkDir.mkdirs()) {
                throw new RuntimeException("Unable to create working directory " + cassandraWorkDirAbsolutePath);
            }
        }
        // Start embedded cassandra
        logger.info("Finished Creating the  embedded Cassandra folders...{}", cassandraWorkDirAbsolutePath);
        logger.info("Starting embedded Cassandra");

        try {
            EmbeddedCassandraServerHelper.startEmbeddedCassandra(cassandraYamlFile,
                                                                 embeddedClusterWorkDirectory);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }

        Cluster cassandraCluster = Cluster.builder()
                                          .addContactPoint(embeddedClusterContactPointHost)
                                          .withPort(embeddedClusterContactPointPort)
                                          .build();
        cassandraSession = cassandraCluster.connect();
    }

    return cassandraSession;
}
 
Example 8
Source File: AbstractCassandraTest.java    From gpmr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startServer() throws InterruptedException, TTransportException, ConfigurationException, IOException, URISyntaxException  {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    Cluster cluster = new Cluster.Builder().addContactPoints("127.0.0.1").withPort(9142).build();
    Session session = cluster.connect();
    CQLDataLoader dataLoader = new CQLDataLoader(session);
    dataLoader.load(new ClassPathCQLDataSet("config/cql/create-tables.cql", true, CASSANDRA_UNIT_KEYSPACE));
    applyScripts(dataLoader, "config/cql/changelog/", "*.cql");
}
 
Example 9
Source File: CassandraTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Before
public void before(final MockTracer tracer) throws Exception {
  if (isJdkSupported) {
    tracer.reset();
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    //EmbeddedCassandraServerHelper.getSession();
  }
}
 
Example 10
Source File: EmbeddedCassandra.java    From conductor with Apache License 2.0 5 votes vote down vote up
private void startEmbeddedCassandra() throws Exception {
    try {
        EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    } catch (Exception e) {
        LOGGER.error("Error starting embedded cassandra server", e);
        throw e;
    }
}
 
Example 11
Source File: TitusCassandraResource.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
    // This improves boot time by a few seconds
    System.setProperty("cassandra.unsafesystem", "true");

    EmbeddedCassandraServerHelper.startEmbeddedCassandra(STARTUP_TIMEOUT);
}
 
Example 12
Source File: CasquatchTestDaoBuilder.java    From casquatch with Apache License 2.0 5 votes vote down vote up
/**
 * Create DAO with embedded cassandra
 * @return builder using embedded cassandra
 */
public CasquatchTestDaoBuilder withEmbedded() {
    try {

        EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.CASSANDRA_RNDPORT_YML_FILE, EmbeddedCassandraServerHelper.DEFAULT_STARTUP_TIMEOUT);
        log.info("Embedded Cassandra Started");
        return (CasquatchTestDaoBuilder) this.withBasicContactPoints(EmbeddedCassandraServerHelper.getHost()+":"+EmbeddedCassandraServerHelper.getNativeTransportPort())
                        .withBasicLoadBalancingPolicyLocalDatacenter("datacenter1")
                        .withBasicRequestConsistency("LOCAL_ONE");
    }
    catch(Exception e) {
        throw new DriverException(e);
    }
}
 
Example 13
Source File: KeyspaceRepositoryIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
@BeforeClass
public static void init() throws ConfigurationException, TTransportException, IOException, InterruptedException {
    // Start an embedded Cassandra Server
    EmbeddedCassandraServerHelper.startEmbeddedCassandra(20000L);
}
 
Example 14
Source File: EmbeddedCassandraConnectorTestBase.java    From debezium-incubator with Apache License 2.0 4 votes vote down vote up
private static void startEmbeddedCassandra() throws Exception {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra(TEST_CASSANDRA_YAML_CONFIG, Duration.ofSeconds(STARTUP_TIMEOUT_IN_SECONDS).toMillis());
}
 
Example 15
Source File: CassandraJUnitRule.java    From cassandra-migration with MIT License 4 votes vote down vote up
@Override
protected void before() throws Exception {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra(ymlFileLocation, TIMEOUT);
    load();
}
 
Example 16
Source File: CassandraMigrationAutoConfigurationTest.java    From cassandra-migration with MIT License 4 votes vote down vote up
private void init() throws Exception {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra(YML_FILE_LOCATION, 30 * 1000L);
    loadTestData();
}
 
Example 17
Source File: CassandraLockProviderIntegrationTest.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
@BeforeAll
public static void startCassandra() throws IOException, InterruptedException {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    cqlSession = EmbeddedCassandraServerHelper.getSession();
    new CQLDataLoader(cqlSession).load(new ClassPathCQLDataSet("shedlock.cql", "shedlock"));
}
 
Example 18
Source File: DatastaxHelper.java    From SimpleFlatMapper with MIT License 4 votes vote down vote up
public static void startCassandra() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    try {
        Thread.currentThread().setContextClassLoader(DatastaxHelper.class.getClassLoader());
        // deal with multiple classloader
        synchronized (CASSANDRA_STARTED) {
            if (System.getProperty(CASSANDRA_STARTED) == null) {
                fixTypeCodec();
                // cassandra does some check on the java version
                // expect a dot not present in java 9 ea 126
                String vmversion = System.getProperty("java.vm.version");
                if (vmversion.startsWith("9-ea")) {
                    System.out.println("override java version prop");
                    System.setProperty("java.vm.version", "25.51-b03");
                }

                System.setProperty("disk_failure_policy", "ignore");

                File configFile = new File("target/embeddedCassandra/cu-cassandra.yaml");

                configFile.getParentFile().mkdirs();

                InputStream is = EmbeddedCassandraServerHelper.class.getResourceAsStream("/cu-cassandra.yaml");
                try {
                    OutputStream os = new FileOutputStream(configFile);

                    byte[] buffer = new byte[4096];
                    try {
                        int l;
                        while ((l = is.read(buffer)) != -1) {
                            os.write(buffer, 0, l);
                        }
                    } finally {
                        os.close();
                    }


                } finally {
                    is.close();
                }

                String cassandraConfig = "file:" + configFile.getAbsolutePath();


                System.setProperty("cassandra.config", cassandraConfig);
                System.setProperty("cassandra.native.epoll.enabled", "false");
                System.setProperty("cassandra.disk_failure_policy", "ignore");

                System.out.println("Starting Cassandra " + cassandraConfig);
                EmbeddedCassandraServerHelper.startEmbeddedCassandra(300000L);
                System.out.println("Started Cassandra");

                System.setProperty(CASSANDRA_STARTED, "true");
            } else {
                System.out.println("CASSANDRA_STARTED = " + System.getProperty(CASSANDRA_STARTED));
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(classLoader);
    }
}
 
Example 19
Source File: BookRepositoryIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
@BeforeClass
public static void init() throws ConfigurationException, TTransportException, IOException, InterruptedException {
    // Start an embedded Cassandra Server
    EmbeddedCassandraServerHelper.startEmbeddedCassandra(20000L);
}
 
Example 20
Source File: Cassandra4Test.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Before
public void before(final MockTracer tracer) throws Exception {
  tracer.reset();
  EmbeddedCassandraServerHelper.startEmbeddedCassandra();
}