1 19 20 package org.netbeans.modules.j2ee.dd.api.web; 21 22 import org.netbeans.modules.j2ee.dd.api.web.*; 23 import org.netbeans.modules.j2ee.dd.api.common.InitParam; 24 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException; 25 import org.netbeans.modules.j2ee.dd.api.common.NameAlreadyUsedException; 26 import java.io.*; 27 import junit.framework.*; 28 import org.netbeans.junit.*; 29 import org.openide.filesystems.*; 30 import java.util.*; 31 32 33 public class DDApiTest extends NbTestCase { 34 private static final String VERSION="2.4"; 35 private static final int TIMEOUT=30; 36 private static final int WF_NUMBER=3; 37 private static final String SERVLET_NAME = "FordServlet"; 38 private static final String SERVLET_CLASS = "org.package.mypackage.CarServlet"; 39 private static final String SERVLET_NAME1 = "VolvoServlet"; 40 private static final String URL_PATTERN = "/ford"; 41 private static final String URL_PATTERN1 = "/volvo"; 42 private static final java.math.BigInteger LOAD_ON_STARTUP = java.math.BigInteger.valueOf(10); 43 private static final java.math.BigInteger LOAD_ON_STARTUP1 = java.math.BigInteger.valueOf(25); 44 private static final String PARAM1 = "car"; 45 private static final String VALUE1 = "Ford"; 46 private static final String VALUE11 = "Volvo"; 47 private static final String PARAM2 = "color"; 48 private static final String VALUE2 = "red"; 49 private static final String PARAM3 = "type"; 50 private static final String VALUE3 = "Puma"; 51 private static final String DESCRIPTION = "the color of the car"; 52 private static final String DESCRIPTION_EN = "the colour of the car"; 53 private static final String DESCRIPTION_DE = "die automobile farbe"; 54 private static final String DESCRIPTION_CZ = "barva automobilu"; 55 private static final String DESCRIPTION_SK = "farba automobilu"; 56 private static final String URL_PATTERN_JSP = "*.jsp"; 57 private static final String PRELUDE = "/jsp/prelude.jsp"; 58 private static final String CODA = "/jsp/coda.jsp"; 59 private static final String LARGE_ICON = "/img/icon32x32.gif"; 60 private static final String SMALL_ICON = "/img/icon16x16.gif"; 61 62 private WebApp webApp; 63 69 public DDApiTest(java.lang.String testName) { 70 super(testName); 71 } 72 73 public static void main(java.lang.String [] args) { 74 junit.textui.TestRunner.run(suite()); 75 } 76 77 public static Test suite() { 78 TestSuite suite = new NbTestSuite(DDApiTest.class); 79 80 return suite; 81 } 82 83 84 public void test_InitDataChecking () { 85 System.out.println("Init Data Checking"); 86 String version = webApp.getVersion(); 87 assertEquals("Incorrect servlet spec.version :",VERSION,version); 88 assertEquals("Incorrect number of servlets :",0,webApp.sizeServlet()); 89 assertEquals("Incorrect Session Timeout :",TIMEOUT,webApp.getSingleSessionConfig().getSessionTimeout().intValue()); 90 assertEquals("Incorrect number of welcome files : ",WF_NUMBER,webApp.getSingleWelcomeFileList().sizeWelcomeFile()); 91 } 92 93 public void test_Servlet() { 94 System.out.println("Testing servlet, servlet-mapping"); 95 try { 96 Servlet servlet = (Servlet) webApp.createBean("Servlet"); 97 servlet.setServletName(SERVLET_NAME); 98 servlet.setServletClass(SERVLET_CLASS); 99 servlet.setLoadOnStartup(LOAD_ON_STARTUP); 100 webApp.addServlet(servlet); 101 ServletMapping mapping = (ServletMapping) webApp.createBean("ServletMapping"); 102 mapping.setServletName(SERVLET_NAME); 103 mapping.setUrlPattern(URL_PATTERN); 104 webApp.addServletMapping(mapping); 105 webApp.write(fo); 106 } catch (ClassNotFoundException ex) { 107 throw new AssertionFailedErrorException("createBean() method failed",ex); 108 } catch (java.io.IOException ex) { 109 throw new AssertionFailedErrorException("write method failed",ex); 110 } 111 assertEquals("Incorrect number of servlets :",1,webApp.sizeServlet()); 112 Servlet s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME); 113 assertTrue("Servlet "+SERVLET_NAME+" not found", null != s); 114 assertEquals("Wrong Servlet Name :",SERVLET_NAME,s.getServletName()); 115 assertEquals("Wrong Servlet Class :",SERVLET_CLASS,s.getServletClass()); 116 assertEquals("Wrong load-on-startup :",LOAD_ON_STARTUP,s.getLoadOnStartup()); 117 118 } 119 120 public void test_InitParams() { 121 System.out.println("Testing init-params, context-params"); 122 Servlet s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME); 123 assertTrue("Servlet "+SERVLET_NAME+" not found", null != s); 124 try { 125 InitParam param = (InitParam) s.createBean("InitParam"); 126 param.setParamName(PARAM1); 127 param.setParamValue(VALUE1); 128 s.addInitParam(param); 129 InitParam clonnedParam1 = (InitParam)param.clone(); 130 param = (InitParam) s.createBean("InitParam"); 131 param.setParamName(PARAM2); 132 param.setParamValue(VALUE2); 133 s.addInitParam(param); 134 InitParam clonnedParam2 = (InitParam)param.clone(); 135 webApp.setContextParam(new InitParam[]{clonnedParam1, clonnedParam2}); 136 webApp.write(fo); 137 } catch (ClassNotFoundException ex) { 138 throw new AssertionFailedErrorException("createBean() method failed",ex); 139 } catch (java.io.IOException ex) { 140 throw new AssertionFailedErrorException("write method failed",ex); 141 } 142 s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME); 143 assertTrue("Servlet "+SERVLET_NAME+" not found", null != s); 144 assertEquals("Incorrect number of context-params :",2,webApp.sizeContextParam()); 145 assertEquals("Incorrect number of init-params in servlet:",2,s.sizeInitParam()); 146 InitParam[] params = webApp.getContextParam(); 148 assertEquals("Incorrect context-param name :",PARAM1,params[0].getParamName()); 149 assertEquals("Incorrect context-param name :",PARAM2,params[1].getParamName()); 150 assertEquals("Incorrect context-param value :",VALUE1,params[0].getParamValue()); 151 assertEquals("Incorrect context-param value :",VALUE2,params[1].getParamValue()); 152 assertEquals("Incorrect servlet's init-param name :",PARAM1,s.getInitParam(0).getParamName()); 154 assertEquals("Incorrect servlet's init-param name :",PARAM2,s.getInitParam(1).getParamName()); 155 assertEquals("Incorrect servlet's init-param value :",VALUE1,s.getInitParam(0).getParamValue()); 156 assertEquals("Incorrect servlet's init-param value :",VALUE2,s.getInitParam(1).getParamValue()); 157 InitParam p = (InitParam)s.findBeanByName("InitParam","ParamName",PARAM2); 159 assertTrue("InitParam "+PARAM2+" not found", null != p); 160 p = (InitParam)webApp.findBeanByName("InitParam","ParamName",PARAM1); 161 assertTrue("Context Param "+PARAM1+" not found", null != p); 162 } 163 164 public void test_VersionNotSupportedException() { 165 System.out.println("Testing VersionNotSupportedException for Taglibs in Servlet2.4"); 166 try { 167 Taglib[] taglibs = webApp.getTaglib(); 168 throw new AssertionFailedError("method getTaglib() shouldn't be supported in version:"+VERSION); 169 } catch (VersionNotSupportedException ex) { 170 System.out.println("Expected exception : "+ex); 171 } 172 try { 173 Taglib taglib = (Taglib) webApp.createBean("Taglib"); 174 taglib.setTaglibLocation("xxx"); 175 taglib.setTaglibUri("xxx"); 176 webApp.addTaglib(taglib); 177 throw new AssertionFailedError("method setTaglib (int i, Taglib taglib) shouldn't be supported in version:"+VERSION); 178 } catch (ClassNotFoundException ex) { 179 throw new AssertionFailedErrorException("createBean() method failed",ex); 180 } catch (VersionNotSupportedException ex) { 181 System.out.println("Expected exception : "+ex); 182 } 183 } 184 185 public void test_Description() { 186 System.out.println("Testing description, description for locales"); 187 Servlet s = (Servlet)webApp.findBeanByName("Servlet","ServletName",SERVLET_NAME); 188 assertTrue("Servlet "+SERVLET_NAME+" not found", null != s); 189 InitParam p = (InitParam)s.findBeanByName("InitParam","ParamName",PARAM2); 190 assertTrue("InitParam "+PARAM2+" not found", null != p); 191 p.setDescription(DESCRIPTION); 192 try { 193 p.setDescription("en",DESCRIPTION_EN); 194 p.setDescription("de",DESCRIPTION); 195 p.setDescription("cz",DESCRIPTION_CZ); 196 p.setDescription("sk",DESCRIPTION_SK); 197 p.setDescription("de",DESCRIPTION_DE); } catch (VersionNotSupportedException ex) { 199 throw new AssertionFailedErrorException("setDescription() method failed",ex); 200 } 201 java.util.Map map = p.getAllDescriptions(); 202 assertEquals("Incorrect size of description :",5,map.size()); 203 assertEquals("Incorrect default description :",DESCRIPTION,map.get(null)); 204 assertEquals("Incorrect english description :",DESCRIPTION_EN,map.get("en")); 205 assertEquals("Incorrect german description :",DESCRIPTION_DE,map.get("de")); 206 assertEquals("Incorrect czech description :",DESCRIPTION_CZ,map.get("cz")); 207 assertEquals("Incorrect slovak description :",DESCRIPTION_SK,map.get("sk")); 208 try { 209 p.removeDescriptionForLocale("de"); 210 } catch (VersionNotSupportedException ex) { 211 throw new AssertionFailedErrorException("removeDescription() method failed",ex); 212 } 213 assertEquals("Incorrect size of description :",4,p.getAllDescriptions().size()); 214 assertEquals("Incorrect default description :",DESCRIPTION,p.getDefaultDescription()); 215 try { 216 assertEquals("Incorrect default description :",DESCRIPTION,p.getDescription(null)); 217 assertEquals("Incorrect english description :",DESCRIPTION_EN,p.getDescription("en")); 218 assertEquals("German description was removed :",null,p.getDescription("de")); 219 assertEquals("Incorrect czech description :",DESCRIPTION_CZ,p.getDescription("cz")); 220 assertEquals("Incorrect slovak description :",DESCRIPTION_SK,p.getDescription("sk")); 221 } catch (VersionNotSupportedException ex) { 222 throw new AssertionFailedErrorException("getDescription(String locale) method failed",ex); 223 } 224 try { 225 webApp.write(fo); 226 } catch (java.io.IOException ex) { 227 throw new AssertionFailedErrorException("write method failed",ex); 228 } 229 } 230 231 public void test_addBean() { 232 System.out.println("Testing addBean method"); 233 try { 234 InitParam context = (InitParam)webApp.addBean("InitParam",null,null, null); 235 context.setParamName(PARAM3); 236 context.setParamValue(VALUE3); 237 JspConfig jspConfig = (JspConfig)webApp.addBean("JspConfig", null, null, null); 238 jspConfig.addBean("JspPropertyGroup",new String []{"UrlPattern","IncludePrelude","IncludeCoda"}, 239 new String []{URL_PATTERN_JSP,PRELUDE,CODA},null); 240 webApp.addBean("Icon",new String []{"LargeIcon","SmallIcon"},new String []{LARGE_ICON,SMALL_ICON},null); 241 } catch (Exception ex){ 242 throw new AssertionFailedErrorException("addBean() method failed for ContextParam,JspConfig or Icon",ex); 243 } 244 try { 246 Servlet servlet = (Servlet)webApp.addBean("Servlet", new String []{"ServletName","ServletClass","LoadOnStartup"}, 247 new Object []{SERVLET_NAME1,SERVLET_CLASS,LOAD_ON_STARTUP1}, "ServletName"); 248 servlet.addBean("InitParam", new String []{"ParamName","ParamValue"}, new String []{PARAM1,VALUE11},null); 249 webApp.addBean("ServletMapping", new String []{"ServletName","UrlPattern"},new String []{SERVLET_NAME1,URL_PATTERN1},"UrlPattern"); 250 } catch (Exception ex){ 251 new AssertionFailedErrorException("addBean() method failed for Servlet",ex); 252 } 253 try { 255 Servlet servlet = (Servlet)webApp.addBean("Servlet", new String []{"ServletName","ServletClass"}, 256 new Object []{SERVLET_NAME1,SERVLET_CLASS}, "ServletName"); 257 throw new AssertionFailedError("Servlet shouldn't have been added because of the same name"); 258 } catch (NameAlreadyUsedException ex){ 259 System.out.println("Expected exception : "+ex); 260 } catch (ClassNotFoundException ex) { 261 new AssertionFailedErrorException("addBean() method failed for Servlet",ex); 262 } 263 try { 264 webApp.write(fo); 265 } catch (java.io.IOException ex) { 266 throw new AssertionFailedErrorException("write method failed",ex); 267 } 268 } 269 270 public void test_Result() { 271 System.out.println("Comparing result with golden file"); 272 273 String testDataDirS = System.getProperty("test.data.dir"); 274 java.io.File pass = new File(getDataDir(),"/web.pass"); 275 File test = FileUtil.toFile(fo); 276 try { 277 BufferedReader reader1 = new BufferedReader(new FileReader(test)); 278 BufferedReader reader2 = new BufferedReader(new FileReader(pass)); 279 String line1=null; 280 Set set1 = new HashSet(); 281 Set set2 = new HashSet(); 282 while((line1=reader1.readLine())!=null) { 283 line1 = line1.trim(); 284 String line2 = reader2.readLine(); 285 if (line2==null) { 286 assertFile("Result different than golden file", pass, test, test.getParentFile()); 287 } 288 line2=line2.trim(); 289 if (line1.startsWith("<description")) { 291 set1.add(line1); 292 set2.add(line2); 293 } else if (!line1.equals(line2)) { 294 assertFile("Result different than golden file", pass, test, test.getParentFile()); 295 } 296 } 297 reader1.close();reader2.close(); 298 if (!set1.equals(set2)) { 299 assertFile("Problem with descriotion elements", pass, test, test.getParentFile()); 300 } 301 } catch (IOException ex) { 302 throw new AssertionFailedErrorException("Comparing to golden file failed",ex); 303 } 304 } 305 306 private static DDProvider ddProvider; 307 private static FileObject fo; 308 private static boolean initialized; 309 310 protected void setUp() throws Exception { 311 super.setUp(); 312 System.out.println("setUp() ......................."); 313 314 if (ddProvider==null) ddProvider = DDProvider.getDefault(); 315 assertTrue("DDProvider object not found",null != ddProvider); 316 317 FileObject dataFolder = FileUtil.toFileObject(getDataDir()); 318 319 if (!initialized){ 320 FileObject old = dataFolder.getFileObject("web", "xml"); 321 if (old != null){ 322 old.delete(); 323 } 324 initialized = true; 325 } 326 327 if (fo==null) { 328 fo = FileUtil.copyFile(dataFolder.getFileObject("web_org","xml"), dataFolder, "web"); 329 } 330 331 332 assertTrue("FileObject web.xml not found",null != fo); 333 334 try { 335 webApp = ddProvider.getDDRoot(fo); 336 } catch (Exception ex) { 337 ex.printStackTrace(); 338 } 339 assertTrue("WebApp object not found", null != webApp); 340 341 } 342 } 343 | Popular Tags |