Пример #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());
  }
Пример #2
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());
  }
Пример #3
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());
  }
Пример #4
0
  private void addBuffer(final Buffer buffer) {
    buffer
        .getTextSource()
        .addListener(
            new TextSource.Listener() {
              public void textSourceChanged(boolean dirtyStateChanged) {
                if (dirtyStateChanged) {
                  fireBufferStateChanged(buffer);
                }
              }
            });

    buffer.addListener(
        new BufferImplementation.Listener() {
          public void bufferSaved(Buffer buffer, File oldFile) {
            final File newFile = buffer.getFile();

            m_agentCacheState.setNewFileTime(newFile.lastModified());

            if (!newFile.equals(oldFile)) {
              if (oldFile != null) {
                m_fileBuffers.remove(oldFile);
              }

              m_fileBuffers.put(newFile, buffer);

              // Fire that bufferChanged because it is associated with a new
              // file.
              fireBufferStateChanged(buffer);
            }

            parseSelectedProperties(newFile);
          }
        });

    synchronized (m_bufferList) {
      m_bufferList.add(buffer);
    }

    m_listeners.apply(
        new ListenerSupport.Informer<Listener>() {
          public void inform(Listener l) {
            l.bufferAdded(buffer);
          }
        });
  }
Пример #5
0
  public void testGetType() throws Exception {
    final StringTextSource textSource = new StringTextSource("");

    final Expectation[] wordsOfExpectation = {
      new Expectation(Buffer.HTML_BUFFER, "somefile/blah.htm"),
      new Expectation(Buffer.HTML_BUFFER, "foo.html"),
      new Expectation(Buffer.JAVA_BUFFER, "eieio.java"),
      new Expectation(Buffer.MSDOS_BATCH_BUFFER, "eat/my.shorts.bat"),
      new Expectation(Buffer.MSDOS_BATCH_BUFFER, "alpha.cmd"),
      new Expectation(Buffer.PROPERTIES_BUFFER, "essential.properties"),
      new Expectation(Buffer.PYTHON_BUFFER, "why/oh.py"),
      new Expectation(Buffer.SHELL_BUFFER, "bishbosh.bash"),
      new Expectation(Buffer.SHELL_BUFFER, "clishclosh.csh"),
      new Expectation(Buffer.SHELL_BUFFER, "kkkkrassh.ksh"),
      new Expectation(Buffer.SHELL_BUFFER, "be/quiet.sh"),
      new Expectation(Buffer.TEXT_BUFFER, "tick.txt"),
      new Expectation(Buffer.TEXT_BUFFER, "tech.text"),
      new Expectation(Buffer.XML_BUFFER, "xplicitly.xml"),
      new Expectation(Buffer.UNKNOWN_BUFFER, "blurb/blah"),
      new Expectation(Buffer.UNKNOWN_BUFFER, "fidledly.foo"),
      new Expectation(Buffer.UNKNOWN_BUFFER, "bah/bah"),
      new Expectation(Buffer.UNKNOWN_BUFFER, "...."),
    };

    for (int i = 0; i < wordsOfExpectation.length; ++i) {
      final Expectation expectation = wordsOfExpectation[i];

      final Buffer buffer =
          new BufferImplementation(s_resources, textSource, expectation.getFile());

      assertEquals(expectation.getType(), buffer.getType());
      assertEquals(textSource, buffer.getTextSource());
    }

    assertEquals(Buffer.HTML_BUFFER, Buffer.HTML_BUFFER);
    AssertUtilities.assertNotEquals(Buffer.HTML_BUFFER, Buffer.TEXT_BUFFER);
    AssertUtilities.assertNotEquals(Buffer.TEXT_BUFFER, Buffer.HTML_BUFFER);
    AssertUtilities.assertNotEquals(Buffer.PROPERTIES_BUFFER, Buffer.UNKNOWN_BUFFER);
    assertEquals(Buffer.PYTHON_BUFFER, Buffer.PYTHON_BUFFER);
  }