Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Switch to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Oct 28, 2023
1 parent 65a69ca commit ba2d484
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 103 deletions.
16 changes: 14 additions & 2 deletions plexus-component-metadata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,20 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-testing</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package org.codehaus.plexus.metadata;

import javax.inject.Inject;

import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.repository.*;
Expand All @@ -33,38 +34,36 @@
import org.codehaus.plexus.metadata.merge.ComponentsXmlMerger;
import org.codehaus.plexus.metadata.merge.Merger;
import org.codehaus.plexus.metadata.merge.PlexusXmlMerger;
import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test for the {@link DefaultComponentDescriptorWriter} class.
*
* @version $Rev$ $Date$
*/
public class DefaultComponentDescriptorWriterTest extends PlexusTestCase {
private DefaultComponentDescriptorWriter descriptorWriter;

// @Override
protected void setUp() throws Exception {
super.setUp();

descriptorWriter = (DefaultComponentDescriptorWriter) lookup(ComponentDescriptorWriter.class);
assertNotNull(descriptorWriter);
}
@PlexusTest
class DefaultComponentDescriptorWriterTest {

// @Override
protected void tearDown() throws Exception {
descriptorWriter = null;
@Inject
private DefaultComponentDescriptorWriter descriptorWriter;

super.tearDown();
}
@Inject
private MetadataGenerator generator;

public void testBasic() throws Exception {
@Test
void testBasic() throws Exception {
ComponentSetDescriptor set = new ComponentSetDescriptor();

ComponentDescriptor component = new ComponentDescriptor();
ComponentDescriptor<String> component = new ComponentDescriptor<>();
component.setImplementation("java.lang.String");
component.setRole("foo");
component.setRoleHint("bar");
Expand Down Expand Up @@ -105,8 +104,8 @@ public void testBasic() throws Exception {
//
}

public void testComponentsOrder() throws Exception {
MetadataGenerator generator = lookup(MetadataGenerator.class);
@Test
void testComponentsOrder() throws Exception {
assertNotNull(generator);

MetadataGenerationRequest request = new MetadataGenerationRequest();
Expand All @@ -118,58 +117,52 @@ public void testComponentsOrder() throws Exception {

generator.generateDescriptor(request);

assertTrue("Descriptor not generated", request.outputFile.exists());
assertTrue(request.outputFile.exists(), "Descriptor not generated");

Document doc = new SAXBuilder().build(request.outputFile);

// check if the components are sorted by role+impl
List<Element> components = doc.getRootElement().getChild("components").getChildren();
assertEquals("Number of components", 5, components.size());
assertEquals(5, components.size(), "Number of components");

assertEquals(
"Component 1 role",
ComponentDescriptorExtractor.class.getName(),
components.get(0).getChild("role").getText());
components.get(0).getChild("role").getText(),
"Component 1 role");
assertEquals(
"Component 1 impl",
ClassComponentDescriptorExtractor.class.getName(),
components.get(0).getChild("implementation").getText());
components.get(0).getChild("implementation").getText(),
"Component 1 impl");

assertEquals(
"Component 2 role",
ComponentDescriptorExtractor.class.getName(),
components.get(1).getChild("role").getText());
components.get(1).getChild("role").getText(),
"Component 2 role");
assertEquals(
"Component 2 impl",
SourceComponentDescriptorExtractor.class.getName(),
components.get(1).getChild("implementation").getText());
components.get(1).getChild("implementation").getText(),
"Component 2 impl");

assertEquals(
"Component 3 role",
MetadataGenerator.class.getName(),
components.get(2).getChild("role").getText());
components.get(2).getChild("role").getText(),
"Component 3 role");
assertEquals(
"Component 3 impl",
DefaultMetadataGenerator.class.getName(),
components.get(2).getChild("implementation").getText());
components.get(2).getChild("implementation").getText(),
"Component 3 impl");

assertEquals(Merger.class.getName(), components.get(3).getChild("role").getText(), "Component 4 role");
assertEquals(
"Component 4 role",
Merger.class.getName(),
components.get(3).getChild("role").getText());
assertEquals(
"Component 4 impl",
ComponentsXmlMerger.class.getName(),
components.get(3).getChild("implementation").getText());
components.get(3).getChild("implementation").getText(),
"Component 4 impl");

