Source Code Cross Referenced for ObjectSaveTest.java in  » Web-Services-AXIS2 » kernal » org » apache » axis2 » engine » 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 Services AXIS2 » kernal » org.apache.axis2.engine 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */
019:
020:        package org.apache.axis2.engine;
021:
022:        import junit.framework.TestCase;
023:
024:        import org.apache.axis2.util.MetaDataEntry;
025:        import org.apache.axis2.util.ObjectStateUtils;
026:        import org.apache.commons.logging.Log;
027:        import org.apache.commons.logging.LogFactory;
028:
029:        import javax.xml.namespace.QName;
030:        import java.io.Externalizable;
031:        import java.io.File;
032:        import java.io.FileInputStream;
033:        import java.io.FileOutputStream;
034:        import java.io.IOException;
035:        import java.io.NotSerializableException;
036:        import java.io.ObjectInputStream;
037:        import java.io.ObjectOutputStream;
038:        import java.io.PrintStream;
039:        import java.io.Serializable;
040:        import java.util.ArrayList;
041:        import java.util.HashMap;
042:        import java.util.LinkedList;
043:
044:        public class ObjectSaveTest extends TestCase {
045:            protected static final Log log = LogFactory
046:                    .getLog(ObjectSaveTest.class);
047:
048:            private String testArg = null;
049:
050:            // simple constructor needed for nested class Externalizable interface
051:            public ObjectSaveTest() {
052:            }
053:
054:            public ObjectSaveTest(String arg0) {
055:                super (arg0);
056:                testArg = new String(arg0);
057:            }
058:
059:            protected void setUp() throws Exception {
060:                // org.apache.log4j.BasicConfigurator.configure();
061:            }
062:
063:            public void testObjectSerializable() throws Exception {
064:                File theFile = null;
065:                String theFilename = null;
066:                boolean saved = false;
067:                boolean restored = false;
068:                boolean done = false;
069:
070:                log
071:                        .debug("ObjectSaveTest:testObjectSerializable():  BEGIN ---------------");
072:
073:                // ---------------------------------------------------------
074:                // setup an object to use
075:                // ---------------------------------------------------------
076:                MetaDataEntry obj = new MetaDataEntry("object_1", "object_1");
077:
078:                // ---------------------------------------------------------
079:                // setup a temporary file to use
080:                // ---------------------------------------------------------
081:                try {
082:                    theFile = File.createTempFile("objectTest", null);
083:                    theFilename = theFile.getName();
084:                    log
085:                            .debug("ObjectSaveTest:testObjectSerializable(): temp file = ["
086:                                    + theFilename + "]");
087:                } catch (Exception ex) {
088:                    log
089:                            .debug("ObjectSaveTest:testObjectSerializable(): error creating temp file = ["
090:                                    + ex.getMessage() + "]");
091:                    theFile = null;
092:                }
093:
094:                if (theFile != null) {
095:                    // ---------------------------------------------------------
096:                    // save to the temporary file
097:                    // ---------------------------------------------------------
098:                    try {
099:                        // setup an output stream to a physical file
100:                        FileOutputStream outStream = new FileOutputStream(
101:                                theFile);
102:
103:                        // attach a stream capable of writing objects to the
104:                        // stream connected to the file
105:                        ObjectOutputStream outObjStream = new ObjectOutputStream(
106:                                outStream);
107:
108:                        // try to save
109:                        log
110:                                .debug("ObjectSaveTest:testObjectSerializable(): saving .....");
111:                        saved = false;
112:                        ObjectStateUtils.writeObject(outObjStream, obj,
113:                                "testObject:Serializable");
114:
115:                        // close out the streams
116:                        outObjStream.flush();
117:                        outObjStream.close();
118:                        outStream.flush();
119:                        outStream.close();
120:
121:                        saved = true;
122:                        log
123:                                .debug("ObjectSaveTest:testObjectSerializable(): ....save operation completed.....");
124:
125:                        long filesize = theFile.length();
126:                        log
127:                                .debug("ObjectSaveTest:testObjectSerializable(): file size after save ["
128:                                        + filesize
129:                                        + "]   temp file = ["
130:                                        + theFilename + "]");
131:                    } catch (Exception ex2) {
132:                        log
133:                                .debug("ObjectSaveTest:testObjectSerializable(): error during save ["
134:                                        + ex2.getClass().getName()
135:                                        + " : "
136:                                        + ex2.getMessage() + "]");
137:                        ex2.printStackTrace();
138:                    }
139:
140:                    assertTrue(saved);
141:
142:                    // ---------------------------------------------------------
143:                    // restore from the temporary file
144:                    // ---------------------------------------------------------
145:                    try {
146:                        // setup an input stream to the file
147:                        FileInputStream inStream = new FileInputStream(theFile);
148:
149:                        // attach a stream capable of reading objects from the
150:                        // stream connected to the file
151:                        ObjectInputStream inObjStream = new ObjectInputStream(
152:                                inStream);
153:
154:                        // try to restore the options
155:                        log
156:                                .debug("ObjectSaveTest:testObjectSerializable(): restoring .....");
157:                        restored = false;
158:                        MetaDataEntry restored_obj = (MetaDataEntry) ObjectStateUtils
159:                                .readObject(inObjStream,
160:                                        "testObject:serializable");
161:                        inObjStream.close();
162:                        inStream.close();
163:
164:                        restored = true;
165:                        log
166:                                .debug("ObjectSaveTest:testObjectSerializable(): ....restored operation completed.....");
167:
168:                    } catch (Exception ex2) {
169:                        log
170:                                .debug("ObjectSaveTest:testObjectSerializable(): error during restore ["
171:                                        + ex2.getClass().getName()
172:                                        + " : "
173:                                        + ex2.getMessage() + "]");
174:                        ex2.printStackTrace();
175:                    }
176:
177:                    assertTrue(restored);
178:
179:                    // if the save/restore of the object succeeded,
180:                    // then don't keep the temporary file around
181:                    boolean removeTmpFile = saved && restored;
182:                    if (removeTmpFile) {
183:                        try {
184:                            theFile.delete();
185:                        } catch (Exception e) {
186:                            // just absorb it
187:                        }
188:                    }
189:
190:                    // indicate that the temp file was created ok
191:                    done = true;
192:                }
193:
194:                // this is false when there are problems with the temporary file
195:                assertTrue(done);
196:
197:                log
198:                        .debug("ObjectSaveTest:testObjectSerializable():  END ---------------");
199:            }
200:
201:            public void testObjectNotSerializable() throws Exception {
202:                File theFile = null;
203:                String theFilename = null;
204:                boolean saved = false;
205:                boolean restored = false;
206:                boolean expected_exception = false;
207:                boolean done = false;
208:
209:                log
210:                        .debug("ObjectSaveTest:testObjectNotSerializable():  BEGIN ---------------");
211:
212:                // ---------------------------------------------------------
213:                // setup an object to use
214:                // ---------------------------------------------------------
215:                NotSerializableObject obj = new NotSerializableObject("nso_1");
216:
217:                // ---------------------------------------------------------
218:                // setup a temporary file to use
219:                // ---------------------------------------------------------
220:                try {
221:                    theFile = File.createTempFile("objectTest", null);
222:                    theFilename = theFile.getName();
223:                    log
224:                            .debug("ObjectSaveTest:testObjectNotSerializable(): temp file = ["
225:                                    + theFilename + "]");
226:                } catch (Exception ex) {
227:                    log
228:                            .debug("ObjectSaveTest:testObjectNotSerializable(): error creating temp file = ["
229:                                    + ex.getMessage() + "]");
230:                    theFile = null;
231:                }
232:
233:                if (theFile != null) {
234:                    // ---------------------------------------------------------
235:                    // save to the temporary file
236:                    // ---------------------------------------------------------
237:                    FileOutputStream outStream = null;
238:                    ObjectOutputStream outObjStream = null;
239:                    try {
240:                        // setup an output stream to a physical file
241:                        outStream = new FileOutputStream(theFile);
242:
243:                        // attach a stream capable of writing objects to the
244:                        // stream connected to the file
245:                        outObjStream = new ObjectOutputStream(outStream);
246:
247:                        // try to save
248:                        log
249:                                .debug("ObjectSaveTest:testObjectNotSerializable(): saving .....");
250:                        saved = false;
251:                        ObjectStateUtils.writeObject(outObjStream, obj,
252:                                "testObject:NotSerializable");
253:
254:                        saved = true;
255:                        log
256:                                .debug("ObjectSaveTest:testObjectNotSerializable(): ....save operation completed.....");
257:
258:                        long filesize = theFile.length();
259:                        log
260:                                .debug("ObjectSaveTest:testObjectNotSerializable(): file size after save ["
261:                                        + filesize
262:                                        + "]   temp file = ["
263:                                        + theFilename + "]");
264:                    } catch (Exception ex2) {
265:                        // expect an error here
266:                        // ObjectStateUtils catches the NotSerializableException and
267:                        // logs it
268:                        if (ex2 instanceof  NotSerializableException) {
269:                            expected_exception = true;
270:                        } else {
271:                            log
272:                                    .debug("ObjectSaveTest:testObjectNotSerializable():  save ["
273:                                            + ex2.getClass().getName()
274:                                            + " : "
275:                                            + ex2.getMessage() + "]");
276:                        }
277:                    }
278:                    // close out the streams
279:                    if (outObjStream != null)
280:                        outObjStream.close();
281:                    if (outStream != null)
282:                        outStream.close();
283:
284:                    // ---------------------------------------------------------
285:                    // restore from the temporary file
286:                    // ---------------------------------------------------------
287:                    try {
288:                        // setup an input stream to the file
289:                        FileInputStream inStream = new FileInputStream(theFile);
290:
291:                        // attach a stream capable of reading objects from the
292:                        // stream connected to the file
293:                        ObjectInputStream inObjStream = new ObjectInputStream(
294:                                inStream);
295:
296:                        // try to restore the options
297:                        log
298:                                .debug("ObjectSaveTest:testObjectSerializable(): restoring .....");
299:                        restored = false;
300:                        Object restored_obj = ObjectStateUtils.readObject(
301:                                inObjStream, "testObject:NotSerializable");
302:                        inObjStream.close();
303:                        inStream.close();
304:
305:                        restored = true;
306:                        log
307:                                .debug("ObjectSaveTest:testObjectNotSerializable(): ....restored operation completed.....");
308:
309:                    } catch (Exception ex) {
310:                        log
311:                                .debug("ObjectSaveTest:testObjectNotSerializable(): error during restore ["
312:                                        + ex.getClass().getName()
313:                                        + " : "
314:                                        + ex.getMessage() + "]");
315:                        ex.printStackTrace();
316:                    }
317:
318:                    assertTrue(restored);
319:
320:                    // if the save/restore of the object succeeded,
321:                    // then don't keep the temporary file around
322:                    boolean removeTmpFile = saved && restored;
323:                    if (removeTmpFile) {
324:                        try {
325:                            theFile.delete();
326:                        } catch (Exception e) {
327:                            // just absorb it
328:                        }
329:                    }
330:
331:                    assertTrue(expected_exception);
332:                }
333:
334:                log
335:                        .debug("ObjectSaveTest:testObjectNotSerializable():  END ---------------");
336:            }
337:
338:            public void testArrayList() throws Exception {
339:                File theFile = null;
340:                String theFilename = null;
341:                boolean saved = false;
342:                boolean restored = false;
343:                boolean done = false;
344:                boolean comparesOK = false;
345:
346:                log
347:                        .debug("ObjectSaveTest:testArrayList():  BEGIN ---------------");
348:
349:                // ---------------------------------------------------------
350:                // setup the object to use
351:                // ---------------------------------------------------------
352:                ArrayList obj = new ArrayList();
353:                obj.add(new Integer(1));
354:                obj.add(new Integer(2));
355:                obj.add(new Integer(3));
356:                obj.add(new String("string1"));
357:                obj.add(new String("string2"));
358:                obj.add(System.out);
359:                obj.add(new Integer(4));
360:                obj.add(new Integer(5));
361:                obj.add(new Integer(6));
362:
363:                int initial_size = obj.size();
364:
365:                // ---------------------------------------------------------
366:                // setup a temporary file to use
367:                // ---------------------------------------------------------
368:                try {
369:                    theFile = File.createTempFile("arraylistTest", null);
370:                    theFilename = theFile.getName();
371:                    log.debug("ObjectSaveTest:testArrayList(): temp file = ["
372:                            + theFilename + "]");
373:                } catch (Exception ex) {
374:                    log
375:                            .debug("ObjectSaveTest:testArrayList(): error creating temp file = ["
376:                                    + ex.getMessage() + "]");
377:                    theFile = null;
378:                }
379:
380:                if (theFile != null) {
381:                    // ---------------------------------------------------------
382:                    // save to the temporary file
383:                    // ---------------------------------------------------------
384:                    try {
385:                        // setup an output stream to a physical file
386:                        FileOutputStream outStream = new FileOutputStream(
387:                                theFile);
388:
389:                        // attach a stream capable of writing objects to the
390:                        // stream connected to the file
391:                        ObjectOutputStream outObjStream = new ObjectOutputStream(
392:                                outStream);
393:
394:                        // try to save
395:                        log
396:                                .debug("ObjectSaveTest:testArrayList(): saving .....");
397:                        saved = false;
398:                        ObjectStateUtils.writeArrayList(outObjStream, obj,
399:                                "testObject:ArrayList");
400:
401:                        // close out the streams
402:                        outObjStream.flush();
403:                        outObjStream.close();
404:                        outStream.flush();
405:                        outStream.close();
406:
407:                        saved = true;
408:                        log
409:                                .debug("ObjectSaveTest:testArrayList(): ....save operation completed.....");
410:
411:                        long filesize = theFile.length();
412:                        log
413:                                .debug("ObjectSaveTest:testArrayList(): file size after save ["
414:                                        + filesize
415:                                        + "]   temp file = ["
416:                                        + theFilename + "]");
417:                    } catch (Exception ex2) {
418:                        log
419:                                .debug("ObjectSaveTest:testArrayList(): error during save ["
420:                                        + ex2.getClass().getName()
421:                                        + " : "
422:                                        + ex2.getMessage() + "]");
423:                        ex2.printStackTrace();
424:                    }
425:
426:                    assertTrue(saved);
427:
428:                    // ---------------------------------------------------------
429:                    // restore from the temporary file
430:                    // ---------------------------------------------------------
431:                    ArrayList restored_obj = null;
432:
433:                    try {
434:                        // setup an input stream to the file
435:                        FileInputStream inStream = new FileInputStream(theFile);
436:
437:                        // attach a stream capable of reading objects from the
438:                        // stream connected to the file
439:                        ObjectInputStream inObjStream = new ObjectInputStream(
440:                                inStream);
441:
442:                        // try to restore the options
443:                        log
444:                                .debug("ObjectSaveTest:testArrayList(): restoring .....");
445:                        restored = false;
446:                        restored_obj = ObjectStateUtils.readArrayList(
447:                                inObjStream, "testObject:ArrayList");
448:                        inObjStream.close();
449:                        inStream.close();
450:
451:                        restored = true;
452:                        log
453:                                .debug("ObjectSaveTest:testArrayList(): ....restored operation completed.....");
454:
455:                    } catch (Exception ex2) {
456:                        log
457:                                .debug("ObjectSaveTest:testArrayList(): error during restore ["
458:                                        + ex2.getClass().getName()
459:                                        + " : "
460:                                        + ex2.getMessage() + "]");
461:                        ex2.printStackTrace();
462:                    }
463:
464:                    // if the save/restore of the object succeeded,
465:                    // then don't keep the temporary file around
466:                    boolean removeTmpFile = saved && restored;
467:                    if (removeTmpFile) {
468:                        try {
469:                            theFile.delete();
470:                        } catch (Exception e) {
471:                            // just absorb it
472:                        }
473:                    }
474:
475:                    assertTrue(restored);
476:
477:                    if (restored_obj != null) {
478:                        int restored_size = restored_obj.size();
479:                        if (restored_size == (initial_size - 1)) {
480:                            comparesOK = true;
481:                        }
482:                    }
483:
484:                    // TODO: check for exact entries
485:
486:                    assertTrue(comparesOK);
487:
488:                    // indicate that the temp file was created ok
489:                    done = true;
490:                }
491:
492:                // this is false when there are problems with the temporary file
493:                assertTrue(done);
494:
495:                log
496:                        .debug("ObjectSaveTest:testArrayList():  END ---------------");
497:            }
498:
499:            public void testHashMap() throws Exception {
500:                File theFile = null;
501:                String theFilename = null;
502:                boolean saved = false;
503:                boolean restored = false;
504:                boolean done = false;
505:                boolean comparesOK = false;
506:
507:                log
508:                        .debug("ObjectSaveTest:testHashMap():  BEGIN ---------------");
509:
510:                // ---------------------------------------------------------
511:                // setup the object to use
512:                // ---------------------------------------------------------
513:                HashMap obj = new HashMap();
514:                obj.put(new String("key1"), new Integer(1));
515:                obj.put(new String("key2"), new Integer(2));
516:                obj.put(new String("key3"), new String("value1"));
517:                obj.put(new String("key4"), System.out);
518:                obj.put(new String("key5"), new Integer(3));
519:                obj.put(new String("key6"), new Integer(4));
520:                obj.put(new String("key7"), System.err);
521:                obj.put(new String("key8"), new Integer(5));
522:                obj.put(new String("key9"), new Integer(6));
523:                obj.put(new NotSerializableObject("TestForHashMapKey"),
524:                        new Integer(7));
525:                obj.put(new String("key10"), new Integer(8));
526:
527:                int initial_size = obj.size();
528:
529:                // ---------------------------------------------------------
530:                // setup a temporary file to use
531:                // ---------------------------------------------------------
532:                try {
533:                    theFile = File.createTempFile("hashmapTest", null);
534:                    theFilename = theFile.getName();
535:                    log.debug("ObjectSaveTest:testHashMap(): temp file = ["
536:                            + theFilename + "]");
537:                } catch (Exception ex) {
538:                    log
539:                            .debug("ObjectSaveTest:testHashMap(): error creating temp file = ["
540:                                    + ex.getMessage() + "]");
541:                    theFile = null;
542:                }
543:
544:                if (theFile != null) {
545:                    // ---------------------------------------------------------
546:                    // save to the temporary file
547:                    // ---------------------------------------------------------
548:                    try {
549:                        // setup an output stream to a physical file
550:                        FileOutputStream outStream = new FileOutputStream(
551:                                theFile);
552:
553:                        // attach a stream capable of writing objects to the
554:                        // stream connected to the file
555:                        ObjectOutputStream outObjStream = new ObjectOutputStream(
556:                                outStream);
557:
558:                        // try to save
559:                        log.debug("ObjectSaveTest:testHashMap(): saving .....");
560:                        saved = false;
561:                        ObjectStateUtils.writeHashMap(outObjStream, obj,
562:                                "testObject:HashMap");
563:
564:                        // close out the streams
565:                        outObjStream.flush();
566:                        outObjStream.close();
567:                        outStream.flush();
568:                        outStream.close();
569:
570:                        saved = true;
571:                        log
572:                                .debug("ObjectSaveTest:testHashMap(): ....save operation completed.....");
573:
574:                        long filesize = theFile.length();
575:                        log
576:                                .debug("ObjectSaveTest:testHashMap(): file size after save ["
577:                                        + filesize
578:                                        + "]   temp file = ["
579:                                        + theFilename + "]");
580:                    } catch (Exception ex2) {
581:                        log
582:                                .debug("ObjectSaveTest:testHashMap(): error during save ["
583:                                        + ex2.getClass().getName()
584:                                        + " : "
585:                                        + ex2.getMessage() + "]");
586:                        ex2.printStackTrace();
587:                    }
588:
589:                    assertTrue(saved);
590:
591:                    // ---------------------------------------------------------
592:                    // restore from the temporary file
593:                    // ---------------------------------------------------------
594:                    HashMap restored_obj = null;
595:
596:                    try {
597:                        // setup an input stream to the file
598:                        FileInputStream inStream = new FileInputStream(theFile);
599:
600:                        // attach a stream capable of reading objects from the
601:                        // stream connected to the file
602:                        ObjectInputStream inObjStream = new ObjectInputStream(
603:                                inStream);
604:
605:                        // try to restore the options
606:                        log
607:                                .debug("ObjectSaveTest:testHashMap(): restoring .....");
608:                        restored = false;
609:                        restored_obj = ObjectStateUtils.readHashMap(
610:                                inObjStream, "testObject:HashMap");
611:                        inObjStream.close();
612:                        inStream.close();
613:
614:                        restored = true;
615:                        log
616:                                .debug("ObjectSaveTest:testHashMap(): ....restored operation completed.....");
617:
618:                    } catch (Exception ex2) {
619:                        log
620:                                .debug("ObjectSaveTest:testHashMap(): error during restore ["
621:                                        + ex2.getClass().getName()
622:                                        + " : "
623:                                        + ex2.getMessage() + "]");
624:                        ex2.printStackTrace();
625:                    }
626:
627:                    // if the save/restore of the object succeeded,
628:                    // then don't keep the temporary file around
629:                    boolean removeTmpFile = saved && restored;
630:                    if (removeTmpFile) {
631:                        try {
632:                            theFile.delete();
633:                        } catch (Exception e) {
634:                            // just absorb it
635:                        }
636:                    }
637:
638:                    assertTrue(restored);
639:
640:                    if (restored_obj != null) {
641:                        int restored_size = restored_obj.size();
642:                        if (restored_size == (initial_size - 3)) {
643:                            // there are entries in the map that are not serializable
644:                            comparesOK = true;
645:                        }
646:                    }
647:
648:                    // TODO: check for exact entries
649:
650:                    assertTrue(comparesOK);
651:
652:                    // indicate that the temp file was created ok
653:                    done = true;
654:                }
655:
656:                // this is false when there are problems with the temporary file
657:                assertTrue(done);
658:
659:                log.debug("ObjectSaveTest:testHashMap():  END ---------------");
660:            }
661:
662:            public void testLinkedList() throws Exception {
663:                File theFile = null;
664:                String theFilename = null;
665:                boolean saved = false;
666:                boolean restored = false;
667:                boolean done = false;
668:                boolean comparesOK = false;
669:
670:                log
671:                        .debug("ObjectSaveTest:testLinkedList():  BEGIN ---------------");
672:
673:                // ---------------------------------------------------------
674:                // setup the object to use
675:                // ---------------------------------------------------------
676:                LinkedList obj = new LinkedList();
677:                obj.add(new Integer(1));
678:                obj.add(new Integer(2));
679:                obj.add(new Integer(3));
680:                obj.add(new String("string1"));
681:                obj.add(new String("string2"));
682:                obj.add(System.in);
683:                obj.add(new Integer(4));
684:                obj.add(new Integer(5));
685:                obj.add(new Integer(6));
686:
687:                int initial_size = obj.size();
688:
689:                // ---------------------------------------------------------
690:                // setup a temporary file to use
691:                // ---------------------------------------------------------
692:                try {
693:                    theFile = File.createTempFile("linkedlistTest", null);
694:                    theFilename = theFile.getName();
695:                    log.debug("ObjectSaveTest:testLinkedList(): temp file = ["
696:                            + theFilename + "]");
697:                } catch (Exception ex) {
698:                    log
699:                            .debug("ObjectSaveTest:testLinkedList(): error creating temp file = ["
700:                                    + ex.getMessage() + "]");
701:                    theFile = null;
702:                }
703:
704:                if (theFile != null) {
705:                    // ---------------------------------------------------------
706:                    // save to the temporary file
707:                    // ---------------------------------------------------------
708:                    try {
709:                        // setup an output stream to a physical file
710:                        FileOutputStream outStream = new FileOutputStream(
711:                                theFile);
712:
713:                        // attach a stream capable of writing objects to the
714:                        // stream connected to the file
715:                        ObjectOutputStream outObjStream = new ObjectOutputStream(
716:                                outStream);
717:
718:                        // try to save
719:                        log
720:                                .debug("ObjectSaveTest:testLinkedList(): saving .....");
721:                        saved = false;
722:                        ObjectStateUtils.writeLinkedList(outObjStream, obj,
723:                                "testObject:LinkedList");
724:
725:                        // close out the streams
726:                        outObjStream.flush();
727:                        outObjStream.close();
728:                        outStream.flush();
729:                        outStream.close();
730:
731:                        saved = true;
732:                        log
733:                                .debug("ObjectSaveTest:testLinkedList(): ....save operation completed.....");
734:
735:                        long filesize = theFile.length();
736:                        log
737:                                .debug("ObjectSaveTest:testLinkedList(): file size after save ["
738:                                        + filesize
739:                                        + "]   temp file = ["
740:                                        + theFilename + "]");
741:                    } catch (Exception ex2) {
742:                        log
743:                                .debug("ObjectSaveTest:testLinkedList(): error during save ["
744:                                        + ex2.getClass().getName()
745:                                        + " : "
746:                                        + ex2.getMessage() + "]");
747:                        ex2.printStackTrace();
748:                    }
749:
750:                    assertTrue(saved);
751:
752:                    // ---------------------------------------------------------
753:                    // restore from the temporary file
754:                    // ---------------------------------------------------------
755:                    LinkedList restored_obj = null;
756:
757:                    try {
758:                        // setup an input stream to the file
759:                        FileInputStream inStream = new FileInputStream(theFile);
760:
761:                        // attach a stream capable of reading objects from the
762:                        // stream connected to the file
763:                        ObjectInputStream inObjStream = new ObjectInputStream(
764:                                inStream);
765:
766:                        // try to restore the options
767:                        log
768:                                .debug("ObjectSaveTest:testLinkedList(): restoring .....");
769:                        restored = false;
770:                        restored_obj = ObjectStateUtils.readLinkedList(
771:                                inObjStream, "testObject:LinkedList");
772:                        inObjStream.close();
773:                        inStream.close();
774:
775:                        restored = true;
776:                        log
777:                                .debug("ObjectSaveTest:testLinkedList(): ....restored operation completed.....");
778:
779:                    } catch (Exception ex2) {
780:                        log
781:                                .debug("ObjectSaveTest:testLinkedList(): error during restore ["
782:                                        + ex2.getClass().getName()
783:                                        + " : "
784:                                        + ex2.getMessage() + "]");
785:                        ex2.printStackTrace();
786:                    }
787:
788:                    // if the save/restore of the object succeeded,
789:                    // then don't keep the temporary file around
790:                    boolean removeTmpFile = saved && restored;
791:                    if (removeTmpFile) {
792:                        try {
793:                            theFile.delete();
794:                        } catch (Exception e) {
795:                            // just absorb it
796:                        }
797:                    }
798:
799:                    assertTrue(restored);
800:
801:                    if (restored_obj != null) {
802:                        int restored_size = restored_obj.size();
803:                        if (restored_size == (initial_size - 1)) {
804:                            comparesOK = true;
805:                        }
806:                    }
807:
808:                    // TODO: check for exact entries
809:
810:                    assertTrue(comparesOK);
811:
812:                    // indicate that the temp file was created ok
813:                    done = true;
814:                }
815:
816:                // this is false when there are problems with the temporary file
817:                assertTrue(done);
818:
819:                log
820:                        .debug("ObjectSaveTest:testLinkedList():  END ---------------");
821:            }
822:
823:            public class NotSerializableObject implements  Externalizable {
824:                private String label = "TestObject";
825:
826:                private String ID = null;
827:
828:                // make sure we have some objects that don't serialize
829:                private PrintStream ps = System.out;
830:
831:                // default constructor needed for Externalizable interface
832:                public NotSerializableObject() {
833:                }
834:
835:                public NotSerializableObject(String identifier) {
836:                    ID = identifier;
837:                    ps = System.out;
838:                }
839:
840:                public void setID(String s) {
841:                    ID = s;
842:                }
843:
844:                public String getID() {
845:                    return ID;
846:                }
847:
848:                public void writeExternal(java.io.ObjectOutput out)
849:                        throws IOException {
850:                    throw new NotSerializableException(
851:                            "Test Object is not serializable");
852:                }
853:
854:                public void readExternal(java.io.ObjectInput in)
855:                        throws IOException, ClassNotFoundException {
856:                    throw new IOException("Test object is not serializable");
857:                }
858:
859:            }
860:
861:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.