Beispiel #1
0
 /**
  * Copy all data from the input stream to the output stream and close the input stream. Exceptions
  * while closing are ignored.
  *
  * @param in the input stream
  * @param out the output stream
  * @return the number of bytes copied
  */
 public static long copyAndCloseInput(InputStream in, OutputStream out) throws IOException {
   try {
     return copy(in, out);
   } finally {
     closeSilently(in);
   }
 }
Beispiel #2
0
 /**
  * Copy all data from the input stream to the output stream and close both streams. Exceptions
  * while closing are ignored.
  *
  * @param in the input stream
  * @param out the output stream
  * @return the number of bytes copied
  */
 public static long copyAndClose(InputStream in, OutputStream out) throws IOException {
   try {
     long len = copyAndCloseInput(in, out);
     out.close();
     return len;
   } finally {
     closeSilently(out);
   }
 }
 private void listTop(String title, int[] list, int max) throws IOException {
   printLine('-');
   int total = 0;
   int totalLines = 0;
   for (int j = 0; j < maxIndex; j++) {
     int l = list[j];
     if (l > 0) {
       total += list[j];
       totalLines++;
     }
   }
   if (max == 0) {
     max = totalLines;
   }
   print(title);
   print("Total: " + total);
   printLine('-');
   String[] text = new String[max];
   int[] index = new int[max];
   for (int i = 0; i < max; i++) {
     int big = list[0];
     int bigIndex = 0;
     for (int j = 1; j < maxIndex; j++) {
       int l = list[j];
       if (l > big) {
         big = l;
         bigIndex = j;
       }
     }
     list[bigIndex] = -(big + 1);
     index[i] = bigIndex;
   }
   LineNumberReader r = null;
   try {
     r = new LineNumberReader(new FileReader("profile.txt"));
     for (int i = 0; i < maxIndex; i++) {
       String line = r.readLine();
       int k = list[i];
       if (k < 0) {
         k = -(k + 1);
         list[i] = k;
         for (int j = 0; j < max; j++) {
           if (index[j] == i) {
             int percent = 100 * k / total;
             text[j] = k + " " + percent + "%: " + line;
           }
         }
       }
     }
     for (int i = 0; i < max; i++) {
       print(text[i]);
     }
   } finally {
     IOUtils.closeSilently(r);
   }
 }
 private void listUnvisited() throws IOException {
   printLine('=');
   print("NOT COVERED");
   printLine('-');
   LineNumberReader r = null;
   BufferedWriter writer = null;
   try {
     r = new LineNumberReader(new FileReader("profile.txt"));
     writer = new BufferedWriter(new FileWriter("notCovered.txt"));
     int unvisited = 0;
     int unvisitedThrow = 0;
     for (int i = 0; i < maxIndex; i++) {
       String line = r.readLine();
       if (count[i] == 0) {
         if (!line.endsWith("throw")) {
           writer.write(line + "\r\n");
           if (LIST_UNVISITED) {
             print(line + "\r\n");
           }
           unvisited++;
         } else {
           unvisitedThrow++;
         }
       }
     }
     int percent = 100 * unvisited / maxIndex;
     print(
         "Not covered: "
             + percent
             + " % "
             + " ("
             + unvisited
             + " of "
             + maxIndex
             + "; throw="
             + unvisitedThrow
             + ")");
   } finally {
     IOUtils.closeSilently(writer);
     IOUtils.closeSilently(r);
   }
 }
 @Override
 public void run() {
   try {
     input = new BufferedInputStream(socket.getInputStream());
     output = new BufferedOutputStream(socket.getOutputStream());
     while (!stop) {
       if (!process()) {
         break;
       }
     }
   } catch (Exception e) {
     TraceSystem.traceThrowable(e);
   }
   IOUtils.closeSilently(output);
   IOUtils.closeSilently(input);
   try {
     socket.close();
   } catch (IOException e) {
     // ignore
   } finally {
     server.remove(this);
   }
 }
Beispiel #6
0
 /**
  * Print a trace message to the trace file.
  *
  * @param s the message
  * @param e the exception or null
  */
 protected void traceOperation(String s, Exception e) {
   FileWriter writer = null;
   try {
     File f = new File(getBaseDir() + "/" + TRACE_FILE_NAME);
     f.getParentFile().mkdirs();
     writer = new FileWriter(f, true);
     PrintWriter w = new PrintWriter(writer);
     s = dateFormat.format(new Date()) + ": " + s;
     w.println(s);
     if (e != null) {
       e.printStackTrace(w);
     }
   } catch (IOException e2) {
     e2.printStackTrace();
   } finally {
     IOUtils.closeSilently(writer);
   }
 }
 private Profile() {
   LineNumberReader r = null;
   try {
     r = new LineNumberReader(new FileReader("profile.txt"));
     while (r.readLine() != null) {
       // nothing - just count lines
     }
     maxIndex = r.getLineNumber();
     count = new int[maxIndex];
     time = new int[maxIndex];
     lastTime = System.currentTimeMillis();
     Runtime.getRuntime().addShutdownHook(this);
   } catch (Exception e) {
     e.printStackTrace();
     System.exit(1);
   } finally {
     IOUtils.closeSilently(r);
   }
 }
