From e72245067764ef63db4dd02dc3e35ba35a5416e6 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Mon, 19 Jun 2017 15:46:56 -0400 Subject: [PATCH] Initial import. --- .gitignore | 23 +++ pom.xml | 43 +++++ .../com/impetus/kundera/entities/Person.java | 142 ++++++++++++++++ src/main/resources/META-INF/persistence.xml | 20 +++ .../java/com/impetus/kundera/CRUDTest.java | 159 ++++++++++++++++++ 5 files changed, 387 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/impetus/kundera/entities/Person.java create mode 100644 src/main/resources/META-INF/persistence.xml create mode 100644 src/test/java/com/impetus/kundera/CRUDTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2676acb --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +target +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..815ced3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + com.impetus.kundera + kundera-cassandra-example + jar + 1.0 + kundera-cassandra-example + http://maven.apache.org + + + 3.6 + + + + + com.impetus.kundera.client + kundera-cassandra + ${kundera.version} + + + junit + junit + 4.12 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + + diff --git a/src/main/java/com/impetus/kundera/entities/Person.java b/src/main/java/com/impetus/kundera/entities/Person.java new file mode 100644 index 0000000..cd02326 --- /dev/null +++ b/src/main/java/com/impetus/kundera/entities/Person.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * * Copyright 2016 Impetus Infotech. + * * + * * 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.impetus.kundera.entities; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * The Class Person. + */ +@Entity +@Table(name = "PERSON") +public class Person +{ + + /** The person id. */ + @Id + @Column(name = "PERSON_ID") + private String personId; + + /** The person first name. */ + @Column(name = "PERSON_FIRST_NAME") + private String personFirstName; + + /** The person last name. */ + @Column(name = "PERSON_LAST_NAME") + private String personLastName; + + /** The age. */ + @Column(name = "AGE") + private int age; + + /** + * Gets the person id. + * + * @return the person id + */ + public String getPersonId() + { + return personId; + } + + /** + * Sets the person id. + * + * @param personId + * the new person id + */ + public void setPersonId(String personId) + { + this.personId = personId; + } + + /** + * Gets the person name. + * + * @return the person name + */ + public String getPersonName() + { + return personFirstName + " " + personLastName; + } + + /** + * Gets the person first name. + * + * @return the person first name + */ + public String getPersonFirstName() + { + return personFirstName; + } + + /** + * Sets the person first name. + * + * @param personFirstName + * the new person first name + */ + public void setPersonFirstName(String personFirstName) + { + this.personFirstName = personFirstName; + } + + /** + * Gets the person last name. + * + * @return the person last name + */ + public String getPersonLastName() + { + return personLastName; + } + + /** + * Sets the person last name. + * + * @param personLastName + * the new person last name + */ + public void setPersonLastName(String personLastName) + { + this.personLastName = personLastName; + } + + /** + * Gets the age. + * + * @return the age + */ + public int getAge() + { + return age; + } + + /** + * Sets the age. + * + * @param age + * the new age + */ + public void setAge(int age) + { + this.age = age; + } + +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..2e2b061 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,20 @@ + + + + com.impetus.kundera.KunderaPersistence + + + + + + + + + + + diff --git a/src/test/java/com/impetus/kundera/CRUDTest.java b/src/test/java/com/impetus/kundera/CRUDTest.java new file mode 100644 index 0000000..5faa520 --- /dev/null +++ b/src/test/java/com/impetus/kundera/CRUDTest.java @@ -0,0 +1,159 @@ +/******************************************************************************* + * * Copyright 2016 Impetus Infotech. + * * + * * 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.impetus.kundera; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.impetus.kundera.entities.Person; + +/** + * The Class CRUDTest. + */ +public class CRUDTest +{ + + /** The Constant PU. */ + private static final String PU = "cassandra_pu"; + + /** The emf. */ + private static EntityManagerFactory emf; + + /** The em. */ + private EntityManager em; + + /** + * Sets the up before class. + * + * @throws Exception + * the exception + */ + @BeforeClass + public static void SetUpBeforeClass() throws Exception + { + emf = Persistence.createEntityManagerFactory(PU); + } + + /** + * Sets the up. + * + * @throws Exception + * the exception + */ + @Before + public void setUp() throws Exception + { + em = emf.createEntityManager(); + } + + /** + * Test crud operations. + * + * @throws Exception + * the exception + */ + @Test + public void testCRUDOperations() throws Exception + { + testInsert(); + testMerge(); + testRemove(); + } + + /** + * Test insert. + * + * @throws Exception + * the exception + */ + private void testInsert() throws Exception + { + Person p = new Person(); + p.setPersonId("101"); + p.setPersonFirstName("James"); + p.setPersonLastName("Bond"); + p.setAge(24); + em.persist(p); + + Person person = em.find(Person.class, "101"); + Assert.assertNotNull(person); + Assert.assertEquals("101", person.getPersonId()); + Assert.assertEquals("James Bond", person.getPersonName()); + + } + + /** + * Test merge. + */ + private void testMerge() + { + Person person = em.find(Person.class, "101"); + person.setPersonFirstName("Bill"); + person.setPersonLastName("Clinton"); + em.merge(person); + + Person p1 = em.find(Person.class, "101"); + Assert.assertEquals("Bill Clinton", p1.getPersonName()); + } + + /** + * Test remove. + */ + private void testRemove() + { + Person p = em.find(Person.class, "101"); + em.remove(p); + + Person p1 = em.find(Person.class, "101"); + Assert.assertNull(p1); + } + + /** + * Tear down. + * + * @throws Exception + * the exception + */ + @After + public void tearDown() throws Exception + { + em.close(); + } + + /** + * Tear down after class. + * + * @throws Exception + * the exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception + { + if (emf != null) + { + emf.close(); + emf = null; + } + } +}