KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.convertor.dvd.DVD;
30 import org.netbeans.api.convertor.store.Store;
31 import org.netbeans.modules.convertor.PropertiesConvertor;
32 import org.netbeans.spi.convertor.Convertor;
33 import org.openide.filesystems.Repository;
34 import org.openide.modules.ModuleInfo;
35 import org.openide.util.Lookup;
36 import org.w3c.dom.Document JavaDoc;
37
38
39 /**
40  *
41  * @author David Konecny
42  */

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

90         String JavaDoc name = StoreConvertorTest.class.getResource("store").getFile() + "/data/Store.xml";
91         InputStream JavaDoc is = new FileInputStream JavaDoc(name);
92         Store sc = (Store)Convertors.read(is);
93         Book b = (Book)sc.books.iterator().next();
94         DVD d = (DVD)sc.dvds.iterator().next();
95         assertEquals(d.ID, 125);
96         assertEquals(d.title, "Tetsuo");
97         assertEquals(d.publisher, "TartanTerror");
98         assertEquals(d.price, 19);
99         assertEquals(b.ID, 4564);
100         assertEquals(b.title, "Electroboy");
101         assertEquals(b.author, "Andy Behrman");
102         assertEquals(b.publisher, "Random House");
103         assertEquals(b.price, 36);
104         is.close();
105         
106         
107         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
108         sc = new Store();
109         Book book = new Book(256, "Better than Life", "Grant Naylor", "Penguin Books", 399);
110         DVD dvd = new DVD(856, "Zentropa", "TartanClassic", 265);
111         sc.addBook(book);
112         sc.addDVD(dvd);
113         Convertors.write(os, sc);
114         
115         byte[] ba = os.toByteArray();
116         os.close();
117         
118         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(ba);
119         Object JavaDoc o = Convertors.read(bis);
120         assertEquals(sc, o);
121         
122         removeConvertor();
123         assertFalse(Convertors.canRead("http://www.netbeans.org/ns/store", "store"));
124         assertFalse(Convertors.canRead("http://www.netbeans.org/ns/store", "dvd"));
125         assertFalse(Convertors.canRead("http://www.netbeans.org/ns/store", "book"));
126         assertFalse(Convertors.canWrite(new Store()));
127         assertFalse(Convertors.canWrite(new DVD()));
128         assertFalse(Convertors.canWrite(new Book()));
129     }
130     
131 }
132
Popular Tags