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 package fr.paris.lutece.plugins.blog.business.portlet;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.plugin.PluginService;
38 import fr.paris.lutece.test.LuteceTestCase;
39 import fr.paris.lutece.util.sql.DAOUtil;
40 import java.sql.SQLException;
41 import java.util.Collection;
42 import java.util.Date;
43 import java.util.List;
44 import org.junit.Test;
45
46
47
48
49 public class BlogPublicationDAOTest extends LuteceTestCase
50 {
51
52 private static final String PLUGIN_NAME = "blog";
53 private static final int BLOG_ID = 10000;
54 private static final int PORTLET_ID = 20000;
55 private static final int STATUS_ENABLED = 1;
56 private static final int ORDER = 1;
57 private static final int TESTCOUNT = 14;
58
59 private static Plugin _plugin = PluginService.getPlugin( PLUGIN_NAME );
60 private static BlogPublicationDAO _dao = new BlogPublicationDAO( );
61 private static String _strDatabaseEngine;
62 private static int nTestCount = 0;
63
64
65
66
67
68
69 @Override
70 protected void setUp( ) throws Exception
71 {
72 super.setUp( );
73
74
75 if ( nTestCount == 0 )
76 {
77 setUpDatabase( );
78 }
79 nTestCount++;
80 }
81
82
83
84
85
86
87 @Override
88 protected void tearDown( ) throws Exception
89 {
90 super.tearDown( );
91
92
93 if ( nTestCount == TESTCOUNT )
94 {
95 tearDownDatabase( );
96 }
97 }
98
99
100
101
102 @Test
103 public void testInsertBlogsId( )
104 {
105 System.out.println( "insertBlogsId" );
106 insert( );
107 }
108
109
110
111
112 @Test
113 public void testStore( )
114 {
115 System.out.println( "store" );
116 BlogPublication blogPublication = getBlogPublication( );
117 _dao.store( blogPublication, _plugin );
118 }
119
120
121
122
123 @Test
124 public void testDeleteBlogsId( )
125 {
126 System.out.println( "deleteBlogsId" );
127 insert( );
128 int nDocId = BLOG_ID;
129 _dao.deleteBlogsId( nDocId, _plugin );
130 }
131
132
133
134
135 @Test
136 public void testDeleteBlogByIdPortlet( )
137 {
138 System.out.println( "deleteBlogByIdPortlet" );
139 insert( );
140 int nIdPortlet = PORTLET_ID;
141 _dao.deleteBlogByIdPortlet( nIdPortlet, _plugin );
142 }
143
144
145
146
147 @Test
148 public void testRemove( )
149 {
150 System.out.println( "remove" );
151 insert( );
152 int nDocId = BLOG_ID;
153 int nIdPortlet = PORTLET_ID;
154 _dao.remove( nDocId, nIdPortlet, _plugin );
155 }
156
157
158
159
160 @Test
161 public void testLoadBlogsId( )
162 {
163 System.out.println( "loadBlogsId" );
164 insert( );
165 int nDocId = BLOG_ID;
166 List<BlogPublication> result = _dao.loadBlogsId( nDocId, _plugin );
167 assertTrue( !result.isEmpty( ) );
168 }
169
170
171
172
173 @Test
174 public void testLoadBlogsByPortlet( )
175 {
176 System.out.println( "loadBlogsByPortlet" );
177 insert( );
178 int nIdPortlet = PORTLET_ID;
179 List<BlogPublication> result = _dao.loadBlogsByPortlet( nIdPortlet, _plugin );
180 assertTrue( !result.isEmpty( ) );
181 }
182
183
184
185
186 @Test
187 public void testLoadBlogsByPortletAndPublicationDate( )
188 {
189 System.out.println( "loadBlogsByPortletAndPublicationDate" );
190 insert( );
191 int nIdPortlet = PORTLET_ID;
192 Date datePublishing = new Date( getTime( -2 ) );
193 Date dateEndPublishing = new Date( getTime( 2 ) );
194 List<BlogPublication> result = _dao.loadBlogsByPortletAndPublicationDate( nIdPortlet, datePublishing, dateEndPublishing, _plugin );
195 assertTrue( !result.isEmpty( ) );
196 }
197
198
199
200
201 @Test
202 public void testLoadBlogsPublication( )
203 {
204 System.out.println( "loadBlogsPublication" );
205 insert( );
206 int nPortletId = PORTLET_ID;
207 int nDocId = BLOG_ID;
208 BlogPublication result = _dao.loadBlogsPublication( nPortletId, nDocId, _plugin );
209 assertNotNull( result );
210 }
211
212
213
214
215 @Test
216 public void testLoadAllBlogsPublication( )
217 {
218 System.out.println( "loadAllBlogsPublication" );
219 insert( );
220 List<BlogPublication> result = _dao.loadAllBlogsPublication( _plugin );
221 assertTrue( !result.isEmpty( ) );
222 }
223
224
225
226
227 @Test
228 public void testSelectSinceDatePublishingAndStatus( )
229 {
230 System.out.println( "selectSinceDatePublishingAndStatus" );
231 insert( );
232 Date datePublishing = new Date( getTime( -4 ) );
233 Date dateEndPublishing = new Date( getTime( 2 ) );
234 int nStatus = STATUS_ENABLED;
235 Collection<BlogPublication> result = _dao.selectSinceDatePublishingAndStatus( datePublishing, dateEndPublishing, nStatus, _plugin );
236 assertTrue( !result.isEmpty( ) );
237 }
238
239
240
241
242 @Test
243 public void testGetPublishedBlogsIdsListByPortletIds( )
244 {
245 System.out.println( "getPublishedBlogsIdsListByPortletIds" );
246 insert( );
247 int [ ] nPortletsIds = {
248 PORTLET_ID
249 };
250 Date datePublishing = new Date( getTime( -2 ) );
251 Date dateEndPublishing = new Date( getTime( 2 ) );
252 List<Integer> result = _dao.getPublishedBlogsIdsListByPortletIds( nPortletsIds, datePublishing, dateEndPublishing, _plugin );
253 assertTrue( !result.isEmpty( ) );
254 }
255
256
257
258
259 @Test
260 public void testGetLastPublishedBlogsIdsListByPortletIds( )
261 {
262 System.out.println( "getLastPublishedBlogsIdsListByPortletIds" );
263 insert( );
264 int [ ] nPortletsIds = {
265 PORTLET_ID
266 };
267 Date dateUpdated = new Date( getTime( 0 ) );
268 List<Integer> result = _dao.getLastPublishedBlogsIdsListByPortletIds( nPortletsIds, dateUpdated, _plugin );
269
270 }
271
272
273
274
275 @Test
276 public void testCountPublicationByIdBlogAndDate( )
277 {
278 System.out.println( "countPublicationByIdBlogAndDate" );
279 insert( );
280 int nIdBlog = BLOG_ID;
281 Date date = new Date( );
282 int result = _dao.countPublicationByIdBlogAndDate( nIdBlog, date, _plugin );
283 assertTrue( result > 0 );
284 }
285
286
287
288
289
290
291 private void setUpDatabase( )
292 {
293 try
294 {
295 _plugin = PluginService.getPlugin( PLUGIN_NAME );
296 _strDatabaseEngine = _plugin.getConnectionService( ).getConnection( ).getMetaData( ).getDatabaseProductName( );
297
298 System.out.println( "Database server : " + _strDatabaseEngine );
299
300 String strMessage = "Disable referential integrity for the server : " + _strDatabaseEngine;
301 if ( _strDatabaseEngine.equalsIgnoreCase( "MySQL" ) )
302 {
303 execute( "SET FOREIGN_KEY_CHECKS=0;", strMessage );
304 }
305 else
306 if ( _strDatabaseEngine.contains( "HSQL" ) )
307 {
308 execute( "SET DATABASE REFERENTIAL INTEGRITY FALSE", strMessage );
309 }
310 }
311 catch( SQLException ex )
312 {
313 System.out.println( ex );
314 }
315 }
316
317
318
319
320 private void tearDownDatabase( ) throws Exception
321 {
322 clean( );
323
324 String strMessage = "Enable referential integrity for the server : " + _strDatabaseEngine;
325 if ( _strDatabaseEngine.equalsIgnoreCase( "MySQL" ) )
326 {
327 execute( "SET FOREIGN_KEY_CHECKS=1;", strMessage );
328 }
329 else
330 if ( _strDatabaseEngine.contains( "HSQL" ) )
331 {
332 execute( "SET DATABASE REFERENTIAL INTEGRITY TRUE", strMessage );
333 }
334 }
335
336
337
338
339 private void clean( )
340 {
341 _dao.deleteBlogsId( BLOG_ID, _plugin );
342 }
343
344
345
346
347 private void insert( )
348 {
349 clean( );
350 BlogPublication blogPublication = getBlogPublication( );
351 _dao.insertBlogsId( blogPublication, _plugin );
352
353 }
354
355
356
357
358
359
360 private BlogPublication getBlogPublication( )
361 {
362 BlogPublication blogPublication = new BlogPublication( );
363 blogPublication.setIdBlog( BLOG_ID );
364 blogPublication.setIdPortlet( PORTLET_ID );
365 blogPublication.setStatus( STATUS_ENABLED );
366 blogPublication.setBlogOrder( ORDER );
367 blogPublication.setDateBeginPublishing( new java.sql.Date( getTime( -3 ) ) );
368 blogPublication.setDateEndPublishing( new java.sql.Date( getTime( 3 ) ) );
369
370 return blogPublication;
371 }
372
373
374
375
376
377
378
379
380 private long getTime( long lDeltaHour )
381 {
382 return new Date( ).getTime( ) + ( lDeltaHour * 36000000L );
383 }
384
385
386
387
388
389
390
391
392
393 private void execute( String strStatement, String strMessage )
394 {
395 try ( DAOUtil dao = new DAOUtil( strStatement ) )
396 {
397 dao.executeUpdate( );
398 System.out.println( strMessage );
399 }
400
401 }
402 }