/*
  * printerStateReasonSet(Severity severity) method testing.
  */
 public final void testPrinterStateReasonSet() {
   reasons = new PrinterStateReasons();
   reasons.put(PrinterStateReason.MEDIA_LOW, Severity.ERROR);
   HashSet set = new HashSet();
   set.add(PrinterStateReason.MEDIA_LOW);
   assertEquals(set, reasons.printerStateReasonSet(Severity.ERROR));
   set = new HashSet();
   assertEquals(set, reasons.printerStateReasonSet(Severity.REPORT));
 }
  /*
   * PrinterStateReasons(Map map) constructor testing.
   */
  public final void testPrinterStateReasonsMap() {
    HashMap map = new HashMap();
    map.put(PrinterStateReason.TONER_LOW, Severity.WARNING);
    map.put(PrinterStateReason.DEVELOPER_LOW, Severity.WARNING);
    map.put(PrinterStateReason.MEDIA_LOW, Severity.ERROR);
    reasons = new PrinterStateReasons(map);

    assertEquals(3, reasons.size());
    assertTrue(reasons.containsKey(PrinterStateReason.TONER_LOW));
    assertTrue(reasons.containsKey(PrinterStateReason.DEVELOPER_LOW));
    assertTrue(reasons.containsKey(PrinterStateReason.MEDIA_LOW));

    try {
      map = new HashMap();
      map.put(PrinterStateReason.MEDIA_LOW, PrinterState.IDLE);
      reasons = new PrinterStateReasons(map);
      fail(
          "Constructor doesn't throw ClassCastException if "
              + "some value in the map isn't Severity");
    } catch (ClassCastException e) {
    }

    try {
      map = new HashMap();
      map.put(PrinterState.IDLE, Severity.ERROR);
      reasons = new PrinterStateReasons(map);
      fail(
          "Constructor doesn't throw ClassCastException if "
              + "some key in the map isn't PrinterStateReason");
    } catch (ClassCastException e) {
    }

    try {
      map = new HashMap();
      Severity severity = null;
      map.put(PrinterStateReason.COVER_OPEN, severity);
      reasons = new PrinterStateReasons(map);
      fail("Constructor doesn't throw NullPointerException if " + "some key in the map is null");
    } catch (NullPointerException e) {
    }

    try {
      map.put(null, Severity.REPORT);
      reasons = new PrinterStateReasons(map);
      fail("Constructor doesn't throw NullPointerException if " + "some value in the map is null");
    } catch (NullPointerException e) {
    }

    try {
      map = null;
      reasons = new PrinterStateReasons(map);
      fail("Constructor doesn't throw NullPointerException if " + "map is null");
    } catch (NullPointerException e) {
    }
  }
  /*
   * printerStateReasonSet(Severity severity) method testing.
   */
  public final void testPrinterStateReasonSet1() {
    reasons = new PrinterStateReasons();
    reasons.put(PrinterStateReason.COVER_OPEN, Severity.ERROR);
    reasons.put(PrinterStateReason.MEDIA_LOW, Severity.WARNING);
    reasons.put(PrinterStateReason.DOOR_OPEN, Severity.ERROR);
    reasons.put(PrinterStateReason.INPUT_TRAY_MISSING, Severity.ERROR);

    Set set = reasons.printerStateReasonSet(Severity.ERROR);
    try {
      set.iterator().remove();
      fail("Unmodifiable set was changed");
    } catch (UnsupportedOperationException e) {
    }

    try {
      set.add(PrinterStateReason.COVER_OPEN);
      fail("Unmodifiable set was changed");
    } catch (UnsupportedOperationException e) {

    }
  }
 /*
  * PrinterStateReasons() constructor testing.
  */
 public final void testPrinterStateReasons() {
   reasons = new PrinterStateReasons();
   assertEquals(0, reasons.size());
 }
 /*
  * getName() method testing.
  */
 public final void testGetName() {
   reasons = new PrinterStateReasons();
   assertEquals("printer-state-reasons", reasons.getName());
 }
 /*
  * getCategory() method testing.
  */
 public final void testGetCategory() {
   reasons = new PrinterStateReasons();
   assertEquals(PrinterStateReasons.class, reasons.getClass());
 }
