title: How can this SwingWorker code be made testable
body: <p>Consider this code:</p>
  
  <pre><code>public void actionPerformed(ActionEvent e) {
      setEnabled(false);
      new SwingWorker&lt;File, Void&gt;() {
  
          private String location = url.getText();
  
          @Override
          protected File doInBackground() throws Exception {
              File file = new File("out.txt");
              Writer writer = null;
              try {
                  writer = new FileWriter(file);
                  creator.write(location, writer);
              } finally {
                  if (writer != null) {
                      writer.close();
                  }
              }
              return file;
          }
  
          @Override
          protected void done() {
              setEnabled(true);
              try {
                  File file = get();
                  JOptionPane.showMessageDialog(FileInputFrame.this,
                      "File has been retrieved and saved to:\n"
                      + file.getAbsolutePath());
                  Desktop.getDesktop().open(file);
              } catch (InterruptedException ex) {
                  logger.log(Level.INFO, "Thread interupted, process aborting.", ex);
                  Thread.currentThread().interrupt();
              } catch (ExecutionException ex) {
                  Throwable cause = ex.getCause() == null ? ex : ex.getCause();
                  logger.log(Level.SEVERE, "An exception occurred that was "
                      + "not supposed to happen.", cause);
                  JOptionPane.showMessageDialog(FileInputFrame.this, "Error: "
                      + cause.getClass().getSimpleName() + " "
                      + cause.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
              } catch (IOException ex) {
                  logger.log(Level.INFO, "Unable to open file for viewing.", ex);
              }
          }
      }.execute();
  </code></pre>
  
  <p><code>url</code> is a JTextField and 'creator' is an injected interface for writing the file (so that part is under test). The location where the file is written is hard coded on purpose because this is intended as an example. And java.util.logging is used simply to avoid an external dependency.</p>
  
  <p>How would you chunk this up to make it unit-testable (including abandoning SwingWorker if needed, but then replacing its functionality, at least as used here).</p>
  
  <p>The way I look at it, the doInBackground is basically alright. The fundamental mechanics are creating a writer and closing it, which is almost too simple to test and the real work is under test. However, the done method is quote problematic, including its coupling with the actionPerformed method the parent class and coordinating the enabling and disabling of the button.</p>
  
  <p>However, pulling that apart is not obvious. Injecting some kind of SwingWorkerFactory makes capturing the GUI fields a lot harder to maintain (it is hard to see how it would be a design improvement). The JOpitonPane and the Desktop have all the "goodness" of Singletons, and exception handling makes it impossible to wrap the get easily.</p>
  
  <p>So what would be a good solution to bring this code under test?</p>