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