1 19 20 package org.netbeans.api.convertor; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.FileInputStream ; 25 import java.io.InputStream ; 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 ; 37 38 39 43 public class ShoppingCartConvertorTest extends NbTestCase { 44 45 46 public ShoppingCartConvertorTest(String name) { 47 super (name); 48 } 49 50 public static void main(String [] args) { 51 TestRunner.run(new NbTestSuite(ShoppingCartConvertorTest.class)); 52 } 53 54 protected void setUp () throws Exception { 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 { 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 { 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 { 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 88 String name = ShoppingCartConvertorTest.class.getResource("shoppingcart").getFile() + "/data/ShoppingCart.xml"; 89 InputStream is = new FileInputStream (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 os = new ByteArrayOutputStream (); 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 bis = new ByteArrayInputStream (ba); 117 Object 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 |