Skip to content

Commit

Permalink
KnownDependenciesResolver to support packages (#17035)
Browse files Browse the repository at this point in the history
* Enhance KnownDependenciesResolver to iterate over packages up to the root package

* add ASF headers
  • Loading branch information
bartoszpop authored Feb 5, 2025
1 parent b4eea1d commit e8365c9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void addMappings(Map<String, String> mappings) {

public MavenGav mavenGavForClass(String className) {
MavenGav answer = null;
String gav = mappings.get(className);
String gav = findGav(className);
if (gav != null) {
answer = MavenGav.parseGav(gav, camelContext.getVersion());
}
Expand All @@ -80,4 +80,13 @@ public MavenGav mavenGavForClass(String className) {
}
return answer;
}

private String findGav(String prefix) {
String gav = mappings.get(prefix);
while (gav == null && prefix.lastIndexOf(".") != -1) {
prefix = prefix.substring(0, prefix.lastIndexOf("."));
gav = mappings.get(prefix);
}
return gav;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.camel.main.download;

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

import org.apache.camel.impl.engine.SimpleCamelContext;
import org.apache.camel.tooling.maven.MavenGav;
import org.junit.jupiter.api.Test;

public class KnownDependenciesResolverTest {

@Test
void mavenGavForClass_returnsClassScopedDependency() {
KnownDependenciesResolver resolver = new KnownDependenciesResolver(new SimpleCamelContext(), null, null);
resolver.loadKnownDependencies();

MavenGav dependency = resolver.mavenGavForClass(SomeClass.class.getName());

assertNotNull(dependency);
assertEquals(dependency.getGroupId(), "com.example");
assertEquals(dependency.getArtifactId(), "class-scoped");
assertEquals(dependency.getVersion(), "1.0.0");
}

@Test
void mavenGavForClass_returnsPackageScopedDependency() {
KnownDependenciesResolver resolver = new KnownDependenciesResolver(new SimpleCamelContext(), null, null);
resolver.loadKnownDependencies();

MavenGav dependency = resolver.mavenGavForClass(SomeClass.class.getPackage().getName());


assertNotNull(dependency);
assertEquals(dependency.getGroupId(), "org.example");
assertEquals(dependency.getArtifactId(), "package-scoped");
assertEquals(dependency.getVersion(), "2.0.0");
}

public static class SomeClass {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------

org.apache.camel.main.download.KnownDependenciesResolverTest$SomeClass=com.example:class-scoped:1.0.0
org.apache.camel.main=org.example:package-scoped:2.0.0

0 comments on commit e8365c9

Please sign in to comment.