KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > convertor > BookConvertorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.convertor;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import org.netbeans.junit.*;
27 import junit.textui.TestRunner;
28 import org.netbeans.api.convertor.book.Book;
29 import org.netbeans.modules.convertor.PropertiesConvertor;
30 import org.netbeans.spi.convertor.Convertor;
31 import org.openide.filesystems.Repository;
32 import org.openide.modules.ModuleInfo;
33 import org.openide.util.Lookup;
34 import org.w3c.dom.Document JavaDoc;
35
36
37 /**
38  *
39  * @author David Konecny
40  */

41 public class BookConvertorTest extends NbTestCase {
42     
43
44     public BookConvertorTest(String JavaDoc name) {
45         super (name);
46     }
47     
48     public static void main(String JavaDoc[] args) {
49         TestRunner.run(new NbTestSuite(BookConvertorTest.class));
50     }
51     
52     protected void setUp () throws Exception JavaDoc {
53         Lookup.getDefault().lookup(ModuleInfo.class);
54         Repository.getDefault ().getDefaultFileSystem ().getRoot ();
55     }
56     
57     private static Convertor conv;
58     
59     public static void setupConvertor() throws Exception JavaDoc {
60         ModuleUtils.DEFAULT.install();
61         ModuleUtils.DEFAULT.enableBookModule(true);
62     }
63     
64     public static void removeConvertor() throws Exception JavaDoc {
65         ModuleUtils.DEFAULT.enableBookModule(false);
66         ModuleUtils.DEFAULT.uninstall();
67     }
68     
69     public void testPropertiesBookConvertor() throws Exception JavaDoc {
70         assertFalse(Convertors.canRead("http://www.netbeans.org/ns/book", "book"));
71         assertFalse(Convertors.canWrite(new Book()));
72         
73         setupConvertor();
74         
75         assertTrue(Convertors.canRead("http://www.netbeans.org/ns/book", "book"));
76         assertTrue(Convertors.canWrite(new Book()));
77
78 // assertEquals(Convertors.getConvertorDescriptor(new Book()), new ConvertorDescriptor("http://www.netbeans.org/ns/book", "org.netbeans.api.convertor.book.Book"));
79

80         String JavaDoc name = BookConvertorTest.class.getResource("book").getFile() + "/data/Book.xml";
81         InputStream JavaDoc is = new FileInputStream JavaDoc(name);
82         Book b = (Book)Convertors.read(is);
83         assertEquals(b.ID, 4564);
84         assertEquals(b.title, "Electroboy");
85         assertEquals(b.author, "Andy Behrman");
86         assertEquals(b.publisher, "Random House");
87         assertEquals(b.price, 36);
88         is.close();
89         
90         
91         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
92         Book book = new Book(951, "Better than Life", "Grant Naylor", "Penguin Books", 99);
93         Convertors.write(os, book);
94         
95         byte[] ba = os.toByteArray();
96         os.close();
97         
98         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(ba);
99         Object JavaDoc o = Convertors.read(bis);
100         assertEquals(book, o);
101         
102         removeConvertor();
103         assertFalse(Convertors.canRead("http://www.netbeans.org/ns/book", "book"));
104         assertFalse(Convertors.canWrite(new Book()));
105     }
106     
107 }
108
Popular Tags