Skip to content

Commit

Permalink
[MNG-7375] prevent potential NPE in Metadata.merge(...)
Browse files Browse the repository at this point in the history
Add unit tests for merge
  • Loading branch information
kwin committed Dec 27, 2021
1 parent 951b5ee commit bc03b17
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion maven-repository-metadata/src/main/mdo/metadata.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit bc03b17

Please sign in to comment.