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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.javadoc.search;
043:
044: import java.io.BufferedInputStream;
045: import java.io.ByteArrayInputStream;
046: import java.io.File;
047: import java.io.InputStream;
048: import java.io.Reader;
049: import org.netbeans.junit.NbTestCase;
050: import org.netbeans.modules.javadoc.search.IndexBuilder.SimpleTitleParser;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.LocalFileSystem;
053:
054: /**
055: *
056: * @author Jan Pokorsky
057: */
058: public class IndexBuilderTest extends NbTestCase {
059:
060: private LocalFileSystem fs;
061: private static final String JDK14_INDEX_PATH = "docs_jdk14/api/index-files";
062: private static final String JDK14_JA_INDEX_PATH = "docs_jdk14_ja/api/index-files";
063: private static final String JDK15_INDEX_PATH = "docs_jdk15/api/index-files";
064: private static final String JDK15_JA_INDEX_PATH = "docs_jdk15_ja/api/index-files";
065:
066: /** Creates a new instance of IndexBuilderTest */
067: public IndexBuilderTest(String testName) {
068: super (testName);
069: }
070:
071: protected void setUp() throws Exception {
072: File dataFile = getDataDir();
073: assertNotNull("missing data file", dataFile);
074: fs = new LocalFileSystem();
075: fs.setRootDirectory(dataFile);
076: }
077:
078: public void testTitleInJDK14() throws Exception {
079: FileObject html = fs.findResource(JDK14_INDEX_PATH
080: + "/index-4.html");
081:
082: InputStream is = new BufferedInputStream(html.getInputStream(),
083: 1024);
084: SimpleTitleParser tp = new SimpleTitleParser(is);
085: try {
086: tp.parse();
087: String titlestr = tp.getTitle();
088: assertEquals("wrong title",
089: "D-Index (Java 2 Platform SE v1.4.2)", titlestr);
090: } finally {
091: is.close();
092: }
093: }
094:
095: public void testTitleInJDK15() throws Exception {
096: FileObject html = fs.findResource(JDK15_INDEX_PATH
097: + "/index-4.html");
098:
099: InputStream is = new BufferedInputStream(html.getInputStream(),
100: 1024);
101: SimpleTitleParser tp = new SimpleTitleParser(is);
102: try {
103: tp.parse();
104: String titlestr = tp.getTitle();
105: assertEquals("wrong title",
106: "D-Index (Java 2 Platform SE 5.0)", titlestr);
107: } finally {
108: is.close();
109: }
110: }
111:
112: public void testEmptyTitle() throws Exception {
113: String content = "<HTML><HEAD><TITLE></TITLE></HEAD></HTML>";
114: InputStream is = new ByteArrayInputStream(content.getBytes());
115: SimpleTitleParser tp = new SimpleTitleParser(is);
116: try {
117: tp.parse();
118: String titlestr = tp.getTitle();
119: assertEquals("wrong title", "", titlestr);
120: } finally {
121: is.close();
122: }
123: }
124:
125: public void testMissingTitle() throws Exception {
126: String content = "<HTML><HEAD></HEAD></HTML>";
127: InputStream is = new ByteArrayInputStream(content.getBytes());
128: SimpleTitleParser tp = new SimpleTitleParser(is);
129: try {
130: tp.parse();
131: String titlestr = tp.getTitle();
132: assertNull("wrong title", titlestr);
133: } finally {
134: is.close();
135: }
136: }
137:
138: public void testTitleInJDK14_ja() throws Exception {
139: FileObject html = fs.findResource(JDK14_JA_INDEX_PATH
140: + "/index-4.html");
141: FileObject html2 = fs.findResource(JDK14_JA_INDEX_PATH
142: + "/index-4.title");
143: japanaseIndexes(html, html2, "iso-2022-jp");
144: }
145:
146: public void testTitleInJDK15_ja() throws Exception {
147: FileObject html = fs.findResource(JDK15_JA_INDEX_PATH
148: + "/index-4.html");
149: FileObject html2 = fs.findResource(JDK15_JA_INDEX_PATH
150: + "/index-4.title");
151: japanaseIndexes(html, html2, "euc-jp");
152: }
153:
154: private void japanaseIndexes(FileObject html, FileObject title,
155: String charset) throws Exception {
156: assertNotNull(html);
157: assertNotNull(title);
158:
159: Reader r = new java.io.InputStreamReader(
160: title.getInputStream(), charset);
161:
162: int ic;
163: StringBuilder sb = new StringBuilder();
164: try {
165: while ((ic = r.read()) != -1) {
166: sb.append((char) ic);
167: }
168: } finally {
169: r.close();
170: }
171:
172: InputStream is = new BufferedInputStream(html.getInputStream(),
173: 1024);
174: SimpleTitleParser tp = new SimpleTitleParser(is);
175: try {
176: tp.parse();
177: String titlestr = tp.getTitle();
178: assertEquals("wrong title", sb.toString(), titlestr);
179: } finally {
180: is.close();
181: }
182: }
183: }
|