예제 #1
0
  public void testUncheckedIOException() throws IOException {
    Path triggerFile = testFolder.resolve(Paths.get("dir2", "IOException"));
    Files.createFile(triggerFile);
    Path triggerDir = testFolder.resolve(Paths.get("empty", "IOException"));
    Files.createDirectories(triggerDir);
    Files.createFile(triggerDir.resolve("file"));
    FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
    FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);

    try {
      fsp.setFaultyMode(false);
      Path fakeRoot = fs.getRoot();
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
        // only one file
        s.forEach(path -> assertEquals(path.getFileName().toString(), "IOException"));
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        // ordered as depth-first
        assertEquals(result, new String[] {"empty", "IOException", "file"});
      }

      fsp.setFaultyMode(true);
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
        s.forEach(path -> fail("should have caused exception"));
      } catch (UncheckedIOException uioe) {
        assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to IOException");
      } catch (UncheckedIOException uioe) {
        assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty").resolve("IOException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to IOException");
      } catch (IOException ioe) {
        assertTrue(ioe instanceof FaultyFileSystem.FaultyException);
      } catch (UncheckedIOException ex) {
        fail("Top level should be repored as is");
      }
    } finally {
      // Cleanup
      if (fs != null) {
        fs.close();
      }
      Files.delete(triggerFile);
      TestUtil.removeAll(triggerDir);
    }
  }
예제 #2
0
  public void testBasic() {
    try (Stream<Path> s = Files.list(testFolder)) {
      Object[] actual = s.sorted().toArray();
      assertEquals(actual, level1);
    } catch (IOException ioe) {
      fail("Unexpected IOException");
    }

    try (Stream<Path> s = Files.list(testFolder.resolve("empty"))) {
      int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
      assertEquals(count, 0, "Expect empty stream.");
    } catch (IOException ioe) {
      fail("Unexpected IOException");
    }
  }
예제 #3
0
  public void testDirectoryIteratorException() throws IOException {
    Path dir = testFolder.resolve("dir2");
    Path trigger = dir.resolve("DirectoryIteratorException");
    Files.createFile(trigger);
    FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
    FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(dir, null);

    try {
      fsp.setFaultyMode(false);
      Path fakeRoot = fs.getRoot();
      try {
        try (Stream<Path> s = Files.list(fakeRoot)) {
          s.forEach(
              path -> assertEquals(path.getFileName().toString(), "DirectoryIteratorException"));
        }
      } catch (UncheckedIOException uioe) {
        fail("Unexpected exception.");
      }

      fsp.setFaultyMode(true);
      try {
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(fakeRoot)) {
          Iterator<Path> itor = ds.iterator();
          while (itor.hasNext()) {
            itor.next();
          }
        }
        fail("Shoule throw DirectoryIteratorException");
      } catch (DirectoryIteratorException die) {
      }

      try {
        try (Stream<Path> s = Files.list(fakeRoot)) {
          s.forEach(path -> fail("should not get here"));
        }
      } catch (UncheckedIOException uioe) {
        assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
      } catch (DirectoryIteratorException die) {
        fail("Should have been converted into UncheckedIOException.");
      }
    } finally {
      // Cleanup
      if (fs != null) {
        fs.close();
      }
      Files.delete(trigger);
    }
  }
예제 #4
0
 public static List<Path> listDir(Path dir) throws IOException {
   List<Path> contents = new LinkedList<>();
   try (Stream<Path> list = Files.list(dir)) {
     list.forEach(contents::add);
   }
   return contents;
 }
