@Test
  public void testSaveLoad() throws FileNotFoundException, IOException {
    System.out.println("---  testSaveLoad...");
    XMLEncoder xmlEncoder = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;

    File f = new File("testProjects/TEMP_PROJECTS/Test.ser");

    System.out.println("Saving to: " + f.getCanonicalPath());

    fos = new FileOutputStream(f);
    bos = new BufferedOutputStream(fos);
    xmlEncoder = new XMLEncoder(bos);

    // Metric m = Metric.PATH_LENGTH_FROM_ROOT;
    Object o1 = pg1;

    System.out.println("Pre: " + o1);

    // ProximalPref p = ProximalPref.MOST_PROX_AT_0;

    xmlEncoder.writeObject(o1);
    xmlEncoder.close();

    FileInputStream fis = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fis);
    XMLDecoder xd = new XMLDecoder(bis);

    Object o2 = xd.readObject();

    System.out.println("Post: " + o2);

    assertEquals(o2, o1);
  }
  @Test
  public void testJson() throws Exception {
    MultiLayerConfiguration conf =
        new NeuralNetConfiguration.Builder()
            .list()
            .layer(0, new RBM.Builder().dist(new NormalDistribution(1, 1e-1)).build())
            .inputPreProcessor(0, new ReshapePreProcessor())
            .build();

    String json = conf.toJson();
    MultiLayerConfiguration from = MultiLayerConfiguration.fromJson(json);
    assertEquals(conf.getConf(0), from.getConf(0));

    Properties props = new Properties();
    props.put("json", json);
    String key = props.getProperty("json");
    assertEquals(json, key);
    File f = new File("props");
    f.deleteOnExit();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
    props.store(bos, "");
    bos.flush();
    bos.close();
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
    Properties props2 = new Properties();
    props2.load(bis);
    bis.close();
    assertEquals(props2.getProperty("json"), props.getProperty("json"));
    String json2 = props2.getProperty("json");
    MultiLayerConfiguration conf3 = MultiLayerConfiguration.fromJson(json2);
    assertEquals(conf.getConf(0), conf3.getConf(0));
  }
示例#3
0
  @Test
  public void canFollowLogfile() throws IOException {
    File tempFile =
        File.createTempFile("commons-io", "", new File(System.getProperty("java.io.tmpdir")));
    tempFile.deleteOnExit();
    System.out.println("Temp file = " + tempFile.getAbsolutePath());
    PrintStream log = new PrintStream(tempFile);
    LogfileFollower follower = new LogfileFollower(tempFile);
    List<String> lines;

    // Empty file:
    lines = follower.newLines();
    assertEquals(0, lines.size());

    // Write two lines:
    log.println("Line 1");
    log.println("Line 2");
    lines = follower.newLines();
    assertEquals(2, lines.size());
    assertEquals("Line 2", lines.get(1));

    // Write one more line:
    log.println("Line 3");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 3", lines.get(0));

    // Write one and a half line and finish later:
    log.println("Line 4");
    log.print("Line 5 begin");
    lines = follower.newLines();
    assertEquals(1, lines.size());

    // End last line and start a new one:
    log.println(" end");
    log.print("Line 6 begin");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 5 begin end", lines.get(0));

    // End last line:
    log.println(" end");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 6 begin end", lines.get(0));

    // A line only missing a newline:
    log.print("Line 7");
    lines = follower.newLines();
    assertEquals(0, lines.size());
    log.println();
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 7", lines.get(0));

    // Delete:
    log.close();
    lines = follower.newLines();
    assertEquals(0, lines.size());
  }
