|
1 | 1 |
|
2 | 2 | package com.baeldung.hibernate.oneToMany.main; |
3 | 3 |
|
4 | | -import org.junit.After; |
5 | | -import org.junit.AfterClass; |
| 4 | + |
| 5 | +import com.baeldung.hibernate.oneToMany.model.Cart; |
| 6 | +import com.baeldung.hibernate.oneToMany.model.Items; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Set; |
| 9 | +import static org.hamcrest.Matchers.hasSize; |
| 10 | +import org.hibernate.Session; |
| 11 | +import org.hibernate.SessionFactory; |
| 12 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 13 | +import org.hibernate.cfg.Configuration; |
| 14 | +import org.hibernate.dialect.HSQLDialect; |
| 15 | +import org.hibernate.service.ServiceRegistry; |
| 16 | +import org.junit.Assert; |
6 | 17 | import org.junit.Before; |
7 | 18 | import org.junit.BeforeClass; |
8 | 19 | import org.junit.Test; |
|
11 | 22 |
|
12 | 23 | public class HibernateOneToManyAnnotationMainTest { |
13 | 24 |
|
| 25 | + private static SessionFactory sessionFactory; |
| 26 | + |
| 27 | + private Session session; |
| 28 | + |
14 | 29 | public HibernateOneToManyAnnotationMainTest() { |
15 | 30 | } |
16 | 31 |
|
17 | | - @BeforeClass |
18 | | - public static void setUpClass() { |
19 | | - } |
20 | 32 |
|
21 | | - @AfterClass |
22 | | - public static void tearDownClass() { |
| 33 | + @BeforeClass |
| 34 | + public static void beforeTests() { |
| 35 | + Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class).setProperty("hibernate.dialect", HSQLDialect.class.getName()).setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName()) |
| 36 | + .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update"); |
| 37 | + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); |
| 38 | + sessionFactory = configuration.buildSessionFactory(serviceRegistry); |
23 | 39 | } |
24 | 40 |
|
25 | 41 | @Before |
26 | 42 | public void setUp() { |
| 43 | + session = sessionFactory.openSession(); |
| 44 | + session.beginTransaction(); |
| 45 | + |
27 | 46 | } |
28 | 47 |
|
29 | | - @After |
30 | | - public void tearDown() { |
| 48 | + |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testAddItemsToCart() { |
| 52 | + Cart cart = new Cart(); |
| 53 | + Set <Items> cartItems = new HashSet<>(); |
| 54 | + cartItems = cart.getItems(); |
| 55 | + Assert.assertNull(cartItems); |
| 56 | + Items item1 = new Items("I10", 10, 1, cart); |
| 57 | + assertNotNull(item1); |
| 58 | + Set<Items> itemsSet = new HashSet<Items>(); |
| 59 | + cart.setItems(itemsSet); |
| 60 | + assertNotNull(cart); |
| 61 | + System.out.println("Items added to cart"); |
| 62 | + |
31 | 63 | } |
| 64 | + |
32 | 65 | @Test |
33 | | - public void testMain() { |
34 | | - System.out.println("main"); |
35 | | - String[] args = null; |
36 | | - HibernateOneToManyAnnotationMain.main(args); |
37 | | - fail("The test failed."); |
| 66 | + public void testSaveCart(){ |
| 67 | + Cart cart = new Cart(); |
| 68 | + Set <Items> cartItems = new HashSet<>(); |
| 69 | + cartItems = cart.getItems(); |
| 70 | + Assert.assertNull(cartItems); |
| 71 | + Items item1 = new Items(); |
| 72 | + item1.setItemId("I10"); |
| 73 | + item1.setItemTotal(10); |
| 74 | + item1.setQuantity(1); |
| 75 | + item1.setCart(cart); |
| 76 | + assertNotNull(item1); |
| 77 | + Set<Items> itemsSet = new HashSet<Items>(); |
| 78 | + itemsSet.add(item1); |
| 79 | + assertNotNull(itemsSet); |
| 80 | + cart.setItems(itemsSet); |
| 81 | + assertNotNull(cart); |
| 82 | + session.persist(cart); |
| 83 | + session.getTransaction().commit(); |
| 84 | + session.close(); |
| 85 | + |
38 | 86 | } |
39 | 87 |
|
| 88 | + |
40 | 89 | } |
0 commit comments