예제 #5
0
 @Test
 public void testRhythmWeight() throws InvalidMidiDataException, IOException {
   List<File> midiFiles =
       Files.list(new File("C:/Users/prombouts/git/neo/neo/resources/melodies/solo/").toPath())
           .map(p -> p.toFile())
           .collect(Collectors.toList());
   for (File file : midiFiles) {
     MidiInfo midiInfo = midiParser.readMidi(file);
     List<Note> notes = midiInfo.getMelodies().get(0).getNotes();
     rhythmWeight.setNotes(notes);
     rhythmWeight.updateRhythmWeight();
     System.out.println(file.getName());
     double profileAverage = rhythmObjective.getProfileAverage(notes, 3.0);
     System.out.println(profileAverage);
     //			for (Note note : notes) {
     //				System.out.print(note.getPitch() + ", " + note.getPositionWeight() + "; ");
     //			}
     //			System.out.println();
     //			Score score = new Score();
     //			Phrase phrase = scoreUtilities.createPhrase(notes);
     //			Part part = new Part(phrase);
     //			score.add(part);
     //			View.notate(score);
     //			Play.midi(score, true);
   }
 }
예제 #6
0
  public void testClosedStream() throws IOException {
    try (Stream<Path> s = Files.list(testFolder)) {
      s.close();
      Object[] actual = s.sorted().toArray();
      fail("Operate on closed stream should throw IllegalStateException");
    } catch (IllegalStateException ex) {
      // expected
    }

    try (Stream<Path> s = Files.walk(testFolder)) {
      s.close();
      Object[] actual = s.sorted().toArray();
      fail("Operate on closed stream should throw IllegalStateException");
    } catch (IllegalStateException ex) {
      // expected
    }

    try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, (p, attr) -> true)) {
      s.close();
      Object[] actual = s.sorted().toArray();
      fail("Operate on closed stream should throw IllegalStateException");
    } catch (IllegalStateException ex) {
      // expected
    }
  }
예제 #7
0
 @Test
 public void dirTest() {
   try {
     Files.list(Paths.get(".")).filter(Files::isDirectory).forEach(System.out::println);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
예제 #8
0
 @Test
 public void arrayTest() {
   try {
     Files.list(Paths.get(".")).forEach(System.out::println);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
예제 #9
0
 @PostConstruct
 @PreDestroy
 private void deleteTemporaryFiles() {
   try {
     Files.list(getTemporaryDir().toPath()).forEach(path -> path.toFile().delete());
   } catch (IOException e) {
     throw new InfrastructureException(e);
   }
 }
예제 #10
0
  public static void main(String[] args) throws IOException {
    try (Stream<Path> stream = Files.list(Paths.get(""))) {
      String joined =
          stream
              .map(String::valueOf)
              .filter(path -> !path.startsWith("."))
              .sorted()
              .collect(Collectors.joining("; "));
      System.out.println("List: " + joined);
    }

    Path start = Paths.get("");
    int maxDepth = 5;
    try (Stream<Path> stream =
        Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".java"))) {
      String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; "));
      System.out.println("Found: " + joined);
    }

    try (Stream<Path> stream = Files.walk(start, maxDepth)) {
      String joined =
          stream
              .map(String::valueOf)
              .filter(path -> path.endsWith(".java"))
              .sorted()
              .collect(Collectors.joining("; "));
      System.out.println("walk(): " + joined);
    }

    List<String> lines = Files.readAllLines(Paths.get("src/golf.sh"));
    lines.add("puts 'foobar'");
    Path path = Paths.get("src/golf-modified.sh");
    Files.write(path, lines);

    try (Stream<String> stream = Files.lines(path)) {
      stream.filter(line -> line.contains("puts")).map(String::trim).forEach(System.out::println);
    }

    System.out.println("a" == "a");
    System.out.println("a" != new String("a"));
    System.out.println(null != "a");
    System.out.println("a".equals("a"));

    try (BufferedReader reader = Files.newBufferedReader(path)) {
      while (reader.ready()) System.out.println(reader.readLine());
    }

    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("hello-world.sh"))) {
      writer.write("puts 'Hello world'");
    }

    try (BufferedReader reader = Files.newBufferedReader(path)) {
      long countPuts = reader.lines().filter(line -> line.contains("put")).count();
      System.out.println(countPuts);
    }
  }
