001: /*
002: * EditorPanelTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.sql;
013:
014: import java.awt.event.ActionEvent;
015: import junit.framework.*;
016: import java.io.File;
017: import java.io.FileOutputStream;
018: import java.io.IOException;
019: import java.io.OutputStreamWriter;
020: import java.io.Reader;
021: import java.io.Writer;
022: import workbench.TestUtil;
023: import workbench.gui.editor.InputHandler;
024: import workbench.resource.Settings;
025: import workbench.util.CharacterRange;
026: import workbench.util.EncodingUtil;
027: import workbench.util.FileUtil;
028: import workbench.util.StringUtil;
029:
030: /**
031: * @author support@sql-workbench.net
032: */
033: public class EditorPanelTest extends TestCase {
034: private TestUtil util;
035:
036: public EditorPanelTest(String testName) {
037: super (testName);
038: util = new TestUtil(testName);
039: try {
040: util.prepareEnvironment();
041: } catch (Exception e) {
042: e.printStackTrace();
043: fail();
044: }
045: }
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: util.emptyBaseDirectory();
050: }
051:
052: private int writeTestFile(File f, String nl) throws IOException {
053: int count = 100;
054: Writer w = new OutputStreamWriter(new FileOutputStream(f),
055: "UTF-8");
056: for (int i = 0; i < count; i++) {
057: w.write("This is test line " + i);
058: w.write(nl);
059: }
060: w.close();
061: return count;
062: }
063:
064: public void _testSaveFile() {
065: String dir = util.getBaseDir();
066: try {
067: Settings set = Settings.getInstance();
068: EditorPanel p = EditorPanel.createTextEditor();
069:
070: set
071: .setExternalEditorLineEnding(Settings.UNIX_LINE_TERMINATOR_PROP_VALUE);
072: set
073: .setInternalEditorLineEnding(Settings.DOS_LINE_TERMINATOR_PROP_VALUE);
074:
075: ActionEvent evt = new ActionEvent(p, 1, "break");
076: p.setAutoIndent(false);
077: p.appendLine("Line1");
078: p.getInputHandler().INSERT_BREAK.actionPerformed(evt);
079: p.appendLine("Line2");
080: p.getInputHandler().INSERT_BREAK.actionPerformed(evt);
081: p.appendLine("Line3");
082: p.getInputHandler().INSERT_BREAK.actionPerformed(evt);
083:
084: String content = p.getText();
085: int pos = content.indexOf("Line2\r\n");
086: assertEquals("Wrong internal line ending (DOS) used", 7,
087: pos);
088:
089: File f = new File(dir, "editor_unx.txt");
090: f.delete();
091: p.saveFile(f, "UTF-8", "\n");
092:
093: Reader r = EncodingUtil.createReader(f, "UTF-8");
094: content = FileUtil.readCharacters(r);
095: f.delete();
096:
097: pos = content.indexOf("Line2\n");
098: assertEquals("Wrong external line ending (Unix) used", 6,
099: pos);
100:
101: set
102: .setExternalEditorLineEnding(Settings.DOS_LINE_TERMINATOR_PROP_VALUE);
103: set
104: .setInternalEditorLineEnding(Settings.UNIX_LINE_TERMINATOR_PROP_VALUE);
105:
106: p = EditorPanel.createTextEditor();
107: evt = new ActionEvent(p, 1, "break");
108: p.setAutoIndent(false);
109: p.appendLine("Line1");
110: InputHandler.INSERT_BREAK.actionPerformed(evt);
111: p.appendLine("Line2");
112: InputHandler.INSERT_BREAK.actionPerformed(evt);
113: p.appendLine("Line3");
114: InputHandler.INSERT_BREAK.actionPerformed(evt);
115:
116: content = p.getText();
117: System.out.println(StringUtil.escapeUnicode(content,
118: CharacterRange.RANGE_8BIT));
119: pos = content.indexOf("Line2\n");
120: assertEquals("Wrong internal line ending (Unix) used", 6,
121: pos);
122:
123: f = new File(dir, "editor_dos.txt");
124: f.delete();
125: p.saveFile(f, "UTF-8", "\r\n");
126: r = EncodingUtil.createReader(f, "UTF-8");
127: content = FileUtil.readCharacters(r);
128:
129: pos = content.indexOf("Line2\r\n");
130: assertEquals("Wrong exteranl line ending (DOS) used", 7,
131: pos);
132: } catch (Exception e) {
133: e.printStackTrace();
134: fail("Error loading file");
135: }
136: }
137:
138: public void testReadFile() {
139: String dir = util.getBaseDir();
140: File f = new File(dir, "editor.txt");
141: try {
142: Settings set = Settings.getInstance();
143: //set.setInternalEditorLineEnding(Settings.UNIX_LINE_TERMINATOR_PROP_VALUE);
144: EditorPanel p = EditorPanel.createTextEditor();
145: int lines = writeTestFile(f, "\n");
146:
147: p.readFile(f, "UTF-8");
148: assertEquals("File not loaded", true, p.hasFileLoaded());
149: assertEquals("Wrong line count", lines + 1, p
150: .getLineCount());
151:
152: p.dispose();
153:
154: set
155: .setInternalEditorLineEnding(Settings.DOS_LINE_TERMINATOR_PROP_VALUE);
156: p = EditorPanel.createTextEditor();
157: lines = writeTestFile(f, "\r\n");
158:
159: p.readFile(f, "UTF-8");
160: assertEquals("File not loaded", true, p.hasFileLoaded());
161: assertEquals("Wrong line count", lines + 1, p
162: .getLineCount());
163: } catch (Exception e) {
164: e.printStackTrace();
165: fail("Error loading file");
166: }
167: }
168:
169: }
|