< prev index next >
   1 /*
   2  * Copyright (c) 2015, 2016, 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 package java.net.http;
  25 
  26 import java.io.*;
  27 import java.net.*;
  28 import java.nio.file.*;
  29 import java.util.concurrent.*;
  30 import java.util.Arrays;
  31 
  32 public class TestUtil {
  33 
  34     final static String fileContent = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // repeated
  35 
  36     public static Path getAFile(int size) throws IOException {
  37         Path p = tempFile();
  38         BufferedWriter writer = Files.newBufferedWriter(p);
  39         int len = fileContent.length();
  40         int iterations = size / len;
  41         int remainder = size - (iterations * len);
  42         for (int i=0; i<iterations; i++)
  43             writer.write(fileContent, 0, len);
  44         writer.write(fileContent, 0, remainder);
  45         writer.close();
  46         return p;
  47     }
  48 
  49     public static Path tempFile() {
  50         try {
  51             Path p = Files.createTempFile("foo", "test");
  52             p.toFile().deleteOnExit();
  53             return p;
  54         } catch (IOException e) {
  55             throw new UncheckedIOException(e);
  56         }
  57     }
  58 
  59     public static Void compareFiles(Path path1, Path path2) {
  60         try {
  61             if (Files.size(path1) != Files.size(path2))
  62                 throw new RuntimeException("File sizes do not match");
  63             compareContents(path1, path2);
  64             return null;
  65         } catch (IOException e) {
  66             throw new UncheckedIOException(e);
  67         }
  68     }
  69 
  70     static void compareContents(Path path1, Path path2) {
  71         try {
  72             byte[] b1 = Files.readAllBytes(path1);
  73             byte[] b2 = Files.readAllBytes(path2);
  74             if (!Arrays.equals(b1, b2))
  75                 throw new RuntimeException ("Files do not match");
  76         } catch (IOException e) {
  77             throw new UncheckedIOException(e);
  78         }
  79     }
  80 
  81 }
< prev index next >