KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > catalog > impl > XAMCatalogWriteModelTest


1 /*
2  * XAMCatalogWriteModelTest.java
3  * JUnit based test
4  *
5  * Created on December 14, 2006, 4:19 PM
6  */

7
8 package org.netbeans.modules.xml.retriever.catalog.impl;
9
10 import javax.swing.text.BadLocationException JavaDoc;
11 import javax.swing.text.Document JavaDoc;
12 import junit.framework.*;
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URI JavaDoc;
16 import java.net.URISyntaxException JavaDoc;
17 import org.netbeans.modules.xml.retriever.catalog.model.CatalogModelFactory;
18 import org.netbeans.modules.xml.retriever.catalog.model.TestUtil;
19 import org.netbeans.modules.xml.xam.ModelSource;
20 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
21 import org.openide.filesystems.FileObject;
22 import org.openide.filesystems.FileUtil;
23
24 /**
25  *
26  * @author girix
27  */

28 public class XAMCatalogWriteModelTest extends TestCase {
29     
30     public XAMCatalogWriteModelTest(String JavaDoc testName) {
31         super(testName);
32     }
33     
34     protected void setUp() throws Exception JavaDoc {
35     }
36     
37     protected void tearDown() throws Exception JavaDoc {
38     }
39     
40     public static Test suite() {
41         TestSuite suite = new TestSuite(XAMCatalogWriteModelTest.class);
42         
43         return suite;
44     }
45     
46     public void testSearchURI() {
47         URI JavaDoc locationURI = null;
48         try {
49             locationURI = new URI JavaDoc("sysIDAttr");
50         } catch (URISyntaxException JavaDoc ex) {
51         }
52         
53         XAMCatalogWriteModelImpl instance = getTestCatModelInstance();
54         
55         URI JavaDoc result = instance.searchURI(locationURI);
56         assertNotNull(result);
57     }
58     
59     public void testAddAndRemoveURI() throws Exception JavaDoc {
60         URI JavaDoc leftURI = new URI JavaDoc("girish");
61         URI JavaDoc rightURI = new URI JavaDoc("kumar");
62         FileObject fileObj = null;
63         MyXAMCatalogWriteModel instance = getTestCatModelInstance();
64         
65         int start = instance.getCatalogEntries().size();
66         
67         instance.addURI(leftURI, rightURI);
68         
69         assertEquals(start+1, instance.getCatalogEntries().size());
70         
71         instance.removeURI(leftURI);
72         
73         assertEquals(start, instance.getCatalogEntries().size());
74     }
75     
76     public MyXAMCatalogWriteModel getTestCatModelInstance(){
77         FileObject inputFile = null;
78         
79         try {
80             inputFile = FileUtil.toFileObject(FileUtil.normalizeFile(new File JavaDoc(XAMCatalogWriteModelTest.class.
81                     getResource("catalog.xml").toURI())));
82         } catch (URISyntaxException JavaDoc ex) {
83             assert false;
84             ex.printStackTrace();
85             return null;
86         }
87         
88         MyXAMCatalogWriteModel instance = null;
89         try {
90             instance = new MyXAMCatalogWriteModel(inputFile);
91         } catch (IOException JavaDoc ex) {
92             assert false;
93         } catch (CatalogModelException ex) {
94             ex.printStackTrace();
95             assert false;
96         }
97         
98         return instance;
99     }
100     
101     public void testAddNextCatalog() throws Exception JavaDoc {
102         URI JavaDoc leftURI = new URI JavaDoc("girish");
103         FileObject fileObj = null;
104         MyXAMCatalogWriteModel instance = getTestCatModelInstance();
105         
106         int start = instance.getCatalogEntries().size();
107         
108         instance.addNextCatalog(leftURI, true);
109         
110         assertEquals(start+1, instance.getCatalogEntries().size());
111         
112         instance.removeNextCatalog(leftURI);
113         
114         assertEquals(start, instance.getCatalogEntries().size());
115     }
116     
117     
118     
119     class MyXAMCatalogWriteModel extends XAMCatalogWriteModelImpl{
120         public MyXAMCatalogWriteModel(FileObject catFile) throws IOException JavaDoc, CatalogModelException{
121             super(catFile);
122         }
123         
124         protected ModelSource createModelSource(FileObject catFileObject) throws CatalogModelException {
125             ModelSource source = null;
126             try {
127                 source = TestUtil.createModelSource(super.catalogFileObject, true);
128             } catch (CatalogModelException ex) {
129                 assert false;
130                 ex.printStackTrace();
131                 return null;
132             }
133             return source;
134         }
135         
136         
137         public String JavaDoc getContent(){
138             Document JavaDoc doc = (Document JavaDoc) getCatalogModel().getModelSource().getLookup().lookup(Document JavaDoc.class);;
139             try {
140                 return doc.getText(0, doc.getLength());
141             } catch (BadLocationException JavaDoc ex) {
142                 ex.printStackTrace();
143             }
144             return null;
145         }
146     }
147 }
148
Popular Tags