Migrate tests to junit 5

This commit is contained in:
Sebastian Sdorra
2020-09-15 09:34:59 +02:00
parent 93f240e087
commit 7bc037bdb0

View File

@@ -21,259 +21,142 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.io.Files;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import sonia.scm.util.IOUtil;
import sonia.scm.util.XmlUtil;
import static org.junit.Assert.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
//~--- JDK imports ------------------------------------------------------------
/**
*
* @author Sebastian Sdorra
*/
public class SmpArchiveTest
{
class SmpArchiveTest {
/**
* Method description
*
*
* @throws IOException
* @throws ParserConfigurationException
* @throws SAXException
*/
@Test
public void testExtract()
throws IOException, ParserConfigurationException, SAXException
{
File archive = createArchive("sonia.sample", "1.0");
File target = tempFolder.newFolder();
void shouldExtractArchive(@TempDir Path tempDir) throws IOException, ParserConfigurationException, SAXException, XMLStreamException {
File archive = createArchive(tempDir, "sonia.sample", "1.0");
File target = tempDir.toFile();
IOUtil.mkdirs(target);
SmpArchive.create(archive).extract(target);
File descriptor = new File(target, PluginConstants.FILE_DESCRIPTOR);
assertTrue(descriptor.exists());
assertThat(descriptor).exists();
try (FileInputStream fis = new FileInputStream(descriptor))
{
try (FileInputStream fis = new FileInputStream(descriptor)) {
Document doc = XmlUtil.createDocument(fis);
assertEquals("plugin", doc.getDocumentElement().getNodeName());
assertThat(doc.getDocumentElement().getNodeName()).isEqualTo("plugin");
}
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testGetPlugin() throws IOException
{
File archive = createArchive("sonia.sample", "1.0");
void shouldReturnPluginDescriptor(@TempDir Path tempDir) throws IOException, XMLStreamException {
File archive = createArchive(tempDir, "sonia.sample", "1.0");
InstalledPluginDescriptor plugin = SmpArchive.create(archive).getPlugin();
assertNotNull(plugin);
assertThat(plugin).isNotNull();
PluginInformation info = plugin.getInformation();
assertNotNull(info);
assertThat(info).isNotNull();;
assertEquals("sonia.sample", info.getName());
assertEquals("1.0", info.getVersion());
assertThat(info.getName()).isEqualTo("sonia.sample");
assertThat(info.getVersion()).isEqualTo("1.0");
}
/**
* Method description
*
* @throws IOException
*/
@Test(expected = PluginException.class)
public void testWithMissingName() throws IOException
{
File archive = createArchive( null, "1.0");
@Test
void shouldFailOnMissingName(@TempDir Path tempDir) throws IOException, XMLStreamException {
File archive = createArchive(tempDir, null, "1.0");
SmpArchive.create(archive).getPlugin();
SmpArchive smp = SmpArchive.create(archive);
assertThrows(PluginException.class, smp::getPlugin);
}
/**
* Method description
*
* @throws IOException
*/
@Test(expected = PluginException.class)
public void testWithMissingVersion() throws IOException
{
File archive = createArchive("sonia.sample", null);
SmpArchive.create(archive).getPlugin();
@Test
void shouldFailOnMissingVersion(@TempDir Path tempDir) throws IOException, XMLStreamException {
File archive = createArchive(tempDir, "sonia.sample", null);
SmpArchive smp = SmpArchive.create(archive);
assertThrows(PluginException.class, smp::getPlugin);
}
/**
* Method description
*
*
* @param name
* @param version
*
* @return
*/
private File createArchive(String name, String version)
{
File archiveFile;
private File createArchive(Path tempDir, String name, String version) throws IOException, XMLStreamException {
File descriptor = tempDir.resolve("descriptor.xml").toFile();
try
{
File descriptor = tempFolder.newFile();
writeDescriptor(descriptor, name, version);
File archiveFile = tempDir.resolve("archive.smp").toFile();
writeDescriptor(descriptor, name, version);
archiveFile = tempFolder.newFile();
try (ZipOutputStream zos =
new ZipOutputStream(new FileOutputStream(archiveFile), Charsets.UTF_8))
{
zos.putNextEntry(new ZipEntry(PluginConstants.PATH_DESCRIPTOR));
Files.copy(descriptor, zos);
zos.closeEntry();
zos.putNextEntry(new ZipEntry("/META-INF/"));
zos.putNextEntry(new ZipEntry("/META-INF/somefile.txt"));
zos.write("some text".getBytes(Charsets.UTF_8));
zos.closeEntry();
}
}
catch (IOException ex)
{
throw Throwables.propagate(ex);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(archiveFile), Charsets.UTF_8)) {
zos.putNextEntry(new ZipEntry(PluginConstants.PATH_DESCRIPTOR));
Files.copy(descriptor, zos);
zos.closeEntry();
zos.putNextEntry(new ZipEntry("/META-INF/"));
zos.putNextEntry(new ZipEntry("/META-INF/somefile.txt"));
zos.write("some text".getBytes(Charsets.UTF_8));
zos.closeEntry();
}
return archiveFile;
}
/**
* Method description
*
*
* @param file
*
* @return
*
* @throws IOException
* @throws XMLStreamException
*/
private XMLStreamWriter createStreamWriter(File file)
throws IOException, XMLStreamException
{
return XMLOutputFactory.newFactory().createXMLStreamWriter(
new FileOutputStream(file));
private XMLStreamWriter createStreamWriter(File file) throws IOException, XMLStreamException {
return XMLOutputFactory.newFactory().createXMLStreamWriter(new FileOutputStream(file));
}
/**
* Method description
*
*
* @param descriptor
* @param name
* @param version
*
* @throws IOException
*/
private void writeDescriptor(File descriptor, String name,
String version)
throws IOException
{
try
{
private void writeDescriptor(File descriptor, String name, String version) throws IOException, XMLStreamException {
IOUtil.mkdirs(descriptor.getParentFile());
IOUtil.mkdirs(descriptor.getParentFile());
XMLStreamWriter writer = null;
XMLStreamWriter writer = null;
try {
writer = createStreamWriter(descriptor);
writer.writeStartDocument();
writer.writeStartElement("plugin");
writer.writeStartElement("information");
writeElement(writer, "name", name);
writeElement(writer, "version", version);
try
{
writer = createStreamWriter(descriptor);
writer.writeStartDocument();
writer.writeStartElement("plugin");
writer.writeStartElement("information");
writeElement(writer, "name", name);
writeElement(writer, "version", version);
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndDocument();
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndDocument();
} finally {
if (writer != null) {
writer.close();
}
finally
{
if (writer != null)
{
writer.close();
}
}
}
catch (XMLStreamException ex)
{
throw Throwables.propagate(ex);
}
}
/**
* Method description
*
*
* @param writer
* @param name
* @param value
*
* @throws XMLStreamException
*/
private void writeElement(XMLStreamWriter writer, String name, String value)
throws XMLStreamException
{
if (!Strings.isNullOrEmpty(value))
{
private void writeElement(XMLStreamWriter writer, String name, String value) throws XMLStreamException {
if (!Strings.isNullOrEmpty(value)) {
writer.writeStartElement(name);
writer.writeCharacters(value);
writer.writeEndElement();
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
}