Example #1
0
  public void testRemove() throws IOException {
    int i = 0;
    TextFile tf = new TextFile(TEST_FILE_NAME);

    for (String name : tf) {
      System.out.println("::: " + name);
      assertEquals(names.get(i++), name);
    }

    // testing remove()
    for (Iterator<String> it = tf.iterator(); it.hasNext(); ) {
      String name = it.next();
      if ("Dirk".equals(name)) {
        it.remove();
        System.out.println("removing dirk");
      }
    }

    names.remove("Dirk");

    i = 0;
    for (String name : tf) {
      System.out.println("::: " + name);
      assertEquals(names.get(i++), name);
    }
  }
Example #2
0
 // Simple test:
 public static void main(String[] args) {
   String file = read("TextFile.java");
   write("test.txt", file);
   TextFile text = new TextFile("test.txt");
   text.write("test2.txt");
   // Break into unique sorted list of words:
   TreeSet<String> words = new TreeSet<String>(new TextFile("TextFile.java", "\\W+"));
   // Display the capitalized words:
   System.out.println(words.headSet("a"));
 }
Example #3
0
  public void testBadRemove() throws IOException {
    TextFile tf = new TextFile(TEST_FILE_NAME);
    Iterator<String> it = tf.iterator();
    try {
      it.remove();
      fail("Expected an IllegalStateException");
    } catch (IllegalStateException success) {

    }
  }
Example #4
0
  public void testAllRemove() throws IOException {
    TextFile tf = new TextFile(TEST_FILE_NAME);

    // testing remove() on first element
    Iterator<String> it = tf.iterator();
    while (it.hasNext()) {
      it.next();
      it.remove();
    }

    it = tf.iterator();
    assertFalse(it.hasNext());
  }
Example #5
0
  public void testFirstRemove() throws IOException {
    int i = 0;
    TextFile tf = new TextFile(TEST_FILE_NAME);

    // testing remove() on first element
    Iterator<String> it = tf.iterator();
    it.next();
    it.remove();

    names.remove("John");

    i = 0;
    for (String name : tf) {
      System.out.println("::: " + name);
      assertEquals(names.get(i++), name);
    }
  }
Example #6
0
  public void testConcurrentModificationException() throws IOException {
    TextFile tf = new TextFile(TEST_FILE_NAME);

    Iterator<String> broken = tf.iterator();

    for (Iterator<String> it = tf.iterator(); it.hasNext(); ) {
      String name = it.next();
      if ("Dirk".equals(name)) {
        it.remove();
      }
    }
    try {
      broken.next();
      fail("Expected a ConcurrentModificationException");
    } catch (ConcurrentModificationException success) {
    }
  }
  @Test
  public void parseTextFileUsingBufferedReader(
      @Mocked final BufferedReader reader, @Mocked FileReader fileReader) throws Exception {
    new NonStrictExpectations() {
      {
        reader.readLine();
        returns("line1", "another,line", null);
      }
    };

    TextFile textFile = new TextFile("file");
    List<String[]> result = textFile.parse();

    assertResultFromTextFileParsing(result);

    new Verifications() {
      {
        reader.close();
      }
    };
  }
  @Test
  public void parseTextFileUsingInterface(@Mocked final TextReader reader) throws Exception {
    new NonStrictExpectations() {
      {
        reader.readLine();
        returns("line1", "another,line", null);
      }
    };

    TextFile textFile = new TextFile(reader, 100);
    List<String[]> result = textFile.parse();

    assertResultFromTextFileParsing(result);

    new VerificationsInOrder() {
      {
        reader.skip(100);
        reader.close();
      }
    };
  }