@Override
  public Element build(Map<String, String> properties, Progress progress)
      throws CruiseControlException {
    final long startTime = System.currentTimeMillis();
    final Element status = new Element("writer");
    OutputStream out = null;
    java.io.File f;

    // Resolve properties in the settings. Fail. if they cannot be resolved
    final String fname =
        Util.parsePropertiesInString(properties, this.file.getAbsolutePath(), true);
    f = new java.io.File(fname);

    try {
      // The output file must not exist
      if (!this.overwrite && f.exists()) {
        throw new IOException("File " + f + " exists but overwrite=false");
      }
      // gzip compression is set on
      if (this.gzip) {
        if (!f.getName().endsWith(".gzip")) {
          f = new java.io.File(f.getAbsolutePath() + ".gzip");
        }
        out = new GZIPOutputStream(new FileOutputStream(f, this.append));
        // not-compressed file is required
      } else {
        out = new FileOutputStream(f, this.append);
      }

      // Pass content to the consumer
      for (Content message : this.messages) {
        new StreamPumper(message.getContent(properties), getStreamConsumer(out, encoding)).run();
      }

    } catch (Exception exc) {
      status.setAttribute("error", "build failed with exception: " + exc.getMessage());
    } finally {
      IO.close(out);
    }

    final long endTime = System.currentTimeMillis();
    status.setAttribute("time", DateUtil.getDurationAsString((endTime - startTime)));

    return status;
  }
示例#2
0
  public void testWrite() throws Exception {
    FileWriter fw = null;
    File outFile = null;

    try {
      outFile = File.createTempFile("FeedTest", "tmp");
      filesToDelete.add(outFile);
      fw = new FileWriter(outFile);
      Feed feed = new Feed(tempFile);
      feed.write(fw);
      fw.close();

      // Feed feed2 = new Feed(outFile);
      assertEquals("CruiseControl Build Results", feed.getTitle());
      assertEquals("http://MyMachine.MyDomain.com/cruisecontrol/", feed.getLink());
      assertEquals(
          "Automated build results for CruiseControl project(s) VERSION_10", feed.getDescription());

      // validate the number of items and the contents of the first item.
      assertEquals(11, feed.getItems().size());
      Item item = (Item) feed.getItems().get(0);
      assertEquals("VERSION_10 build.7 Build Successful", item.getTitle());
      assertEquals(
          "http://MyMachine.MyDomain.com/cruisecontrol/buildresults/"
              + "VERSION_10?log=log20050817084109Lbuild.7",
          item.getLink());
      assertEquals(
          "<em>Build Time:</em> Wed Aug 17 08:41:09 MDT 2005<br/>"
              + "<em>Label:</em> build.7<br/><em>Modifications: </em>1<br/>"
              + "\n<ul><li>//depot/MyProduct/VERSION_10/dev/main/src/datacenter/"
              + "ApplicationServer/PlayTime/default.build"
              + "  by jefferson (deploy the mock object dll)</li></ul>",
          item.getDescription());
    } finally {
      IO.close(fw);
    }
  }