Example #1
0
 /**
  * Set the servlet that provides content for the GzipFilter in being tested.
  *
  * @param servletClass the servlet that will provide content.
  * @return the FilterHolder for configuring the GzipFilter's initParameters with
  */
 public FilterHolder setContentServlet(Class<? extends Servlet> servletClass) throws IOException {
   servletTester = new ServletTester();
   servletTester.setContextPath("/context");
   servletTester.setResourceBase(testdir.getDir().getCanonicalPath());
   ServletHolder servletHolder = servletTester.addServlet(servletClass, "/");
   servletHolder.setInitParameter("baseDir", testdir.getDir().getAbsolutePath());
   FilterHolder holder = servletTester.addFilter(gzipFilterClass, "/*", 0);
   return holder;
 }
Example #2
0
  /**
   * Create a file on the server resource path of a specified filename and size.
   *
   * @param filename the filename to create
   * @param filesize the file size to create (Note: this isn't suitable for creating large
   *     multi-megabyte files)
   */
  public File prepareServerFile(String filename, int filesize) throws IOException {
    File dir = testdir.getDir();
    File testFile = new File(dir, filename);
    // Make sure we have a uniq filename (to work around windows File.delete bug)
    int i = 0;
    while (testFile.exists()) {
      testFile = new File(dir, (i++) + "-" + filename);
    }

    FileOutputStream fos = null;
    ByteArrayInputStream in = null;
    try {
      fos = new FileOutputStream(testFile, false);
      in = new ByteArrayInputStream(generateContent(filesize).getBytes(encoding));
      IO.copy(in, fos);
      return testFile;
    } finally {
      IO.close(in);
      IO.close(fos);
    }
  }