mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-24 03:35:11 +01:00
Fixed bug with Addon for saving non-existent files from jar
Improved addon tests.
This commit is contained in:
parent
d38912cb2f
commit
0ab56028dc
@ -269,9 +269,13 @@ public abstract class Addon {
|
|||||||
java.nio.file.Files.copy(in, outFile.toPath());
|
java.nio.file.Files.copy(in, outFile.toPath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// No file in the jar
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The embedded resource '" + jarResource + "' cannot be found in " + jar.getName());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Bukkit.getLogger().severe(
|
BentoBox.getInstance().logError(
|
||||||
"Could not save from jar file. From " + jarResource + " to " + destinationFolder.getAbsolutePath());
|
"Could not save from jar file. From " + jarResource + " to " + destinationFolder.getAbsolutePath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -404,13 +408,13 @@ public abstract class Addon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a flag for this addon.
|
* Register a flag for this addon.
|
||||||
* @param flag the flag to register.
|
* @param flag the flag to register.
|
||||||
* @return {@code true} if the flag was registered successfully, {@code false} otherwise.
|
* @return {@code true} if the flag was registered successfully, {@code false} otherwise.
|
||||||
* @since 1.5.0
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
public boolean registerFlag(Flag flag) {
|
public boolean registerFlag(Flag flag) {
|
||||||
return getPlugin().getFlagsManager().registerFlag(this, flag);
|
return getPlugin().getFlagsManager().registerFlag(this, flag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,20 +8,33 @@ import static org.junit.Assert.assertTrue;
|
|||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarOutputStream;
|
||||||
|
import java.util.jar.Manifest;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.inventory.ItemFactory;
|
import org.bukkit.inventory.ItemFactory;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -41,12 +54,19 @@ import world.bentobox.bentobox.managers.PlayersManager;
|
|||||||
@PrepareForTest( { BentoBox.class, Bukkit.class })
|
@PrepareForTest( { BentoBox.class, Bukkit.class })
|
||||||
public class AddonTest {
|
public class AddonTest {
|
||||||
|
|
||||||
|
public static int BUFFER_SIZE = 10240;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
static BentoBox plugin;
|
static BentoBox plugin;
|
||||||
static JavaPlugin javaPlugin;
|
static JavaPlugin javaPlugin;
|
||||||
private Server server;
|
private Server server;
|
||||||
@Mock
|
@Mock
|
||||||
private AddonsManager am;
|
private AddonsManager am;
|
||||||
|
private File dataFolder;
|
||||||
|
|
||||||
|
private File jarFile;
|
||||||
|
|
||||||
|
private TestClass test;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
@ -76,6 +96,47 @@ public class AddonTest {
|
|||||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||||
ItemMeta itemMeta = mock(ItemMeta.class);
|
ItemMeta itemMeta = mock(ItemMeta.class);
|
||||||
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
|
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
|
||||||
|
|
||||||
|
// Make the addon
|
||||||
|
dataFolder = new File("dataFolder");
|
||||||
|
jarFile = new File("addon.jar");
|
||||||
|
makeAddon();
|
||||||
|
test = new TestClass();
|
||||||
|
test.setDataFolder(dataFolder);
|
||||||
|
test.setFile(jarFile);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void makeAddon() throws IOException {
|
||||||
|
// Make a config file
|
||||||
|
YamlConfiguration config = new YamlConfiguration();
|
||||||
|
config.set("hello", "this is a test");
|
||||||
|
File configFile = new File("config.yml");
|
||||||
|
config.save(configFile);
|
||||||
|
// Make addon.yml
|
||||||
|
YamlConfiguration yml = new YamlConfiguration();
|
||||||
|
yml.set("name", "TestAddon");
|
||||||
|
yml.set("main", "world.bentobox.test.Test");
|
||||||
|
yml.set("version", "1.0.0");
|
||||||
|
File ymlFile = new File("addon.yml");
|
||||||
|
yml.save(ymlFile);
|
||||||
|
// Make an archive file
|
||||||
|
// Put them into a jar file
|
||||||
|
createJarArchive(jarFile, Arrays.asList(configFile, ymlFile));
|
||||||
|
// Clean up
|
||||||
|
Files.deleteIfExists(configFile.toPath());
|
||||||
|
Files.deleteIfExists(ymlFile.toPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void TearDown() throws IOException {
|
||||||
|
Files.deleteIfExists(jarFile.toPath());
|
||||||
|
if (dataFolder.exists()) {
|
||||||
|
Files.walk(dataFolder.toPath())
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestClass extends Addon {
|
class TestClass extends Addon {
|
||||||
@ -88,36 +149,28 @@ public class AddonTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddon() {
|
public void testAddon() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
assertNotNull(test);
|
assertNotNull(test);
|
||||||
assertFalse(test.isEnabled());
|
assertFalse(test.isEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetPlugin() {
|
public void testGetPlugin() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
assertEquals(plugin, test.getPlugin());
|
assertEquals(plugin, test.getPlugin());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetConfig() {
|
public void testGetConfig() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
// No config file
|
// No config file
|
||||||
assertNull(test.getConfig());
|
assertNull(test.getConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDataFolder() {
|
public void testGetDataFolder() {
|
||||||
TestClass test = new TestClass();
|
assertEquals(dataFolder, test.getDataFolder());
|
||||||
File file = mock(File.class);
|
|
||||||
assertNull(test.getDataFolder());
|
|
||||||
test.setDataFolder(file);
|
|
||||||
assertEquals(file, test.getDataFolder());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDescription() {
|
public void testGetDescription() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
AddonDescription d = new AddonDescription.Builder("main", "name", "1.0").build();
|
AddonDescription d = new AddonDescription.Builder("main", "name", "1.0").build();
|
||||||
assertNull(test.getDescription());
|
assertNull(test.getDescription());
|
||||||
test.setDescription(d);
|
test.setDescription(d);
|
||||||
@ -126,28 +179,21 @@ public class AddonTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFile() {
|
public void testGetFile() {
|
||||||
TestClass test = new TestClass();
|
assertEquals(jarFile, test.getFile());
|
||||||
File file = mock(File.class);
|
|
||||||
assertNull(test.getFile());
|
|
||||||
test.setFile(file);
|
|
||||||
assertEquals(file, test.getFile());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLogger() {
|
public void testGetLogger() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
assertEquals(plugin.getLogger(), test.getLogger());
|
assertEquals(plugin.getLogger(), test.getLogger());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetServer() {
|
public void testGetServer() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
assertEquals(server, test.getServer());
|
assertEquals(server, test.getServer());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsEnabled() {
|
public void testIsEnabled() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
assertFalse(test.isEnabled());
|
assertFalse(test.isEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,50 +201,34 @@ public class AddonTest {
|
|||||||
public void testRegisterListener() {
|
public void testRegisterListener() {
|
||||||
class TestListener implements Listener {}
|
class TestListener implements Listener {}
|
||||||
TestListener listener = new TestListener();
|
TestListener listener = new TestListener();
|
||||||
TestClass test = new TestClass();
|
|
||||||
test.registerListener(listener);
|
test.registerListener(listener);
|
||||||
Mockito.verify(am).registerListener(Mockito.any(), Mockito.eq(listener));
|
Mockito.verify(am).registerListener(Mockito.any(), Mockito.eq(listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSaveDefaultConfig() {
|
public void testSaveDefaultConfig() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
File jarFile = new File("addon.jar");
|
|
||||||
File dataFolder = new File("dataFolder");
|
|
||||||
test.setDataFolder(dataFolder);
|
|
||||||
test.setFile(jarFile);
|
|
||||||
test.saveDefaultConfig();
|
test.saveDefaultConfig();
|
||||||
|
File testConfig = new File(dataFolder, "config.yml");
|
||||||
|
assertTrue(testConfig.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testSaveResourceStringBoolean() {
|
public void testSaveResourceStringBooleanEmptyName() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
test.saveResource("", true);
|
test.saveResource("", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testSaveResourceStringBooleanNull() {
|
public void testSaveResourceStringBooleanSaveANull() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
test.saveResource(null, true);
|
test.saveResource(null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testSaveResourceStringBooleanNoFile() throws IOException {
|
public void testSaveResourceStringBooleanNoFile() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
File jarFile = new File("addon.jar");
|
|
||||||
File dataFolder = new File("dataFolder");
|
|
||||||
test.setDataFolder(dataFolder);
|
|
||||||
test.setFile(jarFile);
|
|
||||||
test.saveResource("no_such_file", true);
|
test.saveResource("no_such_file", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testSaveResourceStringFileBooleanBoolean() {
|
public void testSaveResourceStringFileBooleanBoolean() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
File jarFile = new File("addon.jar");
|
|
||||||
File dataFolder = new File("dataFolder");
|
|
||||||
test.setDataFolder(dataFolder);
|
|
||||||
test.setFile(jarFile);
|
|
||||||
test.saveResource("no_such_file", jarFile, false, false);
|
test.saveResource("no_such_file", jarFile, false, false);
|
||||||
test.saveResource("no_such_file", jarFile, false, true);
|
test.saveResource("no_such_file", jarFile, false, true);
|
||||||
test.saveResource("no_such_file", jarFile, true, false);
|
test.saveResource("no_such_file", jarFile, true, false);
|
||||||
@ -207,41 +237,37 @@ public class AddonTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetResource() {
|
public void testGetResource() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
File jarFile = new File("addon.jar");
|
|
||||||
File dataFolder = new File("dataFolder");
|
|
||||||
test.setDataFolder(dataFolder);
|
|
||||||
test.setFile(jarFile);
|
|
||||||
assertNull(test.getResource("nothing"));
|
assertNull(test.getResource("nothing"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetResourceSomething() {
|
||||||
|
assertNotNull(test.getResource("addon.yml"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetAddonFile() {
|
public void testSetAddonFile() {
|
||||||
TestClass test = new TestClass();
|
File af = new File("af");
|
||||||
File jarFile = new File("addon.jar");
|
test.setFile(af);
|
||||||
test.setFile(jarFile);
|
assertEquals(af, test.getFile());
|
||||||
assertEquals(jarFile, test.getFile());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetDataFolder() {
|
public void testSetDataFolder() {
|
||||||
TestClass test = new TestClass();
|
File df = new File("df");
|
||||||
File dataFolder = new File("dataFolder");
|
test.setDataFolder(df);
|
||||||
test.setDataFolder(dataFolder);
|
assertEquals(df, test.getDataFolder());
|
||||||
assertEquals(dataFolder, test.getDataFolder());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetDescription() {
|
public void testSetDescription() {
|
||||||
TestClass test = new TestClass();
|
AddonDescription desc = new AddonDescription.Builder("main", "name", "2.0").build();
|
||||||
AddonDescription desc = new AddonDescription.Builder("main", "name", "1.0").build();
|
|
||||||
test.setDescription(desc);
|
test.setDescription(desc);
|
||||||
assertEquals(desc, test.getDescription());
|
assertEquals(desc, test.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetEnabled() {
|
public void testSetEnabled() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
test.setState(Addon.State.DISABLED);
|
test.setState(Addon.State.DISABLED);
|
||||||
assertFalse(test.isEnabled());
|
assertFalse(test.isEnabled());
|
||||||
test.setState(Addon.State.ENABLED);
|
test.setState(Addon.State.ENABLED);
|
||||||
@ -250,7 +276,6 @@ public class AddonTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetPlayers() {
|
public void testGetPlayers() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
PlayersManager pm = mock(PlayersManager.class);
|
PlayersManager pm = mock(PlayersManager.class);
|
||||||
when(plugin.getPlayers()).thenReturn(pm);
|
when(plugin.getPlayers()).thenReturn(pm);
|
||||||
assertEquals(pm, test.getPlayers());
|
assertEquals(pm, test.getPlayers());
|
||||||
@ -258,7 +283,6 @@ public class AddonTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetIslands() {
|
public void testGetIslands() {
|
||||||
TestClass test = new TestClass();
|
|
||||||
IslandsManager im = mock(IslandsManager.class);
|
IslandsManager im = mock(IslandsManager.class);
|
||||||
when(plugin.getIslands()).thenReturn(im);
|
when(plugin.getIslands()).thenReturn(im);
|
||||||
assertEquals(im, test.getIslands());
|
assertEquals(im, test.getIslands());
|
||||||
@ -268,7 +292,46 @@ public class AddonTest {
|
|||||||
public void testGetAddonByName() {
|
public void testGetAddonByName() {
|
||||||
AddonsManager am = new AddonsManager(plugin);
|
AddonsManager am = new AddonsManager(plugin);
|
||||||
when(plugin.getAddonsManager()).thenReturn(am);
|
when(plugin.getAddonsManager()).thenReturn(am);
|
||||||
TestClass test = new TestClass();
|
|
||||||
assertEquals(Optional.empty(),test.getAddonByName("addon"));
|
assertEquals(Optional.empty(),test.getAddonByName("addon"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Utility methods
|
||||||
|
*/
|
||||||
|
private void createJarArchive(File archiveFile, List<File> tobeJaredList) {
|
||||||
|
byte buffer[] = new byte[BUFFER_SIZE];
|
||||||
|
// Open archive file
|
||||||
|
try (FileOutputStream stream = new FileOutputStream(archiveFile)) {
|
||||||
|
try (JarOutputStream out = new JarOutputStream(stream, new Manifest())) {
|
||||||
|
for (File j: tobeJaredList) addFile(buffer, stream, out, j);
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
System.out.println("Error: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addFile(byte[] buffer, FileOutputStream stream, JarOutputStream out, File tobeJared) throws IOException {
|
||||||
|
if (tobeJared == null || !tobeJared.exists() || tobeJared.isDirectory())
|
||||||
|
return;
|
||||||
|
// Add archive entry
|
||||||
|
JarEntry jarAdd = new JarEntry(tobeJared.getName());
|
||||||
|
jarAdd.setTime(tobeJared.lastModified());
|
||||||
|
out.putNextEntry(jarAdd);
|
||||||
|
// Write file to archive
|
||||||
|
try (FileInputStream in = new FileInputStream(tobeJared)) {
|
||||||
|
while (true) {
|
||||||
|
int nRead = in.read(buffer, 0, buffer.length);
|
||||||
|
if (nRead <= 0)
|
||||||
|
break;
|
||||||
|
out.write(buffer, 0, nRead);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user