Beispiel #8
0
 private static String getOriginalDbName(String fileName, String db) throws IOException {
   InputStream in = null;
   try {
     in = FileUtils.newInputStream(fileName);
     ZipInputStream zipIn = new ZipInputStream(in);
     String originalDbName = null;
     boolean multiple = false;
     while (true) {
       ZipEntry entry = zipIn.getNextEntry();
       if (entry == null) {
         break;
       }
       String entryName = entry.getName();
       zipIn.closeEntry();
       String name = getDatabaseNameFromFileName(entryName);
       if (name != null) {
         if (db.equals(name)) {
           originalDbName = name;
           // we found the correct database
           break;
         } else if (originalDbName == null) {
           originalDbName = name;
           // we found a database, but maybe another one
         } else {
           // we have found multiple databases, but not the correct
           // one
           multiple = true;
         }
       }
     }
     zipIn.close();
     if (multiple && !db.equals(originalDbName)) {
       throw new IOException("Multiple databases found, but not " + db);
     }
     return originalDbName;
   } finally {
     IOUtils.closeSilently(in);
   }
 }
  private void test(String... args) throws Exception {
    int dbId = -1;
    boolean exit = false;
    String out = "benchmark.html";
    for (int i = 0; i < args.length; i++) {
      String arg = args[i];
      if ("-db".equals(arg)) {
        dbId = Integer.parseInt(args[++i]);
      } else if ("-init".equals(arg)) {
        FileUtils.deleteRecursive("data", true);
      } else if ("-out".equals(arg)) {
        out = args[++i];
      } else if ("-trace".equals(arg)) {
        trace = true;
      } else if ("-exit".equals(arg)) {
        exit = true;
      }
    }
    Properties prop = new Properties();
    InputStream in = getClass().getResourceAsStream("test.properties");
    prop.load(in);
    in.close();
    int size = Integer.parseInt(prop.getProperty("size"));
    ArrayList<Database> dbs = new ArrayList<Database>();
    for (int i = 0; i < 100; i++) {
      if (dbId != -1 && i != dbId) {
        continue;
      }
      String dbString = prop.getProperty("db" + i);
      if (dbString != null) {
        Database db = Database.parse(this, i, dbString);
        if (db != null) {
          db.setTranslations(prop);
          dbs.add(db);
        }
      }
    }
    ArrayList<Bench> tests = new ArrayList<Bench>();
    for (int i = 0; i < 100; i++) {
      String testString = prop.getProperty("test" + i);
      if (testString != null) {
        Bench bench = (Bench) Class.forName(testString).newInstance();
        tests.add(bench);
      }
    }
    testAll(dbs, tests, size);
    collect = false;
    if (dbs.size() == 0) {
      return;
    }
    ArrayList<Object[]> results = dbs.get(0).getResults();
    Connection conn = null;
    PreparedStatement prep = null;
    Statement stat = null;
    PrintWriter writer = null;
    try {
      openResults();
      conn = getResultConnection();
      stat = conn.createStatement();
      prep =
          conn.prepareStatement(
              "INSERT INTO RESULTS(TESTID, TEST, UNIT, DBID, DB, RESULT) VALUES(?, ?, ?, ?, ?, ?)");
      for (int i = 0; i < results.size(); i++) {
        Object[] res = results.get(i);
        prep.setInt(1, i);
        prep.setString(2, res[0].toString());
        prep.setString(3, res[1].toString());
        for (Database db : dbs) {
          prep.setInt(4, db.getId());
          prep.setString(5, db.getName());
          Object[] v = db.getResults().get(i);
          prep.setString(6, v[2].toString());
          prep.execute();
        }
      }

      writer = new PrintWriter(new FileWriter(out));
      ResultSet rs =
          stat.executeQuery(
              "CALL '<table><tr><th>Test Case</th><th>Unit</th>' "
                  + "|| SELECT GROUP_CONCAT('<th>' || DB || '</th>' ORDER BY DBID SEPARATOR '') FROM "
                  + "(SELECT DISTINCT DBID, DB FROM RESULTS)"
                  + "|| '</tr>' || CHAR(10) "
                  + "|| SELECT GROUP_CONCAT('<tr><td>' || TEST || '</td><td>' || UNIT || '</td>' || ( "
                  + "SELECT GROUP_CONCAT('<td>' || RESULT || '</td>' ORDER BY DBID SEPARATOR '') FROM RESULTS R2 WHERE "
                  + "R2.TESTID = R1.TESTID) || '</tr>' ORDER BY TESTID SEPARATOR CHAR(10)) FROM "
                  + "(SELECT DISTINCT TESTID, TEST, UNIT FROM RESULTS) R1"
                  + "|| '</table>'");
      rs.next();
      String result = rs.getString(1);
      writer.println(result);
    } finally {
      JdbcUtils.closeSilently(prep);
      JdbcUtils.closeSilently(stat);
      JdbcUtils.closeSilently(conn);
      IOUtils.closeSilently(writer);
    }

    //        ResultSet rsDbs = conn.createStatement().executeQuery(
    //                "SELECT DB RESULTS GROUP BY DBID, DB ORDER BY DBID");
    //        while(rsDbs.next()) {
    //            writer.println("<th>" + rsDbs.getString(1) + "</th>");
    //        }
    //        ResultSet rs = conn.createStatement().executeQuery(
    //                "SELECT TEST, UNIT FROM RESULTS " +
    //                "GROUP BY TESTID, TEST, UNIT ORDER BY TESTID");
    //        while(rs.next()) {
    //            writer.println("<tr><td>" + rs.getString(1) + "</td>");
    //            writer.println("<td>" + rs.getString(2) + "</td>");
    //            ResultSet rsRes = conn.createStatement().executeQuery(
    //                "SELECT RESULT FROM RESULTS WHERE TESTID=? ORDER BY DBID");
    //
    //
    //        }

    //        PrintWriter writer =
    //            new PrintWriter(new FileWriter("benchmark.html"));
    //        writer.println("<table><tr><th>Test Case</th><th>Unit</th>");
    //        for(int j=0; j<dbs.size(); j++) {
    //            Database db = (Database)dbs.get(j);
    //            writer.println("<th>" + db.getName() + "</th>");
    //        }
    //        writer.println("</tr>");
    //        for(int i=0; i<results.size(); i++) {
    //            Object[] res = (Object[])results.get(i);
    //            writer.println("<tr><td>" + res[0] + "</td>");
    //            writer.println("<td>" + res[1] + "</td>");
    //            for(int j=0; j<dbs.size(); j++) {
    //                Database db  = (Database)dbs.get(j);
    //                ArrayList r = db.getResults();
    //                Object[] v = (Object[])r.get(i);
    //                writer.println(
    //                    "<td  style=\"text-align: right\">" + v[2] + "</td>");
    //            }
    //            writer.println("</tr>");
    //        }
    //        writer.println("</table>");

    if (exit) {
      System.exit(0);
    }
  }
