public void testFindTestSources() throws Exception {
   NbModuleProject p = generateStandaloneModule("p");
   FileObject test = p.getTestSourceDirectory("unit");
   FileObject oneTest = FileUtil.createData(test, "p/OneTest.java");
   FileObject r = FileUtil.createData(test, "p/r.png");
   FileObject otherTest = FileUtil.createData(test, "p/OtherTest.java");
   FileObject pkg = test.getFileObject("p");
   FileObject thirdTest = FileUtil.createData(test, "p2/ThirdTest.java");
   ModuleActions a = new ModuleActions(p);
   assertEquals("null", String.valueOf(a.findTestSources(Lookup.EMPTY, false)));
   assertEquals(
       "unit:p/OneTest.java",
       String.valueOf(a.findTestSources(Lookups.singleton(oneTest), false)));
   assertEquals(
       "unit:p/OneTest.java,p/OtherTest.java",
       String.valueOf(a.findTestSources(Lookups.fixed(oneTest, otherTest), false)));
   assertEquals("null", String.valueOf(a.findTestSources(Lookups.singleton(pkg), false)));
   assertEquals("null", String.valueOf(a.findTestSources(Lookups.singleton(r), false)));
   assertEquals("null", String.valueOf(a.findTestSources(Lookups.fixed(oneTest, r), false)));
   assertEquals("null", String.valueOf(a.findTestSources(Lookup.EMPTY, true)));
   assertEquals(
       "unit:p/OneTest.java", String.valueOf(a.findTestSources(Lookups.singleton(oneTest), true)));
   assertEquals(
       "unit:p/OneTest.java,p/OtherTest.java",
       String.valueOf(a.findTestSources(Lookups.fixed(oneTest, otherTest), true)));
   assertEquals("unit:p/**", String.valueOf(a.findTestSources(Lookups.singleton(pkg), true)));
   assertEquals(
       "unit:p/**,p2/ThirdTest.java",
       String.valueOf(a.findTestSources(Lookups.fixed(pkg, thirdTest), true)));
   assertEquals("null", String.valueOf(a.findTestSources(Lookups.singleton(r), true)));
   assertEquals("null", String.valueOf(a.findTestSources(Lookups.fixed(oneTest, r), true)));
 }
 private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException {
   try {
     ZipInputStream str = new ZipInputStream(source);
     ZipEntry entry;
     while ((entry = str.getNextEntry()) != null) {
       if (entry.isDirectory()) {
         FileUtil.createFolder(projectRoot, entry.getName());
       } else {
         FileObject fo = FileUtil.createData(projectRoot, entry.getName());
         FileLock lock = fo.lock();
         try {
           OutputStream out = fo.getOutputStream(lock);
           try {
             FileUtil.copy(str, out);
           } finally {
             out.close();
           }
         } finally {
           lock.releaseLock();
         }
       }
     }
   } finally {
     source.close();
   }
 }
 private static void unZipFile(InputStream source, FileObject projectRoot, WizardDescriptor wiz)
     throws IOException {
   try {
     ZipInputStream str = new ZipInputStream(source);
     ZipEntry entry;
     while ((entry = str.getNextEntry()) != null) {
       if (entry.isDirectory()) {
         FileUtil.createFolder(projectRoot, entry.getName());
       } else {
         FileObject fo = FileUtil.createData(projectRoot, entry.getName());
         if ("nbproject/project.xml".equals(entry.getName())) {
           // Special handling for setting name of Ant-based projects; customize as needed:
           filterProjectXML(fo, str, projectRoot.getName());
         }
         // if build.xml has been found replace its content with provided values
         else if ("build.xml".equals(entry.getName())) {
           processBuildXML(fo, str, wiz);
         } else if ("nbproject/project.properties".equals(entry.getName())) {
           processProjectProperties(fo, str, wiz, projectRoot.getName());
         } else {
           writeFile(str, fo);
         }
       }
     }
   } finally {
     source.close();
   }
 }
  public void testPreferencesEvents() throws Exception {
    storage.toFolder(true);
    FileUtil.getConfigRoot().addRecursiveListener(new FileListener());
    pref.addNodeChangeListener(new NodeListener());

    String newPath = "a/b/c";
    String[] paths = newPath.split("/");
    FileObject fo = null;
    FileObject fo0 = null;
    for (int i = 0; i < paths.length; i++) {
      String path = paths[i];
      fo = FileUtil.createFolder((fo == null) ? FileUtil.getConfigRoot() : fo, path);
      if (i == 0) {
        fo0 = fo;
      }
      nodeAddedEvent.await();
      assertEquals("Missing node added event", 0, nodeAddedEvent.getCount());
      nodeAddedEvent = new CountDownLatch(1);

      Preferences pref2 = pref.node(fo.getPath());
      pref2.addNodeChangeListener(new NodeListener());
    }

    FileObject fo1 = FileUtil.createData(fo, "a.properties");
    nodeAddedEvent.await();
    assertEquals("Missing node added event", 0, nodeAddedEvent.getCount());

    nodeRemovedEvent = new CountDownLatch(paths.length + 1);
    fo0.delete();
    nodeRemovedEvent.await();
    assertEquals("Missing node removed event", 0, nodeRemovedEvent.getCount());
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    MockServices.setServices(DL.class);

    fo = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), "data.dat");
    obj = DataObject.find(fo);
    assertEquals("MDO: " + obj, MDO.class, obj.getClass());
    node = obj.getNodeDelegate();
  }
  public void testInvalidChildrenNames() throws Exception {
    NbPreferences subnode = pref;
    assertNotNull(subnode);
    PropertiesStorage ps = (PropertiesStorage) pref.fileStorage;
    FileObject fold = ps.toFolder(true);
    assertNotNull(FileUtil.createData(fold, "a/b/c/invalid1"));
    subnode.sync();
    assertEquals(0, subnode.childrenNames().length);

    assertNotNull(FileUtil.createData(fold, "a/b/c/invalid2.huh"));
    subnode.sync();
    assertEquals(0, subnode.childrenNames().length);

    assertNotNull(FileUtil.createData(fold, "a/b/c/invalid3.properties.huh"));
    subnode.sync();
    assertEquals(0, subnode.childrenNames().length);

    assertNotNull(FileUtil.createData(fold, "a/b/c/valid.properties"));
    subnode.sync();
    assertEquals(1, subnode.childrenNames().length);
    assertEquals("a", subnode.childrenNames()[0]);
  }
 @Override
 protected FileSystem createFS(String... resources) throws IOException {
   for (String s : resources) {
     FileObject fo = FileUtil.getConfigFile(s.replaceAll("/.*", ""));
     if (fo != null) {
       fo.delete();
     }
   }
   FileSystem sfs = FileUtil.getConfigRoot().getFileSystem();
   for (String s : resources) {
     assertNotNull("creating: " + s, FileUtil.createData(sfs.getRoot(), s));
   }
   return sfs;
 }
