def setUp(self):
   super(ProvToolboxConverterTestCase, self).setUp()
   self.provtoolbox = ProvToolboxConverter()
   self.in_file = None
   self.out_file = None
   self.config = {}  
   self.config[ProvToolboxConverter.EXECUTABLE] = "python"
   script = os.path.join(
     os.path.dirname(os.path.abspath(inspect.getfile(
           inspect.currentframe()))), "provconvert_dummy.py")
   self.config[ProvToolboxConverter.ARGUMENTS] = " ".join(
     [script,
      "-infile", ProvToolboxConverter.INPUT,
      "-outfile", ProvToolboxConverter.OUTPUT])
   self.config[ProvToolboxConverter.INPUT_FORMATS] = standards.FORMATS
   self.config[ProvToolboxConverter.OUTPUT_FORMATS] = standards.FORMATS
class ProvToolboxConverterTestCase(unittest.TestCase):

  def setUp(self):
    super(ProvToolboxConverterTestCase, self).setUp()
    self.provtoolbox = ProvToolboxConverter()
    self.in_file = None
    self.out_file = None
    self.config = {}  
    self.config[ProvToolboxConverter.EXECUTABLE] = "python"
    script = os.path.join(
      os.path.dirname(os.path.abspath(inspect.getfile(
            inspect.currentframe()))), "provconvert_dummy.py")
    self.config[ProvToolboxConverter.ARGUMENTS] = " ".join(
      [script,
       "-infile", ProvToolboxConverter.INPUT,
       "-outfile", ProvToolboxConverter.OUTPUT])
    self.config[ProvToolboxConverter.INPUT_FORMATS] = standards.FORMATS
    self.config[ProvToolboxConverter.OUTPUT_FORMATS] = standards.FORMATS

  def tearDown(self):
    super(ProvToolboxConverterTestCase, self).tearDown()
    for tmp in [self.in_file, self.out_file]:
      if tmp != None and os.path.isfile(tmp):
        os.remove(tmp)

  def test_init(self):
    self.assertEqual("", self.provtoolbox.executable)
    self.assertEqual([], self.provtoolbox.arguments)
    self.assertEqual([], self.provtoolbox.input_formats)
    self.assertEqual([], self.provtoolbox.output_formats)

  def test_configure(self):
    self.provtoolbox.configure(self.config)
    self.assertEqual(self.config[ProvToolboxConverter.EXECUTABLE].split(), 
                     self.provtoolbox.executable)
    self.assertEqual(self.config[ProvToolboxConverter.ARGUMENTS].split(), 
                     self.provtoolbox.arguments)
    self.assertEqual(self.config[ProvToolboxConverter.INPUT_FORMATS], 
                     self.provtoolbox.input_formats)
    self.assertEqual(self.config[ProvToolboxConverter.OUTPUT_FORMATS], 
                     self.provtoolbox.output_formats)

  def test_configure_no_input(self):
    self.config[ProvToolboxConverter.ARGUMENTS] = \
        "provconvert_dummy.py -outfile " + ProvToolboxConverter.OUTPUT
    with self.assertRaises(ConfigError):
      self.provtoolbox.configure(self.config)

  def test_configure_no_output(self):
    self.config[ProvToolboxConverter.ARGUMENTS] = \
        "provconvert_dummy.py -infile " + ProvToolboxConverter.INPUT 
    with self.assertRaises(ConfigError):
      self.provtoolbox.configure(self.config)

  def test_convert(self):
    self.provtoolbox.configure(self.config)
    (_, self.in_file) = tempfile.mkstemp(suffix="." + standards.JSON)
    self.out_file = "convert." + standards.PROVX
    self.provtoolbox.convert(self.in_file, self.out_file)

  def test_convert_oserror(self):
    self.config[ProvToolboxConverter.EXECUTABLE] = "/nosuchexecutable"
    self.provtoolbox.configure(self.config)
    (_, self.in_file) = tempfile.mkstemp(suffix="." + standards.JSON)
    self.out_file = "convert_oserror." + standards.PROVX
    with self.assertRaises(OSError):
      self.provtoolbox.convert(self.in_file, self.out_file)

  def test_convert_missing_input_file(self):
    self.provtoolbox.configure(self.config)
    self.in_file = "nosuchfile.provx." + standards.PROVX
    self.out_file = "convert_missing_input_file." + standards.PROVX
    with self.assertRaises(ConversionError):
      self.provtoolbox.convert(self.in_file, self.out_file)

  def test_convert_invalid_input_format(self):
    self.provtoolbox.configure(self.config)
    (_, self.in_file) = tempfile.mkstemp(suffix=".nosuchformat")
    self.out_file = "convert_invalid_input_format." + standards.PROVX
    with self.assertRaises(ConversionError):
      self.provtoolbox.convert(self.in_file, self.out_file)

  def test_convert_invalid_output_format(self):
    self.provtoolbox.configure(self.config)
    (_, self.in_file) = tempfile.mkstemp(suffix="." + standards.JSON)
    self.out_file = "convert_invalid_input_format.nosuchformat"
    with self.assertRaises(ConversionError):
      self.provtoolbox.convert(self.in_file, self.out_file)