001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Dimension;
023: import java.awt.event.ActionEvent;
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.Reader;
029: import java.io.Writer;
030: import java.lang.reflect.InvocationTargetException;
031: import javax.swing.Action;
032: import javax.swing.BasicSwingTestCase;
033: import javax.swing.JFrame;
034: import javax.swing.JScrollPane;
035: import javax.swing.JTextArea;
036: import javax.swing.JViewport;
037: import javax.swing.ScrollPaneConstants;
038: import javax.swing.SwingUtilities;
039:
040: public class DefaultEditorKitRTest extends BasicSwingTestCase {
041: protected DefaultEditorKit kit = null;
042:
043: protected JFrame frame;
044:
045: @Override
046: protected void setUp() throws Exception {
047: super .setUp();
048: kit = new DefaultEditorKit();
049: }
050:
051: @Override
052: protected void tearDown() throws Exception {
053: kit = null;
054: if (frame != null) {
055: frame.dispose();
056: frame = null;
057: }
058: super .tearDown();
059: }
060:
061: protected Action getAction(final String actionName) {
062: Action[] actions = kit.getActions();
063: for (int i = 0; i < actions.length; i++) {
064: if (actionName.equals(actions[i].getValue(Action.NAME))) {
065: return actions[i];
066: }
067: }
068: return null;
069: }
070:
071: protected void performAction(final Object source,
072: final Action action, final String command)
073: throws InterruptedException, InvocationTargetException {
074: final ActionEvent actionEvent = new ActionEvent(source,
075: ActionEvent.ACTION_PERFORMED, command);
076: SwingUtilities.invokeAndWait(new Runnable() {
077: public void run() {
078: action.actionPerformed(actionEvent);
079: }
080: });
081: }
082:
083: protected JTextArea getInitedComponent(final int startPos,
084: final int endPos, final String text)
085: throws InterruptedException, InvocationTargetException {
086: JTextArea c = new JTextArea();
087: return initComponent(c, startPos, endPos, text);
088: }
089:
090: private JTextArea initComponent(final JTextArea c,
091: final int startPos, final int endPos, final String text)
092: throws InterruptedException, InvocationTargetException {
093: if (frame != null) {
094: frame.dispose();
095: }
096: c.setText(text);
097: frame = new JFrame();
098: JScrollPane scroll = new JScrollPane(c);
099: ((JViewport) c.getParent())
100: .setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
101: int strHeight = c.getFontMetrics(c.getFont()).getHeight();
102: scroll.setPreferredSize(new Dimension(300, strHeight * 5));
103: scroll
104: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
105: frame.getContentPane().add(scroll);
106: frame.pack();
107: java.awt.EventQueue.invokeAndWait(new Runnable() {
108: public void run() {
109: c.setCaretPosition(startPos);
110: if (endPos >= 0) {
111: c.moveCaretPosition(endPos);
112: }
113: }
114: });
115: return c;
116: }
117:
118: public void testInsertContentActionPerformed() throws Exception {
119: Action action = getAction(DefaultEditorKit.insertContentAction);
120: JTextArea c = getInitedComponent(2, 7, "0123456789");
121: performAction(new JTextArea(), action, "command\ncontent");
122: assertEquals("resulted string", "0123456789", c.getText());
123: c = getInitedComponent(2, 7, "0123456789");
124: c.setEditable(false);
125: performAction(c, action, "command\ncontent");
126: assertEquals("resulted string", "0123456789", c.getText());
127: c = getInitedComponent(2, 7, "0123456789");
128: performAction(c, action, null);
129: assertEquals("resulted string", "0123456789", c.getText());
130: }
131:
132: public void testReadInputStreamDocumentint() throws Exception {
133: final Marker readerMarker = new Marker();
134: DefaultEditorKit kit = new DefaultEditorKit() {
135: private static final long serialVersionUID = 1L;
136:
137: @Override
138: public void read(Reader in, Document doc, int pos)
139: throws IOException, BadLocationException {
140: readerMarker.setOccurred();
141: }
142: };
143: kit.read(new ByteArrayInputStream(new byte[10]),
144: new DefaultStyledDocument(), 0);
145: assertTrue(readerMarker.isOccurred());
146: }
147:
148: public void testWriteOutputStreamDocumentintint() throws Exception {
149: final Marker writeMarker = new Marker();
150: DefaultEditorKit kit = new DefaultEditorKit() {
151: private static final long serialVersionUID = 1L;
152:
153: @Override
154: public void write(Writer out, Document doc, int pos, int len)
155: throws IOException, BadLocationException {
156: writeMarker.setOccurred();
157: }
158: };
159: kit.write(new ByteArrayOutputStream(),
160: new DefaultStyledDocument(), 0, 1);
161: assertTrue(writeMarker.isOccurred());
162: }
163:
164: public void testInsertContentActionPerformed_NullEvent()
165: throws Exception {
166: final TextAction action = (TextAction) getAction(DefaultEditorKit.insertContentAction);
167: final JTextArea c = getInitedComponent(2, 7, "0123456789");
168: assertNotNull(c);
169: frame.setVisible(true);
170: final Marker thrown = new Marker();
171: SwingUtilities.invokeAndWait(new Runnable() {
172: public void run() {
173: try {
174: action.actionPerformed(null);
175: thrown.setOccurred();
176: } catch (NullPointerException e) {
177: }
178: }
179: });
180: assertTrue(thrown.isOccurred());
181: }
182:
183: public void testRead() throws Exception {
184: String str1 = "Windows line-end\r\nUnix-style\nMacOS\rUnknown\n\r";
185: String str2 = "Windows line-end\nUnix-style\nMacOS\nUnknown\n\n";
186: InputStream reader = new ByteArrayInputStream(str1.getBytes());
187: Document doc = new PlainDocument();
188: kit.read(reader, doc, 0);
189: assertEquals(str2, doc.getText(0, doc.getLength()));
190: }
191: }
|