JSSource { public void insert(String str, int lineno, int colno) { final String sourceMethod = "insert"; if (isTraceLogging) { log.entering(JSSource.class.getName(), sourceMethod, new Object[]{str, lineno, colno}); } PositionLocator locator = new PositionLocator(lineno, colno); locator.insert(str); if (isTraceLogging) { log.exiting(JSSource.class.getName(), sourceMethod, "String \"" + str + "\" inserted at " + mid + "(" + lineno + "," + colno + ")"); } } JSSource(String source, String mid); void insert(String str, int lineno, int colno); PositionLocator locate(int lineno, int colno); void appendln(String str); @Override String toString(); void appendAfterArrayLit(Node array, String str); String getModuleId();  }
@Test public void testInsert() throws IOException { String str1 = "This is the first line"; String str2 = "This is the second line"; String str = str1 + "\r\n" + str2; JSSource source = new JSSource(str, null); Assert.assertEquals(str, source.toString()); source.insert("[insertfront]", 2, 0); Assert.assertEquals("This is the first line\r\n[insertfront]This is the second line", source.toString()); source.insert("[insertmiddle]", 2, 4); Assert.assertEquals("This is the first line\r\n[insertfront]This[insertmiddle] is the second line", source.toString()); source.insert("[insertend]", 2, str2.length()); Assert.assertEquals("This is the first line\r\n[insertfront]This[insertmiddle] is the second line[insertend]", source.toString()); try { source.insert("[fail]", 2, str2.length()+1); Assert.fail("Expected exception"); } catch (IllegalStateException ex) {} source = new JSSource(str, null); source.insert("[insertend]", 2, str2.length()); source.insert("[insertmiddle]", 2, 4); source.insert("[insertfront]", 2, 0); Assert.assertEquals("This is the first line\r\n[insertfront]This[insertmiddle] is the second line[insertend]", source.toString()); try { source.insert("[fail]", 2, str2.length()+1); Assert.fail("Expected exception"); } catch (IllegalStateException ex) {} try { source.insert("[fail]", 0, 0); Assert.fail("Expected exception"); } catch (IllegalStateException ex) {} try { source.insert("[fail]", 3, 0); Assert.fail("Expected exception"); } catch (IllegalStateException ex) {} try { source.insert("[fail]", 1, -1); Assert.fail("Expected exception"); } catch (IllegalStateException ex) {} }