7

We have to build some software in Java that at the end prints some documents. Different documents should go to different trays of the printer. Because during development we don't have the same printer available as our customer, we are looking for a little piece of software that mocks a printer. We should be able to configure that mock, for example how many trays there are available.

Does anyone know such a tool for mac or windows?

Noel M
  • 15,812
  • 8
  • 39
  • 47
fkleinko
  • 121
  • 1
  • 5
  • 1
    Would a "PDF printer" work? I'm not sure if the documents created each time would be unique enough to use as part of an automated testing suite, though. I'm also pretty sure that wouldn't expose printer-like things, such as trays. – Charles Dec 20 '12 at 08:33
  • does the mocked printer actually need to do anything with the input, or is it ok if it just accepts the stuff and sends it to NUL? – eis Dec 20 '12 at 08:38
  • have search it in the internet? – ggcodes Dec 20 '12 at 08:40
  • There are also these other threads that deal somewhat with the same topic: http://superuser.com/questions/216099/looking-for-a-fake-printer-driver-for-windows http://stackoverflow.com/questions/4310594/create-a-virtual-printer-in-java?rq=1 - but they're not *exactly* the same thing, as I don't think any of them actually can simulate different trays, assuming the method in my answer doesn't do it. – eis Dec 20 '12 at 09:13

3 Answers3

4

Write an abstraction layer which you implement once for your customer's "real" printer and once for a "virtual" printer. Write integration tests for the customer version, run those tests in your customer's environment. Code against the abstraction layer.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • You're right i need to do something about the design. But always feels good todo some real printing :) – fkleinko Dec 20 '12 at 08:52
  • While I also think somewhat along the same lines, I don't think you can really expect that developers start testing for different setups only at the customer environment... You might need something to simulate different error conditions etc. I do admit that my suggestion doesn't do that either. – eis Dec 20 '12 at 08:54
  • @eis agreed. mideally they'd have a continous integration server running in the customer's environment – Sean Patrick Floyd Dec 20 '12 at 08:57
4

You could create a dummy printer yourself on windows, without any special software.

In Windows 7:

  1. Control Panel
  2. Devices and Printers
  3. [Right click] Add a Printer
  4. Add a local printer
  5. Use an existing port (assuming it already exists, create a new one if it doesn't)
  6. File: (to print to a file), NUL: (to print nowhere) or CON: (to print to console)
  7. Select a printer you wish to emulate from the list of printers.

If you set it as default printer, it should be easy enough to use from java code.

eis
  • 51,991
  • 13
  • 150
  • 199
0

You can install a PDF print which can work as a virtual printer for your Java Application. Basically, what you to do is, install a freely available PDF printer and make your java application discover that print service and print whatever document to that service. I remember, I had the same situation when I did not have a printer, I used the code given below to interface my application with the virtual printer.

public class HelloWorldPrinter implements Printable, ActionListener {


public int print(Graphics g, PageFormat pf, int page) throws
                                                    PrinterException {

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now we perform our rendering */
    g.drawString("Hello world!", 100, 100);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

public void actionPerformed(ActionEvent e) {
     PrinterJob job = PrinterJob.getPrinterJob();
     job.setPrintable(this);

     PrintService[] printServices = PrinterJob.lookupPrintServices(); 
    try {
        job.setPrintService(printServices[0]);
        job.print();
    } catch (PrinterException ex) {
        Logger.getLogger(HelloWorldPrinter.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void main(String args[]) {

    UIManager.put("swing.boldMetal", Boolean.FALSE);
    JFrame f = new JFrame("Hello World Printer");
    f.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    JButton printButton = new JButton("Print Hello World");
    printButton.addActionListener(new HelloWorldPrinter());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);
}
}
Abdul Fatah
  • 463
  • 6
  • 23
  • Yeah, that much is clear, but is it possible to create such a printer in a system when trying to run application and if it does not exist on a system? – zygimantus Oct 20 '20 at 10:12