KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > xdm > model > JSFConfigModelTest


1 /*
2  * JSFConfigModelTest.java
3  * JUnit based test
4  *
5  * Created on January 29, 2007, 3:18 PM
6  */

7
8 package org.netbeans.modules.web.jsf.xdm.model;
9
10 import java.io.File JavaDoc;
11 import java.net.URI JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.logging.Level JavaDoc;
17 import java.util.logging.Logger JavaDoc;
18 import junit.framework.*;
19 import org.netbeans.junit.NbTestCase;
20 import org.netbeans.modules.web.jsf.api.facesmodel.Converter;
21 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
22 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigComponentFactory;
23 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel;
24 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel;
25 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModelFactory;
26 import org.netbeans.modules.web.jsf.api.facesmodel.JSFVersion;
27 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean;
28 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationCase;
29 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationRule;
30 import org.netbeans.modules.web.jsf.impl.facesmodel.JSFConfigModelImpl;
31 import org.netbeans.modules.xml.xam.ModelSource;
32 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
33 import org.netbeans.modules.xml.xam.dom.DocumentModel;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36
37 /**
38  *
39  * @author Petr Pisl
40  */

41 public class JSFConfigModelTest extends NbTestCase {
42     
43     public JSFConfigModelTest(String JavaDoc testName) {
44         super(testName);
45     }
46     
47     
48     @Override JavaDoc
49     protected Level JavaDoc logLevel() {
50         return Level.INFO;
51     }
52
53     protected void setUp() throws Exception JavaDoc {
54         Logger.getLogger(JSFConfigModelImpl.class.getName()).setLevel(Level.FINEST);
55     }
56     
57     protected void tearDown() throws Exception JavaDoc {
58     }
59     
60     public static Test suite() {
61         TestSuite suite = new TestSuite(JSFConfigModelTest.class);
62         return suite;
63     }
64     
65     public void testReadJSFVersion1_1() throws Exception JavaDoc {
66         JSFConfigModel model = Util.loadRegistryModel("faces-config-01.xml");
67         FacesConfig facesConfig = model.getRootComponent();
68         assertNotNull(facesConfig);
69         System.out.println("facesConfig: " + facesConfig);
70         Collection JavaDoc<ManagedBean> managedBeans = facesConfig.getManagedBeans();
71         for (ManagedBean elem : managedBeans) {
72             System.out.println(elem.getManagedBeanName() + ", " + elem.getManagedBeanClass() + ", " + elem.getManagedBeanScope());
73         }
74     }
75     
76     public void testReadJSFJPAExample() throws Exception JavaDoc {
77         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
78         FacesConfig facesConfig = model.getRootComponent();
79         assertNotNull(facesConfig);
80         // testing managed bean
81
Collection JavaDoc<ManagedBean> managedBeans = facesConfig.getManagedBeans();
82         assertEquals("Number of managed beans ", 1, managedBeans.size());
83         ManagedBean managedBean = managedBeans.iterator().next();
84         assertEquals("ManagedBean name ", "usermanager", managedBean.getManagedBeanName());
85         assertEquals("ManagedBean class ", "enterprise.jsf_jpa_war.UserManager", managedBean.getManagedBeanClass());
86         assertEquals("ManagedBean scope ", "request", managedBean.getManagedBeanScope());
87         //testing navigation rule
88
Collection JavaDoc<NavigationRule> navigationRules = facesConfig.getNavigationRules();
89         assertEquals("Number of navigation rules ", 1, navigationRules.size());
90         NavigationRule navigationRule = navigationRules.iterator().next();
91         assertNull("Rule description ", navigationRule.getDescription());
92         assertNull("Rule from-view-id ", navigationRule.getFromViewId());
93         Collection JavaDoc<NavigationCase> navigationCases = navigationRule.getNavigationCases();
94         assertEquals("Number of navigation cases ", 3, navigationCases.size());
95         Iterator JavaDoc<NavigationCase> it = navigationCases.iterator();
96         NavigationCase navigationCase = it.next();
97         assertEquals("login", navigationCase.getFromOutcome());
98         assertEquals("/login.jsp", navigationCase.getToViewId());
99         assertTrue(navigationCase.isRedirected());
100         assertNull(navigationCase.getDescription());
101         assertNull(navigationCase.getFromAction());
102         navigationCase = it.next();
103         assertEquals("create", navigationCase.getFromOutcome());
104         assertEquals("/create.jsp", navigationCase.getToViewId());
105         assertFalse(navigationCase.isRedirected());
106         assertNull(navigationCase.getDescription());
107         assertNull(navigationCase.getFromAction());
108         navigationCase = it.next();
109         assertEquals("app-main", navigationCase.getFromOutcome());
110         assertEquals("/welcomeJSF.jsp", navigationCase.getToViewId());
111         assertTrue(navigationCase.isRedirected());
112         assertNull(navigationCase.getDescription());
113         assertNull(navigationCase.getFromAction());
114     }
115     
116     final String JavaDoc newDescription = "Some text.\n Test description\nnew line\n\nnew second line.";
117     final String JavaDoc newFromViewID = "/haha.jsp";
118     public void testChangeNavigationRuleJSFJPAExample() throws Exception JavaDoc {
119         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
120         FacesConfig facesConfig = model.getRootComponent();
121         assertNotNull(facesConfig);
122         Collection JavaDoc<NavigationRule> navigationRules = facesConfig.getNavigationRules();
123         assertEquals("Number of navigation rules ", 1, navigationRules.size());
124         NavigationRule navigationRule = navigationRules.iterator().next();
125         assertNull("Rule description ", navigationRule.getDescription());
126         assertNull("Rule from-view-id ", navigationRule.getFromViewId());
127         
128         // provide change in the NavigationRule
129
model.startTransaction();
130         navigationRule.setDescription(newDescription);
131         navigationRule.setFromViewId(newFromViewID);
132         model.endTransaction();
133         
134         // test whether the change is in the model
135
navigationRules = facesConfig.getNavigationRules();
136         assertEquals("Number of navigation rules ", 1, navigationRules.size());
137         navigationRule = navigationRules.iterator().next();
138         assertEquals(newDescription, navigationRule.getDescription());
139         assertEquals(newFromViewID, navigationRule.getFromViewId());
140         
141         // save the model into a tmp file and reload. then test again.
142
dumpModelToFile(model, "test-config-01.xml");
143         navigationRules = model.getRootComponent().getNavigationRules();
144         navigationRule = navigationRules.iterator().next();
145         assertEquals("Number of navigation rules ", 1, navigationRules.size());
146         assertEquals(newDescription, navigationRule.getDescription());
147         assertEquals(newFromViewID, navigationRule.getFromViewId());
148         
149         // delete change in the NavigationRule
150
model.startTransaction();
151         navigationRule.setDescription(null);
152         navigationRule.setFromViewId(null);
153         model.endTransaction();
154         
155         navigationRules = model.getRootComponent().getNavigationRules();
156         navigationRule = navigationRules.iterator().next();
157         assertNull(navigationRule.getDescription());
158         assertNull(navigationRule.getFromViewId());
159         dumpModelToFile(model, "test-config-02.xml");
160     }
161     
162     public void testAddRemoveNavigationRuleJSFJPAExample() throws Exception JavaDoc {
163         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
164         FacesConfig facesConfig = model.getRootComponent();
165         assertNotNull(facesConfig);
166         
167         Collection JavaDoc<NavigationRule> navigationRules = facesConfig.getNavigationRules();
168         assertEquals("Number of navigation rules ", 1, navigationRules.size());
169         
170         NavigationRule newRule = model.getFactory().createNavigationRule();
171         newRule.setDescription(newDescription);
172         newRule.setFromViewId(newFromViewID);
173         
174         model.startTransaction();
175         facesConfig.addNavigationRule(newRule);
176         model.endTransaction();
177         
178         // save the model into a tmp file and reload. then test.
179
dumpModelToFile(model, "test-config-03.xml");
180         navigationRules = model.getRootComponent().getNavigationRules();
181         assertEquals("Number of navigation rules ", 2, navigationRules.size());
182         Iterator JavaDoc <NavigationRule> iterator = navigationRules.iterator();
183         iterator.next();
184         newRule = iterator.next();
185         assertEquals(newDescription, newRule.getDescription());
186         assertEquals(newFromViewID, newRule.getFromViewId());
187         
188         model.startTransaction();
189         model.getRootComponent().removeNavigationRule(newRule);
190         model.endTransaction();
191         
192         dumpModelToFile(model, "test-config-04.xml");
193         navigationRules = model.getRootComponent().getNavigationRules();
194         assertEquals("Number of navigation rules ", 1, navigationRules.size());
195     }
196     
197     public void testChangeNavigationCase() throws Exception JavaDoc{
198         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
199         FacesConfig facesConfig = model.getRootComponent();
200         assertNotNull(facesConfig);
201         
202         Collection JavaDoc<NavigationRule> navigationRules = facesConfig.getNavigationRules();
203         assertEquals("Number of navigation rules ", 1, navigationRules.size());
204         NavigationRule navigationRule = navigationRules.iterator().next();
205         Collection JavaDoc<NavigationCase> navigationCases = navigationRule.getNavigationCases();
206         assertEquals("Number of navigation cases ", 3, navigationCases.size());
207         NavigationCase navigationCase = navigationCases.iterator().next();
208         
209         model.startTransaction();
210         navigationCase.setDescription("Test Description");
211         navigationCase.setFromAction("hahatest");
212         navigationCase.setToViewId("welcomme.test");
213         navigationCase.setRedirected(false);
214         model.endTransaction();
215         
216         dumpModelToFile(model, "test-config-01.xml");
217         
218         model.startTransaction();
219         navigationCase.setRedirected(true);
220         model.endTransaction();
221         dumpModelToFile(model, "test-config-02.xml");
222         
223     }
224     
225     public void testAddRemoveNavigationCaseJSFJPAExample() throws Exception JavaDoc {
226         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
227         FacesConfig facesConfig = model.getRootComponent();
228         assertNotNull(facesConfig);
229         
230         Collection JavaDoc<NavigationRule> navigationRules = facesConfig.getNavigationRules();
231         assertEquals("Number of navigation rules ", 1, navigationRules.size());
232         NavigationRule navigationRule = navigationRules.iterator().next();
233         Collection JavaDoc<NavigationCase> navigationCases = navigationRule.getNavigationCases();
234         assertEquals("Number of navigation cases ", 3, navigationCases.size());
235         NavigationCase newCase = model.getFactory().createNavigationCase();
236         newCase.setDescription("Test case description");
237         newCase.setFromOutcome("/fromOutcame.jsp");
238         newCase.setToViewId("/toviewide.jsp");
239         
240         navigationRule.getModel().startTransaction();
241         navigationRule.addNavigationCase(newCase);
242         navigationRule.getModel().endTransaction();
243         
244         System.out.println("pridam case");
245         //Util.dumpToStream(((AbstractDocumentModel)model).getBaseDocument(), System.out);
246
//model = Util.dumpAndReloadModel(model);
247
navigationRules = model.getRootComponent().getNavigationRules();
248         assertEquals("Number of navigation rules ", 1, navigationRules.size());
249         navigationRule = navigationRules.iterator().next();
250         navigationCases = navigationRule.getNavigationCases();
251         assertEquals("Number of navigation cases ", 4, navigationCases.size());
252         
253     }
254     
255     public void testJSFVersion() throws Exception JavaDoc{
256         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
257         assertEquals(JSFVersion.JSF_1_2, model.getVersion());
258         model = Util.loadRegistryModel("faces-config-01.xml");
259         assertEquals(JSFVersion.JSF_1_1, model.getVersion());
260     }
261     
262     public void testComments() throws Exception JavaDoc {
263         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
264         FacesConfig facesConfig = model.getRootComponent();
265         assertNotNull(facesConfig);
266         facesConfig.getPeer();
267         
268     }
269     
270     public void testEditable() throws Exception JavaDoc {
271         File JavaDoc file = new File JavaDoc(getDataDir(), "faces-config1.xml");
272         
273         FileObject fileObject = FileUtil.toFileObject(file);
274         ModelSource model = TestCatalogModel.getDefault().createModelSource(fileObject, false);
275         //JSFConfigModel jsfConfig1 = JSFConfigModelFactory.getInstance().getModel(model);
276
//assertFalse(jsfConfig1.getModelSource().isEditable());
277
assertFalse(model.isEditable());
278         ModelSource model2 = TestCatalogModel.getDefault().createModelSource(fileObject, true);
279         //JSFConfigModel jsfConfig2 = JSFConfigModelFactory.getInstance().getModel(model2);
280
//assertTrue("The model should be editable ", jsfConfig2.getModelSource().isEditable());
281
assertTrue(model2.isEditable());
282     }
283     
284     private void dumpModelToFile (JSFConfigModel model, String JavaDoc fileName) throws Exception JavaDoc{
285         File JavaDoc file = new File JavaDoc(getWorkDir(), fileName);
286         System.out.println("workfile: " + file.getAbsolutePath());
287         Util.dumpToFile(model, file);
288     }
289     
290     public void testConverter() throws Exception JavaDoc {
291         JSFConfigModel model = Util.loadRegistryModel("faces-config-jsfjpa-example.xml");
292         FacesConfig facesConfig = model.getRootComponent();
293         assertNotNull(facesConfig);
294         
295         List JavaDoc <Converter> converters = facesConfig.getConverters();
296         assertEquals("Number of converters ", 1, converters.size());
297         
298     }
299 }
300
Popular Tags