Source Code Cross Referenced for TestDatabaseImageStore.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » cmf » dam » contentstores » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.cmf.dam.contentstores 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003:         * Distributed under the terms of either:
004:         * - the common development and distribution license (CDDL), v1.0; or
005:         * - the GNU Lesser General Public License, v2.1 or later
006:         * $Id: TestDatabaseImageStore.java 3634 2007-01-08 21:42:24Z gbevin $
007:         */
008:        package com.uwyn.rife.cmf.dam.contentstores;
009:
010:        import com.uwyn.rife.cmf.dam.contentstores.exceptions.*;
011:        import java.awt.*;
012:
013:        import com.uwyn.rife.cmf.Content;
014:        import com.uwyn.rife.cmf.ContentInfo;
015:        import com.uwyn.rife.cmf.ContentRepository;
016:        import com.uwyn.rife.cmf.MimeType;
017:        import com.uwyn.rife.cmf.dam.ContentDataUser;
018:        import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContent;
019:        import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentFactory;
020:        import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentInfo;
021:        import com.uwyn.rife.config.RifeConfig;
022:        import com.uwyn.rife.database.Datasource;
023:        import com.uwyn.rife.database.DbPreparedStatement;
024:        import com.uwyn.rife.database.DbPreparedStatementHandler;
025:        import com.uwyn.rife.database.queries.Insert;
026:        import com.uwyn.rife.database.queries.Select;
027:        import com.uwyn.rife.resources.ResourceFinderClasspath;
028:        import com.uwyn.rife.tools.FileUtils;
029:        import com.uwyn.rife.tools.ImageWaiter;
030:        import com.uwyn.rife.tools.InnerClassException;
031:        import java.awt.image.BufferedImage;
032:        import java.io.ByteArrayOutputStream;
033:        import java.io.IOException;
034:        import java.net.URL;
035:        import java.util.Arrays;
036:        import java.util.Iterator;
037:        import javax.imageio.ImageIO;
038:        import javax.imageio.ImageWriter;
039:        import javax.imageio.stream.ImageOutputStream;
040:        import javax.swing.ImageIcon;
041:        import junit.framework.TestCase;
042:
043:        public class TestDatabaseImageStore extends TestCase {
044:            private Datasource mDatasource = null;
045:
046:            private static byte[] sLargeImagePng = null;
047:            static {
048:                int edge = 1200;
049:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
050:                        .getResource("uwyn.gif");
051:                ImageIcon icon = new ImageIcon(image_resource_gif);
052:                Image image_scaled = icon.getImage().getScaledInstance(edge,
053:                        edge, Image.SCALE_FAST);
054:                ImageWaiter.wait(image_scaled);
055:                BufferedImage buffer = new BufferedImage(image_scaled
056:                        .getWidth(null), image_scaled.getHeight(null),
057:                        BufferedImage.TYPE_INT_ARGB);
058:                Graphics2D g2 = buffer.createGraphics();
059:                g2.drawImage(image_scaled, 0, 0, null);
060:                g2.setComposite(AlphaComposite.getInstance(
061:                        AlphaComposite.SRC_OVER, 0.5f));
062:                GradientPaint gradient1 = new GradientPaint(0, 0, Color.red,
063:                        175, 175, Color.yellow, true);
064:                g2.setPaint(gradient1);
065:                g2.fill(new Rectangle(0, 0, edge, edge));
066:                GradientPaint gradient2 = new GradientPaint(100, 50,
067:                        Color.green, 205, 375, Color.blue, true);
068:                g2.setPaint(gradient2);
069:                g2.fill(new Rectangle(0, 0, edge, edge));
070:                g2.dispose();
071:
072:                try {
073:                    Iterator<ImageWriter> writers = ImageIO
074:                            .getImageWritersByMIMEType("image/png");
075:                    ImageWriter writer = writers.next();
076:                    ByteArrayOutputStream byte_out = new ByteArrayOutputStream();
077:                    ImageOutputStream image_out = ImageIO
078:                            .createImageOutputStream(byte_out);
079:                    writer.setOutput(image_out);
080:                    writer.write(buffer);
081:                    writer.dispose();
082:                    byte_out.flush();
083:                    byte_out.close();
084:
085:                    sLargeImagePng = byte_out.toByteArray();
086:                } catch (IOException e) {
087:                    e.printStackTrace();
088:                }
089:            }
090:
091:            public TestDatabaseImageStore(Datasource datasource,
092:                    String datasourceName, String name) {
093:                super (name);
094:
095:                mDatasource = datasource;
096:            }
097:
098:            public void setUp() throws Exception {
099:                DatabaseContentFactory.getInstance(mDatasource).install();
100:            }
101:
102:            public void tearDown() throws Exception {
103:                try {
104:                    DatabaseContentFactory.getInstance(mDatasource).remove();
105:                } catch (Throwable e) {
106:                    // discart errors
107:                }
108:            }
109:
110:            public void testInstallError() throws Exception {
111:                DatabaseImageStore store = DatabaseImageStoreFactory
112:                        .getInstance(mDatasource);
113:                try {
114:                    store.install();
115:                    fail();
116:                } catch (InstallContentStoreErrorException e) {
117:                    assertNotNull(e.getCause());
118:                }
119:            }
120:
121:            public void testRemoveError() throws Exception {
122:                DatabaseContentFactory.getInstance(mDatasource).remove();
123:                DatabaseImageStore store = DatabaseImageStoreFactory
124:                        .getInstance(mDatasource);
125:                try {
126:                    store.remove();
127:                    fail();
128:                } catch (RemoveContentStoreErrorException e) {
129:                    assertNotNull(e.getCause());
130:                }
131:            }
132:
133:            public void testGetSupportedMimeTypes() {
134:                DatabaseImageStore store = DatabaseImageStoreFactory
135:                        .getInstance(mDatasource);
136:                assertTrue(store.getSupportedMimeTypes().size() > 0);
137:            }
138:
139:            public void testGetContentType() {
140:                DatabaseImageStore store = DatabaseImageStoreFactory
141:                        .getInstance(mDatasource);
142:                ContentInfo info = new ContentInfo();
143:                info.setMimeType(MimeType.IMAGE_GIF.toString());
144:                assertNotNull(store.getContentType(info));
145:                info.setMimeType(MimeType.APPLICATION_XHTML.toString());
146:                assertNull(store.getContentType(info));
147:            }
148:
149:            public void testGetFormatter() {
150:                DatabaseImageStore store = DatabaseImageStoreFactory
151:                        .getInstance(mDatasource);
152:                assertNotNull(store.getFormatter(MimeType.IMAGE_GIF, false));
153:                assertNull(store.getFormatter(MimeType.APPLICATION_XHTML, true));
154:            }
155:
156:            public void testStoreContentData() throws Exception {
157:                final int[] id = new int[] { 1 };
158:                final DatabaseContent manager = DatabaseContentFactory
159:                        .getInstance(mDatasource);
160:                Insert insert = null;
161:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
162:                        .getAliasedDriver())) {
163:                    insert = new Insert(mDatasource).into(
164:                            RifeConfig.Cmf.getTableContentInfo())
165:                            .fieldsParametersExcluded(
166:                                    DatabaseContentInfo.class,
167:                                    new String[] { "contentId" })
168:                            .fieldParameter("version").fieldParameter(
169:                                    "repositoryId");
170:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
171:                        .equals(mDatasource.getAliasedDriver())) {
172:                    insert = new Insert(mDatasource).into(
173:                            RifeConfig.Cmf.getTableContentInfo())
174:                            .fieldsParametersExcluded(
175:                                    DatabaseContentInfo.class,
176:                                    new String[] { "path" }).fieldParameter(
177:                                    "\"path\"", "path").fieldParameter(
178:                                    "version").fieldParameter("repositoryId");
179:                } else {
180:                    insert = new Insert(mDatasource).into(
181:                            RifeConfig.Cmf.getTableContentInfo())
182:                            .fieldsParameters(DatabaseContentInfo.class)
183:                            .fieldParameter("version").fieldParameter(
184:                                    "repositoryId");
185:                }
186:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
187:                    public void setParameters(DbPreparedStatement statement) {
188:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
189:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
190:                                .equals(mDatasource.getAliasedDriver())) {
191:                            content_info.setContentId(id[0]);
192:                        }
193:                        content_info.setFragment(false);
194:                        content_info.setPath("/testpath");
195:                        content_info.setMimeType(MimeType.IMAGE_PNG.toString());
196:                        statement
197:                                .setInt("version", 1)
198:                                .setInt(
199:                                        "repositoryId",
200:                                        manager
201:                                                .executeGetFirstInt(new Select(
202:                                                        mDatasource)
203:                                                        .from(
204:                                                                RifeConfig.Cmf
205:                                                                        .getTableContentRepository())
206:                                                        .field("repositoryId")
207:                                                        .where(
208:                                                                "name",
209:                                                                "=",
210:                                                                ContentRepository.DEFAULT)))
211:                                .setBean(content_info);
212:                    }
213:                });
214:
215:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
216:                        .getResource("uwyn.gif");
217:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
218:
219:                DatabaseImageStore store = DatabaseImageStoreFactory
220:                        .getInstance(mDatasource);
221:                Content content = new Content(MimeType.IMAGE_PNG,
222:                        data_image_gif);
223:                assertTrue(store.storeContentData(id[0], content, null));
224:
225:                URL image_resource_png = ResourceFinderClasspath.getInstance()
226:                        .getResource("uwyn.png");
227:                final byte[] data_image_png = FileUtils
228:                        .readBytes(image_resource_png);
229:
230:                store.useContentData(id[0], new ContentDataUser() {
231:                    public Object useContentData(Object contentData)
232:                            throws InnerClassException {
233:                        assertTrue(Arrays.equals(data_image_png,
234:                                (byte[]) contentData));
235:                        return null;
236:                    }
237:                });
238:            }
239:
240:            public void testStoreContentDataLarge() throws Exception {
241:                final int[] id = new int[] { 1 };
242:                final DatabaseContent manager = DatabaseContentFactory
243:                        .getInstance(mDatasource);
244:                Insert insert = null;
245:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
246:                        .getAliasedDriver())) {
247:                    insert = new Insert(mDatasource).into(
248:                            RifeConfig.Cmf.getTableContentInfo())
249:                            .fieldsParametersExcluded(
250:                                    DatabaseContentInfo.class,
251:                                    new String[] { "contentId" })
252:                            .fieldParameter("version").fieldParameter(
253:                                    "repositoryId");
254:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
255:                        .equals(mDatasource.getAliasedDriver())) {
256:                    insert = new Insert(mDatasource).into(
257:                            RifeConfig.Cmf.getTableContentInfo())
258:                            .fieldsParametersExcluded(
259:                                    DatabaseContentInfo.class,
260:                                    new String[] { "path" }).fieldParameter(
261:                                    "\"path\"", "path").fieldParameter(
262:                                    "version").fieldParameter("repositoryId");
263:                } else {
264:                    insert = new Insert(mDatasource).into(
265:                            RifeConfig.Cmf.getTableContentInfo())
266:                            .fieldsParameters(DatabaseContentInfo.class)
267:                            .fieldParameter("version").fieldParameter(
268:                                    "repositoryId");
269:                }
270:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
271:                    public void setParameters(DbPreparedStatement statement) {
272:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
273:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
274:                                .equals(mDatasource.getAliasedDriver())) {
275:                            content_info.setContentId(id[0]);
276:                        }
277:                        content_info.setFragment(false);
278:                        content_info.setPath("/testpath");
279:                        content_info.setMimeType(MimeType.IMAGE_PNG.toString());
280:                        statement
281:                                .setInt("version", 1)
282:                                .setInt(
283:                                        "repositoryId",
284:                                        manager
285:                                                .executeGetFirstInt(new Select(
286:                                                        mDatasource)
287:                                                        .from(
288:                                                                RifeConfig.Cmf
289:                                                                        .getTableContentRepository())
290:                                                        .field("repositoryId")
291:                                                        .where(
292:                                                                "name",
293:                                                                "=",
294:                                                                ContentRepository.DEFAULT)))
295:                                .setBean(content_info);
296:                    }
297:                });
298:
299:                DatabaseImageStore store = DatabaseImageStoreFactory
300:                        .getInstance(mDatasource);
301:                Content content = new Content(MimeType.IMAGE_PNG,
302:                        sLargeImagePng);
303:                assertTrue(store.storeContentData(id[0], content, null));
304:
305:                store.useContentData(id[0], new ContentDataUser() {
306:                    public Object useContentData(Object contentData)
307:                            throws InnerClassException {
308:                        assertTrue(Arrays.equals(sLargeImagePng,
309:                                (byte[]) contentData));
310:                        return null;
311:                    }
312:                });
313:            }
314:
315:            public void testStoreContentDataContentEmpty() throws Exception {
316:                final int[] id = new int[] { 1 };
317:                final DatabaseContent manager = DatabaseContentFactory
318:                        .getInstance(mDatasource);
319:                Insert insert = null;
320:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
321:                        .getAliasedDriver())) {
322:                    insert = new Insert(mDatasource).into(
323:                            RifeConfig.Cmf.getTableContentInfo())
324:                            .fieldsParametersExcluded(
325:                                    DatabaseContentInfo.class,
326:                                    new String[] { "contentId" })
327:                            .fieldParameter("version").fieldParameter(
328:                                    "repositoryId");
329:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
330:                        .equals(mDatasource.getAliasedDriver())) {
331:                    insert = new Insert(mDatasource).into(
332:                            RifeConfig.Cmf.getTableContentInfo())
333:                            .fieldsParametersExcluded(
334:                                    DatabaseContentInfo.class,
335:                                    new String[] { "path" }).fieldParameter(
336:                                    "\"path\"", "path").fieldParameter(
337:                                    "version").fieldParameter("repositoryId");
338:                } else {
339:                    insert = new Insert(mDatasource).into(
340:                            RifeConfig.Cmf.getTableContentInfo())
341:                            .fieldsParameters(DatabaseContentInfo.class)
342:                            .fieldParameter("version").fieldParameter(
343:                                    "repositoryId");
344:                }
345:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
346:                    public void setParameters(DbPreparedStatement statement) {
347:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
348:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
349:                                .equals(mDatasource.getAliasedDriver())) {
350:                            content_info.setContentId(id[0]);
351:                        }
352:                        content_info.setFragment(false);
353:                        content_info.setPath("/testpath");
354:                        content_info.setMimeType(MimeType.IMAGE_PNG.toString());
355:                        statement
356:                                .setInt("version", 1)
357:                                .setInt(
358:                                        "repositoryId",
359:                                        manager
360:                                                .executeGetFirstInt(new Select(
361:                                                        mDatasource)
362:                                                        .from(
363:                                                                RifeConfig.Cmf
364:                                                                        .getTableContentRepository())
365:                                                        .field("repositoryId")
366:                                                        .where(
367:                                                                "name",
368:                                                                "=",
369:                                                                ContentRepository.DEFAULT)))
370:                                .setBean(content_info);
371:                    }
372:                });
373:
374:                DatabaseImageStore store = DatabaseImageStoreFactory
375:                        .getInstance(mDatasource);
376:
377:                Content content = new Content(MimeType.IMAGE_PNG, null);
378:                assertTrue(store.storeContentData(id[0], content, null));
379:                store.useContentData(id[0], new ContentDataUser() {
380:                    public Object useContentData(Object contentData)
381:                            throws InnerClassException {
382:                        assertNull(contentData);
383:                        return null;
384:                    }
385:                });
386:            }
387:
388:            public void testStoreContentDataContentNull() throws Exception {
389:                final int[] id = new int[] { 1 };
390:                final DatabaseContent manager = DatabaseContentFactory
391:                        .getInstance(mDatasource);
392:                Insert insert = null;
393:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
394:                        .getAliasedDriver())) {
395:                    insert = new Insert(mDatasource).into(
396:                            RifeConfig.Cmf.getTableContentInfo())
397:                            .fieldsParametersExcluded(
398:                                    DatabaseContentInfo.class,
399:                                    new String[] { "contentId" })
400:                            .fieldParameter("version").fieldParameter(
401:                                    "repositoryId");
402:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
403:                        .equals(mDatasource.getAliasedDriver())) {
404:                    insert = new Insert(mDatasource).into(
405:                            RifeConfig.Cmf.getTableContentInfo())
406:                            .fieldsParametersExcluded(
407:                                    DatabaseContentInfo.class,
408:                                    new String[] { "path" }).fieldParameter(
409:                                    "\"path\"", "path").fieldParameter(
410:                                    "version").fieldParameter("repositoryId");
411:                } else {
412:                    insert = new Insert(mDatasource).into(
413:                            RifeConfig.Cmf.getTableContentInfo())
414:                            .fieldsParameters(DatabaseContentInfo.class)
415:                            .fieldParameter("version").fieldParameter(
416:                                    "repositoryId");
417:                }
418:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
419:                    public void setParameters(DbPreparedStatement statement) {
420:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
421:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
422:                                .equals(mDatasource.getAliasedDriver())) {
423:                            content_info.setContentId(id[0]);
424:                        }
425:                        content_info.setFragment(false);
426:                        content_info.setPath("/testpath");
427:                        content_info.setMimeType(MimeType.IMAGE_PNG.toString());
428:                        statement
429:                                .setInt("version", 1)
430:                                .setInt(
431:                                        "repositoryId",
432:                                        manager
433:                                                .executeGetFirstInt(new Select(
434:                                                        mDatasource)
435:                                                        .from(
436:                                                                RifeConfig.Cmf
437:                                                                        .getTableContentRepository())
438:                                                        .field("repositoryId")
439:                                                        .where(
440:                                                                "name",
441:                                                                "=",
442:                                                                ContentRepository.DEFAULT)))
443:                                .setBean(content_info);
444:                    }
445:                });
446:
447:                DatabaseImageStore store = DatabaseImageStoreFactory
448:                        .getInstance(mDatasource);
449:
450:                assertTrue(store.storeContentData(id[0], null, null));
451:                store.useContentData(id[0], new ContentDataUser() {
452:                    public Object useContentData(Object contentData)
453:                            throws InnerClassException {
454:                        assertNull(contentData);
455:                        return null;
456:                    }
457:                });
458:            }
459:
460:            public void testStoreContentDataMimeTypeWithoutFormatter()
461:                    throws Exception {
462:                final int[] id = new int[] { 1 };
463:                final DatabaseContent manager = DatabaseContentFactory
464:                        .getInstance(mDatasource);
465:                Insert insert = null;
466:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
467:                        .getAliasedDriver())) {
468:                    insert = new Insert(mDatasource).into(
469:                            RifeConfig.Cmf.getTableContentInfo())
470:                            .fieldsParametersExcluded(
471:                                    DatabaseContentInfo.class,
472:                                    new String[] { "contentId" })
473:                            .fieldParameter("version").fieldParameter(
474:                                    "repositoryId");
475:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
476:                        .equals(mDatasource.getAliasedDriver())) {
477:                    insert = new Insert(mDatasource).into(
478:                            RifeConfig.Cmf.getTableContentInfo())
479:                            .fieldsParametersExcluded(
480:                                    DatabaseContentInfo.class,
481:                                    new String[] { "path" }).fieldParameter(
482:                                    "\"path\"", "path").fieldParameter(
483:                                    "version").fieldParameter("repositoryId");
484:                } else {
485:                    insert = new Insert(mDatasource).into(
486:                            RifeConfig.Cmf.getTableContentInfo())
487:                            .fieldsParameters(DatabaseContentInfo.class)
488:                            .fieldParameter("version").fieldParameter(
489:                                    "repositoryId");
490:                }
491:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
492:                    public void setParameters(DbPreparedStatement statement) {
493:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
494:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
495:                                .equals(mDatasource.getAliasedDriver())) {
496:                            content_info.setContentId(id[0]);
497:                        }
498:                        content_info.setFragment(false);
499:                        content_info.setPath("/testpath");
500:                        content_info.setMimeType(MimeType.APPLICATION_XHTML
501:                                .toString());
502:                        statement
503:                                .setInt("version", 1)
504:                                .setInt(
505:                                        "repositoryId",
506:                                        manager
507:                                                .executeGetFirstInt(new Select(
508:                                                        mDatasource)
509:                                                        .from(
510:                                                                RifeConfig.Cmf
511:                                                                        .getTableContentRepository())
512:                                                        .field("repositoryId")
513:                                                        .where(
514:                                                                "name",
515:                                                                "=",
516:                                                                ContentRepository.DEFAULT)))
517:                                .setBean(content_info);
518:                    }
519:                });
520:
521:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
522:                        .getResource("uwyn.gif");
523:                final byte[] data_image_gif = FileUtils
524:                        .readBytes(image_resource_gif);
525:
526:                DatabaseImageStore store = DatabaseImageStoreFactory
527:                        .getInstance(mDatasource);
528:                Content content = new Content(MimeType.APPLICATION_XHTML,
529:                        data_image_gif);
530:                assertTrue(store.storeContentData(id[0], content, null));
531:
532:                store.useContentData(id[0], new ContentDataUser() {
533:                    public Object useContentData(Object contentData)
534:                            throws InnerClassException {
535:                        assertTrue(Arrays.equals(data_image_gif,
536:                                (byte[]) contentData));
537:                        return null;
538:                    }
539:                });
540:            }
541:
542:            public void testStoreContentDataIllegalArgument() throws Exception {
543:                DatabaseImageStore store = DatabaseImageStoreFactory
544:                        .getInstance(mDatasource);
545:
546:                try {
547:                    store.storeContentData(-1, null, null);
548:                    fail();
549:                } catch (IllegalArgumentException e) {
550:                    assertTrue(true);
551:                }
552:
553:                try {
554:                    store.storeContentData(1, new Content(MimeType.IMAGE_GIF,
555:                            new Object()), null);
556:                    fail();
557:                } catch (IllegalArgumentException e) {
558:                    assertTrue(true);
559:                }
560:            }
561:
562:            public void testStoreContentDataError() throws Exception {
563:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
564:                        .getResource("uwyn.gif");
565:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
566:
567:                DatabaseImageStore store = DatabaseImageStoreFactory
568:                        .getInstance(mDatasource);
569:                Content content = new Content(MimeType.IMAGE_PNG,
570:                        data_image_gif);
571:                try {
572:                    store.storeContentData(2, content, null);
573:                    assertTrue("com.mysql.jdbc.Driver".equals(mDatasource
574:                            .getAliasedDriver()));
575:                } catch (StoreContentDataErrorException e) {
576:                    assertEquals(2, e.getId());
577:                }
578:            }
579:
580:            public void testUseContentData() throws Exception {
581:                DatabaseContent manager = DatabaseContentFactory
582:                        .getInstance(mDatasource);
583:
584:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
585:                        .getResource("uwyn.gif");
586:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
587:
588:                Content content = new Content(MimeType.IMAGE_PNG,
589:                        data_image_gif);
590:                assertTrue(manager.storeContent("/imagegif", content, null));
591:                DatabaseContentInfo content_info = manager
592:                        .getContentInfo("/imagegif");
593:
594:                DatabaseImageStore store = DatabaseImageStoreFactory
595:                        .getInstance(mDatasource);
596:
597:                URL image_resource_png = ResourceFinderClasspath.getInstance()
598:                        .getResource("uwyn.png");
599:                final byte[] data_image_png = FileUtils
600:                        .readBytes(image_resource_png);
601:
602:                store.useContentData(content_info.getContentId(),
603:                        new ContentDataUser() {
604:                            public Object useContentData(Object contentData)
605:                                    throws InnerClassException {
606:                                assertTrue(Arrays.equals(data_image_png,
607:                                        (byte[]) contentData));
608:                                return null;
609:                            }
610:                        });
611:            }
612:
613:            public void testUseContentDataUnknown() throws Exception {
614:                DatabaseImageStore store = DatabaseImageStoreFactory
615:                        .getInstance(mDatasource);
616:
617:                store.useContentData(232, new ContentDataUser() {
618:                    public Object useContentData(Object contentData)
619:                            throws InnerClassException {
620:                        assertNull(contentData);
621:                        return null;
622:                    }
623:                });
624:            }
625:
626:            public void testUseContentDataIllegalArgument() throws Exception {
627:                DatabaseImageStore store = DatabaseImageStoreFactory
628:                        .getInstance(mDatasource);
629:
630:                try {
631:                    store.useContentData(-1, new ContentDataUser() {
632:                        public Object useContentData(Object contentData)
633:                                throws InnerClassException {
634:                            fail();
635:                            return null;
636:                        }
637:                    });
638:                } catch (IllegalArgumentException e) {
639:                    assertTrue(true);
640:                }
641:
642:                try {
643:                    store.useContentData(23, null);
644:                    fail();
645:                } catch (IllegalArgumentException e) {
646:                    assertTrue(true);
647:                }
648:            }
649:
650:            public void testUseContentDataError() throws Exception {
651:                DatabaseContentFactory.getInstance(mDatasource).remove();
652:                DatabaseImageStore store = DatabaseImageStoreFactory
653:                        .getInstance(mDatasource);
654:                try {
655:                    store.useContentData(2, new ContentDataUser() {
656:                        public Object useContentData(Object contentData)
657:                                throws InnerClassException {
658:                            fail();
659:                            return null;
660:                        }
661:                    });
662:                } catch (UseContentDataErrorException e) {
663:                    assertEquals(2, e.getId());
664:                }
665:            }
666:
667:            public void testHasContentData() throws Exception {
668:                DatabaseContent manager = DatabaseContentFactory
669:                        .getInstance(mDatasource);
670:
671:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
672:                        .getResource("uwyn.gif");
673:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
674:
675:                Content content = new Content(MimeType.IMAGE_PNG,
676:                        data_image_gif);
677:                assertTrue(manager.storeContent("/imagegif", content, null));
678:                DatabaseContentInfo content_info = manager
679:                        .getContentInfo("/imagegif");
680:
681:                DatabaseImageStore store = DatabaseImageStoreFactory
682:                        .getInstance(mDatasource);
683:                assertTrue(store.hasContentData(content_info.getContentId()));
684:            }
685:
686:            public void testHasContentDataContentEmpty() throws Exception {
687:                DatabaseContent manager = DatabaseContentFactory
688:                        .getInstance(mDatasource);
689:
690:                Content content = new Content(MimeType.IMAGE_PNG, null);
691:                assertTrue(manager.storeContent("/imagegif", content, null));
692:                DatabaseContentInfo content_info = manager
693:                        .getContentInfo("/imagegif");
694:
695:                DatabaseImageStore store = DatabaseImageStoreFactory
696:                        .getInstance(mDatasource);
697:                assertFalse(store.hasContentData(content_info.getContentId()));
698:            }
699:
700:            public void testHasContentDataUnknown() throws Exception {
701:                DatabaseImageStore store = DatabaseImageStoreFactory
702:                        .getInstance(mDatasource);
703:                assertFalse(store.hasContentData(3));
704:            }
705:
706:            public void testHasContentDataIllegalArgument() throws Exception {
707:                DatabaseImageStore store = DatabaseImageStoreFactory
708:                        .getInstance(mDatasource);
709:                try {
710:                    store.hasContentData(-1);
711:                    fail();
712:                } catch (IllegalArgumentException e) {
713:                    assertTrue(true);
714:                }
715:            }
716:
717:            public void testHasContentDataError() throws Exception {
718:                DatabaseContentFactory.getInstance(mDatasource).remove();
719:                DatabaseImageStore store = DatabaseImageStoreFactory
720:                        .getInstance(mDatasource);
721:                try {
722:                    store.hasContentData(2);
723:                    fail();
724:                } catch (HasContentDataErrorException e) {
725:                    assertEquals(2, e.getId());
726:                }
727:            }
728:
729:            public void testDeleteContentData() throws Exception {
730:                DatabaseContent manager = DatabaseContentFactory
731:                        .getInstance(mDatasource);
732:
733:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
734:                        .getResource("uwyn.gif");
735:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
736:
737:                Content content = new Content(MimeType.IMAGE_PNG,
738:                        data_image_gif);
739:                manager.storeContent("/imagegif", content, null);
740:                DatabaseContentInfo content_info = manager
741:                        .getContentInfo("/imagegif");
742:
743:                DatabaseImageStore store = DatabaseImageStoreFactory
744:                        .getInstance(mDatasource);
745:                assertTrue(store.hasContentData(content_info.getContentId()));
746:                assertTrue(store.deleteContentData(content_info.getContentId()));
747:                assertFalse(store.hasContentData(content_info.getContentId()));
748:                assertFalse(store
749:                        .deleteContentData(content_info.getContentId()));
750:            }
751:
752:            public void testDeleteContentDataUnknown() throws Exception {
753:                DatabaseImageStore store = DatabaseImageStoreFactory
754:                        .getInstance(mDatasource);
755:                assertFalse(store.deleteContentData(3));
756:            }
757:
758:            public void testDeleteContentDataIllegalArgument() throws Exception {
759:                DatabaseImageStore store = DatabaseImageStoreFactory
760:                        .getInstance(mDatasource);
761:                try {
762:                    store.deleteContentData(-1);
763:                    fail();
764:                } catch (IllegalArgumentException e) {
765:                    assertTrue(true);
766:                }
767:            }
768:
769:            public void testDeleteContentDataError() throws Exception {
770:                DatabaseContentFactory.getInstance(mDatasource).remove();
771:                DatabaseImageStore store = DatabaseImageStoreFactory
772:                        .getInstance(mDatasource);
773:                try {
774:                    store.deleteContentData(2);
775:                    fail();
776:                } catch (DeleteContentDataErrorException e) {
777:                    assertEquals(2, e.getId());
778:                }
779:            }
780:
781:            public void testRetrieveSize() throws Exception {
782:                DatabaseContent manager = DatabaseContentFactory
783:                        .getInstance(mDatasource);
784:
785:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
786:                        .getResource("uwyn.gif");
787:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
788:
789:                Content content = new Content(MimeType.IMAGE_PNG,
790:                        data_image_gif);
791:                assertTrue(manager.storeContent("/imagegif", content, null));
792:                DatabaseContentInfo content_info = manager
793:                        .getContentInfo("/imagegif");
794:
795:                DatabaseImageStore store = DatabaseImageStoreFactory
796:                        .getInstance(mDatasource);
797:
798:                URL image_resource_png = ResourceFinderClasspath.getInstance()
799:                        .getResource("uwyn.png");
800:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
801:
802:                assertEquals(store.getSize(content_info.getContentId()),
803:                        data_image_png.length);
804:            }
805:
806:            public void testRetrieveSizeIllegalArgument() throws Exception {
807:                DatabaseImageStore store = DatabaseImageStoreFactory
808:                        .getInstance(mDatasource);
809:
810:                try {
811:                    store.getSize(-1);
812:                    fail();
813:                } catch (IllegalArgumentException e) {
814:                    assertTrue(true);
815:                }
816:            }
817:
818:            public void testRetrieveSizeError() throws Exception {
819:                DatabaseContentFactory.getInstance(mDatasource).remove();
820:                DatabaseImageStore store = DatabaseImageStoreFactory
821:                        .getInstance(mDatasource);
822:                try {
823:                    store.getSize(2);
824:                    fail();
825:                } catch (RetrieveSizeErrorException e) {
826:                    assertEquals(2, e.getId());
827:                }
828:            }
829:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.