### Question:
FilePluginInstaller implements RevertablePluginInstaller { public void installPlugin(String key, PluginArtifact pluginArtifact) { Validate.notNull(key, "The plugin key must be specified"); Validate.notNull(pluginArtifact, "The plugin artifact must not be null"); File newPluginFile = new File(directory, pluginArtifact.getName()); try { backup(key, newPluginFile); if (newPluginFile.exists()) { newPluginFile.delete(); } } catch (IOException e) { log.warn("Unable to backup old file", e); } OutputStream os = null; InputStream in = null; try { os = new FileOutputStream(newPluginFile); in = pluginArtifact.getInputStream(); IOUtils.copy(in, os); } catch (IOException e) { throw new RuntimeException("Could not install plugin: " + pluginArtifact, e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(os); } } FilePluginInstaller(File directory); void installPlugin(String key, PluginArtifact pluginArtifact); void revertInstalledPlugin(String pluginKey); void clearBackups(); static final String ORIGINAL_PREFIX; }### Answer:
@Test public void testInstallPlugin() throws IOException { FilePluginInstaller installer = new FilePluginInstaller(pluginDir); File pluginFile = File.createTempFile("plugin", ".jar", tmpDir); installer.installPlugin("foo", new XmlPluginArtifact(pluginFile)); assertTrue(new File(pluginDir, pluginFile.getName()).exists()); }
@Test public void testInstallPluginWithExisting() throws IOException { FilePluginInstaller installer = new FilePluginInstaller(pluginDir); File pluginFile = File.createTempFile("plugin", ".jar", pluginDir); FileUtils.writeStringToFile(pluginFile, "foo"); File upgradedFile = new File(tmpDir, pluginFile.getName()); FileUtils.writeStringToFile(upgradedFile, "bar"); installer.installPlugin("foo", new XmlPluginArtifact(upgradedFile)); assertEquals("bar", FileUtils.readFileToString(pluginFile)); assertTrue(new File(pluginDir, FilePluginInstaller.ORIGINAL_PREFIX + pluginFile.getName()).exists()); }
@Test public void testInstallPluginWithExistingOld() throws IOException { FilePluginInstaller installer = new FilePluginInstaller(pluginDir); File pluginFile = File.createTempFile("plugin", ".jar", pluginDir); FileUtils.writeStringToFile(pluginFile, "foo"); File upgradedFile = new File(tmpDir, pluginFile.getName()); FileUtils.writeStringToFile(upgradedFile, "bar"); File upgraded2File = new File(tmpDir, pluginFile.getName()); FileUtils.writeStringToFile(upgraded2File, "bar"); installer.installPlugin("foo", new XmlPluginArtifact(upgradedFile)); assertEquals("bar", FileUtils.readFileToString(pluginFile)); assertTrue(new File(pluginDir, FilePluginInstaller.ORIGINAL_PREFIX + pluginFile.getName()).exists()); }