Source Code Cross Referenced for TestDatabaseRawStore.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: TestDatabaseRawStore.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:
012:        import com.uwyn.rife.cmf.Content;
013:        import com.uwyn.rife.cmf.ContentInfo;
014:        import com.uwyn.rife.cmf.ContentRepository;
015:        import com.uwyn.rife.cmf.MimeType;
016:        import com.uwyn.rife.cmf.dam.ContentDataUser;
017:        import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContent;
018:        import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentFactory;
019:        import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentInfo;
020:        import com.uwyn.rife.config.RifeConfig;
021:        import com.uwyn.rife.database.Datasource;
022:        import com.uwyn.rife.database.DbPreparedStatement;
023:        import com.uwyn.rife.database.DbPreparedStatementHandler;
024:        import com.uwyn.rife.database.queries.Insert;
025:        import com.uwyn.rife.database.queries.Select;
026:        import com.uwyn.rife.tools.FileUtils;
027:        import com.uwyn.rife.tools.InnerClassException;
028:        import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
029:        import java.io.ByteArrayInputStream;
030:        import java.io.InputStream;
031:        import java.util.Arrays;
032:        import java.util.HashMap;
033:        import junit.framework.TestCase;
034:
035:        public class TestDatabaseRawStore extends TestCase {
036:            private Datasource mDatasource = null;
037:
038:            public TestDatabaseRawStore(Datasource datasource,
039:                    String datasourceName, String name) {
040:                super (name);
041:
042:                mDatasource = datasource;
043:            }
044:
045:            private byte[] getSmallRaw() {
046:                int size = 60 * 1024; // 60kb
047:                byte[] binary = new byte[size];
048:                for (int i = 0; i < size; i++) {
049:                    binary[i] = (byte) (i % 127);
050:                }
051:
052:                return binary;
053:            }
054:
055:            private byte[] getLargeRaw() {
056:                int size = 1024 * 1024 * 4; // 4Mb
057:                byte[] binary = new byte[size];
058:                for (int i = 0; i < size; i++) {
059:                    binary[i] = (byte) (i % 255);
060:                }
061:
062:                return binary;
063:            }
064:
065:            public void setUp() throws Exception {
066:                DatabaseContentFactory.getInstance(mDatasource).install();
067:            }
068:
069:            public void tearDown() throws Exception {
070:                try {
071:                    DatabaseContentFactory.getInstance(mDatasource).remove();
072:                } catch (Throwable e) {
073:                    // discart errors
074:                }
075:            }
076:
077:            public void testInstallError() throws Exception {
078:                DatabaseRawStore store = DatabaseRawStoreFactory
079:                        .getInstance(mDatasource);
080:                try {
081:                    store.install();
082:                    fail();
083:                } catch (InstallContentStoreErrorException e) {
084:                    assertNotNull(e.getCause());
085:                }
086:            }
087:
088:            public void testRemoveError() throws Exception {
089:                DatabaseContentFactory.getInstance(mDatasource).remove();
090:                DatabaseRawStore store = DatabaseRawStoreFactory
091:                        .getInstance(mDatasource);
092:                try {
093:                    store.remove();
094:                    fail();
095:                } catch (RemoveContentStoreErrorException e) {
096:                    assertNotNull(e.getCause());
097:                }
098:            }
099:
100:            public void testGetSupportedMimeTypes() {
101:                DatabaseRawStore store = DatabaseRawStoreFactory
102:                        .getInstance(mDatasource);
103:                assertTrue(store.getSupportedMimeTypes().size() > 0);
104:            }
105:
106:            public void testGetContentType() {
107:                DatabaseRawStore store = DatabaseRawStoreFactory
108:                        .getInstance(mDatasource);
109:                ContentInfo info = new ContentInfo();
110:                info.setMimeType(MimeType.RAW.toString());
111:                assertNull(store.getContentType(info));
112:                info.setAttributes(new HashMap<String, String>() {
113:                    {
114:                        put("content-type", "application/something");
115:                    }
116:                });
117:                assertNotNull(store.getContentType(info));
118:                info.setAttributes(null);
119:                assertNull(store.getContentType(info));
120:                info.setName("test.ogg");
121:                assertNotNull(store.getContentType(info));
122:                info.setName("test.unknown");
123:                assertNull(store.getContentType(info));
124:                info.setMimeType(MimeType.APPLICATION_XHTML.toString());
125:                assertNull(store.getContentType(info));
126:            }
127:
128:            public void testGetFormatter() {
129:                DatabaseRawStore store = DatabaseRawStoreFactory
130:                        .getInstance(mDatasource);
131:                assertNotNull(store.getFormatter(MimeType.RAW, false));
132:                assertNull(store.getFormatter(MimeType.APPLICATION_XHTML, true));
133:            }
134:
135:            public void testStoreContentDataStream() throws Exception {
136:                final int[] id = new int[] { 1 };
137:                final DatabaseContent manager = DatabaseContentFactory
138:                        .getInstance(mDatasource);
139:                Insert insert = null;
140:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
141:                        .getAliasedDriver())) {
142:                    insert = new Insert(mDatasource).into(
143:                            RifeConfig.Cmf.getTableContentInfo())
144:                            .fieldsParametersExcluded(
145:                                    DatabaseContentInfo.class,
146:                                    new String[] { "contentId" })
147:                            .fieldParameter("version").fieldParameter(
148:                                    "repositoryId");
149:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
150:                        .equals(mDatasource.getAliasedDriver())) {
151:                    insert = new Insert(mDatasource).into(
152:                            RifeConfig.Cmf.getTableContentInfo())
153:                            .fieldsParametersExcluded(
154:                                    DatabaseContentInfo.class,
155:                                    new String[] { "path" }).fieldParameter(
156:                                    "\"path\"", "path").fieldParameter(
157:                                    "version").fieldParameter("repositoryId");
158:                } else {
159:                    insert = new Insert(mDatasource).into(
160:                            RifeConfig.Cmf.getTableContentInfo())
161:                            .fieldsParameters(DatabaseContentInfo.class)
162:                            .fieldParameter("version").fieldParameter(
163:                                    "repositoryId");
164:                }
165:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
166:                    public void setParameters(DbPreparedStatement statement) {
167:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
168:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
169:                                .equals(mDatasource.getAliasedDriver())) {
170:                            content_info.setContentId(id[0]);
171:                        }
172:                        content_info.setFragment(false);
173:                        content_info.setPath("/testpath");
174:                        content_info.setMimeType(MimeType.RAW.toString());
175:                        statement
176:                                .setInt("version", 1)
177:                                .setInt(
178:                                        "repositoryId",
179:                                        manager
180:                                                .executeGetFirstInt(new Select(
181:                                                        mDatasource)
182:                                                        .from(
183:                                                                RifeConfig.Cmf
184:                                                                        .getTableContentRepository())
185:                                                        .field("repositoryId")
186:                                                        .where(
187:                                                                "name",
188:                                                                "=",
189:                                                                ContentRepository.DEFAULT)))
190:                                .setBean(content_info);
191:                    }
192:                });
193:
194:                final byte[] raw = getSmallRaw();
195:
196:                DatabaseRawStore store = DatabaseRawStoreFactory
197:                        .getInstance(mDatasource);
198:                Content content = new Content(MimeType.RAW,
199:                        new ByteArrayInputStream(raw));
200:                assertTrue(store.storeContentData(id[0], content, null));
201:
202:                store.useContentData(id[0], new ContentDataUser() {
203:                    public Object useContentData(Object contentData)
204:                            throws InnerClassException {
205:                        try {
206:                            byte[] received = FileUtils
207:                                    .readBytes((InputStream) contentData);
208:                            assertTrue(Arrays.equals(raw, received));
209:                        } catch (FileUtilsErrorException e) {
210:                            throwException(e);
211:                        }
212:                        return null;
213:                    }
214:                });
215:            }
216:
217:            public void testStoreContentDataBytes() throws Exception {
218:                final int[] id = new int[] { 1 };
219:                final DatabaseContent manager = DatabaseContentFactory
220:                        .getInstance(mDatasource);
221:                Insert insert = null;
222:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
223:                        .getAliasedDriver())) {
224:                    insert = new Insert(mDatasource).into(
225:                            RifeConfig.Cmf.getTableContentInfo())
226:                            .fieldsParametersExcluded(
227:                                    DatabaseContentInfo.class,
228:                                    new String[] { "contentId" })
229:                            .fieldParameter("version").fieldParameter(
230:                                    "repositoryId");
231:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
232:                        .equals(mDatasource.getAliasedDriver())) {
233:                    insert = new Insert(mDatasource).into(
234:                            RifeConfig.Cmf.getTableContentInfo())
235:                            .fieldsParametersExcluded(
236:                                    DatabaseContentInfo.class,
237:                                    new String[] { "path" }).fieldParameter(
238:                                    "\"path\"", "path").fieldParameter(
239:                                    "version").fieldParameter("repositoryId");
240:                } else {
241:                    insert = new Insert(mDatasource).into(
242:                            RifeConfig.Cmf.getTableContentInfo())
243:                            .fieldsParameters(DatabaseContentInfo.class)
244:                            .fieldParameter("version").fieldParameter(
245:                                    "repositoryId");
246:                }
247:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
248:                    public void setParameters(DbPreparedStatement statement) {
249:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
250:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
251:                                .equals(mDatasource.getAliasedDriver())) {
252:                            content_info.setContentId(id[0]);
253:                        }
254:                        content_info.setFragment(false);
255:                        content_info.setPath("/testpath");
256:                        content_info.setMimeType(MimeType.RAW.toString());
257:                        statement
258:                                .setInt("version", 1)
259:                                .setInt(
260:                                        "repositoryId",
261:                                        manager
262:                                                .executeGetFirstInt(new Select(
263:                                                        mDatasource)
264:                                                        .from(
265:                                                                RifeConfig.Cmf
266:                                                                        .getTableContentRepository())
267:                                                        .field("repositoryId")
268:                                                        .where(
269:                                                                "name",
270:                                                                "=",
271:                                                                ContentRepository.DEFAULT)))
272:                                .setBean(content_info);
273:                    }
274:                });
275:
276:                final byte[] raw = getSmallRaw();
277:
278:                DatabaseRawStore store = DatabaseRawStoreFactory
279:                        .getInstance(mDatasource);
280:                Content content = new Content(MimeType.RAW, raw);
281:                assertTrue(store.storeContentData(id[0], content, null));
282:
283:                store.useContentData(id[0], new ContentDataUser() {
284:                    public Object useContentData(Object contentData)
285:                            throws InnerClassException {
286:                        try {
287:                            byte[] received = FileUtils
288:                                    .readBytes((InputStream) contentData);
289:                            assertTrue(Arrays.equals(raw, received));
290:                        } catch (FileUtilsErrorException e) {
291:                            throwException(e);
292:                        }
293:                        return null;
294:                    }
295:                });
296:            }
297:
298:            public void testStoreContentDataLargeStream() throws Exception {
299:                final int[] id = new int[] { 1 };
300:                final DatabaseContent manager = DatabaseContentFactory
301:                        .getInstance(mDatasource);
302:                Insert insert = null;
303:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
304:                        .getAliasedDriver())) {
305:                    insert = new Insert(mDatasource).into(
306:                            RifeConfig.Cmf.getTableContentInfo())
307:                            .fieldsParametersExcluded(
308:                                    DatabaseContentInfo.class,
309:                                    new String[] { "contentId" })
310:                            .fieldParameter("version").fieldParameter(
311:                                    "repositoryId");
312:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
313:                        .equals(mDatasource.getAliasedDriver())) {
314:                    insert = new Insert(mDatasource).into(
315:                            RifeConfig.Cmf.getTableContentInfo())
316:                            .fieldsParametersExcluded(
317:                                    DatabaseContentInfo.class,
318:                                    new String[] { "path" }).fieldParameter(
319:                                    "\"path\"", "path").fieldParameter(
320:                                    "version").fieldParameter("repositoryId");
321:                } else {
322:                    insert = new Insert(mDatasource).into(
323:                            RifeConfig.Cmf.getTableContentInfo())
324:                            .fieldsParameters(DatabaseContentInfo.class)
325:                            .fieldParameter("version").fieldParameter(
326:                                    "repositoryId");
327:                }
328:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
329:                    public void setParameters(DbPreparedStatement statement) {
330:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
331:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
332:                                .equals(mDatasource.getAliasedDriver())) {
333:                            content_info.setContentId(id[0]);
334:                        }
335:                        content_info.setFragment(false);
336:                        content_info.setPath("/testpath");
337:                        content_info.setMimeType(MimeType.RAW.toString());
338:                        statement
339:                                .setInt("version", 1)
340:                                .setInt(
341:                                        "repositoryId",
342:                                        manager
343:                                                .executeGetFirstInt(new Select(
344:                                                        mDatasource)
345:                                                        .from(
346:                                                                RifeConfig.Cmf
347:                                                                        .getTableContentRepository())
348:                                                        .field("repositoryId")
349:                                                        .where(
350:                                                                "name",
351:                                                                "=",
352:                                                                ContentRepository.DEFAULT)))
353:                                .setBean(content_info);
354:                    }
355:                });
356:
357:                final byte[] raw = getLargeRaw();
358:
359:                DatabaseRawStore store = DatabaseRawStoreFactory
360:                        .getInstance(mDatasource);
361:                Content content = new Content(MimeType.RAW,
362:                        new ByteArrayInputStream(raw));
363:                assertTrue(store.storeContentData(id[0], content, null));
364:
365:                store.useContentData(id[0], new ContentDataUser() {
366:                    public Object useContentData(Object contentData)
367:                            throws InnerClassException {
368:                        try {
369:                            assertTrue(Arrays.equals(raw, FileUtils
370:                                    .readBytes((InputStream) contentData)));
371:                        } catch (FileUtilsErrorException e) {
372:                            throwException(e);
373:                        }
374:                        return null;
375:                    }
376:                });
377:            }
378:
379:            public void testStoreContentDataLargeBytes() throws Exception {
380:                final int[] id = new int[] { 1 };
381:                final DatabaseContent manager = DatabaseContentFactory
382:                        .getInstance(mDatasource);
383:                Insert insert = null;
384:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
385:                        .getAliasedDriver())) {
386:                    insert = new Insert(mDatasource).into(
387:                            RifeConfig.Cmf.getTableContentInfo())
388:                            .fieldsParametersExcluded(
389:                                    DatabaseContentInfo.class,
390:                                    new String[] { "contentId" })
391:                            .fieldParameter("version").fieldParameter(
392:                                    "repositoryId");
393:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
394:                        .equals(mDatasource.getAliasedDriver())) {
395:                    insert = new Insert(mDatasource).into(
396:                            RifeConfig.Cmf.getTableContentInfo())
397:                            .fieldsParametersExcluded(
398:                                    DatabaseContentInfo.class,
399:                                    new String[] { "path" }).fieldParameter(
400:                                    "\"path\"", "path").fieldParameter(
401:                                    "version").fieldParameter("repositoryId");
402:                } else {
403:                    insert = new Insert(mDatasource).into(
404:                            RifeConfig.Cmf.getTableContentInfo())
405:                            .fieldsParameters(DatabaseContentInfo.class)
406:                            .fieldParameter("version").fieldParameter(
407:                                    "repositoryId");
408:                }
409:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
410:                    public void setParameters(DbPreparedStatement statement) {
411:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
412:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
413:                                .equals(mDatasource.getAliasedDriver())) {
414:                            content_info.setContentId(id[0]);
415:                        }
416:                        content_info.setFragment(false);
417:                        content_info.setPath("/testpath");
418:                        content_info.setMimeType(MimeType.RAW.toString());
419:                        statement
420:                                .setInt("version", 1)
421:                                .setInt(
422:                                        "repositoryId",
423:                                        manager
424:                                                .executeGetFirstInt(new Select(
425:                                                        mDatasource)
426:                                                        .from(
427:                                                                RifeConfig.Cmf
428:                                                                        .getTableContentRepository())
429:                                                        .field("repositoryId")
430:                                                        .where(
431:                                                                "name",
432:                                                                "=",
433:                                                                ContentRepository.DEFAULT)))
434:                                .setBean(content_info);
435:                    }
436:                });
437:
438:                final byte[] raw = getLargeRaw();
439:
440:                DatabaseRawStore store = DatabaseRawStoreFactory
441:                        .getInstance(mDatasource);
442:                Content content = new Content(MimeType.RAW, raw);
443:                assertTrue(store.storeContentData(id[0], content, null));
444:
445:                store.useContentData(id[0], new ContentDataUser() {
446:                    public Object useContentData(Object contentData)
447:                            throws InnerClassException {
448:                        try {
449:                            assertTrue(Arrays.equals(raw, FileUtils
450:                                    .readBytes((InputStream) contentData)));
451:                        } catch (FileUtilsErrorException e) {
452:                            throwException(e);
453:                        }
454:                        return null;
455:                    }
456:                });
457:            }
458:
459:            public void testStoreContentDataContentEmpty() throws Exception {
460:                final int[] id = new int[] { 1 };
461:                final DatabaseContent manager = DatabaseContentFactory
462:                        .getInstance(mDatasource);
463:                Insert insert = null;
464:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
465:                        .getAliasedDriver())) {
466:                    insert = new Insert(mDatasource).into(
467:                            RifeConfig.Cmf.getTableContentInfo())
468:                            .fieldsParametersExcluded(
469:                                    DatabaseContentInfo.class,
470:                                    new String[] { "contentId" })
471:                            .fieldParameter("version").fieldParameter(
472:                                    "repositoryId");
473:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
474:                        .equals(mDatasource.getAliasedDriver())) {
475:                    insert = new Insert(mDatasource).into(
476:                            RifeConfig.Cmf.getTableContentInfo())
477:                            .fieldsParametersExcluded(
478:                                    DatabaseContentInfo.class,
479:                                    new String[] { "path" }).fieldParameter(
480:                                    "\"path\"", "path").fieldParameter(
481:                                    "version").fieldParameter("repositoryId");
482:                } else {
483:                    insert = new Insert(mDatasource).into(
484:                            RifeConfig.Cmf.getTableContentInfo())
485:                            .fieldsParameters(DatabaseContentInfo.class)
486:                            .fieldParameter("version").fieldParameter(
487:                                    "repositoryId");
488:                }
489:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
490:                    public void setParameters(DbPreparedStatement statement) {
491:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
492:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
493:                                .equals(mDatasource.getAliasedDriver())) {
494:                            content_info.setContentId(id[0]);
495:                        }
496:                        content_info.setFragment(false);
497:                        content_info.setPath("/testpath");
498:                        content_info.setMimeType(MimeType.RAW.toString());
499:                        statement
500:                                .setInt("version", 1)
501:                                .setInt(
502:                                        "repositoryId",
503:                                        manager
504:                                                .executeGetFirstInt(new Select(
505:                                                        mDatasource)
506:                                                        .from(
507:                                                                RifeConfig.Cmf
508:                                                                        .getTableContentRepository())
509:                                                        .field("repositoryId")
510:                                                        .where(
511:                                                                "name",
512:                                                                "=",
513:                                                                ContentRepository.DEFAULT)))
514:                                .setBean(content_info);
515:                    }
516:                });
517:
518:                DatabaseRawStore store = DatabaseRawStoreFactory
519:                        .getInstance(mDatasource);
520:
521:                Content content = new Content(MimeType.RAW, null);
522:                assertTrue(store.storeContentData(id[0], content, null));
523:                store.useContentData(id[0], new ContentDataUser() {
524:                    public Object useContentData(Object contentData)
525:                            throws InnerClassException {
526:                        assertNull(contentData);
527:                        return null;
528:                    }
529:                });
530:            }
531:
532:            public void testStoreContentDataContentNull() throws Exception {
533:                final int[] id = new int[] { 1 };
534:                final DatabaseContent manager = DatabaseContentFactory
535:                        .getInstance(mDatasource);
536:                Insert insert = null;
537:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
538:                        .getAliasedDriver())) {
539:                    insert = new Insert(mDatasource).into(
540:                            RifeConfig.Cmf.getTableContentInfo())
541:                            .fieldsParametersExcluded(
542:                                    DatabaseContentInfo.class,
543:                                    new String[] { "contentId" })
544:                            .fieldParameter("version").fieldParameter(
545:                                    "repositoryId");
546:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
547:                        .equals(mDatasource.getAliasedDriver())) {
548:                    insert = new Insert(mDatasource).into(
549:                            RifeConfig.Cmf.getTableContentInfo())
550:                            .fieldsParametersExcluded(
551:                                    DatabaseContentInfo.class,
552:                                    new String[] { "path" }).fieldParameter(
553:                                    "\"path\"", "path").fieldParameter(
554:                                    "version").fieldParameter("repositoryId");
555:                } else {
556:                    insert = new Insert(mDatasource).into(
557:                            RifeConfig.Cmf.getTableContentInfo())
558:                            .fieldsParameters(DatabaseContentInfo.class)
559:                            .fieldParameter("version").fieldParameter(
560:                                    "repositoryId");
561:                }
562:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
563:                    public void setParameters(DbPreparedStatement statement) {
564:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
565:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
566:                                .equals(mDatasource.getAliasedDriver())) {
567:                            content_info.setContentId(id[0]);
568:                        }
569:                        content_info.setFragment(false);
570:                        content_info.setPath("/testpath");
571:                        content_info.setMimeType(MimeType.RAW.toString());
572:                        statement
573:                                .setInt("version", 1)
574:                                .setInt(
575:                                        "repositoryId",
576:                                        manager
577:                                                .executeGetFirstInt(new Select(
578:                                                        mDatasource)
579:                                                        .from(
580:                                                                RifeConfig.Cmf
581:                                                                        .getTableContentRepository())
582:                                                        .field("repositoryId")
583:                                                        .where(
584:                                                                "name",
585:                                                                "=",
586:                                                                ContentRepository.DEFAULT)))
587:                                .setBean(content_info);
588:                    }
589:                });
590:
591:                DatabaseRawStore store = DatabaseRawStoreFactory
592:                        .getInstance(mDatasource);
593:
594:                assertTrue(store.storeContentData(id[0], null, null));
595:                store.useContentData(id[0], new ContentDataUser() {
596:                    public Object useContentData(Object contentData)
597:                            throws InnerClassException {
598:                        assertNull(contentData);
599:                        return null;
600:                    }
601:                });
602:            }
603:
604:            public void testStoreContentDataMimeTypeWithoutFormatter()
605:                    throws Exception {
606:                final int[] id = new int[] { 1 };
607:                final DatabaseContent manager = DatabaseContentFactory
608:                        .getInstance(mDatasource);
609:                Insert insert = null;
610:                if ("org.apache.derby.jdbc.EmbeddedDriver".equals(mDatasource
611:                        .getAliasedDriver())) {
612:                    insert = new Insert(mDatasource).into(
613:                            RifeConfig.Cmf.getTableContentInfo())
614:                            .fieldsParametersExcluded(
615:                                    DatabaseContentInfo.class,
616:                                    new String[] { "contentId" })
617:                            .fieldParameter("version").fieldParameter(
618:                                    "repositoryId");
619:                } else if ("in.co.daffodil.db.jdbc.DaffodilDBDriver"
620:                        .equals(mDatasource.getAliasedDriver())) {
621:                    insert = new Insert(mDatasource).into(
622:                            RifeConfig.Cmf.getTableContentInfo())
623:                            .fieldsParametersExcluded(
624:                                    DatabaseContentInfo.class,
625:                                    new String[] { "path" }).fieldParameter(
626:                                    "\"path\"", "path").fieldParameter(
627:                                    "version").fieldParameter("repositoryId");
628:                } else {
629:                    insert = new Insert(mDatasource).into(
630:                            RifeConfig.Cmf.getTableContentInfo())
631:                            .fieldsParameters(DatabaseContentInfo.class)
632:                            .fieldParameter("version").fieldParameter(
633:                                    "repositoryId");
634:                }
635:                manager.executeUpdate(insert, new DbPreparedStatementHandler() {
636:                    public void setParameters(DbPreparedStatement statement) {
637:                        DatabaseContentInfo content_info = new DatabaseContentInfo();
638:                        if (!"org.apache.derby.jdbc.EmbeddedDriver"
639:                                .equals(mDatasource.getAliasedDriver())) {
640:                            content_info.setContentId(id[0]);
641:                        }
642:                        content_info.setFragment(false);
643:                        content_info.setPath("/testpath");
644:                        content_info.setMimeType(MimeType.IMAGE_GIF.toString());
645:                        statement
646:                                .setInt("version", 1)
647:                                .setInt(
648:                                        "repositoryId",
649:                                        manager
650:                                                .executeGetFirstInt(new Select(
651:                                                        mDatasource)
652:                                                        .from(
653:                                                                RifeConfig.Cmf
654:                                                                        .getTableContentRepository())
655:                                                        .field("repositoryId")
656:                                                        .where(
657:                                                                "name",
658:                                                                "=",
659:                                                                ContentRepository.DEFAULT)))
660:                                .setBean(content_info);
661:                    }
662:                });
663:
664:                final byte[] raw = getSmallRaw();
665:
666:                DatabaseRawStore store = DatabaseRawStoreFactory
667:                        .getInstance(mDatasource);
668:                Content content = new Content(MimeType.IMAGE_GIF,
669:                        new ByteArrayInputStream(raw));
670:                assertTrue(store.storeContentData(id[0], content, null));
671:
672:                store.useContentData(id[0], new ContentDataUser() {
673:                    public Object useContentData(Object contentData)
674:                            throws InnerClassException {
675:                        try {
676:                            assertTrue(Arrays.equals(raw, FileUtils
677:                                    .readBytes((InputStream) contentData)));
678:                        } catch (FileUtilsErrorException e) {
679:                            throwException(e);
680:                        }
681:                        return null;
682:                    }
683:                });
684:            }
685:
686:            public void testStoreContentDataIllegalArgument() throws Exception {
687:                DatabaseRawStore store = DatabaseRawStoreFactory
688:                        .getInstance(mDatasource);
689:
690:                try {
691:                    store.storeContentData(-1, null, null);
692:                    fail();
693:                } catch (IllegalArgumentException e) {
694:                    assertTrue(true);
695:                }
696:
697:                try {
698:                    store.storeContentData(1, new Content(MimeType.RAW,
699:                            new Object()), null);
700:                    fail();
701:                } catch (IllegalArgumentException e) {
702:                    assertTrue(true);
703:                }
704:            }
705:
706:            public void testStoreContentDataError() throws Exception {
707:                byte[] raw = getSmallRaw();
708:
709:                DatabaseRawStore store = DatabaseRawStoreFactory
710:                        .getInstance(mDatasource);
711:                Content content = new Content(MimeType.RAW,
712:                        new ByteArrayInputStream(raw));
713:                try {
714:                    store.storeContentData(2, content, null);
715:                    assertTrue("com.mysql.jdbc.Driver".equals(mDatasource
716:                            .getAliasedDriver()));
717:                } catch (StoreContentDataErrorException e) {
718:                    assertEquals(2, e.getId());
719:                }
720:            }
721:
722:            public void testUseContentData() throws Exception {
723:                DatabaseContent manager = DatabaseContentFactory
724:                        .getInstance(mDatasource);
725:
726:                final byte[] raw = getSmallRaw();
727:
728:                Content content = new Content(MimeType.RAW,
729:                        new ByteArrayInputStream(raw));
730:                assertTrue(manager.storeContent("/rawdata", content, null));
731:                DatabaseContentInfo content_info = manager
732:                        .getContentInfo("/rawdata");
733:
734:                DatabaseRawStore store = DatabaseRawStoreFactory
735:                        .getInstance(mDatasource);
736:
737:                store.useContentData(content_info.getContentId(),
738:                        new ContentDataUser() {
739:                            public Object useContentData(Object contentData)
740:                                    throws InnerClassException {
741:                                try {
742:                                    assertTrue(Arrays
743:                                            .equals(
744:                                                    raw,
745:                                                    FileUtils
746:                                                            .readBytes((InputStream) contentData)));
747:                                } catch (FileUtilsErrorException e) {
748:                                    throwException(e);
749:                                }
750:                                return null;
751:                            }
752:                        });
753:            }
754:
755:            public void testUseContentDataUnknown() throws Exception {
756:                DatabaseRawStore store = DatabaseRawStoreFactory
757:                        .getInstance(mDatasource);
758:
759:                store.useContentData(232, new ContentDataUser() {
760:                    public Object useContentData(Object contentData)
761:                            throws InnerClassException {
762:                        assertNull(contentData);
763:                        return null;
764:                    }
765:                });
766:            }
767:
768:            public void testUseContentDataIllegalArgument() throws Exception {
769:                DatabaseRawStore store = DatabaseRawStoreFactory
770:                        .getInstance(mDatasource);
771:
772:                try {
773:                    store.useContentData(-1, new ContentDataUser() {
774:                        public Object useContentData(Object contentData)
775:                                throws InnerClassException {
776:                            fail();
777:                            return null;
778:                        }
779:                    });
780:                } catch (IllegalArgumentException e) {
781:                    assertTrue(true);
782:                }
783:
784:                try {
785:                    store.useContentData(-1, null);
786:                    fail();
787:                } catch (IllegalArgumentException e) {
788:                    assertTrue(true);
789:                }
790:            }
791:
792:            public void testUseContentDataError() throws Exception {
793:                DatabaseContentFactory.getInstance(mDatasource).remove();
794:                DatabaseRawStore store = DatabaseRawStoreFactory
795:                        .getInstance(mDatasource);
796:                try {
797:                    store.useContentData(2, new ContentDataUser() {
798:                        public Object useContentData(Object contentData)
799:                                throws InnerClassException {
800:                            fail();
801:                            return null;
802:                        }
803:                    });
804:                } catch (UseContentDataErrorException e) {
805:                    assertEquals(2, e.getId());
806:                }
807:            }
808:
809:            public void testHasContentData() throws Exception {
810:                DatabaseContent manager = DatabaseContentFactory
811:                        .getInstance(mDatasource);
812:
813:                byte[] raw = getSmallRaw();
814:
815:                Content content = new Content(MimeType.RAW,
816:                        new ByteArrayInputStream(raw));
817:                assertTrue(manager.storeContent("/rawdata", content, null));
818:                DatabaseContentInfo content_info = manager
819:                        .getContentInfo("/rawdata");
820:
821:                DatabaseRawStore store = DatabaseRawStoreFactory
822:                        .getInstance(mDatasource);
823:                assertTrue(store.hasContentData(content_info.getContentId()));
824:            }
825:
826:            public void testHasContentDataContentEmpty() throws Exception {
827:                DatabaseContent manager = DatabaseContentFactory
828:                        .getInstance(mDatasource);
829:
830:                Content content = new Content(MimeType.RAW, null);
831:                assertTrue(manager.storeContent("/rawdata", content, null));
832:                DatabaseContentInfo content_info = manager
833:                        .getContentInfo("/rawdata");
834:
835:                DatabaseRawStore store = DatabaseRawStoreFactory
836:                        .getInstance(mDatasource);
837:                assertFalse(store.hasContentData(content_info.getContentId()));
838:            }
839:
840:            public void testHasContentDataUnknown() throws Exception {
841:                DatabaseRawStore store = DatabaseRawStoreFactory
842:                        .getInstance(mDatasource);
843:                assertFalse(store.hasContentData(3));
844:            }
845:
846:            public void testHasContentDataIllegalArgument() throws Exception {
847:                DatabaseRawStore store = DatabaseRawStoreFactory
848:                        .getInstance(mDatasource);
849:                try {
850:                    store.hasContentData(-1);
851:                    fail();
852:                } catch (IllegalArgumentException e) {
853:                    assertTrue(true);
854:                }
855:            }
856:
857:            public void testHasContentDataError() throws Exception {
858:                DatabaseContentFactory.getInstance(mDatasource).remove();
859:                DatabaseRawStore store = DatabaseRawStoreFactory
860:                        .getInstance(mDatasource);
861:                try {
862:                    store.hasContentData(2);
863:                    fail();
864:                } catch (HasContentDataErrorException e) {
865:                    assertEquals(2, e.getId());
866:                }
867:            }
868:
869:            public void testDeleteContentData() throws Exception {
870:                DatabaseContent manager = DatabaseContentFactory
871:                        .getInstance(mDatasource);
872:
873:                byte[] raw = getSmallRaw();
874:
875:                Content content = new Content(MimeType.RAW,
876:                        new ByteArrayInputStream(raw));
877:                assertTrue(manager.storeContent("/rawdata", content, null));
878:                DatabaseContentInfo content_info = manager
879:                        .getContentInfo("/rawdata");
880:
881:                DatabaseRawStore store = DatabaseRawStoreFactory
882:                        .getInstance(mDatasource);
883:                assertTrue(store.hasContentData(content_info.getContentId()));
884:                assertTrue(store.deleteContentData(content_info.getContentId()));
885:                assertFalse(store.hasContentData(content_info.getContentId()));
886:                assertFalse(store
887:                        .deleteContentData(content_info.getContentId()));
888:            }
889:
890:            public void testDeleteContentDataUnknown() throws Exception {
891:                DatabaseRawStore store = DatabaseRawStoreFactory
892:                        .getInstance(mDatasource);
893:                assertFalse(store.deleteContentData(3));
894:            }
895:
896:            public void testDeleteContentDataIllegalArgument() throws Exception {
897:                DatabaseRawStore store = DatabaseRawStoreFactory
898:                        .getInstance(mDatasource);
899:                try {
900:                    store.deleteContentData(-1);
901:                    fail();
902:                } catch (IllegalArgumentException e) {
903:                    assertTrue(true);
904:                }
905:            }
906:
907:            public void testDeleteContentDataError() throws Exception {
908:                DatabaseContentFactory.getInstance(mDatasource).remove();
909:                DatabaseRawStore store = DatabaseRawStoreFactory
910:                        .getInstance(mDatasource);
911:                try {
912:                    store.deleteContentData(2);
913:                    fail();
914:                } catch (DeleteContentDataErrorException e) {
915:                    assertEquals(2, e.getId());
916:                }
917:            }
918:
919:            public void testRetrieveSize() throws Exception {
920:                DatabaseContent manager = DatabaseContentFactory
921:                        .getInstance(mDatasource);
922:
923:                byte[] raw = getSmallRaw();
924:
925:                Content content = new Content(MimeType.RAW,
926:                        new ByteArrayInputStream(raw));
927:                assertTrue(manager.storeContent("/rawdata", content, null));
928:                DatabaseContentInfo content_info = manager
929:                        .getContentInfo("/rawdata");
930:
931:                DatabaseRawStore store = DatabaseRawStoreFactory
932:                        .getInstance(mDatasource);
933:
934:                assertEquals(store.getSize(content_info.getContentId()),
935:                        raw.length);
936:            }
937:
938:            public void testRetrieveSizeIllegalArgument() throws Exception {
939:                DatabaseRawStore store = DatabaseRawStoreFactory
940:                        .getInstance(mDatasource);
941:
942:                try {
943:                    store.getSize(-1);
944:                    fail();
945:                } catch (IllegalArgumentException e) {
946:                    assertTrue(true);
947:                }
948:            }
949:
950:            public void testRetrieveSizeError() throws Exception {
951:                DatabaseContentFactory.getInstance(mDatasource).remove();
952:                DatabaseRawStore store = DatabaseRawStoreFactory
953:                        .getInstance(mDatasource);
954:                try {
955:                    store.getSize(2);
956:                    fail();
957:                } catch (RetrieveSizeErrorException e) {
958:                    assertEquals(2, e.getId());
959:                }
960:            }
961:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.