-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHB_spring.txt
383 lines (256 loc) · 8.04 KB
/
HB_spring.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
0.
backend code representing the Model
Spring DAO
--------------------------------------
Maven, quick start as archetype, give proper group id and artifact id.
JDK
Add proper maven dependency
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.16.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
back end service component testing using unit testing
--------------------------------------
1.
package com.niit.ItemBackend.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages="com.niit.ItemBackend")
public class AppContext {
@Bean(name="dataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:~/niitdb);
dataSource.setUsername("sa");
dataSource.setPassword("password");
return dataSource;
}
public Properties getHibernateProperties() {
Properties properties=new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
return properties;
}
@Bean(name="sessionFactory")
@Autowired
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
LocalSessionFactoryBean sessionFactory=new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setHibernateProperties(getHibernateProperties());
sessionFactory.setPackagesToScan("com.niit.ItemBackend.model");
return sessionFactory;
}
@Bean(name="transactionManager")
@Autowired
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager=new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
}
------------------------------------
2.
package com.niit.ItemBackend.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.lang.NonNull;
@Entity
@Table(name="Item")
public class Item {
@Id
private int itemId;
private String description;
private float price;
private int quantity;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
--------------------------------
3.
package com.niit.ItemBackend.dao;
import com.niit.ItemBackend.model.*;
public interface ItemDAO {
public boolean addItem(Item item);
}
-----------------------------------
4.
package com.niit.ItemBackend.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.niit.ItemBackend.model.Item;
import com.niit.ItemBackend.model.*;;
@Repository("itemDAO")
@Transactional
public class ItemImpl implements ItemDAO {
@Autowired
private SessionFactory sessionFactory;
public boolean addItem(Item item) {
// TODO Auto-generated method stub
//return false;
sessionFactory.getCurrentSession().save(item);
return true;
}
}
----------------------------------------------
5.
package com.niit.ItemBackend.service;
import com.niit.ItemBackend.model.Item;
public interface ItemService {
public boolean addItem(Item item);
}
-------------------------------------------
6.
package com.niit.ItemBackend.service;
import java.util.List;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.niit.ItemBackend.model.Item;
import com.niit.ItemBackend.dao.*;;
@Service
@Repository("itemService")
@Transactional
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemDAO itemDAO;
public boolean addItem(Item item) {
// TODO Auto-generated method stub
// return false;
itemDAO.addItem(item);
return true;
}
}
---------------------------------------
7.
package com.niit.ItemBackend.test;
import com.niit.ItemBackend.dao.*;
import com.niit.ItemBackend.config.*;
import com.niit.ItemBackend.model.*;
import com.niit.ItemBackend.service.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringJUnitConfig(classes=AppContext.class)
public class ItemTest {
@Autowired
private ItemService itemService;
Item item;
@Before
public void setUp() throws Exception {
item=new Item();
item.setItemId(30);
item.setDescription("mngo");
item.setPrice((float)10.9);
item.setQuantity(10);
}
@After
public void tearDown() throws Exception {
item=null;
}
@Test //positive test
public void testUpdateItem()
{
// fail("Not yet implemented");
assertEquals(true, itemService.addItem(item));
}
}
----------------------------------