1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 import java.nio.file.*;
  26 import java.util.Arrays;
  27 
  28 public class TestUtil {
  29 
  30     final static String fileContent = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // repeated
  31 
  32     public static Path getAFile(int size) throws IOException {
  33         Path p = tempFile();
  34         BufferedWriter writer = Files.newBufferedWriter(p);
  35         int len = fileContent.length();
  36         int iterations = size / len;
  37         int remainder = size - (iterations * len);
  38         for (int i=0; i<iterations; i++)
  39             writer.write(fileContent, 0, len);
  40         writer.write(fileContent, 0, remainder);
  41         writer.close();
  42         return p;
  43     }
  44 
  45     public static Path tempFile() {
  46         try {
  47             Path p = Files.createTempFile("foo", "test");
  48             p.toFile().deleteOnExit();
  49             return p;
  50         } catch (IOException e) {
  51             throw new UncheckedIOException(e);
  52         }
  53     }
  54 
  55     public static Void compareFiles(Path path1, Path path2) {
  56         //System.err.printf("Comparing %s and %s\n", path1.toString(), path2.toString());
  57         try {
  58             long size1 = Files.size(path1);
  59             long size2 = Files.size(path2);
  60             if (size1 != size2) {
  61                 String msg = "File sizes do not match " +
  62                         Long.toString(size1) + "/" + Long.toString(size2);
  63                 throw new RuntimeException(msg);
  64             }
  65             compareContents(path1, path2);
  66             return null;
  67         } catch (IOException e) {
  68             throw new UncheckedIOException(e);
  69         }
  70     }
  71 
  72     static void compareContents(Path path1, Path path2) {
  73         try {
  74             byte[] b1 = Files.readAllBytes(path1);
  75             byte[] b2 = Files.readAllBytes(path2);
  76             if (!Arrays.equals(b1, b2))
  77                 throw new RuntimeException ("Files do not match");
  78         } catch (IOException e) {
  79             throw new UncheckedIOException(e);
  80         }
  81     }
  82 
  83 }