예제 #11
0
 public static void m3(Path pathToDir) throws IOException {
   // 读取目录,但不是递归包含的
   try (Stream<Path> entris = Files.list(pathToDir)) {
     entris.forEach(System.out::println);
   }
   // 可以读取子目录,递归的
   try (Stream<Path> entris = Files.walk(pathToDir)) {
     entris.forEach(System.out::println);
   }
 }
 public List<Path> getAvailableDevices() throws IOException {
   List<Path> deviceList = Collections.emptyList();
   try {
     deviceList = Files.list(Paths.get(deviceBasePath)).collect(Collectors.toList());
   } catch (Exception e) {
     e.printStackTrace();
     throw new IOException(DEFAULT_ERROR_MESSAGE, e);
   }
   return deviceList;
 }
예제 #13
0
 public void loadServerConfigFiles() {
   if (!Files.exists(serverStorageDir)) {
     LOGGER.info("Server storage directory doesn't exist, not loading anything");
     return;
   }
   try (Stream<Path> files = Files.list(serverStorageDir)) {
     files
         .filter(p -> p.getFileName().toString().endsWith(".json"))
         .forEach(this::loadServerConfig);
   } catch (IOException e) {
     LOGGER.warn("Unable to load server storage files", e);
     return;
   }
 }
  @Test
  public void testi10nFiles() throws IOException {
    for (Path p : Files.list(Paths.get("src/main/resources/l10n")).collect(Collectors.toList())) {
      String[] parts = p.getFileName().toString().split("\\.")[0].split("_");
      String prefix = "l10n/" + parts[0];
      Locale locale;
      if (parts.length == 3) {
        locale = new Locale(parts[1], parts[2]);
      } else {
        locale = new Locale(parts[1]);
      }

      checkPropertiesFile(locale, prefix);
    }
  }
  public static void main(String[] args) {
    // Methods in Comparator
    List<Person> people = new ArrayList<>();
    people.sort(
        Comparator.comparing(Person::getLastName)
            .thenComparing(Person::getFirstName)
            .thenComparing(
                Person::getEmailAddress, Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)));

    // Old way of initializing ThreadLocal:
    ThreadLocal<List<String>> oldThreadLocalString =
        new ThreadLocal<List<String>>() {
          @Override
          public List<String> initialValue() {
            return new ArrayList<>();
          }
        };
    System.out.println(oldThreadLocalString.get());

    // New way:
    ThreadLocal<List<String>> newThreadLocalString = ThreadLocal.withInitial(ArrayList::new);
    System.out.println(newThreadLocalString.get());

    // Java Optional
    Optional<Integer> optional = new ArrayList<Integer>().stream().min(Integer::compareTo);
    System.out.println(optional);

    // Files can now return streams
    try {
      Stream stream = Files.list(Paths.get("c:\\temp\\"));
      stream = Files.lines(Paths.get("c:\\temp\\"), Charset.forName("UTF_32"));
      stream = Files.find(Paths.get("c:\\"), 5, (T, U) -> (T == U));

    } catch (IOException e) {
      UncheckedIOException ex = new UncheckedIOException("cause", e);
      System.out.println(ex.getMessage());
    }

    // Rejoice, for we finally have string joins!
    String joinExample = String.join(",", "a", "b", "c", "4", "E", "6");
    System.out.println(joinExample);

    StringJoiner joiner = new StringJoiner("-");
    joiner.add("abbie");
    joiner.add("doobie");
    System.out.println(joiner.toString());
  }
예제 #16
0
 @Test
 public void iso88599TOeuckr() throws IOException {
   Stream<Path> s = Files.list(new File(CrawlerUtils.getResourcePath("convert")).toPath());
   Iterator<Path> iter = s.iterator();
   while (iter.hasNext()) {
     Path p = iter.next();
     LOG.info(
         new String(p.getFileName().toString().getBytes("ISO-8859-1"), Charset.forName("euc-kr")));
     String newName =
         new String(p.getFileName().toString().getBytes("ISO-8859-1"), Charset.forName("euc-kr"));
     p.toFile().renameTo(p.getParent().resolve(newName).toFile());
   }
   s.close();
   LOG.info(
       new String(
           "/tmp/firefox-downloads/hello".getBytes("ISO-8859-1"), Charset.forName("euc-kr")));
 }
