1/*
2 * Copyright (c) 2016, 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/* @test
25   @bug 8158734
26   @summary JEditorPane.createEditorKitForContentType throws NPE after 6882559
27   @author Mikhail Cherkasov
28   @run main bug8158734
29*/
30
31import javax.swing.*;
32import javax.swing.text.*;
33import java.io.*;
34import java.lang.reflect.InvocationTargetException;
35
36
37public class bug8158734 {
38
39    public static final String TYPE = "test/test";
40    public static final String TYPE_2 = "test2/test2";
41
42    static boolean myClassloaderWasUsed = false;
43
44    static class MyEditorKit extends EditorKit {
45        @Override
46        public String getContentType() {
47            return null;
48        }
49
50        @Override
51        public ViewFactory getViewFactory() {
52            return null;
53        }
54
55        @Override
56        public Action[] getActions() {
57            return new Action[0];
58        }
59
60        @Override
61        public Caret createCaret() {
62            return null;
63        }
64
65        @Override
66        public Document createDefaultDocument() {
67            return null;
68        }
69
70        @Override
71        public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException {
72        }
73
74        @Override
75        public void write(OutputStream out, Document doc, int pos, int len) throws IOException, BadLocationException {
76
77        }
78
79        @Override
80        public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException {
81        }
82
83        @Override
84        public void write(Writer out, Document doc, int pos, int len) throws IOException, BadLocationException {
85        }
86    }
87
88    static class MyClassloader extends ClassLoader {
89        @Override
90        public Class<?> loadClass(String name) throws ClassNotFoundException {
91            myClassloaderWasUsed = true;
92            return super.loadClass(name);
93        }
94    }
95
96    public static void main(String[] args) throws InvocationTargetException, InterruptedException {
97        SwingUtilities.invokeAndWait(new Runnable() {
98            @Override
99            public void run() {
100                JEditorPane c = new JEditorPane();
101                c.setContentType(TYPE);
102
103                final MyClassloader loader = new MyClassloader();
104                JEditorPane.registerEditorKitForContentType(TYPE_2, MyEditorKit.class.getName(), loader);
105                JEditorPane.registerEditorKitForContentType(TYPE_2, MyEditorKit.class.getName(), null);
106                JEditorPane.createEditorKitForContentType(TYPE_2);
107
108                if (myClassloaderWasUsed) {
109                    throw new RuntimeException("Class loader has not been removed for '" + TYPE_2 + "' type");
110                }
111            }
112        });
113
114    }
115}