|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.charset.Charset; |
| 27 | + |
| 28 | +import javax.swing.JEditorPane; |
| 29 | +import javax.swing.text.BadLocationException; |
| 30 | +import javax.swing.text.Document; |
| 31 | +import javax.swing.text.Element; |
| 32 | + |
| 33 | +/* |
| 34 | + * @test |
| 35 | + * @bug 8328953 |
| 36 | + * @summary Verifies JEditorPane.read doesn't throw ChangedCharSetException |
| 37 | + but handles it and reads HTML in the specified encoding |
| 38 | + * @run main EditorPaneCharset |
| 39 | + */ |
| 40 | + |
| 41 | +public final class EditorPaneCharset { |
| 42 | + private static final String CYRILLIC_TEXT = |
| 43 | + "\u041F\u0440\u0438\u0432\u0435\u0442, \u043C\u0438\u0440!"; |
| 44 | + private static final String HTML_CYRILLIC = |
| 45 | + "<html lang=\"ru\">\n" + |
| 46 | + "<head>\n" + |
| 47 | + " <meta http-equiv=\"Content-Type\" " + |
| 48 | + " content=\"text/html; charset=windows-1251\">\n" + |
| 49 | + "</head><body>\n" + |
| 50 | + "<p>" + CYRILLIC_TEXT + "</p>\n" + |
| 51 | + "</body></html>\n"; |
| 52 | + |
| 53 | + public static void main(String[] args) throws IOException, BadLocationException { |
| 54 | + JEditorPane editorPane = new JEditorPane(); |
| 55 | + editorPane.setContentType("text/html"); |
| 56 | + Document document = editorPane.getDocument(); |
| 57 | + |
| 58 | + // Shouldn't throw ChangedCharSetException |
| 59 | + editorPane.read( |
| 60 | + new ByteArrayInputStream( |
| 61 | + HTML_CYRILLIC.getBytes( |
| 62 | + Charset.forName("windows-1251"))), |
| 63 | + document); |
| 64 | + |
| 65 | + Element root = document.getDefaultRootElement(); |
| 66 | + Element body = root.getElement(1); |
| 67 | + Element p = body.getElement(0); |
| 68 | + String pText = document.getText(p.getStartOffset(), |
| 69 | + p.getEndOffset() - p.getStartOffset() - 1); |
| 70 | + if (!CYRILLIC_TEXT.equals(pText)) { |
| 71 | + throw new RuntimeException("Text doesn't match"); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Jun 3, 2024
Review
Issues