Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugin;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

class CacheUtilsTest {

private Plugin pluginOne;
private Plugin pluginTwo;

@BeforeEach
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could just initialize the fields

Copy link
Contributor Author

@TheRealHaui TheRealHaui Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which then would lead every test method having
private Plugin pluginOne = new Plugin();
private Plugin pluginTwo = new Plugin();
at the beginning repeatedly.
Don't you think that using the (at)BeforeEach approach is better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that is not how JUnit works. Just initialize the fields. A new test object is used for every test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew of course because of the way JUnit works that it specifically instantiates objects.
However, wasn't aware of that - test adapted and pushed.

void doBeforeEachTest() {
pluginOne = new Plugin();
pluginTwo = new Plugin();
}

@Test
void testPluginEqualsEmptyPlugins() {
assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertTrue(CacheUtils.pluginEquals(pluginOne, pluginTwo));
}

@Test
void testPluginEqualsSetArtifactId() {
pluginOne.setArtifactId("myArtifact");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

pluginTwo.setArtifactId("myArtifact");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertTrue(CacheUtils.pluginEquals(pluginOne, pluginTwo));
}

@Test
void testPluginEqualsSetGroupId() {
pluginOne.setGroupId("myGroupId");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

pluginTwo.setGroupId("myGroupId");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertTrue(CacheUtils.pluginEquals(pluginOne, pluginTwo));
}

@Test
void testPluginEqualsSetVersion() {
pluginOne.setVersion("myVersion");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

pluginTwo.setVersion("myVersion");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertTrue(CacheUtils.pluginEquals(pluginOne, pluginTwo));
}

@Test
void testPluginEqualsSetExtension() {
pluginOne.setExtensions("true");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

pluginTwo.setExtensions("TRUE");
}

@Test
void testPluginEqualsSetDependency() {
Dependency dependencyOne = new Dependency();
pluginOne.addDependency(dependencyOne);

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

Dependency dependencyTwo = new Dependency();
pluginTwo.addDependency(dependencyTwo);

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertTrue(CacheUtils.pluginEquals(pluginOne, pluginTwo));

dependencyTwo.setGroupId("myDependencyGroupId");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

dependencyOne.setGroupId("myDependencyGroupId");
dependencyTwo.setArtifactId("myDependencyArtifactId");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

dependencyTwo.setVersion("myDependencyVersion");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));

dependencyOne.setArtifactId("myDependencyArtifactId");
dependencyTwo.setVersion("myDependencyVersion");

assertTrue(CacheUtils.pluginEquals(pluginOne, pluginOne));
assertFalse(CacheUtils.pluginEquals(pluginOne, pluginTwo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.api.xml.XmlService;
Expand All @@ -44,6 +46,10 @@ public int read(char[] cbuf, int off, int len) throws IOException {
XmlNode node1 = XmlService.read(r);
XmlNode node2 = XmlService.read(r);
assertEquals(node1, node2);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a separate test method

XmlNode node3 = XmlService.read(new ByteArrayInputStream(doc.getBytes(StandardCharsets.UTF_8)), null);
XmlNode node4 = XmlService.read(new ByteArrayInputStream(doc.getBytes(StandardCharsets.UTF_8)), null);
assertEquals(node3, node4);
}

@Test
Expand Down