示例#8
0
 private Tgm getModel(GtaModelReference gtaModelReference) {
   TgmDataObject tgmDO = null;
   Tgm model = null;
   String path = project.getProjectDirectory() + File.separator + gtaModelReference.getPath();
   try {
     File datasetF = new File(path);
     FileObject datasetFO = FileUtil.createData(datasetF);
     DataObject datasetDO = DataObject.find(datasetFO);
     if (datasetDO != null) {
       tgmDO = (TgmDataObject) datasetDO;
       model = tgmDO.getTgm();
     }
   } catch (DataObjectExistsException ex) {
     Exceptions.printStackTrace(ex);
   } catch (IOException ex) {
     Exceptions.printStackTrace(ex);
   }
   return model;
 }
示例#9
0
  private void createFile() {
    if (result != null && result.indexOf('\\') == -1) { // NOI18N

      if (!targetFolder.getPrimaryFile().canWrite()) {
        return;
      }

      DataObject dObj = null;

      try {
        FileObject fo =
            type == TYPE_FILE
                ? FileUtil.createData(targetFolder.getPrimaryFile(), result)
                : FileUtil.createFolder(targetFolder.getPrimaryFile(), result);
        if (fo != null) {
          dObj = DataObject.find(fo);
        }
      } catch (DataObjectNotFoundException e) {
        // No data object no open
      } catch (IOException e) {
        // XXX
      }

      if (result != null) {
        // handle new template in SystemFileSystem
        DataObject rootDO = findTemplate("/Templates"); // NOI18N
        if (rootDO != null && dObj != null) {
          if (FileUtil.isParentOf(rootDO.getPrimaryFile(), dObj.getPrimaryFile())) {
            try {
              dObj.setTemplate(true);
            } catch (IOException e) {
              // can ignore
            }
          }
        }
      }
      if (dObj != null) {
        ProjectUtilities.openAndSelectNewObject(dObj);
      }
    }
  }
 private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException {
   try {
     ZipInputStream str = new ZipInputStream(source);
     ZipEntry entry;
     while ((entry = str.getNextEntry()) != null) {
       if (entry.isDirectory()) {
         FileUtil.createFolder(projectRoot, entry.getName());
       } else {
         FileObject fo = FileUtil.createData(projectRoot, entry.getName());
         if ("nbproject/project.xml".equals(entry.getName())) {
           // Special handling for setting name of Ant-based projects; customize as needed:
           filterProjectXML(fo, str, projectRoot.getName());
         } else {
           writeFile(str, fo);
         }
       }
     }
   } finally {
     source.close();
   }
 }
示例#11
0
 private DatasetTimp[] getDatasets(GtaDatasetContainer gtaDatasetContainer) {
   TimpDatasetDataObject timpDatasetDO = null;
   int numberOfDatasets = gtaDatasetContainer.getDatasets().size();
   datasets = new DatasetTimp[numberOfDatasets];
   for (int i = 0; i < numberOfDatasets; i++) {
     GtaDataset gtaDataset = gtaDatasetContainer.getDatasets().get(i);
     String path = project.getProjectDirectory() + File.separator + gtaDataset.getPath();
     try {
       File datasetF = new File(path);
       FileObject datasetFO = FileUtil.createData(datasetF);
       DataObject datasetDO = DataObject.find(datasetFO);
       if (datasetDO != null) {
         timpDatasetDO = (TimpDatasetDataObject) datasetDO;
         datasets[i] = timpDatasetDO.getDatasetTimp();
       }
     } catch (DataObjectExistsException ex) {
       Exceptions.printStackTrace(ex);
     } catch (IOException ex) {
       Exceptions.printStackTrace(ex);
     }
   }
   return datasets;
 }
示例#12
0
 private Tgm getModel(String filename, String relativeFilePath) {
   TgmDataObject tgmDO;
   Tgm model = null;
   String newPath =
       project.getProjectDirectory()
           + File.separator
           + relativeFilePath
           + File.separator
           + filename;
   try {
     File datasetF = new File(newPath);
     FileObject datasetFO = FileUtil.createData(datasetF);
     DataObject datasetDO = DataObject.find(datasetFO);
     if (datasetDO != null) {
       tgmDO = (TgmDataObject) datasetDO;
       model = tgmDO.getTgm();
     }
   } catch (DataObjectExistsException ex) {
     Exceptions.printStackTrace(ex);
   } catch (IOException ex) {
     Exceptions.printStackTrace(ex);
   }
   return model;
 }
  /** @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());
  }