KickJava   Java API By Example, From Geeks To Geeks.

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


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.shoppingcart.ShoppingCart;
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 ShoppingCartConvertorTest extends NbTestCase {
44     
45
46     public ShoppingCartConvertorTest(String JavaDoc name) {
47         super (name);
48     }
49     
50     public static void main(String JavaDoc[] args) {
51         TestRunner.run(new NbTestSuite(ShoppingCartConvertorTest.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     private static Convertor conv2;
61     private static Convertor conv3;
62     
63     public static void setupConvertor() throws Exception JavaDoc {
64         ModuleUtils.DEFAULT.install();
65         ModuleUtils.DEFAULT.enableDVDConvertorModule(true);
66         ModuleUtils.DEFAULT.enableBookModule(true);
67         ModuleUtils.DEFAULT.enableShoppingCartConvertorModule(true);
68     }
69     
70     public static void removeConvertor() throws Exception JavaDoc {
71         ModuleUtils.DEFAULT.enableShoppingCartConvertorModule(false);
72         ModuleUtils.DEFAULT.enableDVDConvertorModule(false);
73         ModuleUtils.DEFAULT.enableBookModule(false);
74         ModuleUtils.DEFAULT.uninstall();
75     }
76     
77     public void testShoppingCartConvertor() throws Exception JavaDoc {
78         assertFalse(Convertors.canRead("http://www.netbeans.org/ns/shoppingcart", "shoppingcart"));
79         assertFalse(Convertors.canWrite(new ShoppingCart()));
80         
81         setupConvertor();
82         
83         assertTrue(Convertors.canRead("http://www.netbeans.org/ns/shoppingcart", "shoppingcart"));
84         assertTrue(Convertors.canWrite(new ShoppingCart()));
85
86 // assertEquals(Convertors.getConvertorDescriptor(new ShoppingCart()), new ConvertorDescriptor("http://www.netbeans.org/ns/shoppingcart", "org.netbeans.api.convertor.shoppingcart.ShoppingCart"));
87

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