View Javadoc

1   // Copyright (C) 2004 - 2012 Philip Aston
2   // Copyright (C) 2005 Martin Wagner
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package net.grinder.console.editor;
24  
25  import java.io.EOFException;
26  import java.io.File;
27  import java.io.FileNotFoundException;
28  import java.io.IOException;
29  import java.net.UnknownHostException;
30  
31  import net.grinder.console.common.DisplayMessageConsoleException;
32  import net.grinder.console.common.Resources;
33  import net.grinder.console.common.ResourcesImplementation;
34  import net.grinder.testutility.AbstractFileTestCase;
35  
36  
37  /**
38   * Unit test for {@link BufferImplementation}.
39   *
40   * @author Philip Aston
41   */
42  public class TestBuffer extends AbstractFileTestCase {
43  
44    private static String LINE_SEPARATOR = System.getProperty("line.separator");
45  
46    private static final Resources s_resources =
47        new ResourcesImplementation(
48          "net.grinder.console.common.resources.Console");
49  
50    public void testBufferWithNoFile() throws Exception {
51      final String text = "Some text for testing with";
52  
53      final TextSource textSource = new StringTextSource(text);
54  
55      assertEquals(text, textSource.getText());
56  
57      final Buffer buffer = new BufferImplementation(s_resources, textSource, "My Buffer");
58  
59      assertNotNull(textSource.getText());
60      assertEquals(text, textSource.getText());
61      assertEquals("My Buffer", buffer.getDisplayName());
62      assertTrue( buffer.toString().indexOf(buffer.getDisplayName()) >= 0);
63  
64      try {
65        buffer.load();
66        fail("Expected EditorException");
67      }
68      catch (EditorException e) {
69      }
70  
71      try {
72        buffer.save();
73        fail("Expected EditorException");
74      }
75      catch (EditorException e) {
76      }
77  
78      assertTrue(!buffer.isDirty());
79      assertTrue(buffer.isUpToDate());
80      assertNull(buffer.getFile());
81      assertEquals(textSource, buffer.getTextSource());
82  
83      assertEquals(Buffer.Type.TEXT_BUFFER, buffer.getType());
84  
85      assertTrue(!buffer.isDirty());
86      assertTrue(buffer.isUpToDate());
87      assertNull(buffer.getFile());
88      assertEquals(textSource, buffer.getTextSource());
89    }
90  
91    private static final class Expectation {
92      private final Buffer.Type m_type;
93      private final File m_file;
94  
95      public Expectation(Buffer.Type type, String filename) {
96        m_type = type;
97        m_file = new File(filename);
98      }
99  
100     public Buffer.Type getType() {
101       return m_type;
102     }
103 
104     public File getFile() {
105       return m_file;
106     }
107   }
108 
109   public void testGetType() throws Exception {
110     final StringTextSource textSource = new StringTextSource("");
111 
112     final Expectation[] wordsOfExpectation = {
113       new Expectation(Buffer.Type.HTML_BUFFER, "somefile/blah.htm"),
114       new Expectation(Buffer.Type.HTML_BUFFER, "foo.html"),
115       new Expectation(Buffer.Type.JAVA_BUFFER, "eieio.java"),
116       new Expectation(Buffer.Type.MSDOS_BATCH_BUFFER, "eat/my.shorts.bat"),
117       new Expectation(Buffer.Type.MSDOS_BATCH_BUFFER, "alpha.cmd"),
118       new Expectation(Buffer.Type.PROPERTIES_BUFFER, "essential.properties"),
119       new Expectation(Buffer.Type.PYTHON_BUFFER, "why/oh.py"),
120       new Expectation(Buffer.Type.SHELL_BUFFER, "bishbosh.bash"),
121       new Expectation(Buffer.Type.SHELL_BUFFER, "clishclosh.csh"),
122       new Expectation(Buffer.Type.SHELL_BUFFER, "kkkkrassh.ksh"),
123       new Expectation(Buffer.Type.SHELL_BUFFER, "be/quiet.sh"),
124       new Expectation(Buffer.Type.TEXT_BUFFER, "tick.txt"),
125       new Expectation(Buffer.Type.TEXT_BUFFER, "tech.text"),
126       new Expectation(Buffer.Type.XML_BUFFER, "xplicitly.xml"),
127       new Expectation(Buffer.Type.TEXT_BUFFER, "blurb/blah"),
128       new Expectation(Buffer.Type.TEXT_BUFFER, "fidledly.foo"),
129       new Expectation(Buffer.Type.TEXT_BUFFER, "bah/bah"),
130       new Expectation(Buffer.Type.TEXT_BUFFER, "...."),
131     };
132 
133     for (int i=0; i<wordsOfExpectation.length; ++i) {
134       final Expectation expectation = wordsOfExpectation[i];
135 
136       final Buffer buffer =
137         new BufferImplementation(s_resources, textSource, expectation.getFile());
138 
139       assertEquals(expectation.getType(), buffer.getType());
140       assertEquals(textSource, buffer.getTextSource());
141     }
142   }
143 
144   public void testBufferWithAssociatedFile() throws Exception {
145 
146     final String s0 =
147       "A shield for your eyes\na beast in the well on your hand";
148 
149     final String s1 =
150       "Catch the mean beast\nin the well in the hell on the back\n" +
151       "Watch out! You've got no shield\n" +
152       "Break up! He's got no peace";
153 
154     final StringTextSource textSource = new StringTextSource(s0);
155     assertSame(s0, textSource.getText());
156 
157     final File file = new File(getDirectory(), "myfile.txt");
158 
159     final Buffer buffer = new BufferImplementation(s_resources, textSource, file);
160 
161     assertEquals(Buffer.Type.TEXT_BUFFER, buffer.getType());
162     assertTrue(!buffer.isDirty());
163     assertTrue(!buffer.isUpToDate());
164     assertEquals(file, buffer.getFile());
165     assertEquals(textSource, buffer.getTextSource());
166 
167     buffer.save();
168 
169     assertTrue(!buffer.isDirty());
170     assertTrue(buffer.isUpToDate());
171 
172     assertSame(s0, textSource.getText());
173 
174     textSource.setText(s1);
175     textSource.markDirty();
176 
177     assertTrue(buffer.isDirty());
178     assertTrue(buffer.isUpToDate());
179     assertSame(s1, textSource.getText());
180 
181     buffer.load();
182 
183     assertTrue(!buffer.isDirty());
184     assertTrue(buffer.isUpToDate());
185     assertEquals(canonicaliseLineEndings(s0), textSource.getText());
186     assertNotSame(s0, textSource.getText());
187 
188     // Add an error margin, as Linux does not support setting the modification
189     // date with millisecond precision.
190     assertTrue(file.setLastModified(System.currentTimeMillis() + 1000));
191 
192     assertTrue(!buffer.isUpToDate());
193 
194     buffer.load();
195 
196     assertTrue(buffer.isUpToDate());
197     assertEquals(textSource, buffer.getTextSource());
198   }
199 
200   private static String canonicaliseLineEndings(final String s0) {
201     return s0.replaceAll("\n", LINE_SEPARATOR) + LINE_SEPARATOR;
202   }
203 
204   public void testBufferWithLargeFile() throws Exception {
205     final char[] chars = "0123456789abcdef".toCharArray();
206     final char[] manyChars = new char[10000];
207 
208     for (int i=0; i<manyChars.length; ++i) {
209       manyChars[i] = chars[i % chars.length];
210     }
211 
212     final String s0 = new String(manyChars);
213     final StringTextSource textSource = new StringTextSource(s0);
214     assertSame(s0, textSource.getText());
215 
216     final File file = new File(getDirectory(), "myfile.txt");
217 
218     final Buffer buffer = new BufferImplementation(s_resources, textSource, file);
219 
220     assertEquals(Buffer.Type.TEXT_BUFFER, buffer.getType());
221     assertTrue(!buffer.isDirty());
222     assertTrue(!buffer.isUpToDate());
223     assertEquals(file, buffer.getFile());
224     assertEquals(textSource, buffer.getTextSource());
225 
226     buffer.save();
227 
228     assertTrue(!buffer.isDirty());
229     assertTrue(buffer.isUpToDate());
230     assertSame(s0, textSource.getText());
231 
232     buffer.load();
233 
234     assertTrue(!buffer.isDirty());
235     assertTrue(buffer.isUpToDate());
236     assertEquals(canonicaliseLineEndings(s0), textSource.getText());
237     assertNotSame(s0, textSource.getText());
238   }
239 
240   public void testBufferWithBadAssociatedFile() throws Exception {
241 
242     final StringTextSource textSource = new StringTextSource("");
243 
244     final Buffer buffer = new BufferImplementation(s_resources, textSource, getDirectory());
245 
246     try {
247       buffer.load();
248       fail("Expected DisplayMessageConsoleException");
249     }
250     catch (DisplayMessageConsoleException e) {
251       assertTrue(e.getCause() instanceof IOException);
252     }
253 
254     try {
255       buffer.save();
256       fail("Expected DisplayMessageConsoleException");
257     }
258     catch (DisplayMessageConsoleException e) {
259       assertTrue(e.getCause() instanceof IOException);
260     }
261   }
262 
263   private static final class ExtractReasonExpectation {
264     private final IOException m_ioException;
265     private final String m_reason;
266 
267     public ExtractReasonExpectation(IOException ioException, String reason) {
268       m_ioException = ioException;
269       m_reason = reason;
270     }
271 
272     public IOException getIOException() {
273       return m_ioException;
274     }
275 
276     public String getReason() {
277       return m_reason;
278     }
279   }
280 
281   public void testExtractReasonFromIOException() throws Exception {
282     final ExtractReasonExpectation[] wordsOfExpectation = {
283       new ExtractReasonExpectation(new EOFException("Blah"), ""),
284       new ExtractReasonExpectation(new IOException("Blah (foo)"), ""),
285       new ExtractReasonExpectation(new UnknownHostException("Blah"), ""),
286       new ExtractReasonExpectation(new FileNotFoundException("Blah"), ""),
287       new ExtractReasonExpectation(
288         new FileNotFoundException("Blah (Some info)"),
289         "Some info"),
290       new ExtractReasonExpectation(
291         new FileNotFoundException("Blah (invalid"),
292         ""),
293       new ExtractReasonExpectation(
294         new FileNotFoundException("Blah (a different message) (blah)"),
295         "a different message"),
296     };
297 
298     for (int i = 0; i < wordsOfExpectation.length; ++i) {
299       final ExtractReasonExpectation expectation = wordsOfExpectation[i];
300 
301       final String reason =
302         BufferImplementation.extractReasonFromIOException(expectation.getIOException());
303 
304       assertEquals(expectation.getReason(), reason);
305     }
306   }
307 }