Skip to content

Commit b0f7e8d

Browse files
Devender Yadavkarthikprasad13
authored andcommitted
#697 added test case
1 parent 1990d0c commit b0f7e8d

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*******************************************************************************
2+
* * Copyright 2015 Impetus Infotech.
3+
* *
4+
* * Licensed under the Apache License, Version 2.0 (the "License");
5+
* * you may not use this file except in compliance with the License.
6+
* * You may obtain a copy of the License at
7+
* *
8+
* * http://www.apache.org/licenses/LICENSE-2.0
9+
* *
10+
* * Unless required by applicable law or agreed to in writing, software
11+
* * distributed under the License is distributed on an "AS IS" BASIS,
12+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* * See the License for the specific language governing permissions and
14+
* * limitations under the License.
15+
******************************************************************************/
16+
package com.impetus.client.mongodb.index;
17+
18+
import javax.persistence.Column;
19+
import javax.persistence.Entity;
20+
import javax.persistence.Id;
21+
22+
import com.impetus.kundera.index.Index;
23+
import com.impetus.kundera.index.IndexCollection;
24+
25+
/**
26+
* The Class Book.
27+
*
28+
* @author devender.yadav
29+
*/
30+
@Entity
31+
@IndexCollection(columns = { @Index(name = "category", type = "unique") })
32+
public class Book
33+
{
34+
35+
/** The id. */
36+
@Id
37+
private String id;
38+
39+
/** The title. */
40+
@Column
41+
private String title;
42+
43+
/** The category. */
44+
@Column
45+
private String category;
46+
47+
/** The num pages. */
48+
@Column
49+
private int numPages;
50+
51+
/**
52+
* Instantiates a new book.
53+
*/
54+
public Book()
55+
{
56+
57+
}
58+
59+
/**
60+
* Instantiates a new book.
61+
*
62+
* @param id
63+
* the id
64+
* @param title
65+
* the title
66+
* @param category
67+
* the category
68+
* @param numPages
69+
* the num pages
70+
*/
71+
public Book(String id, String title, String category, int numPages)
72+
{
73+
this.id = id;
74+
this.title = title;
75+
this.category = category;
76+
this.numPages = numPages;
77+
}
78+
79+
/**
80+
* Gets the id.
81+
*
82+
* @return the id
83+
*/
84+
public String getId()
85+
{
86+
return id;
87+
}
88+
89+
/**
90+
* Sets the id.
91+
*
92+
* @param id
93+
* the new id
94+
*/
95+
public void setId(String id)
96+
{
97+
this.id = id;
98+
}
99+
100+
/**
101+
* Gets the title.
102+
*
103+
* @return the title
104+
*/
105+
public String getTitle()
106+
{
107+
return title;
108+
}
109+
110+
/**
111+
* Sets the title.
112+
*
113+
* @param title
114+
* the new title
115+
*/
116+
public void setTitle(String title)
117+
{
118+
this.title = title;
119+
}
120+
121+
/**
122+
* Gets the category.
123+
*
124+
* @return the category
125+
*/
126+
public String getCategory()
127+
{
128+
return category;
129+
}
130+
131+
/**
132+
* Sets the category.
133+
*
134+
* @param category
135+
* the new category
136+
*/
137+
public void setCategory(String category)
138+
{
139+
this.category = category;
140+
}
141+
142+
/**
143+
* Gets the num pages.
144+
*
145+
* @return the num pages
146+
*/
147+
public int getNumPages()
148+
{
149+
return numPages;
150+
}
151+
152+
/**
153+
* Sets the num pages.
154+
*
155+
* @param number
156+
* the new num pages
157+
*/
158+
public void setNumPages(int number)
159+
{
160+
this.numPages = number;
161+
}
162+
163+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.impetus.client.mongodb.index;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.EntityManagerFactory;
5+
import javax.persistence.Persistence;
6+
7+
import junit.framework.Assert;
8+
9+
import org.junit.After;
10+
import org.junit.AfterClass;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import com.impetus.client.utils.MongoUtils;
16+
17+
/**
18+
* The Class UniqueIndexTest.
19+
*
20+
* @author devender.yadav
21+
*/
22+
public class UniqueIndexTest
23+
{
24+
25+
/** The Constant _PU. */
26+
private static final String PU = "mongo_pu";
27+
28+
/** The emf. */
29+
private static EntityManagerFactory emf;
30+
31+
/** The em. */
32+
private EntityManager em;
33+
34+
/**
35+
* Sets the up before class.
36+
*
37+
* @throws Exception
38+
* the exception
39+
*/
40+
@BeforeClass
41+
public static void SetUpBeforeClass() throws Exception
42+
{
43+
emf = Persistence.createEntityManagerFactory(PU);
44+
}
45+
46+
/**
47+
* Sets the up.
48+
*
49+
* @throws Exception
50+
* the exception
51+
*/
52+
@Before
53+
public void setUp() throws Exception
54+
{
55+
em = emf.createEntityManager();
56+
}
57+
58+
/**
59+
* Test CRUD GridFS.
60+
*
61+
* @throws Exception
62+
* the exceptiono
63+
*/
64+
@Test
65+
public void testInsert() throws Exception
66+
{
67+
Book book1 = new Book("1", "title1", "cat1", 100);
68+
Book book2 = new Book("2", "title2", "cat2", 200);
69+
Book book3 = new Book("3", "title3", "cat2", 200);
70+
71+
try
72+
{
73+
em.clear();
74+
em.persist(book1);
75+
76+
Book book = em.find(Book.class, "1");
77+
Assert.assertNotNull(book);
78+
Assert.assertEquals("title1", book.getTitle());
79+
Assert.assertEquals("cat1", book.getCategory());
80+
Assert.assertEquals(100, book.getNumPages());
81+
82+
em.clear();
83+
em.persist(book2);
84+
85+
book = em.find(Book.class, "2");
86+
Assert.assertNotNull(book);
87+
Assert.assertEquals("title2", book.getTitle());
88+
Assert.assertEquals("cat2", book.getCategory());
89+
Assert.assertEquals(200, book.getNumPages());
90+
91+
em.clear();
92+
em.persist(book3);
93+
Assert.fail();
94+
}
95+
catch (Exception ex)
96+
{
97+
Book b = em.find(Book.class, "3");
98+
Assert.assertNull(b);
99+
}
100+
}
101+
102+
/**
103+
* Tear down.
104+
*
105+
* @throws Exception
106+
* the exception
107+
*/
108+
@After
109+
public void tearDown() throws Exception
110+
{
111+
em.close();
112+
}
113+
114+
/**
115+
* Tear down after class.
116+
*
117+
* @throws Exception
118+
* the exception
119+
*/
120+
@AfterClass
121+
public static void tearDownAfterClass() throws Exception
122+
{
123+
MongoUtils.dropDatabase(emf, PU);
124+
emf.close();
125+
}
126+
127+
}

0 commit comments

Comments
 (0)