예제 #17
0
  public static void main(String[] args) throws IOException {
    /** Walk in catalog */
    Files.list(new File("e:\\").toPath())
        .filter(i -> i.getFileName().toString().contains("C"))
        .limit(7)
        .forEach(System.out::println);

    /** Read File */
    List<String> strings = Files.lines(Paths.get("1.txt")).collect(Collectors.<String>toList());
    //        strings.forEach(System.out::println);

    /** Change File */
    Files.lines(Paths.get("1.txt"))
        .map(String::toUpperCase)
        .filter(i -> i.length() > 2)
        .forEach(System.out::println);
  }
예제 #18
0
  /**
   * Delete everything recursively
   *
   * @param path
   */
  public static void recursiveDelete(Path path) throws IOException {
    logger.debug("Deleting %s", path);
    final Iterator<Path> it = Files.list(path).iterator();
    while (it.hasNext()) {
      final Path entry = it.next();

      logger.debug("Considering " + entry);

      if (Files.isDirectory(entry)) {
        recursiveDelete(entry);
      } else {
        Files.delete(entry);
      }
    }

    // Deleting self
    Files.delete(path);
  }
예제 #19
0
  private static void iterate(Integer count) {

    /*
    Create folder with FILE_COUNT files in it.
     */
    System.out.println("calling runtime.gc() and creating temp files, count=" + count);
    runtime.gc();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    runtime.gc();
    try {
      if (TEMP_DIR.exists()) {
        Files.list(TEMP_DIR.toPath())
            .forEach(
                path -> {
                  try {
                    Files.delete(path);
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                });
        Files.delete(TEMP_DIR.toPath());
      }
      Files.createDirectory(TEMP_DIR.toPath());
    } catch (IOException e) {
      e.printStackTrace();
    }
    for (Integer n = count; n < count * 2; n++) {
      File tmp = new File(TEMP_DIR + FILE_SEPARATOR + "temp-" + n.toString() + ".tmp");
      try {
        FileOutputStream fos = new FileOutputStream(tmp);
        fos.write(0x13);
        fos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    /*
    File.listFiles()
     */
    int countListFiles = 0;
    long startListFiles = System.nanoTime();
    long beforeListFile = runtime.totalMemory() - runtime.freeMemory();
    //noinspection ConstantConditions
    for (File f : TEMP_DIR.listFiles()) {
      if (f == null) {
        System.out.println("file is null");
      } else if (f.length() != 1) {
        System.out.println("file " + f.getName() + " size=" + f.length() + " not 1");
      }
      countListFiles += 1;
    }
    long stopListFiles = System.nanoTime();
    long afterListFile = runtime.totalMemory() - runtime.freeMemory();

    /*
    Files.newDirectoryStream
     */
    int countDirectoryStream = 0;
    long startDirectoryStream = System.nanoTime();
    long beforeDirectoryStream = runtime.totalMemory() - runtime.freeMemory();
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(TEMP_DIR.toPath())) {
      for (Path p : directoryStream) {
        File pfile = p.toFile();
        if (pfile.length() != 1) {
          System.out.println("file " + pfile.getName() + " size=" + pfile.length() + " not 1");
        }
        countDirectoryStream += 1;
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    long stopDirectoryStream = System.nanoTime();
    long afterDirectoryStream = runtime.totalMemory() - runtime.freeMemory();

    /*
    Files.walkFileTree
     */
    int countWalkFileTree = 0;
    long startWalkFileTree = System.nanoTime();
    long beforeWalkFileTree = runtime.totalMemory() - runtime.freeMemory();
    try {
      CountingFileVisitor<Path> visitor = new CountingFileVisitor<>();
      Files.walkFileTree(TEMP_DIR.toPath(), visitor);
      countWalkFileTree = visitor.getCount();
    } catch (IOException e) {
      e.printStackTrace();
    }
    long stopWalkFileTree = System.nanoTime();
    long afterWalkFileTree = runtime.totalMemory() - runtime.freeMemory();

    /*
    Files.list
     */
    AtomicInteger countFilesList = new AtomicInteger(0);
    long startFilesList = System.nanoTime();
    long beforeFilesList = runtime.totalMemory() - runtime.freeMemory();
    try {
      Files.list(TEMP_DIR.toPath())
          .forEach(
              path -> {
                File pfile = path.toFile();
                if (pfile.length() != 1) {
                  System.out.println(
                      "file " + pfile.getName() + " size=" + pfile.length() + " not 1");
                }
                countFilesList.incrementAndGet();
              });
    } catch (IOException e) {
      e.printStackTrace();
    }
    long stopFilesList = System.nanoTime();
    long afterFilesList = runtime.totalMemory() - runtime.freeMemory();

    System.out.println(
        "      listFiles count="
            + countListFiles
            + " duration="
            + (stopListFiles - startListFiles)
            + " memory="
            + beforeListFile
            + "-"
            + afterListFile
            + " ("
            + (afterListFile - beforeListFile)
            + ")");
    System.out.println(
        "directoryStream count="
            + countDirectoryStream
            + " duration="
            + (stopDirectoryStream - startDirectoryStream)
            + " memory="
            + beforeDirectoryStream
            + "-"
            + afterDirectoryStream
            + " ("
            + (afterDirectoryStream - beforeDirectoryStream)
            + ")");
    System.out.println(
        "   walkFileTree count="
            + countWalkFileTree
            + " duration="
            + (stopWalkFileTree - startWalkFileTree)
            + " memory="
            + beforeWalkFileTree
            + "-"
            + afterWalkFileTree
            + " ("
            + (afterWalkFileTree - beforeWalkFileTree)
            + ")");
    System.out.println(
        "      filesList count="
            + countFilesList
            + " duration="
            + (stopFilesList - startFilesList)
            + " memory="
            + beforeFilesList
            + "-"
            + afterFilesList
            + " ("
            + (afterFilesList - beforeFilesList)
            + ")");

    /*
    Cleanup temp files
     */
    System.out.println("cleaning up temp files\n");
    for (Integer n = count; n < count * 2; n++) {
      File tmp = new File(TEMP_DIR + FILE_SEPARATOR + "temp-" + n.toString() + ".tmp");
      try {
        Files.delete(tmp.toPath());
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    try {
      Files.delete(TEMP_DIR.toPath());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
예제 #20
0
  public void reloadSettings() {
    this.overrideSettings = null;

    Path cpath = this.resolvePath("/config");

    if ((cpath == null) || Files.notExists(cpath)) return;

    Path cspath = cpath.resolve("settings.xml");

    if (Files.exists(cspath)) {
      FuncResult<CharSequence> res = IOUtil.readEntireFile(cspath);

      if (res.isEmptyResult()) return;

      FuncResult<XElement> xres = XmlReader.parse(res.getResult(), true);

      if (xres.isEmptyResult()) return;

      this.overrideSettings = xres.getResult();

      if (this.overrideSettings.hasAttribute("Locale"))
        this.locale = this.overrideSettings.getAttribute("Locale");
    }

    // TODO check for and load dictionaries, variables, etc

    this.schema = null;

    Path shpath = cpath.resolve("schema.xml");

    if (Files.exists(shpath)) {
      this.schema = new SchemaManager();
      this.schema.setChain(Hub.instance.getSchema());
      this.schema.loadSchema(shpath);
      this.schema.compile();
    }

    // dictionary

    this.dictionary = null;

    Path dicpath = cpath.resolve("dictionary.xml");

    if (Files.exists(dicpath)) {
      this.dictionary = new Dictionary();
      this.dictionary.setParent(Hub.instance.getResources().getDictionary());
      this.dictionary.load(dicpath);

      this.localedef = this.getLocaleDefinition(this.getDefaultLocale());
    }

    this.registered.clear();
    this.routers.clear();

    Path dpath = this.resolvePath("");
    Path spath = dpath.resolve("services");

    if (Files.exists(spath)) {
      try (Stream<Path> str = Files.list(spath)) {
        str.forEach(
            path -> {
              // only directories are services - files in dir are features
              if (!Files.isDirectory(path)) return;

              String name = path.getFileName().toString();

              this.registerService(new DomainServiceAdapter(name, path, dpath));
            });
      } catch (IOException x) {
        // TODO Auto-generated catch block
        x.printStackTrace();
      }
    }

    // watcher comes after services so it can register a service if it likes... if this came before
    // it would be cleared from the registered list
    if (this.watcher == null) this.watcher = new DomainWatcherAdapter(dpath);

    this.watcher.init(this);

    for (Bucket b : this.buckets.values()) b.tryExecuteMethod("Kill", this);

    this.buckets.clear();

    this.prepDomainSchedule();
  }
예제 #21
0
파일: DataDiff.java 프로젝트: digoal/metl
  protected void calculateDiff(ISendMessageCallback callback) {
    Map<ModelEntity, String> changeSqls = new HashMap<>();
    Map<ModelEntity, String> addSqls = new HashMap<>();
    Map<ModelEntity, String> delSqls = new HashMap<>();
    Component component = context.getFlowStep().getComponent();
    for (ModelEntity entity : entities) {
      StringBuilder addSql = new StringBuilder("select ");
      StringBuilder chgSql = new StringBuilder(addSql);
      StringBuilder delSql = new StringBuilder(addSql);
      appendColumns(addSql, "curr.", entity);
      appendColumns(delSql, "orig.", entity);
      appendColumns(chgSql, "curr.", entity);

      addSql.append(
          " from " + entity.getName() + "_2 curr left join " + entity.getName() + "_1 orig on ");
      delSql.append(
          " from " + entity.getName() + "_1 orig left join " + entity.getName() + "_2 curr on ");
      chgSql.append(
          " from " + entity.getName() + "_1 orig join " + entity.getName() + "_2 curr on ");
      boolean secondPk = false;
      for (ModelAttribute attribute : entity.getModelAttributes()) {
        if (attribute.isPk()) {
          if (secondPk) {
            addSql.append(" and ");
            delSql.append(" and ");
            chgSql.append(" and ");
          }
          addSql
              .append("curr.")
              .append(attribute.getName())
              .append("=")
              .append("orig.")
              .append(attribute.getName());
          delSql
              .append("curr.")
              .append(attribute.getName())
              .append("=")
              .append("orig.")
              .append(attribute.getName());
          chgSql
              .append("curr.")
              .append(attribute.getName())
              .append("=")
              .append("orig.")
              .append(attribute.getName());
          secondPk = true;
        }
      }

      addSql.append(" where ");
      delSql.append(" where ");
      chgSql.append(" where ");
      secondPk = false;
      boolean secondCol = false;
      for (ModelAttribute attribute : entity.getModelAttributes()) {
        if (attribute.isPk()) {
          if (secondPk) {
            addSql.append(" or ");
            delSql.append(" or ");
          }
          addSql.append("orig.").append(attribute.getName()).append(" is null");
          delSql.append("curr.").append(attribute.getName()).append(" is null");
          secondPk = true;
        } else {
          ComponentAttributeSetting matchColumnSetting =
              component.getSingleAttributeSetting(
                  attribute.getId(), DataDiff.ATTRIBUTE_COMPARE_ENABLED);
          boolean matchColumn =
              matchColumnSetting != null
                  ? Boolean.parseBoolean(matchColumnSetting.getValue())
                  : true;
          if (matchColumn) {
            if (secondCol) {
              chgSql.append(" or ");
            }
            chgSql
                .append("curr.")
                .append(attribute.getName())
                .append(" != ")
                .append("orig.")
                .append(attribute.getName());
            chgSql.append(" or ");
            chgSql
                .append("curr.")
                .append(attribute.getName())
                .append(" is null and ")
                .append("orig.")
                .append(attribute.getName())
                .append(" is not null ");
            chgSql.append(" or ");
            chgSql
                .append("curr.")
                .append(attribute.getName())
                .append(" is not null and ")
                .append("orig.")
                .append(attribute.getName())
                .append(" is null ");
            secondCol = true;
          }
        }
      }

      // we only want to do a change compare if this entity has
      // cols to compare other than the primary key.
      if (!entity.hasOnlyPrimaryKeys() && secondCol) {
        changeSqls.put(entity, chgSql.toString());
        log(LogLevel.INFO, "Generated diff sql for CHG: %s", chgSql);
      }
      log(LogLevel.INFO, "Generated diff sql for ADD: %s", addSql);
      log(LogLevel.INFO, "Generated diff sql for DEL: %s", delSql);
      addSqls.put(entity, addSql.toString());
      delSqls.put(entity, delSql.toString());
    }

    RdbmsReader reader = new RdbmsReader();
    reader.setDataSource(databasePlatform.getDataSource());
    reader.setContext(context);
    reader.setComponentDefinition(componentDefinition);
    reader.setRowsPerMessage(rowsPerMessage);
    reader.setThreadNumber(threadNumber);

    for (ModelEntity entity : entities) {
      ComponentEntitySetting add =
          component.getSingleEntitySetting(entity.getId(), DataDiff.ENTITY_ADD_ENABLED);
      ComponentEntitySetting chg =
          component.getSingleEntitySetting(entity.getId(), DataDiff.ENTITY_CHG_ENABLED);
      boolean addEnabled = add != null ? Boolean.parseBoolean(add.getValue()) : true;
      boolean chgEnabled = chg != null ? Boolean.parseBoolean(chg.getValue()) : true;
      if (addEnabled) {
        reader.setSql(addSqls.get(entity));
        reader.setEntityChangeType(ChangeType.ADD);
        reader.handle(new ControlMessage(this.context.getFlowStep().getId()), callback, false);
        info("Sent %d ADD records for %s", reader.getRowReadDuringHandle(), entity.getName());
      }

      if (chgEnabled && changeSqls.get(entity) != null) {
        reader.setSql(changeSqls.get(entity));
        reader.setEntityChangeType(ChangeType.CHG);
        reader.handle(new ControlMessage(this.context.getFlowStep().getId()), callback, false);
        info("Sent %d CHG records for %s", reader.getRowReadDuringHandle(), entity.getName());
      }
    }

    for (int i = entities.size() - 1; i >= 0; i--) {
      ModelEntity entity = entities.get(i);
      ComponentEntitySetting del =
          component.getSingleEntitySetting(entity.getId(), DataDiff.ENTITY_DEL_ENABLED);
      boolean delEnabled = del != null ? Boolean.parseBoolean(del.getValue()) : true;

      if (delEnabled) {
        reader.setSql(delSqls.get(entity));
        reader.setEntityChangeType(ChangeType.DEL);
        reader.handle(new ControlMessage(this.context.getFlowStep().getId()), callback, false);
        info("Sent %d DEL records for %s", reader.getRowReadDuringHandle(), entity.getName());
      }
    }

    ResettableBasicDataSource ds = databasePlatform.getDataSource();

    ds.close();

    if (!inMemoryCompare) {
      try {
        Files.list(Paths.get(System.getProperty("h2.baseDir")))
            .filter(path -> path.toFile().getName().startsWith(databaseName))
            .forEach(path -> deleteDatabaseFile(path.toFile()));
      } catch (IOException e) {
        log.warn("Failed to delete file", e);
      }
    }

    databasePlatform = null;
    databaseName = null;
    databaseWriter = null;
  }
예제 #22
0
  public void testSecurityException() throws IOException {
    Path empty = testFolder.resolve("empty");
    Path triggerFile = Files.createFile(empty.resolve("SecurityException"));
    Path sampleFile = Files.createDirectories(empty.resolve("sample"));

    Path dir2 = testFolder.resolve("dir2");
    Path triggerDir = Files.createDirectories(dir2.resolve("SecurityException"));
    Files.createFile(triggerDir.resolve("fileInSE"));
    Path sample = Files.createFile(dir2.resolve("file"));

    Path triggerLink = null;
    Path linkTriggerDir = null;
    Path linkTriggerFile = null;
    if (supportsLinks) {
      Path dir = testFolder.resolve("dir");
      triggerLink = Files.createSymbolicLink(dir.resolve("SecurityException"), empty);
      linkTriggerDir = Files.createSymbolicLink(dir.resolve("lnDirSE"), triggerDir);
      linkTriggerFile = Files.createSymbolicLink(dir.resolve("lnFileSE"), triggerFile);
    }

    FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
    FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);

    try {
      fsp.setFaultyMode(false);
      Path fakeRoot = fs.getRoot();
      // validate setting
      try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"SecurityException", "sample"});
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"dir2", "SecurityException", "fileInSE", "file"});
      }

      if (supportsLinks) {
        try (Stream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
          String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
          assertEqualsNoOrder(
              result,
              new String[] {"d1", "f1", "lnDir2", "SecurityException", "lnDirSE", "lnFileSE"});
        }
      }

      // execute test
      fsp.setFaultyMode(true);
      // ignore file cause SecurityException
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"empty", "sample"});
      }
      // skip folder cause SecurityException
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"dir2", "file"});
      }

      if (supportsLinks) {
        // not following links
        try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"))) {
          String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
          assertEqualsNoOrder(
              result, new String[] {"dir", "d1", "f1", "lnDir2", "lnDirSE", "lnFileSE"});
        }

        // following links
        try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"), FileVisitOption.FOLLOW_LINKS)) {
          String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
          // ?? Should fileInSE show up?
          // With FaultyFS, it does as no exception thrown for link to "SecurityException" with read
          // on "lnXxxSE"
          assertEqualsNoOrder(
              result,
              new String[] {
                "dir", "d1", "f1", "lnDir2", "file", "lnDirSE", "lnFileSE", "fileInSE"
              });
        }
      }

      // list instead of walk
      try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"sample"});
      }
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"file"});
      }

      // root cause SecurityException should be reported
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      // Walk a file cause SecurityException, we should get SE
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      // List a file cause SecurityException, we should get SE as cannot read attribute
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }
    } finally {
      // Cleanup
      if (fs != null) {
        fs.close();
      }
      if (supportsLinks) {
        Files.delete(triggerLink);
        Files.delete(linkTriggerDir);
        Files.delete(linkTriggerFile);
      }
      Files.delete(triggerFile);
      Files.delete(sampleFile);
      Files.delete(sample);
      TestUtil.removeAll(triggerDir);
    }
  }
