mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-23 19:25:12 +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());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No file in the jar
|
||||
throw new IllegalArgumentException(
|
||||
"The embedded resource '" + jarResource + "' cannot be found in " + jar.getName());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Bukkit.getLogger().severe(
|
||||
BentoBox.getInstance().logError(
|
||||
"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.
|
||||
* @param flag the flag to register.
|
||||
* @return {@code true} if the flag was registered successfully, {@code false} otherwise.
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public boolean registerFlag(Flag flag) {
|
||||
return getPlugin().getFlagsManager().registerFlag(this, flag);
|
||||
}
|
||||
/**
|
||||
* Register a flag for this addon.
|
||||
* @param flag the flag to register.
|
||||
* @return {@code true} if the flag was registered successfully, {@code false} otherwise.
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public boolean registerFlag(Flag 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.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
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.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -41,12 +54,19 @@ import world.bentobox.bentobox.managers.PlayersManager;
|
||||
@PrepareForTest( { BentoBox.class, Bukkit.class })
|
||||
public class AddonTest {
|
||||
|
||||
public static int BUFFER_SIZE = 10240;
|
||||
|
||||
@Mock
|
||||
static BentoBox plugin;
|
||||
static JavaPlugin javaPlugin;
|
||||
private Server server;
|
||||
@Mock
|
||||
private AddonsManager am;
|
||||
private File dataFolder;
|
||||
|
||||
private File jarFile;
|
||||
|
||||
private TestClass test;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
@ -76,6 +96,47 @@ public class AddonTest {
|
||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||
ItemMeta itemMeta = mock(ItemMeta.class);
|
||||
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 {
|
||||
@ -88,36 +149,28 @@ public class AddonTest {
|
||||
|
||||
@Test
|
||||
public void testAddon() {
|
||||
TestClass test = new TestClass();
|
||||
assertNotNull(test);
|
||||
assertFalse(test.isEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPlugin() {
|
||||
TestClass test = new TestClass();
|
||||
assertEquals(plugin, test.getPlugin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConfig() {
|
||||
TestClass test = new TestClass();
|
||||
// No config file
|
||||
assertNull(test.getConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataFolder() {
|
||||
TestClass test = new TestClass();
|
||||
File file = mock(File.class);
|
||||
assertNull(test.getDataFolder());
|
||||
test.setDataFolder(file);
|
||||
assertEquals(file, test.getDataFolder());
|
||||
assertEquals(dataFolder, test.getDataFolder());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDescription() {
|
||||
TestClass test = new TestClass();
|
||||
AddonDescription d = new AddonDescription.Builder("main", "name", "1.0").build();
|
||||
assertNull(test.getDescription());
|
||||
test.setDescription(d);
|
||||
@ -126,28 +179,21 @@ public class AddonTest {
|
||||
|
||||
@Test
|
||||
public void testGetFile() {
|
||||
TestClass test = new TestClass();
|
||||
File file = mock(File.class);
|
||||
assertNull(test.getFile());
|
||||
test.setFile(file);
|
||||
assertEquals(file, test.getFile());
|
||||
assertEquals(jarFile, test.getFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLogger() {
|
||||
TestClass test = new TestClass();
|
||||
assertEquals(plugin.getLogger(), test.getLogger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetServer() {
|
||||
TestClass test = new TestClass();
|
||||
assertEquals(server, test.getServer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEnabled() {
|
||||
TestClass test = new TestClass();
|
||||
assertFalse(test.isEnabled());
|
||||
}
|
||||
|
||||
@ -155,50 +201,34 @@ public class AddonTest {
|
||||
public void testRegisterListener() {
|
||||
class TestListener implements Listener {}
|
||||
TestListener listener = new TestListener();
|
||||
TestClass test = new TestClass();
|
||||
test.registerListener(listener);
|
||||
Mockito.verify(am).registerListener(Mockito.any(), Mockito.eq(listener));
|
||||
}
|
||||
|
||||
@Test
|
||||
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();
|
||||
File testConfig = new File(dataFolder, "config.yml");
|
||||
assertTrue(testConfig.exists());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSaveResourceStringBoolean() {
|
||||
TestClass test = new TestClass();
|
||||
public void testSaveResourceStringBooleanEmptyName() {
|
||||
test.saveResource("", true);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSaveResourceStringBooleanNull() {
|
||||
TestClass test = new TestClass();
|
||||
public void testSaveResourceStringBooleanSaveANull() {
|
||||
test.saveResource(null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveResourceStringBooleanNoFile() throws IOException {
|
||||
TestClass test = new TestClass();
|
||||
File jarFile = new File("addon.jar");
|
||||
File dataFolder = new File("dataFolder");
|
||||
test.setDataFolder(dataFolder);
|
||||
test.setFile(jarFile);
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSaveResourceStringBooleanNoFile() {
|
||||
test.saveResource("no_such_file", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
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, true);
|
||||
test.saveResource("no_such_file", jarFile, true, false);
|
||||
@ -207,41 +237,37 @@ public class AddonTest {
|
||||
|
||||
@Test
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetResourceSomething() {
|
||||
assertNotNull(test.getResource("addon.yml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAddonFile() {
|
||||
TestClass test = new TestClass();
|
||||
File jarFile = new File("addon.jar");
|
||||
test.setFile(jarFile);
|
||||
assertEquals(jarFile, test.getFile());
|
||||
File af = new File("af");
|
||||
test.setFile(af);
|
||||
assertEquals(af, test.getFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDataFolder() {
|
||||
TestClass test = new TestClass();
|
||||
File dataFolder = new File("dataFolder");
|
||||
test.setDataFolder(dataFolder);
|
||||
assertEquals(dataFolder, test.getDataFolder());
|
||||
File df = new File("df");
|
||||
test.setDataFolder(df);
|
||||
assertEquals(df, test.getDataFolder());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDescription() {
|
||||
TestClass test = new TestClass();
|
||||
AddonDescription desc = new AddonDescription.Builder("main", "name", "1.0").build();
|
||||
AddonDescription desc = new AddonDescription.Builder("main", "name", "2.0").build();
|
||||
test.setDescription(desc);
|
||||
assertEquals(desc, test.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetEnabled() {
|
||||
TestClass test = new TestClass();
|
||||
test.setState(Addon.State.DISABLED);
|
||||
assertFalse(test.isEnabled());
|
||||
test.setState(Addon.State.ENABLED);
|
||||
@ -250,7 +276,6 @@ public class AddonTest {
|
||||
|
||||
@Test
|
||||
public void testGetPlayers() {
|
||||
TestClass test = new TestClass();
|
||||
PlayersManager pm = mock(PlayersManager.class);
|
||||
when(plugin.getPlayers()).thenReturn(pm);
|
||||
assertEquals(pm, test.getPlayers());
|
||||
@ -258,7 +283,6 @@ public class AddonTest {
|
||||
|
||||
@Test
|
||||
public void testGetIslands() {
|
||||
TestClass test = new TestClass();
|
||||
IslandsManager im = mock(IslandsManager.class);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
assertEquals(im, test.getIslands());
|
||||
@ -268,7 +292,46 @@ public class AddonTest {
|
||||
public void testGetAddonByName() {
|
||||
AddonsManager am = new AddonsManager(plugin);
|
||||
when(plugin.getAddonsManager()).thenReturn(am);
|
||||
TestClass test = new TestClass();
|
||||
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