Exemple #1
0
  public void testBufferWithAssociatedFile() throws Exception {

    final String s0 = "A shield for your eyes\na beast in the well on your hand";

    final String s1 =
        "Catch the mean beast\nin the well in the hell on the back\n"
            + "Watch out! You've got no shield\n"
            + "Break up! He's got no peace";

    final StringTextSource textSource = new StringTextSource(s0);
    assertSame(s0, textSource.getText());

    final File file = new File(getDirectory(), "myfile.txt");

    final Buffer buffer = new BufferImplementation(s_resources, textSource, file);

    assertEquals(Buffer.TEXT_BUFFER, buffer.getType());
    assertTrue(!buffer.isDirty());
    assertTrue(!buffer.isUpToDate());
    assertEquals(file, buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());

    buffer.save();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());

    assertSame(s0, textSource.getText());

    textSource.setText(s1);
    textSource.markDirty();

    assertTrue(buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertSame(s1, textSource.getText());

    buffer.load();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertEquals(canonicaliseLineEndings(s0), textSource.getText());
    assertNotSame(s0, textSource.getText());

    // Add an error margin, as Linux does not support setting the modification
    // date with millisecond precision.
    assertTrue(file.setLastModified(System.currentTimeMillis() + 1000));

    assertTrue(!buffer.isUpToDate());

    buffer.load();

    assertTrue(buffer.isUpToDate());
    assertEquals(textSource, buffer.getTextSource());
  }
  /**
   * Select the buffer for the given file.
   *
   * @param file The file.
   * @return The buffer.
   * @throws ConsoleException If a buffer could not be selected for the file.
   */
  public Buffer selectBufferForFile(File file) throws ConsoleException {
    final Buffer existingBuffer = getBufferForFile(file);
    final Buffer buffer;

    if (existingBuffer != null) {
      buffer = existingBuffer;

      selectBuffer(buffer);

      if (!buffer.isUpToDate()) {
        // The user's edits conflict with a file system change.
        // We ensure the buffer is selected before firing this event because
        // the UI might only raise out of date warnings for selected buffers.
        fireBufferNotUpToDate(buffer);
      }
    } else {
      buffer = new BufferImplementation(m_resources, m_textSourceFactory.create(), file);
      buffer.load();
      addBuffer(buffer);

      m_fileBuffers.put(file, buffer);

      selectBuffer(buffer);
    }

    return buffer;
  }
Exemple #3
0
  public void testBufferWithLargeFile() throws Exception {
    final char[] chars = "0123456789abcdef".toCharArray();
    final char[] manyChars = new char[10000];

    for (int i = 0; i < manyChars.length; ++i) {
      manyChars[i] = chars[i % chars.length];
    }

    final String s0 = new String(manyChars);
    final StringTextSource textSource = new StringTextSource(s0);
    assertSame(s0, textSource.getText());

    final File file = new File(getDirectory(), "myfile.txt");

    final Buffer buffer = new BufferImplementation(s_resources, textSource, file);

    assertEquals(Buffer.TEXT_BUFFER, buffer.getType());
    assertTrue(!buffer.isDirty());
    assertTrue(!buffer.isUpToDate());
    assertEquals(file, buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());

    buffer.save();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertSame(s0, textSource.getText());

    buffer.load();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertEquals(canonicaliseLineEndings(s0), textSource.getText());
    assertNotSame(s0, textSource.getText());
  }
Exemple #4
0
  public void testBufferWithBadAssociatedFile() throws Exception {

    final StringTextSource textSource = new StringTextSource("");

    final Buffer buffer = new BufferImplementation(s_resources, textSource, getDirectory());

    try {
      buffer.load();
      fail("Expected DisplayMessageConsoleException");
    } catch (DisplayMessageConsoleException e) {
      assertTrue(e.getCause() instanceof IOException);
    }

    try {
      buffer.save();
      fail("Expected DisplayMessageConsoleException");
    } catch (DisplayMessageConsoleException e) {
      assertTrue(e.getCause() instanceof IOException);
    }
  }
Exemple #5
0
  public void testBufferWithNoFile() throws Exception {
    final String text = "Some text for testing with";

    final TextSource textSource = new StringTextSource(text);

    assertEquals(text, textSource.getText());

    final Buffer buffer = new BufferImplementation(s_resources, textSource, "My Buffer");

    assertNotNull(textSource.getText());
    assertEquals(text, textSource.getText());
    assertEquals("My Buffer", buffer.getDisplayName());
    assertTrue(buffer.toString().indexOf(buffer.getDisplayName()) >= 0);

    try {
      buffer.load();
      fail("Expected EditorException");
    } catch (EditorException e) {
    }

    try {
      buffer.save();
      fail("Expected EditorException");
    } catch (EditorException e) {
    }

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertNull(buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());

    assertEquals(Buffer.UNKNOWN_BUFFER, buffer.getType());

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertNull(buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());
  }