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.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 ; 35 36 37 41 public class BookConvertorTest extends NbTestCase { 42 43 44 public BookConvertorTest(String name) { 45 super (name); 46 } 47 48 public static void main(String [] args) { 49 TestRunner.run(new NbTestSuite(BookConvertorTest.class)); 50 } 51 52 protected void setUp () throws Exception { 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 { 60 ModuleUtils.DEFAULT.install(); 61 ModuleUtils.DEFAULT.enableBookModule(true); 62 } 63 64 public static void removeConvertor() throws Exception { 65 ModuleUtils.DEFAULT.enableBookModule(false); 66 ModuleUtils.DEFAULT.uninstall(); 67 } 68 69 public void testPropertiesBookConvertor() throws Exception { 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 80 String name = BookConvertorTest.class.getResource("book").getFile() + "/data/Book.xml"; 81 InputStream is = new FileInputStream (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 os = new ByteArrayOutputStream (); 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 bis = new ByteArrayInputStream (ba); 99 Object 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 |