assertEquals(Merger.class.getName(), components.get(4).getChild("role").getText(), "Component 5 role");
assertEquals(
"Component 5 role",
Merger.class.getName(),
components.get(4).getChild("role").getText());
assertEquals(
"Component 5 impl",
PlexusXmlMerger.class.getName(),
components.get(4).getChild("implementation").getText());
components.get(4).getChild("implementation").getText(),
"Component 5 impl");
}

// TODO copied from PlexusTools.buildConfiguration() - find a better way to do this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@

import java.util.List;

import junit.framework.TestCase;
import org.codehaus.plexus.component.repository.ComponentDescriptor;
import org.codehaus.plexus.component.repository.ComponentRequirement;
import org.codehaus.plexus.component.repository.ComponentRequirementList;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.metadata.gleaner.ann.AnnotatedComponent;
import org.codehaus.plexus.metadata.gleaner.ann.AnnotatedComponentRole;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Eugene Kuleshov
*/
public class AnnotationComponentGleanerTest extends TestCase {
class AnnotationComponentGleanerTest {

public void testGlean() throws Exception {
@Test
void testGlean() throws Exception {
AnnotationComponentGleaner gleaner = new AnnotationComponentGleaner();
Class<AnnotatedComponent> c = AnnotatedComponent.class;
ComponentDescriptor<?> descriptor = gleaner.glean(c.getName(), c.getClassLoader());
Expand All @@ -40,7 +44,7 @@ public void testGlean() throws Exception {
assertEquals(AnnotatedComponentRole.class.getName(), descriptor.getRole());

List<ComponentRequirement> requirements = descriptor.getRequirements();
assertEquals(requirements.toString(), 2, requirements.size());
assertEquals(2, requirements.size(), requirements.toString());

ComponentRequirement requirement = requirements.get(0);
assertEquals("dependency", requirement.getFieldName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,45 @@
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaSource;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.component.repository.ComponentDescriptor;
import org.codehaus.plexus.component.repository.ComponentRequirement;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for the {@link QDoxComponentGleaner} class.
*
* @version $Rev$ $Date$
*/
public class QDoxComponentGleanerTest extends PlexusTestCase {
class QDoxComponentGleanerTest {
private QDoxComponentGleaner gleaner;

private JavaProjectBuilder builder;

// @Override
protected void setUp() throws Exception {
super.setUp();

@BeforeEach
public void setUp() {
gleaner = new QDoxComponentGleaner();
builder = new JavaProjectBuilder();
}

// @Override
protected void tearDown() throws Exception {
@AfterEach
protected void tearDown() {
gleaner = null;
builder = null;

super.tearDown();
}

private JavaSource addSource(final String name) throws IOException {
File url = new File(
getBasedir(),
"src/test/java/" + getClass().getPackage().getName().replace('.', '/') + "/" + name);
".", "src/test/java/" + this.getClass().getPackage().getName().replace('.', '/') + "/" + name);
assertTrue(url.exists());
return builder.addSource(url);
}
Expand Down Expand Up @@ -87,12 +91,14 @@ private ComponentDescriptor<?> glean(final String name) throws Exception {
return glean(name, null);
}

public void testNoAnnotationsClass() throws Exception {
@Test
void testNoAnnotationsClass() throws Exception {
ComponentDescriptor<?> component = glean("NoAnnotationsClass.java");
assertNull(component);
}

public void testAbstractClass() throws Exception {
@Test
void testAbstractClass() throws Exception {
ComponentDescriptor<?> component = glean("AbstractClass.java");
assertNull(component);
}
Expand All @@ -104,12 +110,14 @@ public void testAbstractWithAnnoClass() throws Exception {
}
*/

public void testNoAnnotationsIntf() throws Exception {
@Test
void testNoAnnotationsIntf() throws Exception {
ComponentDescriptor<?> component = glean("NoAnnotationsIntf.java");
assertNull(component);
}

public void testMyComponent() throws Exception {
@Test
void testMyComponent() throws Exception {
addSource("ChildComponent.java");
ComponentDescriptor<?> component = glean("MyComponent.java");
assertNotNull(component);
Expand Down

0 comments on commit ba2d484

Please sign in to comment.