예제 #23
0
 static void filesTest() {
   // Files.list
   try {
     try (Stream<Path> stream = Files.list(Paths.get("/opt"))) {
       String joined =
           stream
               .map(String::valueOf)
               .filter(path -> !path.startsWith("."))
               .sorted()
               .collect(Collectors.joining("; "));
       System.out.println("List path /opt : " + joined);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.find
   Path start = Paths.get("/Users/alibaba/Downloads/2016113");
   int maxDepth = 5;
   try (Stream<Path> stream =
       Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".js"))) {
     String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; "));
     System.out.println("Files find : " + joined);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.walk
   try (Stream<Path> stream = Files.walk(start, maxDepth)) {
     String joined =
         stream
             .map(String::valueOf)
             .filter(path -> path.endsWith(".js"))
             .sorted()
             .collect(Collectors.joining("; "));
     System.out.println("Files walk : " + joined);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.readAllLines
   try {
     String p = "/Users/alibaba/linuxsir.txt";
     List<String> lines = Files.readAllLines(Paths.get(p));
     lines.add("print('foobar');");
     Files.write(Paths.get(p), lines);
     lines.remove(lines.size() - 1);
     System.out.println("readAllLines " + lines);
     Files.write(Paths.get(p), lines);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.lines
   try (Stream<String> stream = Files.lines(Paths.get("/Users/alibaba/linuxsir.txt"))) {
     stream.filter(line -> line.contains("w")).map(String::valueOf).forEach(System.out::println);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.newBufferedReader&Files.newBufferedWriter
   Path path = Paths.get("/Users/alibaba/linuxsir.txt");
   try (BufferedReader reader = Files.newBufferedReader(path)) {
     System.out.println(reader.readLine());
   } catch (IOException e) {
     e.printStackTrace();
   }
   path = Paths.get("/Users/alibaba/output.txt");
   try (BufferedWriter writer = Files.newBufferedWriter(path)) {
     writer.write("print('Hello World')");
   } catch (IOException e) {
     e.printStackTrace();
   }
 }