0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Evgeniya G. Maenkova
0019: * @version $Revision$
0020: */package javax.swing;
0021:
0022: import java.awt.Container;
0023: import java.awt.Dimension;
0024: import java.awt.GridLayout;
0025: import java.beans.PropertyChangeEvent;
0026: import java.beans.PropertyChangeListener;
0027: import java.io.ByteArrayInputStream;
0028: import java.io.IOException;
0029: import java.net.MalformedURLException;
0030: import java.net.URL;
0031: import java.util.ArrayList;
0032: import java.util.HashMap;
0033: import java.util.Map;
0034: import javax.accessibility.AccessibleContext;
0035: import javax.swing.event.HyperlinkEvent;
0036: import javax.swing.event.HyperlinkListener;
0037: import javax.swing.text.AbstractDocument;
0038: import javax.swing.text.AttributeSet;
0039: import javax.swing.text.BadLocationException;
0040: import javax.swing.text.DefaultEditorKit;
0041: import javax.swing.text.DefaultStyledDocument;
0042: import javax.swing.text.Document;
0043: import javax.swing.text.EditorKit;
0044: import javax.swing.text.Element;
0045: import javax.swing.text.SimpleAttributeSet;
0046: import javax.swing.text.StyledEditorKit;
0047:
0048: //import javax.swing.text.html.HTMLEditorKit;
0049: //import javax.swing.text.rtf.RTFEditorKit;
0050: public class JEditorPaneTest extends SwingTestCase {
0051: JEditorPane jep;
0052:
0053: JEditorPane jep1;
0054:
0055: JFrame jf;
0056:
0057: String fireOrder;
0058:
0059: static final String plainString = "justPlain";
0060:
0061: static final String rtfString = "{\\rtf1\\ansi\\ansicpg1251\\deff0\\deflang1049{\\fonttbl{\\fswiss\\fcharset0 Arial;}}\n"
0062: + "{\\*\\generator Msftedit 5.41.15.1503;}\\viewkind4\\uc1\\pard\\lang1033\\f0\\fs20 blablabla\\par\n"
0063: + "}\n";
0064:
0065: static final String htmlString = "<html>"
0066: + " <head>"
0067: + " <meta http-equiv=\"Content-Typecontent=text/html;\" charset=\"iso-8859-1\">"
0068: + " <meta content=\"MSHTML5.50.4522.1800\" name=\"GENERATOR\">"
0069: + " "
0070: + " </head>"
0071: + " <body>"
0072: + " <div bgcolor=\"#ffffff\">"
0073: + " <div>"
0074: + " <font size=\"4\">Thank you for the quote you sentreguarding account"
0075: + " #99999999999999. &160;I just have a couple of questions.Please let me"
0076: + " know. </font>"
0077: + " </div>"
0078: + " <div>"
0079: + " <font size=\"4\">    Thank you, we look forward to hearing from you.</font>"
0080: + " </div>" + " </div>" + " </body>" + "</html>";
0081:
0082: final URL TEST_URL;
0083: {
0084: URL tmpUrl = null;
0085: try {
0086: tmpUrl = new URL("http://www.apache.org/");
0087: } catch (final MalformedURLException e) {
0088: assertFalse("unexpected exception :" + e.getMessage(), true);
0089: }
0090: TEST_URL = tmpUrl;
0091: }
0092:
0093: @Override
0094: protected void setUp() throws Exception {
0095: super .setUp();
0096: timeoutDelay = 10 * DEFAULT_TIMEOUT_DELAY;
0097: setIgnoreNotImplemented(true);
0098: fireOrder = "";
0099: jep = new JEditorPane();
0100: jep1 = new JEditorPane();
0101: jf = new JFrame();
0102: Container c = jf.getContentPane();
0103: c.setLayout(new GridLayout(2, 1, 1, 1));
0104: c.add(new JScrollPane(jep1));
0105: c.add(jep);
0106: jf.setSize(200, 300);
0107: }
0108:
0109: @Override
0110: protected void tearDown() throws Exception {
0111: jf.dispose();
0112: super .tearDown();
0113: }
0114:
0115: final class SimpleHyperlinkListener implements HyperlinkListener {
0116: String name;
0117:
0118: HyperlinkEvent event;
0119:
0120: public SimpleHyperlinkListener(final String s) {
0121: name = s;
0122: }
0123:
0124: public void hyperlinkUpdate(final HyperlinkEvent he) {
0125: event = he;
0126: fireOrder += name;
0127: }
0128: }
0129:
0130: static final class SimpleEditorKit extends DefaultEditorKit {
0131: private static final long serialVersionUID = 1L;
0132:
0133: boolean wasCallInstall = false;
0134:
0135: boolean wasCallDeinstall = false;
0136:
0137: @Override
0138: public void deinstall(final JEditorPane jep) {
0139: wasCallDeinstall = true;
0140: super .deinstall(jep);
0141: }
0142:
0143: @Override
0144: public void install(final JEditorPane jep) {
0145: wasCallInstall = true;
0146: super .install(jep);
0147: }
0148:
0149: final void resetDbgInfo() {
0150: wasCallInstall = false;
0151: wasCallDeinstall = false;
0152: }
0153: }
0154:
0155: final class SimplePropertyChangeListener implements
0156: PropertyChangeListener {
0157: PropertyChangeEvent event;
0158:
0159: public void propertyChange(final PropertyChangeEvent pce) {
0160: //System.out.println(pce.getPropertyName());
0161: event = pce;
0162: }
0163:
0164: final void resetDbgInfo() {
0165: event = null;
0166: }
0167: }
0168:
0169: final class SimpleClassLoader extends ClassLoader {
0170: boolean wasCallLoadClass = false;
0171:
0172: @Override
0173: public Class<?> loadClass(final String s)
0174: throws ClassNotFoundException {
0175: wasCallLoadClass = true;
0176: return super .loadClass(s);
0177: }
0178:
0179: @Override
0180: protected synchronized Class<?> loadClass(final String s,
0181: final boolean b) throws ClassNotFoundException {
0182: wasCallLoadClass = true;
0183: return super .loadClass(s, b);
0184: }
0185: }
0186:
0187: private void assertEquals(final PropertyChangeEvent event1,
0188: final PropertyChangeEvent event2) {
0189: assertNotNull(event1);
0190: assertNotNull(event2);
0191: assertEquals(event1.getPropertyName(), event2.getPropertyName());
0192: assertEquals(event1.getOldValue(), event2.getOldValue());
0193: assertEquals(event1.getNewValue(), event2.getNewValue());
0194: assertEquals(event1.getSource(), event2.getSource());
0195: }
0196:
0197: public void testGetAccessibleContext() {
0198: if (true) {
0199: throw new UnsupportedOperationException("Not implemented");
0200: }
0201: if (isHarmony()) {
0202: AccessibleContext accessibleContext = jep
0203: .getAccessibleContext();
0204: assertEquals(
0205: "javax.swing.JEditorPane$AccessibleJEditorPane",
0206: getClassName(accessibleContext));
0207: assertEquals(jep.getAccessibleContext(), accessibleContext);
0208: jep.setContentType("text/html");
0209: assertEquals(
0210: "javax.swing.JEditorPane$AccessibleJEditorPaneHTML",
0211: getClassName(jep.getAccessibleContext()));
0212: }
0213: }
0214:
0215: public void testGetPreferredSize() {
0216: if (isHarmony()) {
0217: jep1.setMinimumSize(new Dimension(3, 3));
0218: JViewport parent = (JViewport) jep1.getParent();
0219: Dimension minSize = jep1.getMinimumSize();
0220: parent.setSize(new Dimension(minSize.width - 1,
0221: minSize.height - 1));
0222: assertEquals(minSize, jep1.getPreferredSize());
0223: }
0224: }
0225:
0226: public void testGetUIClassID() {
0227: assertEquals("EditorPaneUI", jep.getUIClassID());
0228: }
0229:
0230: public void testGetScrollableTracksViewportWidthHeight() {
0231: JViewport parent = (JViewport) jep1.getParent();
0232: jep1.setMinimumSize(new Dimension(3, 3));
0233: Dimension minSize = jep1.getMinimumSize();
0234: parent.setSize(new Dimension(minSize.width - 1,
0235: minSize.height - 1));
0236: assertFalse(jep1.getScrollableTracksViewportHeight());
0237: assertFalse(jep1.getScrollableTracksViewportWidth());
0238: }
0239:
0240: private AttributeSet getAttributeSetByIndex(
0241: final AbstractDocument d, final int offset) {
0242: AttributeSet as = null;
0243: Element elem = d.getDefaultRootElement();
0244: while (elem.getElementCount() > 0) {
0245: elem = elem.getElement(elem.getElementIndex(offset));
0246: as = elem.getAttributes();
0247: }
0248: return as;
0249: }
0250:
0251: public void testReplaceSelection() {
0252: jep.setText("testReplaceSelection");
0253: jep.setSelectionStart(4);
0254: jep.setSelectionEnd(7);
0255: jep.replaceSelection("XXX");
0256: assertEquals("testXXXlaceSelection", jep.getText());
0257: assertNull(jep.getSelectedText());
0258: jep.setSelectionStart(2);
0259: jep.setSelectionEnd(4);
0260: jep.replaceSelection(null);
0261: assertEquals("teXXXlaceSelection", jep.getText());
0262: assertNull(jep.getSelectedText());
0263: jep.setSelectionStart(0);
0264: jep.setSelectionEnd(2);
0265: jep.replaceSelection(null);
0266: assertEquals("XXXlaceSelection", jep.getText());
0267: assertNull(jep.getSelectedText());
0268: jep.setCaretPosition(3);
0269: jep.replaceSelection("YYY");
0270: assertEquals("XXXYYYlaceSelection", jep.getText());
0271: assertNull(jep.getSelectedText());
0272: }
0273:
0274: public void testReplaceSelectionNotEditable() {
0275: jep.setText("replaceSelectionNotEditable");
0276: jep.setEditable(false);
0277: jep.setSelectionStart(3);
0278: jep.setSelectionEnd(5);
0279: jep.replaceSelection("YYY");
0280: assertEquals("la", jep.getSelectedText());
0281: assertEquals("replaceSelectionNotEditable", jep.getText());
0282: }
0283:
0284: public void testReplaceSelectionWithAttributes() {
0285: if (true) {
0286: throw new UnsupportedOperationException("Not implemented");
0287: }
0288: AbstractDocument doc = new DefaultStyledDocument();
0289: SimpleAttributeSet as1 = new SimpleAttributeSet();
0290: as1.addAttribute("key1", "value1");
0291: SimpleAttributeSet as2 = new SimpleAttributeSet();
0292: as2.addAttribute("key2", "value2");
0293: try {
0294: doc.insertString(0, "testReplaceSelection", as1);
0295: doc.insertString(4, "INSERT", as2);
0296: } catch (final BadLocationException e) {
0297: assertFalse("unexpected exception :" + e.getMessage(), true);
0298: }
0299: //temporarily commented-out: HTMLEditorKit not implemented
0300: //jep.setEditorKit(new RTFEditorKit());
0301: jep.setEditorKit(new StyledEditorKit());
0302: jep.setDocument(doc);
0303: jep.setSelectionStart(6);
0304: jep.setSelectionEnd(7);
0305: jep.replaceSelection("YYY");
0306: for (int i = 0; i < doc.getLength(); i++) {
0307: AttributeSet as = getAttributeSetByIndex(doc, i);
0308: if (i > 3 && i < 12) {
0309: assertEquals(as2, as);
0310: } else {
0311: assertEquals(as1, as);
0312: }
0313: }
0314: }
0315:
0316: static final String getDocContent(final Document doc) {
0317: String content = null;
0318: try {
0319: content = doc.getText(0, doc.getLength());
0320: } catch (BadLocationException e) {
0321: }
0322: return content;
0323: }
0324:
0325: public void testSetGetText() {
0326: if (true) {
0327: throw new UnsupportedOperationException("Not implemented");
0328: }
0329: jep.setText(plainString);
0330: assertEquals("plain", plainString, jep.getText());
0331: jep.setContentType("text/rtf");
0332: jep.setText(rtfString);
0333: assertEquals("blablabla\n", getDocContent(jep.getDocument()));
0334: jep.setContentType("text/html");
0335: jep.setText(htmlString);
0336: assertEquals(removeMeta(htmlString), removeMeta(jep.getText()
0337: .replaceAll("\n", "")));
0338:
0339: JEditorPane e = new JEditorPane();
0340:
0341: try {
0342: e.setText((String) null);
0343: } catch (NullPointerException npe) {
0344: fail("NullPointerException thrown");
0345: }
0346: }
0347:
0348: // commented due to unimplemented functionality
0349: /*public void testCreateEditorKitForContentType() {
0350: String content = "testCreateEditorKitForContentType";
0351: assertNull(JEditorPane.createEditorKitForContentType(content));
0352: JEditorPane
0353: .registerEditorKitForContentType(content,
0354: "javax.swing.text.DefaultEditorKit");
0355: EditorKit rtfKit1 = JEditorPane
0356: .createEditorKitForContentType("text/rtf");
0357: EditorKit rtfKit2 = JEditorPane
0358: .createEditorKitForContentType("text/rtf");
0359: EditorKit htmlKit1 = JEditorPane
0360: .createEditorKitForContentType("text/html");
0361: EditorKit htmlKit2 = JEditorPane
0362: .createEditorKitForContentType("text/html");
0363: EditorKit plainKit1 = JEditorPane
0364: .createEditorKitForContentType("text/plain");
0365:
0366: EditorKit plainKit2 = JEditorPane
0367: .createEditorKitForContentType("text/plain");
0368: EditorKit contentKit1 = JEditorPane
0369: .createEditorKitForContentType(content);
0370: EditorKit contentKit2 = JEditorPane
0371: .createEditorKitForContentType(content);
0372:
0373: assertEquals("javax.swing.text.rtf.RTFEditorKit", getClassName(rtfKit1));
0374: assertEquals("javax.swing.text.html.HTMLEditorKit",
0375: getClassName(htmlKit1));
0376: assertEquals("javax.swing.JEditorPane$PlainEditorKit",
0377: getClassName(plainKit1));
0378: assertEquals("javax.swing.text.DefaultEditorKit",
0379: getClassName(contentKit1));
0380:
0381: assertNotSame(rtfKit1, rtfKit2);
0382: assertNotSame(htmlKit1, htmlKit2);
0383: assertNotSame(plainKit1, plainKit2);
0384: assertNotSame(contentKit1, contentKit2);
0385: }*/
0386: public void testRegisterEditorKitForContentTypeStringString() {
0387: JEditorPane.registerEditorKitForContentType("text/test1",
0388: "javax.swing.text.DefaultEditorKit");
0389: assertEquals("javax.swing.text.DefaultEditorKit", JEditorPane
0390: .getEditorKitClassNameForContentType("text/test1"));
0391: JEditorPane.registerEditorKitForContentType("text/test1", "");
0392: JEditorPane.registerEditorKitForContentType("text/test1",
0393: "javax.swing.JEditorPaneTest$SimpleEditorKit");
0394: assertEquals(
0395: "javax.swing.JEditorPaneTest$SimpleEditorKit",
0396: JEditorPane
0397: .getEditorKitClassNameForContentType("text/test1"));
0398:
0399: try {
0400: JEditorPane.registerEditorKitForContentType(null, null);
0401: fail("NPE should be thrown");
0402: } catch (NullPointerException npe) {
0403: // PASSED
0404: }
0405: }
0406:
0407: public void testRegisterEditorKitForContentTypeStringStringClassLoader() {
0408: if (true) {
0409: throw new UnsupportedOperationException("Not implemented");
0410: }
0411: SimpleClassLoader cl = new SimpleClassLoader();
0412: cl.wasCallLoadClass = false;
0413: JEditorPane.registerEditorKitForContentType("content1",
0414: "javax.swing.text.DefaultEditorKit", cl);
0415: JEditorPane.registerEditorKitForContentType("content2",
0416: "javax.swing.text.DefaultEditorKit");
0417: assertFalse(cl.wasCallLoadClass);
0418: jep.setContentType("content1");
0419: assertTrue(cl.wasCallLoadClass);
0420: cl.wasCallLoadClass = false;
0421: jep.setContentType("content2");
0422: assertFalse(cl.wasCallLoadClass);
0423: }
0424:
0425: private void checkBaseValues(final String docClass,
0426: final String kitClass, final String contentType,
0427: final URL currentPage, final JEditorPane c) {
0428: assertEquals(docClass, getClassName(c.getDocument()));
0429: assertEquals(kitClass, getClassName(c.getEditorKit()));
0430: assertEquals(contentType, c.getContentType());
0431: assertEquals(currentPage, c.getPage());
0432: }
0433:
0434: public void testJEditorPane() {
0435: checkBaseValues("javax.swing.text.PlainDocument",
0436: "javax.swing.JEditorPane$PlainEditorKit", "text/plain",
0437: null, jep);
0438: }
0439:
0440: public void testJEditorPaneString() {
0441: if (true) {
0442: throw new UnsupportedOperationException("Not implemented");
0443: }
0444: try {
0445: jep = new JEditorPane("http://www.apache.org/");
0446: } catch (IOException e) {
0447: assertFalse("Unexpected exception: " + e.getMessage(), true);
0448: }
0449: checkBaseValues("javax.swing.text.html.HTMLDocument",
0450: "javax.swing.text.html.HTMLEditorKit", "text/html",
0451: TEST_URL, jep);
0452: }
0453:
0454: public void testJEditorPaneStringString() {
0455: if (true) {
0456: throw new UnsupportedOperationException("Not implemented");
0457: }
0458: jep = new JEditorPane("text/html", htmlString);
0459: checkBaseValues("javax.swing.text.html.HTMLDocument",
0460: "javax.swing.text.html.HTMLEditorKit", "text/html",
0461: null, jep);
0462: assertEquals(removeMeta(htmlString), removeMeta(jep.getText()
0463: .replaceAll("\n", "")));
0464: }
0465:
0466: public void testJEditorPaneURL() {
0467: if (true) {
0468: throw new UnsupportedOperationException("Not implemented");
0469: }
0470: try {
0471: jep = new JEditorPane(TEST_URL);
0472: } catch (IOException e) {
0473: assertFalse("Unexpected exception: " + e.getMessage(), true);
0474: }
0475: checkBaseValues("javax.swing.text.html.HTMLDocument",
0476: "javax.swing.text.html.HTMLEditorKit", "text/html",
0477: TEST_URL, jep);
0478: }
0479:
0480: public void testJEditorPaneJarHTML() throws Exception {
0481: // Regression for HARMONY-4529
0482: URL jar = getClass().getResource("testhtml.jar");
0483: URL url = new URL("jar:" + jar + "!/index.html");
0484: new JEditorPane(url);
0485: }
0486:
0487: private void assertEquals(final ArrayList<HyperlinkListener> a,
0488: final Object objects[]) {
0489: int size = a.size();
0490: assertEquals(size, objects.length);
0491: for (int i = 0; i < size; i++) {
0492: assertEquals(a.get(i), objects[i]);
0493: }
0494: }
0495:
0496: public void testAddRemoveGetHyperlinkListener() {
0497: HyperlinkListener listener1 = new SimpleHyperlinkListener("1");
0498: HyperlinkListener listener2 = new SimpleHyperlinkListener("2");
0499: HyperlinkListener listener3 = new SimpleHyperlinkListener("3");
0500: HyperlinkListener listeners[];
0501: ArrayList<HyperlinkListener> testList = new ArrayList<HyperlinkListener>();
0502: jep.addHyperlinkListener(listener1);
0503: listeners = jep.getHyperlinkListeners();
0504: testList.add(listener1);
0505: assertEquals(testList, listeners);
0506: jep.addHyperlinkListener(listener2);
0507: listeners = jep.getHyperlinkListeners();
0508: testList.add(0, listener2);
0509: assertEquals(testList, listeners);
0510: jep.addHyperlinkListener(listener3);
0511: listeners = jep.getHyperlinkListeners();
0512: testList.add(0, listener3);
0513: assertEquals(testList, listeners);
0514: jep.addHyperlinkListener(listener1);
0515: listeners = jep.getHyperlinkListeners();
0516: testList.add(0, listener1);
0517: assertEquals(testList, listeners);
0518: jep.removeHyperlinkListener(listener2);
0519: listeners = jep.getHyperlinkListeners();
0520: testList.remove(listener2);
0521: assertEquals(testList, listeners);
0522: jep.removeHyperlinkListener(listener1);
0523: listeners = jep.getHyperlinkListeners();
0524: testList.remove(listener1);
0525: assertEquals(testList, listeners);
0526: jep.removeHyperlinkListener(listener3);
0527: listeners = jep.getHyperlinkListeners();
0528: testList.remove(listener3);
0529: assertEquals(testList, listeners);
0530: jep.removeHyperlinkListener(listener1);
0531: listeners = jep.getHyperlinkListeners();
0532: testList.remove(listener1);
0533: assertEquals(testList, listeners);
0534: }
0535:
0536: public void testCreateDefaultEditorKit() {
0537: EditorKit kit1 = jep.createDefaultEditorKit();
0538: EditorKit kit2 = jep.createDefaultEditorKit();
0539: assertEquals("javax.swing.JEditorPane$PlainEditorKit", kit1
0540: .getClass().getName());
0541: assertNotSame(kit1, kit2);
0542: }
0543:
0544: private HyperlinkEvent getHyperlinkEvent(
0545: final HyperlinkEvent.EventType type) {
0546: return new HyperlinkEvent(jep, type, TEST_URL);
0547: }
0548:
0549: public void testFireHyperlinkUpdate() {
0550: SimpleHyperlinkListener listener1 = new SimpleHyperlinkListener(
0551: "1");
0552: SimpleHyperlinkListener listener2 = new SimpleHyperlinkListener(
0553: "2");
0554: SimpleHyperlinkListener listener3 = new SimpleHyperlinkListener(
0555: "3");
0556: jep.addHyperlinkListener(listener1);
0557: jep.addHyperlinkListener(listener2);
0558: jep.addHyperlinkListener(listener3);
0559: HyperlinkEvent event = getHyperlinkEvent(HyperlinkEvent.EventType.ACTIVATED);
0560: jep.fireHyperlinkUpdate(event);
0561: assertEquals("321", fireOrder);
0562: assertEquals(event, listener1.event);
0563: assertEquals(event, listener2.event);
0564: assertEquals(event, listener3.event);
0565: fireOrder = "";
0566: event = getHyperlinkEvent(HyperlinkEvent.EventType.EXITED);
0567: jep.fireHyperlinkUpdate(event);
0568: assertEquals("321", fireOrder);
0569: assertEquals(event, listener1.event);
0570: assertEquals(event, listener2.event);
0571: assertEquals(event, listener3.event);
0572: fireOrder = "";
0573: event = getHyperlinkEvent(HyperlinkEvent.EventType.ENTERED);
0574: jep.fireHyperlinkUpdate(event);
0575: assertEquals("321", fireOrder);
0576: assertEquals(event, listener1.event);
0577: assertEquals(event, listener2.event);
0578: assertEquals(event, listener3.event);
0579: }
0580:
0581: public void testSetGetPage1() {
0582: SimplePropertyChangeListener listener = new SimplePropertyChangeListener();
0583: jep.addPropertyChangeListener(listener);
0584: assertNull(jep.getPage());
0585: try {
0586: jep.setPage(TEST_URL);
0587: } catch (final IOException e) {
0588: assertTrue("Unexpected exception :" + e.getMessage(), false);
0589: }
0590: //assertEquals(TEST_URL, jep.getPage());
0591:
0592: JEditorPane e = new JEditorPane();
0593:
0594: try {
0595: e.setPage((java.net.URL) null);
0596: fail("IOException must be thrown");
0597: } catch (java.io.IOException ioe) {
0598: // PASSED
0599: }
0600: }
0601:
0602: public void testSetGetPage2() {
0603: if (true) {
0604: throw new UnsupportedOperationException("Not implemented");
0605: }
0606: String testUrlString = "http://www.apache.org/";
0607: try {
0608: jep.setPage(testUrlString);
0609: } catch (final IOException e) {
0610: assertTrue("Unexpected exception :" + e.getMessage(), false);
0611: }
0612: assertEquals(testUrlString, jep.getPage().toString());
0613: }
0614:
0615: //temporarily commented-out: HTMLEditorKit not implemented
0616: /* public void testGetStream() {
0617: try {
0618: Document doc = jep.getDocument();
0619: assertEquals(getClassName(TEST_URL.openStream()), getClassName(jep
0620: .getStream(TEST_URL)));
0621: assertTrue(jep.getEditorKit() instanceof HTMLEditorKit);
0622: //4825653
0623: //System.out.println(jep.getDocument().getProperty(Document.StreamDescriptionProperty));
0624: } catch (final IOException e) {
0625: assertFalse("Unexpected exception: " + e.getMessage(), true);
0626: }
0627: }*/
0628: public void testReadHTML() {
0629: if (true) {
0630: throw new UnsupportedOperationException("Not implemented");
0631: }
0632: jep.setContentType("text/html");
0633: byte bytes[] = htmlString.getBytes();
0634: ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
0635: try {
0636: jep.read(stream, "text/html");
0637: } catch (IOException e) {
0638: assertFalse("Unexpected exception :", true);
0639: }
0640: assertEquals("text/html", jep.getContentType());
0641: /*
0642: * String docContent = null; try { Document doc = jep.getDocument();
0643: * docContent = doc.getText(0, doc.getLength()); }
0644: * catch(BadLocationException e){ assertFalse("Unexpected exception :",
0645: * true); } System.out.println("_____" + docContent + "_____"); String
0646: * temp = " \nThank you for the quote you sentreguarding account
0647: * #99999999999999. &160;I just have a couple of questions.Please let
0648: * me know. \n" + " Thank you, we look forward to hearing from you.";
0649: * System.out.println("___" + temp.replaceAll("\n","X") + "____");
0650: * System.out.println("___" + docContent.replaceAll("\n","X") +
0651: * "____"); System.out.println(temp.length() + " "+
0652: * docContent.length()); for (int i = 0 ; i < 182; i ++){
0653: * System.out.println("__" + temp.charAt(i) + "___"+
0654: * docContent.charAt(i)+ "___" + (temp.charAt(i) ==
0655: * docContent.charAt(i)) + " ..." + (docContent.charAt(i) == ' ')); }
0656: * assertTrue("BLA",temp.replaceAll("\n","X").equals(docContent.replaceAll("\n","X")));
0657: * //assertEquals(temp, docContent);
0658: * //assertEquals(htmlString,jep.getText().replaceAll("\n",""));
0659: *
0660: */
0661: assertEquals(removeMeta(htmlString), removeMeta(jep.getText()
0662: .replaceAll("\n", "").replaceAll("\r", "")));
0663: }
0664:
0665: static final String removeMeta(final String s) {
0666: return s.replaceAll("meta[^>]*", "meta...");
0667: }
0668:
0669: public void testReadPlain() {
0670: byte bytes[] = plainString.getBytes();
0671: ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
0672: try {
0673: jep.read(stream, "text/plain");
0674: } catch (IOException e) {
0675: assertFalse("Unexpected exception :", true);
0676: }
0677: assertEquals("text/plain", jep.getContentType());
0678: assertEquals(plainString, jep.getText());
0679: }
0680:
0681: public void testReadRTF() {
0682: if (true) {
0683: throw new UnsupportedOperationException("Not implemented");
0684: }
0685: jep.setContentType("text/rtf");
0686: byte bytes[] = rtfString.getBytes();
0687: ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
0688: try {
0689: jep.read(stream, "text/rtf");
0690: } catch (IOException e) {
0691: assertFalse("Unexpected exception :", true);
0692: }
0693: assertEquals("text/rtf", jep.getContentType());
0694: String docContent = null;
0695: try {
0696: Document doc = jep.getDocument();
0697: docContent = doc.getText(0, doc.getLength());
0698: } catch (BadLocationException e) {
0699: assertFalse("Unexpected exception :", true);
0700: }
0701: assertEquals("blablabla\n", docContent);
0702: }
0703:
0704: public void testScrollToReference() {
0705: }
0706:
0707: public void testSetGetContentType() {
0708: if (true) {
0709: throw new UnsupportedOperationException("Not implemented");
0710: }
0711: assertEquals("text/plain", jep.getContentType());
0712: jep.setContentType("text/rtf");
0713: assertEquals("javax.swing.text.rtf.RTFEditorKit", jep
0714: .getEditorKit().getClass().getName());
0715: assertEquals("javax.swing.text.DefaultStyledDocument",
0716: getClassName(jep.getDocument()));
0717: jep.setContentType("text/html");
0718: assertEquals("javax.swing.text.html.HTMLEditorKit", jep
0719: .getEditorKit().getClass().getName());
0720: assertEquals("javax.swing.text.html.HTMLDocument",
0721: getClassName(jep.getDocument()));
0722: jep.setContentType("text/plain");
0723: assertEquals("javax.swing.JEditorPane$PlainEditorKit", jep
0724: .getEditorKit().getClass().getName());
0725: assertEquals("javax.swing.text.PlainDocument", getClassName(jep
0726: .getDocument()));
0727: jep.setContentType("text/test");
0728: assertEquals("javax.swing.JEditorPane$PlainEditorKit", jep
0729: .getEditorKit().getClass().getName());
0730: assertEquals("javax.swing.text.PlainDocument", getClassName(jep
0731: .getDocument()));
0732: JEditorPane.registerEditorKitForContentType("text/test",
0733: "javax.swing.text.DefaultEditorKit");
0734: jep.setContentType("text/test");
0735: assertEquals("javax.swing.text.DefaultEditorKit", jep
0736: .getEditorKit().getClass().getName());
0737: assertEquals("javax.swing.text.PlainDocument", getClassName(jep
0738: .getDocument()));
0739: JEditorPane.registerEditorKitForContentType("text/test", "");
0740:
0741: JEditorPane e = new JEditorPane();
0742:
0743: try {
0744: e.setContentType((String) null);
0745: fail("NPE must be thrown");
0746: } catch (NullPointerException npe) {
0747: // PASSED
0748: }
0749: }
0750:
0751: static void checkContentType(String fileName, String expected)
0752: throws IOException {
0753: if (!fileName.startsWith("http")) {
0754: fileName = "content-type/" + fileName;
0755: URL url = JEditorPaneTest.class.getResource(fileName);
0756: assertNotNull("Resource not found: " + fileName, url);
0757: fileName = url.toString();
0758: }
0759: JEditorPane pane = new JEditorPane(fileName);
0760: assertEquals(pane.getContentType(), expected);
0761: assertEquals(pane.getEditorKit().getContentType(), expected);
0762: }
0763:
0764: public void testGetContentType() throws IOException {
0765: // Regression for HARMONY-4696
0766: checkContentType("txt", "text/plain");
0767: checkContentType("html", "text/html");
0768: checkContentType("rtf", "text/plain");
0769: checkContentType("txt.txt", "text/plain");
0770: checkContentType("txt.html", "text/html");
0771: checkContentType("txt.rtf", "text/plain"); // Change to "text/rtf" when RTFEditorKit becomes available.
0772: checkContentType("html.txt", "text/plain");
0773: checkContentType("html.html", "text/html");
0774: checkContentType("html.rtf", "text/plain"); // Change to "text/rtf" when RTFEditorKit becomes available.
0775: checkContentType("rtf.txt", "text/plain");
0776: checkContentType("rtf.html", "text/html");
0777: checkContentType("rtf.rtf", "text/plain"); // Change to "text/rtf" when RTFEditorKit becomes available.
0778: checkContentType("http://www.apache.org", "text/html");
0779: }
0780:
0781: private String getClassName(final Object obj) {
0782: assertNotNull(obj);
0783: return obj.getClass().getName();
0784: }
0785:
0786: //commented due to unimplemented functionality
0787: /*public void testSetGetEditorKitForContentType() {
0788: String content = "testSetGetEditorKitForContentType";
0789: assertEquals("javax.swing.JEditorPane$PlainEditorKit", getClassName(jep
0790: .getEditorKitForContentType("...")));
0791: assertEquals("javax.swing.JEditorPane$PlainEditorKit", getClassName(jep
0792: .getEditorKitForContentType("text/plain")));
0793: assertEquals("javax.swing.text.html.HTMLEditorKit", getClassName(jep
0794: .getEditorKitForContentType("text/html")));
0795: assertEquals("javax.swing.text.rtf.RTFEditorKit", getClassName(jep
0796: .getEditorKitForContentType("text/rtf")));
0797: assertEquals("javax.swing.JEditorPane$PlainEditorKit", getClassName(jep
0798: .getEditorKitForContentType(content)));
0799:
0800: JEditorPane
0801: .registerEditorKitForContentType(content,
0802: "javax.swing.text.DefaultEditorKit");
0803: assertEquals("javax.swing.text.DefaultEditorKit", getClassName(jep
0804: .getEditorKitForContentType(content)));
0805: assertEquals("javax.swing.text.DefaultEditorKit", JEditorPane
0806: .getEditorKitClassNameForContentType(content));
0807: SimpleEditorKit kit = new SimpleEditorKit();
0808: jep.setEditorKitForContentType(content, kit);
0809: assertEquals("javax.swing.JEditorPaneTest$SimpleEditorKit",
0810: getClassName(jep.getEditorKitForContentType(content)));
0811: assertEquals("javax.swing.text.DefaultEditorKit", JEditorPane
0812: .getEditorKitClassNameForContentType(content));
0813: }*/
0814: public void testSetGetEditorKit() {
0815: if (true) {
0816: throw new UnsupportedOperationException("Not implemented");
0817: }
0818: SimplePropertyChangeListener listener = new SimplePropertyChangeListener();
0819: jep.addPropertyChangeListener("editorKit", listener);
0820: PropertyChangeEvent event;
0821: EditorKit kit0 = jep.getEditorKit();
0822: SimpleEditorKit kit1 = new SimpleEditorKit();
0823: SimpleEditorKit kit2 = new SimpleEditorKit();
0824: assertEquals("javax.swing.JEditorPane$PlainEditorKit", kit0
0825: .getClass().getName());
0826: event = new PropertyChangeEvent(jep, "editorKit", kit0, kit1);
0827: jep.setEditorKit(kit1);
0828: assertEquals(kit1, jep.getEditorKit());
0829: assertEquals(event, listener.event);
0830: assertTrue(kit1.wasCallInstall);
0831: assertFalse(kit1.wasCallDeinstall);
0832: listener.resetDbgInfo();
0833: kit1.resetDbgInfo();
0834: event = new PropertyChangeEvent(jep, "editorKit", kit1, kit2);
0835: jep.setEditorKit(kit2);
0836: assertEquals(kit2, jep.getEditorKit());
0837: assertEquals(event, listener.event);
0838: assertTrue(kit2.wasCallInstall);
0839: assertFalse(kit2.wasCallDeinstall);
0840: assertFalse(kit1.wasCallInstall);
0841: assertTrue(kit1.wasCallDeinstall);
0842: kit2.resetDbgInfo();
0843: jep.setEditorKit(null);
0844: kit0 = jep.getEditorKit();
0845: event = new PropertyChangeEvent(jep, "editorKit", kit2, kit0);
0846: assertEquals("javax.swing.JEditorPane$PlainEditorKit", kit0
0847: .getClass().getName());
0848: assertTrue(kit2.wasCallDeinstall);
0849: //temporarily commented-out: HTMLEditorKit,
0850: //DefaultStyledDocument not implemented
0851: /*assertEquals("text/plain", jep.getContentType());
0852: jep.setEditorKit(new HTMLEditorKit());
0853: assertEquals("text/html", jep.getContentType());
0854: assertEquals("javax.swing.text.html.HTMLDocument", getClassName(jep
0855: .getDocument()));
0856:
0857: jep.setEditorKit(new DefaultEditorKit());
0858: assertEquals("text/plain", jep.getContentType());
0859: assertEquals("javax.swing.text.PlainDocument", getClassName(jep
0860: .getDocument()));
0861:
0862: jep.setEditorKit(new StyledEditorKit());
0863: assertEquals("text/plain", jep.getContentType());
0864: assertEquals("javax.swing.text.DefaultStyledDocument", getClassName(jep
0865: .getDocument()));
0866:
0867: jep.setEditorKit(new RTFEditorKit());
0868: assertEquals("text/rtf", jep.getContentType());
0869: assertEquals("javax.swing.text.DefaultStyledDocument", getClassName(jep
0870: .getDocument())); */
0871: }
0872:
0873: public void testConstants() {
0874: assertEquals("JEditorPane.honorDisplayProperties",
0875: JEditorPane.HONOR_DISPLAY_PROPERTIES);
0876: assertEquals("JEditorPane.w3cLengthUnits",
0877: JEditorPane.W3C_LENGTH_UNITS);
0878: }
0879:
0880: public void testPlainEditorKit() {
0881: EditorKit kit = jep.getEditorKit();
0882: assertEquals(kit, kit.getViewFactory());
0883: }
0884:
0885: public void testGetEditorKitClassNameForContentType() {
0886: assertEquals(
0887: "javax.swing.JEditorPane$PlainEditorKit",
0888: JEditorPane
0889: .getEditorKitClassNameForContentType("text/plain"));
0890: assertEquals("javax.swing.text.html.HTMLEditorKit", JEditorPane
0891: .getEditorKitClassNameForContentType("text/html"));
0892: assertEquals("javax.swing.text.rtf.RTFEditorKit", JEditorPane
0893: .getEditorKitClassNameForContentType("text/rtf"));
0894: assertNull(JEditorPane
0895: .getEditorKitClassNameForContentType("..."));
0896:
0897: // Regression test for HARMONY-2571
0898: try {
0899: JEditorPane.getEditorKitClassNameForContentType(null);
0900: fail("NPE expected");
0901: } catch (NullPointerException e) {
0902: }
0903: }
0904:
0905: public void testSetEditorKitForContentType() throws Exception {
0906: try {
0907: JEditorPane ep = new JEditorPane();
0908: ep.setEditorKitForContentType("abc", null);
0909: fail("NPE expected");
0910: } catch (NullPointerException npe) {
0911: // PASSED
0912: }
0913:
0914: try {
0915: JEditorPane ep = new JEditorPane();
0916: ep.setEditorKitForContentType(null, new DefaultEditorKit());
0917: fail("NPE expected");
0918: } catch (NullPointerException npe) {
0919: // PASSED
0920: }
0921:
0922: try {
0923: JEditorPane ep = new JEditorPane();
0924: ep.setEditorKitForContentType(null, null);
0925: fail("NPE expected");
0926: } catch (NullPointerException npe) {
0927: // PASSED
0928: }
0929: }
0930:
0931: public void testIsFocusCycleRoot() throws Exception {
0932: // Regression test for HARMONY-2573
0933: assertTrue(new JEditorPane().isFocusCycleRoot());
0934: }
0935:
0936: public void testCreateEditorKitForContentType() throws Exception {
0937:
0938: // Regression test for HARMONY-3453, HARMONY-3454
0939: final ClassLoader classLoader1 = new ArrayClassLoader();
0940: final ClassLoader classLoader2 = new ArrayClassLoader();
0941: final ClassLoader classLoader3 = new ArrayClassLoader();
0942:
0943: class ThreadCheckEditorKit extends Thread {
0944: private boolean register;
0945: public EditorKit[] result = new EditorKit[9];
0946:
0947: public ThreadCheckEditorKit(boolean register) {
0948: this .register = register;
0949:
0950: if (register) {
0951: setContextClassLoader(classLoader1);
0952: }
0953: }
0954:
0955: public void run() {
0956: result[0] = JEditorPane
0957: .createEditorKitForContentType("testContentType1");
0958: result[1] = JEditorPane
0959: .createEditorKitForContentType("testContentType2");
0960: result[2] = JEditorPane
0961: .createEditorKitForContentType("testContentType3");
0962:
0963: if (register) {
0964: JEditorPane.registerEditorKitForContentType(
0965: "testContentType1", "MyEditorKit");
0966: JEditorPane.registerEditorKitForContentType( // This throws NPE on RI
0967: "testContentType2", "MyEditorKit", null); // see HARMONY-3453.
0968: JEditorPane.registerEditorKitForContentType(
0969: "testContentType3", "MyEditorKit",
0970: (register ? classLoader2 : classLoader3));
0971: }
0972: result[3] = JEditorPane
0973: .createEditorKitForContentType("testContentType1");
0974: result[4] = JEditorPane
0975: .createEditorKitForContentType("testContentType2");
0976: result[5] = JEditorPane
0977: .createEditorKitForContentType("testContentType3");
0978:
0979: result[6] = JEditorPane
0980: .createEditorKitForContentType("testContentType1");
0981: result[7] = JEditorPane
0982: .createEditorKitForContentType("testContentType2");
0983: result[8] = JEditorPane
0984: .createEditorKitForContentType("testContentType3");
0985: }
0986:
0987: public void go() {
0988: start();
0989:
0990: while (true) {
0991: try {
0992: join();
0993: return;
0994: } catch (InterruptedException e) {
0995: // Ignored.
0996: }
0997: }
0998: }
0999: }
1000: ;
1001:
1002: ThreadCheckEditorKit thread1 = new ThreadCheckEditorKit(true);
1003: thread1.go();
1004: ThreadCheckEditorKit thread2 = new ThreadCheckEditorKit(false);
1005: thread2.go();
1006:
1007: Map<EditorKit, Object> kitMap = new HashMap<EditorKit, Object>();
1008: EditorKit result;
1009:
1010: for (int i = 0; i < 9; i++) {
1011: result = thread1.result[i];
1012:
1013: if (i < 3) {
1014: assertNull(result);
1015: } else {
1016: assertNotNull(result);
1017: kitMap.put(result, null);
1018: assertEquals(result.getClass().getClassLoader(),
1019: ((i % 3) == 2) ? classLoader2 : classLoader1);
1020: }
1021: result = thread2.result[i];
1022: assertNotNull(result);
1023: kitMap.put(result, null);
1024: assertEquals(result.getClass().getClassLoader(),
1025: (((i % 3) == 2) ? classLoader2 : classLoader1));
1026: }
1027: // Make sure all returned values are unique.
1028: assertEquals(kitMap.size(), 15);
1029: }
1030:
1031: /**
1032: * Special classloader for testCreateEditorKitForContentType().
1033: */
1034: private static class ArrayClassLoader extends ClassLoader {
1035:
1036: private static byte[] bytesMyEditorKit = new byte[] {
1037: /*
1038: * public class MyEditorKit extends DefaultEditorKit {}
1039: */
1040: -54, -2, -70, -66, 0, 0, 0, 49, 0, 13, 10, 0, 3, 0, 10, 7, 0,
1041: 11, 7, 0, 12, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1,
1042: 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15,
1043: 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97,
1044: 98, 108, 101, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70,
1045: 105, 108, 101, 1, 0, 16, 77, 121, 69, 100, 105, 116,
1046: 111, 114, 75, 105, 116, 46, 106, 97, 118, 97, 12, 0, 4,
1047: 0, 5, 1, 0, 11, 77, 121, 69, 100, 105, 116, 111, 114,
1048: 75, 105, 116, 1, 0, 33, 106, 97, 118, 97, 120, 47, 115,
1049: 119, 105, 110, 103, 47, 116, 101, 120, 116, 47, 68,
1050: 101, 102, 97, 117, 108, 116, 69, 100, 105, 116, 111,
1051: 114, 75, 105, 116, 0, 33, 0, 2, 0, 3, 0, 0, 0, 0, 0, 1,
1052: 0, 1, 0, 4, 0, 5, 0, 1, 0, 6, 0, 0, 0, 29, 0, 1, 0, 1,
1053: 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0, 7, 0, 0,
1054: 0, 6, 0, 1, 0, 0, 0, 5, 0, 1, 0, 8, 0, 0, 0, 2, 0, 9 };
1055:
1056: protected Class findClass(String name)
1057: throws ClassNotFoundException {
1058: if ("MyEditorKit".equals(name)) {
1059: return defineClass("MyEditorKit", bytesMyEditorKit, 0,
1060: bytesMyEditorKit.length);
1061: } else {
1062: throw new ClassNotFoundException(name);
1063: }
1064: }
1065: }
1066: }
|