From 9076b869b338749e4bec6e8cc89ca176cf079086 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Thu, 24 Dec 2009 01:58:25 +0000 Subject: [PATCH] [MRM-1283] collapse groups properly across multiple repositories git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@893683 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva/web/action/BrowseAction.java | 70 +++++++++++-------- .../memory/TestMetadataResolver.java | 17 ++--- .../archiva/web/action/BrowseActionTest.java | 35 ++++++++-- 3 files changed, 76 insertions(+), 46 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java index 086c3ab3e..ba0eeeb15 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java @@ -69,26 +69,33 @@ public class BrowseAction } Set namespaces = new LinkedHashSet(); + + // TODO: this logic should be optional, particularly remembering we want to keep this code simple + // it is located here to avoid the content repository implementation needing to do too much for what + // is essentially presentation code + Set namespacesToCollapse = new LinkedHashSet(); for ( String repoId : selectedRepos ) { - Collection rootNamespaces = metadataResolver.getRootNamespaces( repoId ); - // TODO: this logic should be optional, particularly remembering we want to keep this code simple - // it is located here to avoid the content repository implementation needing to do too much for what - // is essentially presentation code - for ( String n : rootNamespaces ) - { - // TODO: check performance of this - namespaces.add( collapseNamespaces( repoId, n ) ); - } + namespacesToCollapse.addAll( metadataResolver.getRootNamespaces( repoId ) ); + } + + for ( String n : namespacesToCollapse ) + { + // TODO: check performance of this + namespaces.add( collapseNamespaces( selectedRepos, n ) ); } this.namespaces = getSortedList( namespaces ); return SUCCESS; } - private String collapseNamespaces( String repoId, String n ) + private String collapseNamespaces( Collection repoIds, String n ) { - Collection subNamespaces = metadataResolver.getNamespaces( repoId, n ); + Set subNamespaces = new LinkedHashSet(); + for ( String repoId : repoIds ) + { + subNamespaces.addAll( metadataResolver.getNamespaces( repoId, n ) ); + } if ( subNamespaces.size() != 1 ) { if ( log.isDebugEnabled() ) @@ -99,19 +106,19 @@ public class BrowseAction } else { - Collection projects = metadataResolver.getProjects( repoId, n ); - if ( projects != null && !projects.isEmpty() ) + for ( String repoId : repoIds ) { - if ( log.isDebugEnabled() ) + Collection projects = metadataResolver.getProjects( repoId, n ); + if ( projects != null && !projects.isEmpty() ) { - log.debug( n + " is not collapsible as it has projects" ); + if ( log.isDebugEnabled() ) + { + log.debug( n + " is not collapsible as it has projects" ); + } + return n; } - return n; - } - else - { - return collapseNamespaces( repoId, n + "." + subNamespaces.iterator().next() ); } + return collapseNamespaces( repoIds, n + "." + subNamespaces.iterator().next() ); } } @@ -130,23 +137,26 @@ public class BrowseAction return GlobalResults.ACCESS_TO_NO_REPOS; } - Set namespaces = new LinkedHashSet(); Set projects = new LinkedHashSet(); + + Set namespacesToCollapse = new LinkedHashSet(); for ( String repoId : selectedRepos ) { - Collection childNamespaces = metadataResolver.getNamespaces( repoId, groupId ); - // TODO: this logic should be optional, particularly remembering we want to keep this code simple - // it is located here to avoid the content repository implementation needing to do too much for what - // is essentially presentation code - for ( String n : childNamespaces ) - { - // TODO: check performance of this - namespaces.add( collapseNamespaces( repoId, groupId + "." + n ) ); - } + namespacesToCollapse.addAll( metadataResolver.getNamespaces( repoId, groupId ) ); projects.addAll( metadataResolver.getProjects( repoId, groupId ) ); } + // TODO: this logic should be optional, particularly remembering we want to keep this code simple + // it is located here to avoid the content repository implementation needing to do too much for what + // is essentially presentation code + Set namespaces = new LinkedHashSet(); + for ( String n : namespacesToCollapse ) + { + // TODO: check performance of this + namespaces.add( collapseNamespaces( selectedRepos, groupId + "." + n ) ); + } + this.namespaces = getSortedList( namespaces ); this.projectIds = getSortedList( projects ); return SUCCESS; diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataResolver.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataResolver.java index 71bd1fa98..052ad0fc2 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataResolver.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataResolver.java @@ -43,7 +43,7 @@ public class TestMetadataResolver private Map> references = new HashMap>(); - private List namespaces; + private Map> namespaces = new HashMap>(); private Map> projectsInNamespace = new HashMap>(); @@ -77,14 +77,14 @@ public class TestMetadataResolver public Collection getRootNamespaces( String repoId ) { - return getNamespaces( null ); + return getNamespaces( repoId, null ); } - private Collection getNamespaces( String baseNamespace ) + public Collection getNamespaces( String repoId, String baseNamespace ) { Set namespaces = new LinkedHashSet(); int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0; - for ( String namespace : this.namespaces ) + for ( String namespace : this.namespaces.get( repoId ) ) { if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) ) { @@ -102,11 +102,6 @@ public class TestMetadataResolver return namespaces; } - public Collection getNamespaces( String repoId, String namespace ) - { - return getNamespaces( namespace ); - } - public Collection getProjects( String repoId, String namespace ) { Collection list = projectsInNamespace.get( namespace ); @@ -167,8 +162,8 @@ public class TestMetadataResolver this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references ); } - public void setNamespaces( List namespaces ) + public void setNamespaces( String repoId, List namespaces ) { - this.namespaces = namespaces; + this.namespaces.put( repoId, namespaces ); } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/BrowseActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/BrowseActionTest.java index a2cdb6fd0..c80cb3e3e 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/BrowseActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/BrowseActionTest.java @@ -38,6 +38,8 @@ public class BrowseActionTest Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle", "repeat.repeat" ); + private static final String OTHER_TEST_REPO = "other-repo"; + public void testInstantiation() { assertFalse( action == lookup( Action.class, ACTION_HINT ) ); @@ -45,7 +47,7 @@ public class BrowseActionTest public void testBrowse() { - metadataResolver.setNamespaces( GROUPS ); + metadataResolver.setNamespaces( TEST_REPO, GROUPS ); String result = action.browse(); assertSuccessResult( result ); @@ -120,7 +122,7 @@ public class BrowseActionTest String selectedGroupId = "org"; List groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" ); - metadataResolver.setNamespaces( groups ); + metadataResolver.setNamespaces( TEST_REPO, groups ); action.setGroupId( selectedGroupId ); String result = action.browseGroup(); assertSuccessResult( result ); @@ -141,7 +143,7 @@ public class BrowseActionTest String selectedGroupId = "org.apache"; List groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" ); - metadataResolver.setNamespaces( groups ); + metadataResolver.setNamespaces( TEST_REPO, groups ); metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() ); action.setGroupId( selectedGroupId ); String result = action.browseGroup(); @@ -161,7 +163,7 @@ public class BrowseActionTest { List groups = Arrays.asList( "org.apache.archiva", "org.apache" ); - metadataResolver.setNamespaces( groups ); + metadataResolver.setNamespaces( TEST_REPO, groups ); // add an artifact in the tree to make sure "single" is not collapsed metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() ); @@ -178,13 +180,36 @@ public class BrowseActionTest assertNull( action.getSharedModel() ); } + public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories() + { + setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) ); + + metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) ); + metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) ); + + // add an artifact in the tree to make sure "single" is not collapsed + metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() ); + + String result = action.browse(); + assertSuccessResult( result ); + + assertEquals( Collections.singletonList( "org" ), action.getNamespaces() ); + assertNull( action.getProjectIds() ); + assertNull( action.getProjectVersions() ); + + assertNull( action.getGroupId() ); + assertNull( action.getArtifactId() ); + assertNull( action.getRepositoryId() ); + assertNull( action.getSharedModel() ); + } + public void testBrowseGroupWithCollapsedGroupsAndArtifacts() { String artifacts = "apache"; String selectedGroupId = "org.apache"; List groups = Arrays.asList( "org.apache.archiva", "org.apache" ); - metadataResolver.setNamespaces( groups ); + metadataResolver.setNamespaces( TEST_REPO, groups ); // add an artifact in the tree to make sure "single" is not collapsed metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() ); -- 2.39.5