示例#4
0
  public void testSerial() {
    assertTrue(_nbhm.isEmpty());
    final String k1 = "k1";
    final String k2 = "k2";
    assertThat(_nbhm.put(k1, "v1"), nullValue());
    assertThat(_nbhm.put(k2, "v2"), nullValue());

    // Serialize it out
    try {
      FileOutputStream fos = new FileOutputStream("NBHM_test.txt");
      ObjectOutputStream out = new ObjectOutputStream(fos);
      out.writeObject(_nbhm);
      out.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    // Read it back
    try {
      File f = new File("NBHM_test.txt");
      FileInputStream fis = new FileInputStream(f);
      ObjectInputStream in = new ObjectInputStream(fis);
      NonBlockingIdentityHashMap nbhm = (NonBlockingIdentityHashMap) in.readObject();
      in.close();
      assertThat(
          "serialization works",
          nbhm.toString(),
          anyOf(is("{k1=v1, k2=v2}"), is("{k2=v2, k1=v1}")));
      if (!f.delete()) throw new IOException("delete failed");
    } catch (IOException ex) {
      ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
  }
示例#5
0
  private void compareBinaryFolder(String path, boolean res) throws BrutException, IOException {
    Boolean exists = true;

    String tmp = "";
    if (res) {
      tmp = File.separatorChar + "res" + File.separatorChar;
    }

    String location = tmp + path;

    FileDirectory fileDirectory = new FileDirectory(sTestOrigDir + location);

    Set<String> files = fileDirectory.getFiles(true);
    for (String filename : files) {

      File control = new File((sTestOrigDir + location), filename);
      File test = new File((sTestNewDir + location), filename);

      if (!test.isFile() || !control.isFile()) {
        exists = false;
      }
    }

    assertTrue(exists);
  }
示例#6
0
 @After
 public void tearDown() {
   for (File file : new File[] {_fileExisting, _fileText}) {
     if (file.exists()) {
       file.delete();
     }
   }
 }
 private static void delete(File directory) {
   for (File file : directory.listFiles()) {
     if (file.isDirectory()) {
       delete(file);
     } else {
       assertTrue(file.delete());
     }
   }
   assertTrue(directory.delete());
 }
 public File getTestOutputDirectory() {
   final File directory =
       new File(
           System.getProperty(
               "maven.project.build.testOutputDirectory",
               System.getProperty("project.build.testOutputDirectory", "target/test-classes")));
   assertTrue(directory.isDirectory());
   assertTrue(directory.canWrite());
   return directory;
 }
 /**
  * Set up.
  *
  * @throws Exception
  */
 @BeforeClass
 public static void setUpClass() throws Exception {
   URL sigFileV67Url = new URL(SIGNATURE_FILE_V67_URL);
   InputStream sigFileStream = sigFileV67Url.openStream();
   File tmpSigFile = File.createTempFile("tmpsigfile", ".xml");
   FileOutputStream fos = new FileOutputStream(tmpSigFile);
   IOUtils.copy(sigFileStream, fos);
   fos.close();
   dihj = DroidIdentification.getInstance(tmpSigFile.getAbsolutePath());
 }
示例#10
0
 public void moveToDirectory(File target) {
   if (target.exists() && !target.isDirectory()) {
     throw new RuntimeException(String.format("Target '%s' is not a directory", target));
   }
   try {
     FileUtils.moveFileToDirectory(this, target, true);
   } catch (IOException e) {
     throw new RuntimeException(
         String.format("Could not move test file '%s' to directory '%s'", this, target), e);
   }
 }
  private void assertExpectedFileIO() {
    File realFile = new File(FILE_NAME);
    boolean realFileCreated = realFile.exists();

    if (realFileCreated) {
      realFile.delete();
    }

    assertFalse("Real file created", realFileCreated);
    assertTrue("File not written", testOutput.toString().startsWith("File written"));
  }
示例#12
0
 private static File join(File file, Object[] path) {
   File current = file.getAbsoluteFile();
   for (Object p : path) {
     current = new File(current, p.toString());
   }
   try {
     return current.getCanonicalFile();
   } catch (IOException e) {
     throw new RuntimeException(String.format("Could not canonicalise '%s'.", current), e);
   }
 }
示例#13
0
 @Before
 public void setUp() throws Exception {
   textbuddy = new TextBuddy2();
   System.out.println("before :");
   TextBuddy2.add();
   textFile1 = new File("textfile.txt");
   textFile1.createNewFile();
   expected = new File("expected.txt");
   expected.createNewFile();
   TextBuddy2.displayContent("textfile.txt");
 }
 /**
  * Test ODT file format identification
  *
  * @throws IOException
  */
 @Test
 public void testIdentifyOdt() throws IOException {
   InputStream odtTestFileStream =
       DroidIdentificationTest.class.getResourceAsStream("testfile.odt");
   File tmpOdtTestFile = File.createTempFile("odttestfile", ".odt");
   FileOutputStream fos = new FileOutputStream(tmpOdtTestFile);
   IOUtils.copy(odtTestFileStream, fos);
   fos.close();
   String result = dihj.identify(tmpOdtTestFile.getAbsolutePath());
   if (result.isEmpty()) {
     fail("No identification result");
   }
   assertEquals("fmt/291", result);
 }
示例#15
0
  public void testExecutePs2pdfConvert() throws Exception {

    LOG.info("TEST ps2pdf-convert");
    Tool tool = repo.getTool("ps2pdf");

    String tmpInputFile = this.getClass().getClassLoader().getResource("ps2pdf-input.ps").getFile();

    ToolProcessor processor = new ToolProcessor(tool);
    Operation operation = processor.findOperation("convert");
    processor.setOperation(operation);

    Map<String, String> mapInput = new HashMap<String, String>();

    LOG.debug("tool = " + tool.getName());

    LOG.debug("tmpInputFile = " + tmpInputFile);

    mapInput = new HashMap<String, String>();
    mapInput.put("inFile", tmpInputFile);

    String tmpOutputFile = File.createTempFile("ps2pdf", ".pdf").getAbsolutePath();
    LOG.debug("tmpOutputFile = " + tmpOutputFile);

    Map<String, String> mapOutput = new HashMap<String, String>();
    mapOutput.put("outFile", tmpOutputFile);

    processor.setInputFileParameters(mapInput);
    processor.setOutputFileParameters(mapOutput);
    try {
      processor.execute();
    } catch (IOException ex) {
      LOG.error("Exception during execution (maybe unresolved system dependency?): " + ex);
    }
  }
  public static File getSparseBinarySVDLIBCFile() throws Exception {
    File f = File.createTempFile("unit-test", ".dat");
    DataOutputStream pw = new DataOutputStream(new FileOutputStream(f));
    pw.writeInt(3);
    pw.writeInt(3);
    pw.writeInt(6);

    pw.writeInt(2);
    pw.writeInt(0);
    pw.writeFloat(2.3f);
    pw.writeInt(2);
    pw.writeFloat(3.8f);

    pw.writeInt(1);
    pw.writeInt(1);
    pw.writeFloat(1.3f);

    pw.writeInt(3);
    pw.writeInt(0);
    pw.writeFloat(4.2f);
    pw.writeInt(1);
    pw.writeFloat(2.2f);
    pw.writeInt(2);
    pw.writeFloat(0.5f);

    pw.close();
    return f;
  }
 @Test
 public void test_main_offline() throws IOException {
   File offline = new File("OfflineData.bin");
   if (offline.exists() && !offline.isDirectory()) {
     this.getLogger().info("OfflineData.bin file exists.");
     if (GraphicsEnvironment.isHeadless()) {
       this.binFileReader = new BinFileReader(offline);
     } else {
       this.binFileReader = new BinFileReaderGui(offline);
     }
     do_read_from_bin();
     System.out.println("");
   } else {
     getLogger().warning("No OfflineData.bin file!");
   }
 }
示例#18
0
  @Test(expected = ArchiveException.class)
  public void tesGunzip_extractBzip2() throws IOException, ArchiveException {
    final File inputFile = new File(inputUrl.getPath(), "pol.txt.bz2");
    final File outputFile = File.createTempFile("output", "txt");

    new Tar().gunzip(inputFile, outputFile);
  }
示例#19
0
  public void testExecutePs2pdfConvertStreamed() throws Exception {

    LOG.info("TEST ps2pdf-convert-streamed");
    Tool tool = repo.getTool("ps2pdf");

    String tmpInputFile = this.getClass().getClassLoader().getResource("ps2pdf-input.ps").getFile();
    String tmpOutputFile = File.createTempFile("ps2pdf", ".pdf").getAbsolutePath();

    ToolProcessor processor = new ToolProcessor(tool);
    Operation operation = processor.findOperation("convert-streamed");
    processor.setOperation(operation);

    LOG.debug("tmpInputFile = " + tmpInputFile);

    LOG.debug("tmpOutputFile = " + tmpOutputFile);

    FileInputStream fin = new FileInputStream(new File(tmpInputFile));
    StreamProcessor in = new StreamProcessor(fin);
    in = new StreamProcessor(fin);
    in.next(processor);

    FileOutputStream fout = new FileOutputStream(new File(tmpOutputFile));
    processor.next(new StreamProcessor(fout));

    try {
      in.execute();
    } catch (IOException ex) {
      LOG.error("Exception during execution (maybe unresolved system dependency?): " + ex);
    }
  }
示例#20
0
  @Before
  public void setUp() throws Exception {
    _rgchPwTest = new char[] {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};

    _fileExisting = File.createTempFile("obzvaultexisting", ".vault");
    _strDecryptedExisting = "existing";
    OBZVaultDocument doc = new OBZVaultDocument();
    doc.setAppVersion("X.Y.DEV");
    doc.setText(_strDecryptedExisting);
    doc.saveAsVaultDoc(_fileExisting, _rgchPwTest);

    _fileText = File.createTempFile("obzvaulttext", ".txt");
    _strText = "Hello world!";
    OutputStream os = new FileOutputStream(_fileText);
    os.write(_strText.getBytes());
    os.close();
  }
示例#21
0
  @Test
  public void testBunzip2() throws IOException, ArchiveException {
    final File inputFile = new File(inputUrl.getPath(), "pol.txt.bz2");
    final File outputFile = File.createTempFile("output", "txt");

    new Tar().bunzip2(inputFile, outputFile);

    assertEquals("PlayOnLinux", new String(FileUtils.readFileToByteArray(outputFile)));
  }
示例#22
0
  private static void moveFile(String from, String to) throws IOException {
    File fromFile = new File(from);
    File toFile = new File(to);
    if (fromFile.exists()) {
      FileOutputStream fos = new FileOutputStream(toFile);
      FileInputStream fis = new FileInputStream(fromFile);

      byte[] buffer = new byte[1024];
      int len = fis.read(buffer);
      while (len != -1) {
        fos.write(buffer, 0, len);
        len = fis.read(buffer);
      }

      fos.close();
      fis.close();
      fromFile.delete();
    }
  }
示例#23
0
  /** Test of getFiles method, of class Module. */
  @Test
  public void testGetFiles() {
    System.out.println("getFiles");
    // Set up
    Module instance = new Module();

    List<File> expResult = new ArrayList<>();

    // Add 5 files to the list
    for (int i = 0; i < 5; i++) {
      File file = new File();
      file.setId("Id " + i);
      file.setName("Name " + i);
      file.setFileblob(("Blob " + i).getBytes());
      expResult.add(file);
    }

    instance.setFiles(expResult);

    // Run method and check result
    assertEquals(expResult, instance.getFiles());
  }
示例#24
0
 private void visit(Set<String> names, String prefix, File file) {
   for (File child : file.listFiles()) {
     if (child.isFile()) {
       names.add(prefix + child.getName());
     } else if (child.isDirectory()) {
       visit(names, prefix + child.getName() + "/", child);
     }
   }
 }
 public static void writePackage(KnowledgePackage pkg, File dest) {
   dest.deleteOnExit();
   OutputStream out = null;
   try {
     out = new BufferedOutputStream(new FileOutputStream(dest));
     DroolsStreamUtils.streamOut(out, pkg);
   } catch (Exception e) {
     throw new RuntimeException(e);
   } finally {
     if (out != null) {
       try {
         out.close();
       } catch (IOException e) {
       }
     }
   }
 }
  @Test
  public void shouldLookForClassesInTargetDirectories() throws Exception {
    newDir = new File("tempClassDir");
    List<File> buildPaths = asList(newDir);
    ClasspathProvider classpath =
        new StandaloneClasspath(
            buildPaths,
            FakeEnvironments.systemClasspath() + pathSeparator + newDir.getAbsolutePath());

    String classname = "org.fakeco.Foobar";
    createClass(classname);

    builder = new JavaClassBuilder(classpath);
    JavaClass javaClass = builder.createClass(classname);
    assertEquals(classname, javaClass.getName());
    assertFalse(javaClass.isATest());
  }
示例#27
0
  private void testUncompress(String fileName) throws IOException, ArchiveException {
    final File inputFile = new File(inputUrl.getPath(), fileName);
    final File temporaryDirectory = Files.createTempDir();

    temporaryDirectory.deleteOnExit();

    final List<File> extractedFiles = new Extractor().uncompress(inputFile, temporaryDirectory);

    assertTrue(new File(temporaryDirectory, "directory1").isDirectory());
    final File file1 = new File(temporaryDirectory, "file1.txt");
    final File file2 = new File(temporaryDirectory, "file2.txt");
    final File file0 = new File(new File(temporaryDirectory, "directory1"), "file0.txt");

    assertTrue(file1.exists());
    assertTrue(file2.exists());
    assertTrue(file0.exists());

    assertEquals("file1content", new String(FileUtils.readFileToByteArray(file1)));
    assertEquals("file2content", new String(FileUtils.readFileToByteArray(file2)));
    assertEquals("file0content", new String(FileUtils.readFileToByteArray(file0)));

    assertEquals(5, extractedFiles.size());
  }
示例#28
0
  @Test
  public void testUncompress_withSymbolicLinks() throws IOException, ArchiveException {
    final File inputFile = new File(inputUrl.getPath(), "tarLink.tar.gz");
    final File temporaryDirectory = Files.createTempDir();

    temporaryDirectory.deleteOnExit();

    final List<File> extractedFiles = new Extractor().uncompress(inputFile, temporaryDirectory);

    final File file1 = new File(temporaryDirectory, "file1.txt");
    final File file2 = new File(temporaryDirectory, "file1_link.txt");

    assertTrue(file1.exists());
    assertTrue(file2.exists());

    assertEquals("file1content", new String(FileUtils.readFileToByteArray(file1)));
    assertEquals("file1content", new String(FileUtils.readFileToByteArray(file2)));

    assertTrue(java.nio.file.Files.isSymbolicLink(Paths.get(file2.getPath())));
  }
示例#29
0
  private void checkFolderExists(String path) throws BrutException {
    File f = new File(sTestNewDir, path);

    assertTrue(f.isDirectory());
  }
 protected void processFile(File f) {
   System.out.println("#### find: " + f.getPath());
 }