/** @throws java.lang.Exception */
  @RandomlyFails
  public void testDocumentModification() throws Exception {

    // 1) register tasks and parsers
    MockServices.setServices(MockMimeLookup.class, MyScheduler.class);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    final CountDownLatch latch3 = new CountDownLatch(3);
    final int[] fooParser = {1};
    final int[] fooParserResult = {1};
    final int[] fooEmbeddingProvider = {1};
    final int[] fooTask = {1};
    final int[] booParser = {1};
    final int[] booParserResult = {1};
    final int[] booTask = {1};
    final TestComparator test =
        new TestComparator(
            "1 - reschedule all schedulers\n"
                + "foo get embeddings 1 (Snapshot 1), \n"
                + "Snapshot 1: Toto je testovaci file, na kterem se budou delat hnusne pokusy!!!, \n"
                + "Snapshot 2: stovaci fi, \n"
                + "foo parse 1 (Snapshot 1, FooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + "foo get result 1 (FooParserResultTask 1), \n"
                + "foo task 1 (FooResult 1 (Snapshot 1), SchedulerEvent 1), \n"
                + "foo invalidate 1, \n"
                + "boo parse 1 (Snapshot 2, BooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + "boo get result 1 (BooParserResultTask 1), \n"
                + "boo task 1 (BooResult 1 (Snapshot 2), SchedulerEvent 1), \n"
                + "boo invalidate 1, \n"
                + "2 - insert 14 chars on offset 22\n"
                + "foo get embeddings 1 (Snapshot 3), \n"
                + "Snapshot 3: Toto je testovaci file (druha verze), na kterem se budou delat hnusne pokusy!!!, \n"
                + "Snapshot 4: stovaci fi, \n"
                + "foo parse 1 (Snapshot 3, FooParserResultTask 1, SourceModificationEvent 18:37), \n"
                + "foo get result 1 (FooParserResultTask 1), \n"
                + "foo task 1 (FooResult 2 (Snapshot 3), SchedulerEvent 1), \n"
                + "foo invalidate 2, \n"
                + "boo parse 1 (Snapshot 4, BooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + // !! source unchanged
                "boo get result 1 (BooParserResultTask 1), \n"
                + "boo task 1 (BooResult 2 (Snapshot 4), SchedulerEvent 1), \n"
                + "boo invalidate 2, \n"
                + "3 - remove 5 chars on offset 44\n"
                + "foo get embeddings 1 (Snapshot 5), \n"
                + "Snapshot 5: Toto je testovaci file (druha verze), na ktee budou delat hnusne pokusy!!!, \n"
                + "Snapshot 6: stovaci fi, \n"
                + "foo parse 1 (Snapshot 5, FooParserResultTask 1, SourceModificationEvent 41:45), \n"
                + "foo get result 1 (FooParserResultTask 1), \n"
                + "foo task 1 (FooResult 3 (Snapshot 5), SchedulerEvent 2), \n"
                + "foo invalidate 3, \n"
                + "boo parse 1 (Snapshot 6, BooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + // !! source unchanged
                "boo get result 1 (BooParserResultTask 1), \n"
                + "boo task 1 (BooResult 3 (Snapshot 6), SchedulerEvent 2), \n"
                + "boo invalidate 3, \n"
                + "4 - end\n");

    MockMimeLookup.setInstances(
        MimePath.get("text/foo"),
        new ParserFactory() {
          public Parser createParser(Collection<Snapshot> snapshots2) {
            return new Parser() {

              private Snapshot last;
              private int i = fooParser[0]++;

              public void parse(Snapshot snapshot, Task task, SourceModificationEvent event)
                  throws ParseException {
                test.check(
                    "foo parse "
                        + i
                        + " (Snapshot "
                        + test.get(snapshot)
                        + ", "
                        + task
                        + ", "
                        + event
                        + "), \n");
                last = snapshot;
              }

              public Result getResult(Task task) throws ParseException {
                test.check("foo get result " + i + " (" + task + "), \n");
                return new Result(last) {

                  public void invalidate() {
                    test.check("foo invalidate " + i + ", \n");
                  }

                  private int i = fooParserResult[0]++;

                  @Override
                  public String toString() {
                    return "FooResult " + i + " (Snapshot " + test.get(getSnapshot()) + ")";
                  }
                };
              }

              public void cancel() {}

              public void addChangeListener(ChangeListener changeListener) {}

              public void removeChangeListener(ChangeListener changeListener) {}
            };
          }
        },
        new TaskFactory() {
          public Collection<SchedulerTask> create(Snapshot snapshot) {
            return Arrays.asList(
                new SchedulerTask[] {
                  new EmbeddingProvider() {

                    private int i = fooEmbeddingProvider[0]++;

                    public List<Embedding> getEmbeddings(Snapshot snapshot) {
                      test.check(
                          "foo get embeddings " + i + " (Snapshot " + test.get(snapshot) + "), \n");
                      test.check(
                          "Snapshot " + test.get(snapshot) + ": " + snapshot.getText() + ", \n");
                      Embedding embedding = snapshot.create(10, 10, "text/boo");
                      test.get(embedding.getSnapshot());
                      test.check(
                          "Snapshot "
                              + test.get(embedding.getSnapshot())
                              + ": "
                              + embedding.getSnapshot().getText()
                              + ", \n");
                      return Arrays.asList(new Embedding[] {embedding});
                    }

                    public int getPriority() {
                      return 10;
                    }

                    public void cancel() {}
                  },
                  new ParserResultTask() {

                    public void run(Result result, SchedulerEvent event) {
                      test.check(
                          "foo task "
                              + i
                              + " ("
                              + result
                              + ", SchedulerEvent "
                              + test.get(event)
                              + "), \n");
                    }

                    public int getPriority() {
                      return 100;
                    }

                    public Class<? extends Scheduler> getSchedulerClass() {
                      return Scheduler.EDITOR_SENSITIVE_TASK_SCHEDULER;
                    }

                    public void cancel() {}

                    private int i = fooTask[0]++;

                    @Override
                    public String toString() {
                      return "FooParserResultTask " + i;
                    }
                  }
                });
          }
        });
    MockMimeLookup.setInstances(
        MimePath.get("text/boo"),
        new ParserFactory() {
          public Parser createParser(Collection<Snapshot> snapshots2) {
            return new Parser() {

              private Snapshot last;
              private int i = booParser[0]++;

              public void parse(Snapshot snapshot, Task task, SourceModificationEvent event)
                  throws ParseException {
                test.check(
                    "boo parse "
                        + i
                        + " (Snapshot "
                        + test.get(snapshot)
                        + ", "
                        + task
                        + ", "
                        + event
                        + "), \n");
                last = snapshot;
              }

              public Result getResult(Task task) throws ParseException {
                test.check("boo get result " + i + " (" + task + "), \n");
                return new Result(last) {
                  public void invalidate() {
                    test.check("boo invalidate " + i + ", \n");
                    latch1.countDown();
                    latch2.countDown();
                    latch3.countDown();
                  }

                  private int i = booParserResult[0]++;

                  @Override
                  public String toString() {
                    return "BooResult " + i + " (Snapshot " + test.get(getSnapshot()) + ")";
                  }
                };
              }

              public void cancel() {}

              public void addChangeListener(ChangeListener changeListener) {}

              public void removeChangeListener(ChangeListener changeListener) {}
            };
          }
        },
        new TaskFactory() {
          public Collection<SchedulerTask> create(Snapshot snapshot) {
            return Arrays.asList(
                new SchedulerTask[] {
                  new ParserResultTask() {

                    private int i = booTask[0]++;

                    public void run(Result result, SchedulerEvent event) {
                      test.check(
                          "boo task "
                              + i
                              + " ("
                              + result
                              + ", SchedulerEvent "
                              + test.get(event)
                              + "), \n");
                    }

                    public int getPriority() {
                      return 150;
                    }

                    public Class<? extends Scheduler> getSchedulerClass() {
                      return Scheduler.EDITOR_SENSITIVE_TASK_SCHEDULER;
                    }

                    public void cancel() {}

                    @Override
                    public String toString() {
                      return "BooParserResultTask " + i;
                    }
                  }
                });
          }
        });

    // 2) create source file
    clearWorkDir();
    FileObject workDir = FileUtil.toFileObject(getWorkDir());
    FileObject testFile = FileUtil.createData(workDir, "bla.foo");
    FileUtil.setMIMEType("foo", "text/foo");
    OutputStream outputStream = testFile.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(outputStream);
    writer.append("Toto je testovaci file, na kterem se budou delat hnusne pokusy!!!");
    writer.close();
    Source source = Source.create(testFile);
    Document document = source.getDocument(true);
    document.putProperty("mimeType", "text/foo");
    document.putProperty(Language.class, new ALanguageHierarchy().language());
    TokenHierarchy th = TokenHierarchy.get(document);
    TokenSequence ts = th.tokenSequence();
    ts.tokenCount();
    test.check("1 - reschedule all schedulers\n");

    // 3) shcedulle CurrentDocumentScheduler
    for (Scheduler scheduler : Schedulers.getSchedulers())
      if (scheduler instanceof CurrentDocumentScheduler)
        ((CurrentDocumentScheduler) scheduler).schedule(source);
    latch1.await();
    test.check("2 - insert 14 chars on offset 22\n");

    document.insertString(22, " (druha verze)", null);
    latch2.await();
    test.check("3 - remove 5 chars on offset 44\n");

    document.remove(44, 5);
    latch3.await();
    test.check("4 - end\n");

    assertEquals("", test.getResult());
  }
예제 #2
0
 static {
   FileUtil.setMIMEType("test", "text/x-test");
 }