Example #7
0
  public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {

    synchronized (this) {
      if (printing) {
        throw new PrintException("already printing");
      } else {
        printing = true;
      }
    }

    PrinterState prnState = (PrinterState) service.getAttribute(PrinterState.class);
    if (prnState == PrinterState.STOPPED) {
      PrinterStateReasons prnStateReasons =
          (PrinterStateReasons) service.getAttribute(PrinterStateReasons.class);
      if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) {
        throw new PrintException("PrintService is no longer available.");
      }
    }

    if ((PrinterIsAcceptingJobs) (service.getAttribute(PrinterIsAcceptingJobs.class))
        == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
      throw new PrintException("Printer is not accepting job.");
    }

    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;

    try {
      data = doc.getPrintData();
    } catch (IOException e) {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintException("can't get print data: " + e.toString());
    }

    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintJobFlavorException("invalid flavor", flavor);
    }

    initializeAttributeSets(doc, attributes);

    getAttributeValues(flavor);

    String repClassName = flavor.getRepresentationClassName();

    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF)
        || flavor.equals(DocFlavor.INPUT_STREAM.JPEG)
        || flavor.equals(DocFlavor.INPUT_STREAM.PNG)
        || flavor.equals(DocFlavor.BYTE_ARRAY.GIF)
        || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG)
        || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
      try {
        instream = doc.getStreamForBytes();
        if (instream == null) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException("No stream for data");
        }
        printableJob(new ImagePrinter(instream));
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      } catch (IOException ioe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(ioe);
      }
    } else if (flavor.equals(DocFlavor.URL.GIF)
        || flavor.equals(DocFlavor.URL.JPEG)
        || flavor.equals(DocFlavor.URL.PNG)) {
      try {
        printableJob(new ImagePrinter((URL) data));
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
      try {
        pageableJob((Pageable) doc.getPrintData());
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      } catch (IOException ioe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(ioe);
      }
    } else if (repClassName.equals("java.awt.print.Printable")) {
      try {
        printableJob((Printable) doc.getPrintData());
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      } catch (IOException ioe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(ioe);
      }
    } else if (repClassName.equals("[B")
        || repClassName.equals("java.io.InputStream")
        || repClassName.equals("java.net.URL")) {

      if (repClassName.equals("java.net.URL")) {
        URL url = (URL) data;
        try {
          instream = url.openStream();
        } catch (IOException e) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(e.toString());
        }
      } else {
        try {
          instream = doc.getStreamForBytes();
        } catch (IOException ioe) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(ioe.toString());
        }
      }

      if (instream == null) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("No stream for data");
      }

      if (mDestination != null) { // if destination attribute is set
        try {
          FileOutputStream fos = new FileOutputStream(mDestination);
          byte[] buffer = new byte[1024];
          int cread;

          while ((cread = instream.read(buffer, 0, buffer.length)) >= 0) {
            fos.write(buffer, 0, cread);
          }
          fos.flush();
          fos.close();
        } catch (FileNotFoundException fnfe) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(fnfe.toString());
        } catch (IOException ioe) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(ioe.toString());
        }
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        notifyEvent(PrintJobEvent.JOB_COMPLETE);
        service.wakeNotifier();
        return;
      }

      if (!startPrintRawData(service.getName(), jobName)) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("Print job failed to start.");
      }
      BufferedInputStream bin = new BufferedInputStream(instream);
      int bread = 0;
      try {
        byte[] buffer = new byte[PRINTBUFFERLEN];

        while ((bread = bin.read(buffer, 0, PRINTBUFFERLEN)) >= 0) {
          if (!printRawData(buffer, bread)) {
            bin.close();
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("Problem while spooling data");
          }
        }
        bin.close();
        if (!endPrintRawData()) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException("Print job failed to close properly.");
        }
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
      } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(e.toString());
      } finally {
        notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
      }
    } else {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintException("unrecognized class: " + repClassName);
    }
    service.wakeNotifier();
  }