From bc03b1762f6fab02985145e70bf6edd97102e2dc Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 27 Dec 2021 12:00:09 +0100 Subject: [PATCH] [MNG-7375] prevent potential NPE in Metadata.merge(...) Add unit tests for merge --- .../src/main/mdo/metadata.mdo | 2 +- .../repository/metadata/MetadataTest.java | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 maven-repository-metadata/src/test/java/org/apache/maven/artifact/repository/metadata/MetadataTest.java diff --git a/maven-repository-metadata/src/main/mdo/metadata.mdo b/maven-repository-metadata/src/main/mdo/metadata.mdo index a6f5299fa6e1..c998fd3b1894 100644 --- a/maven-repository-metadata/src/main/mdo/metadata.mdo +++ b/maven-repository-metadata/src/main/mdo/metadata.mdo @@ -97,7 +97,7 @@ under the License. for ( Plugin preExisting : getPlugins() ) { - if ( preExisting.getPrefix().equals( plugin.getPrefix() ) ) + if ( java.util.Objects.equals( preExisting.getPrefix(), plugin.getPrefix() ) ) { found = true; break; diff --git a/maven-repository-metadata/src/test/java/org/apache/maven/artifact/repository/metadata/MetadataTest.java b/maven-repository-metadata/src/test/java/org/apache/maven/artifact/repository/metadata/MetadataTest.java new file mode 100644 index 000000000000..692a178bf139 --- /dev/null +++ b/maven-repository-metadata/src/test/java/org/apache/maven/artifact/repository/metadata/MetadataTest.java @@ -0,0 +1,82 @@ +package org.apache.maven.artifact.repository.metadata; + +/* + * 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. + */ + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class MetadataTest { + + @Test + void testMergeWithEmptyMetadata() { + Metadata metadata = new Metadata(); + + Metadata newMetadata = new Metadata(); + Assertions.assertFalse(metadata.merge(newMetadata)); + } + + @Test + void testMergeWithDifferentPrefixes() { + Metadata metadata = new Metadata(); + Plugin plugin = new Plugin(); + plugin.setArtifactId("myArtifactId"); + plugin.setName("My Name"); + plugin.setPrefix("myprefix"); + metadata.addPlugin(plugin); + + Metadata newMetadata = new Metadata(); + Plugin newPlugin = plugin.clone(); + newPlugin.setArtifactId("myNewArtifactId"); + plugin.setPrefix("mynewprefix"); + newMetadata.addPlugin(newPlugin); + Assertions.assertTrue(metadata.merge(newMetadata)); + } + + @Test + void testMergeWithSamePrefixAndDifferentArtifactIds() { + Metadata metadata = new Metadata(); + Plugin plugin = new Plugin(); + plugin.setArtifactId("myArtifactId"); + plugin.setName("My Name"); + plugin.setPrefix("myprefix"); + metadata.addPlugin(plugin); + + Metadata newMetadata = new Metadata(); + Plugin newPlugin = plugin.clone(); + newPlugin.setArtifactId("myNewArtifactId"); + newMetadata.addPlugin(newPlugin); + Assertions.assertFalse(metadata.merge(newMetadata)); + } + + @Test + void testMergeWithEmptyPrefix() { + Metadata metadata = new Metadata(); + Plugin plugin = new Plugin(); + plugin.setArtifactId("myArtifactId"); + plugin.setName("My Name"); + metadata.addPlugin(plugin); + + Metadata newMetadata = new Metadata(); + Plugin newPlugin = plugin.clone(); + newPlugin.setPrefix("myprefix"); + newMetadata.addPlugin(newPlugin); + Assertions.assertTrue(metadata.merge(newMetadata)); + } +}