Beispiel #10
0
 /**
  * Restores database files.
  *
  * @param zipFileName the name of the backup file
  * @param directory the directory name
  * @param db the database name (null for all databases)
  * @throws DbException if there is an IOException
  */
 public static void execute(String zipFileName, String directory, String db) {
   InputStream in = null;
   try {
     if (!FileUtils.exists(zipFileName)) {
       throw new IOException("File not found: " + zipFileName);
     }
     String originalDbName = null;
     int originalDbLen = 0;
     if (db != null) {
       originalDbName = getOriginalDbName(zipFileName, db);
       if (originalDbName == null) {
         throw new IOException("No database named " + db + " found");
       }
       if (originalDbName.startsWith(SysProperties.FILE_SEPARATOR)) {
         originalDbName = originalDbName.substring(1);
       }
       originalDbLen = originalDbName.length();
     }
     in = FileUtils.newInputStream(zipFileName);
     ZipInputStream zipIn = new ZipInputStream(in);
     while (true) {
       ZipEntry entry = zipIn.getNextEntry();
       if (entry == null) {
         break;
       }
       String fileName = entry.getName();
       // restoring windows backups on linux and vice versa
       fileName = fileName.replace('\\', SysProperties.FILE_SEPARATOR.charAt(0));
       fileName = fileName.replace('/', SysProperties.FILE_SEPARATOR.charAt(0));
       if (fileName.startsWith(SysProperties.FILE_SEPARATOR)) {
         fileName = fileName.substring(1);
       }
       boolean copy = false;
       if (db == null) {
         copy = true;
       } else if (fileName.startsWith(originalDbName + ".")) {
         fileName = db + fileName.substring(originalDbLen);
         copy = true;
       }
       if (copy) {
         OutputStream o = null;
         try {
           o =
               FileUtils.newOutputStream(
                   directory + SysProperties.FILE_SEPARATOR + fileName, false);
           IOUtils.copy(zipIn, o);
           o.close();
         } finally {
           IOUtils.closeSilently(o);
         }
       }
       zipIn.closeEntry();
     }
     zipIn.closeEntry();
     zipIn.close();
   } catch (IOException e) {
     throw DbException.convertIOException(e, zipFileName);
   } finally {
     IOUtils.closeSilently(in);
   }
 }