001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.web.jspparser;
041:
042: import java.io.File;
043: import java.lang.ref.Reference;
044: import java.lang.ref.WeakReference;
045: import java.util.Collection;
046: import java.util.Iterator;
047: import java.util.Map;
048: import javax.servlet.jsp.tagext.TagLibraryInfo;
049: import org.netbeans.junit.NbTestCase;
050: import org.netbeans.modules.web.api.webmodule.WebModule;
051: import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
052: import org.netbeans.modules.web.jsps.parserapi.JspParserAPI.ParseResult;
053: import org.netbeans.modules.web.jsps.parserapi.JspParserFactory;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056:
057: /**
058: * Various test cases for jsp parser cache.
059: * @author Tomas Mysik
060: */
061: public class CacheTest extends NbTestCase {
062:
063: public CacheTest(String name) {
064: super (name);
065: }
066:
067: @Override
068: protected void setUp() throws Exception {
069: super .setUp();
070: TestUtil.setup(this );
071: }
072:
073: public void testJspParserImpl() throws Exception {
074: JspParserAPI jspParser = JspParserFactory.getJspParser();
075: assertTrue(jspParser instanceof JspParserImpl);
076: }
077:
078: public void testCachedWebModules() throws Exception {
079: JspParserImpl jspParser = getJspParser();
080:
081: FileObject jspFo1 = TestUtil.getProjectFile(this , "project2",
082: "/web/basic.jspx");
083: WebModule webModule1 = TestUtil.getWebModule(jspFo1);
084: jspParser.analyzePage(jspFo1, webModule1,
085: JspParserAPI.ERROR_IGNORE);
086:
087: FileObject jspFo2 = TestUtil.getProjectFile(this , "project2",
088: "/web/main.jsp");
089: WebModule webModule2 = TestUtil.getWebModule(jspFo2);
090: jspParser.analyzePage(jspFo1, webModule2,
091: JspParserAPI.ERROR_IGNORE);
092:
093: assertTrue("Only 1 web module should be cached",
094: jspParser.parseSupports.size() == 1);
095: }
096:
097: public void testGCedWebModules() throws Exception {
098: JspParserImpl jspParser = getJspParser();
099:
100: FileObject jspFo = TestUtil.getProjectFile(this , "project2",
101: "/web/basic.jspx");
102: WebModule webModule = TestUtil.createWebModule(jspFo
103: .getParent());
104: jspParser.analyzePage(jspFo, webModule,
105: JspParserAPI.ERROR_IGNORE);
106:
107: Reference<WebAppParseProxy> proxy = new WeakReference<WebAppParseProxy>(
108: jspParser.parseSupports.get(webModule));
109: assertNotNull("WebModule should be cached", proxy.get());
110:
111: Reference<WebModule> wmRef = new WeakReference<WebModule>(
112: webModule);
113: webModule = null;
114: assertGC("web module should be garbage collected", wmRef);
115: jspParser.parseSupports.size();
116: assertGC("parse proxy should be garbage collected", proxy);
117: }
118:
119: public void testCachedTagLibMaps() throws Exception {
120: JspParserImpl jspParser = getJspParser();
121:
122: FileObject jspFo = TestUtil.getProjectFile(this ,
123: "emptyWebProject", "/web/index.jsp");
124: WebModule webModule = TestUtil.getWebModule(jspFo);
125:
126: Map<String, String[]> taglibMap1 = jspParser
127: .getTaglibMap(webModule);
128: Map<String, String[]> taglibMap2 = jspParser
129: .getTaglibMap(webModule);
130:
131: String url1 = taglibMap1.get("/TestTagLibrary")[0];
132: String url2 = taglibMap2.get("/TestTagLibrary")[0];
133: assertNotNull(url1);
134: assertNotNull(url2);
135: assertSame("TagLibMaps should be exactly the same", url1, url2);
136: }
137:
138: // disabled because this functionality is not implemented
139: public void xxxtestCachedTagLibInfos() throws Exception {
140: JspParserImpl jspParser = getJspParser();
141:
142: FileObject jspFo = TestUtil.getProjectFile(this , "project2",
143: "/web/basic.jspx");
144: WebModule webModule = TestUtil.getWebModule(jspFo);
145:
146: ParseResult result = jspParser.analyzePage(jspFo, webModule,
147: JspParserAPI.ERROR_IGNORE);
148: Collection<TagLibraryInfo> tagLibs1 = result.getPageInfo()
149: .getTaglibs();
150:
151: jspFo = TestUtil.getProjectFile(this , "project2",
152: "/web/basic.jspx");
153: result = jspParser.analyzePage(jspFo, webModule,
154: JspParserAPI.ERROR_IGNORE);
155: Collection<TagLibraryInfo> tagLibs2 = result.getPageInfo()
156: .getTaglibs();
157:
158: assertTrue(tagLibs1.size() > 0);
159: assertTrue(tagLibs2.size() > 0);
160: assertTrue(tagLibs1.size() == tagLibs2.size());
161:
162: Iterator<TagLibraryInfo> iter1 = tagLibs1.iterator();
163: Iterator<TagLibraryInfo> iter2 = tagLibs2.iterator();
164: while (iter1.hasNext()) {
165: TagLibraryInfo tagLibraryInfo1 = iter1.next();
166: TagLibraryInfo tagLibraryInfo2 = iter2.next();
167: assertNotNull(tagLibraryInfo1);
168: assertNotNull(tagLibraryInfo2);
169: assertTrue("TagLibInfos should be exactly the same",
170: tagLibraryInfo1 == tagLibraryInfo2);
171: }
172: }
173:
174: public void testChangedTldFile() throws Exception {
175: JspParserImpl jspParser = getJspParser();
176:
177: FileObject jspFo = TestUtil.getProjectFile(this ,
178: "emptyWebProject", "/web/index.jsp");
179: WebModule webModule = TestUtil.getWebModule(jspFo);
180:
181: Map<String, String[]> taglibMap1 = jspParser
182: .getTaglibMap(webModule);
183:
184: // touch file
185: touchFile("emptyWebProject", "/web/WEB-INF/c.tld");
186:
187: Map<String, String[]> taglibMap2 = jspParser
188: .getTaglibMap(webModule);
189:
190: String url1 = taglibMap1.get("http://java.sun.com/jstl/core")[0];
191: String url2 = taglibMap2.get("http://java.sun.com/jstl/core")[0];
192: assertNotNull(url1);
193: assertNotNull(url2);
194: assertNotSame("TagLibMaps should not be exactly the same",
195: url1, url2);
196: assertEquals("TagLibMaps should be equal", url1, url2);
197: }
198:
199: public void testAddedTldFile() throws Exception {
200: JspParserImpl jspParser = getJspParser();
201:
202: FileObject jspFo = TestUtil.getProjectFile(this ,
203: "emptyWebProject", "/web/index.jsp");
204: WebModule webModule = TestUtil.getWebModule(jspFo);
205:
206: Map<String, String[]> taglibMap1 = jspParser
207: .getTaglibMap(webModule);
208:
209: String[] url = taglibMap1.get("http://java.sun.com/jstl/xml");
210: assertNull("Url should not be found", url);
211:
212: // add file
213: addXmlTld();
214:
215: Map<String, String[]> taglibMap2 = jspParser
216: .getTaglibMap(webModule);
217:
218: url = taglibMap2.get("http://java.sun.com/jstl/xml");
219: assertNotNull("Url should be found", url);
220:
221: String url1 = taglibMap1.get("http://java.sun.com/jstl/core")[0];
222: String url2 = taglibMap2.get("http://java.sun.com/jstl/core")[0];
223: assertNotNull(url1);
224: assertNotNull(url2);
225: assertNotSame("TagLibMaps should not be exactly the same",
226: url1, url2);
227: assertEquals("TagLibMaps should be equal", url1, url2);
228:
229: // cleanup
230: jspParser = null;
231: removeXmlTld();
232: }
233:
234: public void testRemovedTldFile() throws Exception {
235: // add file to have possibility to remove it
236: addXmlTld();
237:
238: JspParserImpl jspParser = getJspParser();
239:
240: FileObject jspFo = TestUtil.getProjectFile(this ,
241: "emptyWebProject", "/web/index.jsp");
242: WebModule webModule = TestUtil.getWebModule(jspFo);
243:
244: Map<String, String[]> taglibMap1 = jspParser
245: .getTaglibMap(webModule);
246:
247: String[] url = taglibMap1.get("http://java.sun.com/jstl/xml");
248: assertNotNull("Url should be found", url);
249:
250: // touch file
251: removeXmlTld();
252:
253: Map<String, String[]> taglibMap2 = jspParser
254: .getTaglibMap(webModule);
255:
256: url = taglibMap2.get("http://java.sun.com/jstl/xml");
257: assertNull("Url should not be found", url);
258:
259: String url1 = taglibMap1.get("/TestTagLibrary")[0];
260: String url2 = taglibMap2.get("/TestTagLibrary")[0];
261: assertNotNull(url1);
262: assertNotNull(url2);
263: assertNotSame("TagLibMaps should not be exactly the same",
264: url1, url2);
265: assertEquals("TagLibMaps should be equal", url1, url2);
266: }
267:
268: public void testChangedWebXml() throws Exception {
269: JspParserImpl jspParser = getJspParser();
270:
271: FileObject jspFo = TestUtil.getProjectFile(this ,
272: "emptyWebProject", "/web/index.jsp");
273: WebModule webModule = TestUtil.getWebModule(jspFo);
274:
275: Map<String, String[]> taglibMap1 = jspParser
276: .getTaglibMap(webModule);
277:
278: // touch file
279: touchFile("emptyWebProject", "/web/WEB-INF/web.xml");
280:
281: Map<String, String[]> taglibMap2 = jspParser
282: .getTaglibMap(webModule);
283:
284: String url1 = taglibMap1.get("http://java.sun.com/jstl/core")[0];
285: String url2 = taglibMap2.get("http://java.sun.com/jstl/core")[0];
286: assertNotNull(url1);
287: assertNotNull(url2);
288: assertNotSame("TagLibMaps should not be exactly the same",
289: url1, url2);
290: assertEquals("TagLibMaps should be equal", url1, url2);
291: }
292:
293: private static JspParserImpl getJspParser() {
294: return (JspParserImpl) JspParserFactory.getJspParser();
295: }
296:
297: private void addXmlTld() throws Exception {
298: FileObject xml = TestUtil.getProjectFile(this , "project2",
299: "/web/WEB-INF/META-INF/x.tld");
300: FileObject destDir = TestUtil.getProjectFile(this ,
301: "emptyWebProject", "/web/WEB-INF/");
302: xml.copy(destDir, xml.getName(), xml.getExt());
303: xml = TestUtil.getProjectFile(this , "emptyWebProject",
304: "/web/WEB-INF/x.tld");
305: }
306:
307: private void removeXmlTld() throws Exception {
308: removeFile("emptyWebProject", "/web/WEB-INF/x.tld");
309: }
310:
311: private void touchFile(String projectName, String projectFile)
312: throws Exception {
313: FileObject fmtFo = TestUtil.getProjectFile(this , projectName,
314: projectFile);
315: assertNotNull(fmtFo);
316: File fmt = FileUtil.toFile(fmtFo);
317: assertTrue("Changing timestamp should succeed", fmt
318: .setLastModified(System.currentTimeMillis() + 1000));
319: FileUtil.refreshFor(fmt);
320: }
321:
322: private void removeFile(String projectName, String projectFile)
323: throws Exception {
324: FileObject fmtFo = TestUtil.getProjectFile(this, projectName,
325: projectFile);
326: assertNotNull(fmtFo);
327: fmtFo.delete();
328: }
329: }
|