Browse Source

restructure querying to make construction more intuitive

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@367157 13f79535-47bb-0310-9956-ffa450edef68
tags/archiva-0.9-alpha-1
Brett Porter 18 years ago
parent
commit
d9fa724185

+ 33
- 128
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java View File

@@ -24,10 +24,9 @@ import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.maven.repository.indexing.query.AbstractCompoundQuery;
import org.apache.maven.repository.indexing.query.OptionalQuery;
import org.apache.maven.repository.indexing.query.CompoundQuery;
import org.apache.maven.repository.indexing.query.CompoundQueryTerm;
import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.RequiredQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;

import java.io.IOException;
@@ -43,18 +42,12 @@ public abstract class AbstractRepositoryIndexSearcher
{
protected RepositoryIndex index;

private BooleanQuery bQry;

private BooleanQuery mainQry;

private boolean isRequired = true;

/**
* Constructor
*
* @param index the index object
*/
public AbstractRepositoryIndexSearcher( RepositoryIndex index )
protected AbstractRepositoryIndexSearcher( RepositoryIndex index )
{
this.index = index;
}
@@ -70,9 +63,7 @@ public abstract class AbstractRepositoryIndexSearcher
public List search( Query query )
throws RepositoryIndexSearchException
{
List artifactList = null;
IndexSearcher searcher = null;
Hits hits = null;
IndexSearcher searcher;

try
{
@@ -83,43 +74,21 @@ public abstract class AbstractRepositoryIndexSearcher
throw new RepositoryIndexSearchException( e.getMessage(), e );
}

if ( query instanceof SinglePhraseQuery )
Hits hits;
try
{
SinglePhraseQuery singleQry = (SinglePhraseQuery) query;
createSubQuery();
try
{
addQuery( singleQry.getField(), singleQry.getValue(), true, false );
hits = searcher.search( bQry );
}
catch ( IOException ie )
{
throw new RepositoryIndexSearchException( ie.getMessage(), ie );
}
catch ( ParseException pe )
{
throw new RepositoryIndexSearchException( pe.getMessage(), pe );
}

hits = searcher.search( createLuceneQuery( query ) );
}
else if ( query instanceof RequiredQuery || query instanceof OptionalQuery )
catch ( IOException e )
{
createMainQuery();
try
{
buildCompoundQuery( query );
hits = searcher.search( mainQry );
}
catch ( IOException ie )
{
throw new RepositoryIndexSearchException( ie.getMessage(), ie );
}
catch ( ParseException pe )
{
throw new RepositoryIndexSearchException( pe.getMessage(), pe );
}
throw new RepositoryIndexSearchException( e.getMessage(), e );
}
catch ( ParseException e )
{
throw new RepositoryIndexSearchException( e.getMessage(), e );
}

List artifactList;
try
{
artifactList = buildList( hits );
@@ -133,44 +102,7 @@ public abstract class AbstractRepositoryIndexSearcher
return artifactList;
}

/**
* Create a main BooleanQuery object that will contain the other
* BooleanQuery objects.
*/
private void createMainQuery()
{
mainQry = new BooleanQuery();
}

/**
* Add the other BooleanQuery objects to the main BooleanQuery object
*
* @param required specifies if the search is AND or OR
* @param prohibited specifies if NOT will be used in the search
*/
private void addToMainQuery( boolean required, boolean prohibited )
{
mainQry.add( bQry, required, prohibited );
}

/**
* Create a new BooleanQuery object for nested search
*/
private void createSubQuery()
{
bQry = new BooleanQuery();
}

/**
* Add query to the globally declared BooleanQuery object
*
* @param field the name of the field in the index where the value is to be searched
* @param value the value to be searched in the index
* @param required specifies if the search is AND or OR
* @param prohibited specifies if NOT will be used in the search
* @throws ParseException
*/
private void addQuery( String field, String value, boolean required, boolean prohibited )
private org.apache.lucene.search.Query createLuceneQuery( String field, String value )
throws ParseException
{
org.apache.lucene.search.Query qry;
@@ -184,62 +116,35 @@ public abstract class AbstractRepositoryIndexSearcher
QueryParser parser = new QueryParser( field, index.getAnalyzer() );
qry = parser.parse( value );
}
bQry.add( qry, required, prohibited );
return qry;
}

/**
* Build or construct the query that will be used by the searcher
*
* @param query the query object that contains the search criteria
* @throws ParseException
*/
private void buildCompoundQuery( Query query )
private org.apache.lucene.search.Query createLuceneQuery( Query query )
throws ParseException
{
AbstractCompoundQuery cQry = null;
boolean required = false;

if ( query instanceof RequiredQuery )
{
cQry = (RequiredQuery) query;
required = true;
}
else
{
cQry = (OptionalQuery) query;
required = false;
}
org.apache.lucene.search.Query retVal;

boolean reset = true;

// get the query list and iterate through each
List queries = cQry.getQueryList();
for ( Iterator iter = queries.iterator(); iter.hasNext(); )
if ( query instanceof CompoundQuery )
{
Query query2 = (Query) iter.next();

if ( query2 instanceof SinglePhraseQuery )
BooleanQuery booleanQuery = new BooleanQuery();
CompoundQuery compoundQuery = (CompoundQuery) query;
List queries = compoundQuery.getQueries();
for ( Iterator i = queries.iterator(); i.hasNext(); )
{
SinglePhraseQuery sQry = (SinglePhraseQuery) query2;
if ( reset )
{
createSubQuery();
}
addQuery( sQry.getField(), sQry.getValue(), required, false );
reset = false;
CompoundQueryTerm subquery = (CompoundQueryTerm) i.next();

if ( !iter.hasNext() )
{
addToMainQuery( isRequired, false );
}
org.apache.lucene.search.Query luceneQuery = createLuceneQuery( subquery.getQuery() );

booleanQuery.add( luceneQuery, subquery.isRequired(), subquery.isProhibited() );
}
else if ( query2 instanceof RequiredQuery || query2 instanceof OptionalQuery )
{
isRequired = required;
buildCompoundQuery( query2 );
}
retVal = booleanQuery;
}
else
{
SinglePhraseQuery singlePhraseQuery = (SinglePhraseQuery) query;
retVal = createLuceneQuery( singlePhraseQuery.getField(), singlePhraseQuery.getValue() );
}
return retVal;
}

/**

+ 0
- 8
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndexSearcher.java View File

@@ -25,14 +25,6 @@ import org.apache.maven.artifact.factory.ArtifactFactory;
public class PomRepositoryIndexSearcher
extends AbstractRepositoryIndexSearcher
{
private static final String GROUPID = "groupId";

private static final String ARTIFACTID = "artifactId";

private static final String VERSION = "version";

private static final String PACKAGING = "packaging";

private ArtifactFactory factory;

public PomRepositoryIndexSearcher( RepositoryIndex index, ArtifactFactory artifactFactory )

+ 51
- 0
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/AbstractCompoundQueryTerm.java View File

@@ -0,0 +1,51 @@
package org.apache.maven.repository.indexing.query;

/*
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed 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.
*/

/**
* Base of all query terms.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public abstract class AbstractCompoundQueryTerm
implements CompoundQueryTerm
{
/**
* The query being added.
*/
private Query query;

protected AbstractCompoundQueryTerm( Query query )
{
this.query = query;
}

public boolean isRequired()
{
return false;
}

public boolean isProhibited()
{
return false;
}

public Query getQuery()
{
return query;
}
}

+ 36
- 0
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/AndQueryTerm.java View File

@@ -0,0 +1,36 @@
package org.apache.maven.repository.indexing.query;

/*
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed 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.
*/

/**
* A Boolean AND join for queries.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class AndQueryTerm
extends AbstractCompoundQueryTerm
{
public AndQueryTerm( Query query )
{
super( query );
}

public boolean isRequired()
{
return true;
}
}

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/AbstractCompoundQuery.java → maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQuery.java View File

@@ -1,14 +1,13 @@
package org.apache.maven.repository.indexing.query;

/*
* Copyright 2001-2005 The Apache Software Foundation.
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed 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,
@@ -23,22 +22,32 @@ import java.util.List;
/**
* @author Edwin Punzalan
*/
public abstract class AbstractCompoundQuery
public class CompoundQuery
implements Query
{
protected List queries;

public AbstractCompoundQuery()
public CompoundQuery()
{
queries = new ArrayList();
}

public void add( Query query )
public void and( Query query )
{
queries.add( query );
queries.add( new AndQueryTerm( query ) );
}

public List getQueryList()
public void or( Query query )
{
queries.add( new OrQueryTerm( query ) );
}

public void not( Query query )
{
queries.add( new NotQueryTerm( query ) );
}

public List getQueries()
{
return queries;
}

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/OptionalQuery.java → maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQueryTerm.java View File

@@ -1,14 +1,13 @@
package org.apache.maven.repository.indexing.query;

/*
* Copyright 2001-2005 The Apache Software Foundation.
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed 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,
@@ -18,9 +17,15 @@ package org.apache.maven.repository.indexing.query;
*/

/**
* @author Edwin Punzalan
* Term in a compound query.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class OptionalQuery
extends AbstractCompoundQuery
public interface CompoundQueryTerm
{
boolean isRequired();

boolean isProhibited();

Query getQuery();
}

+ 36
- 0
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/NotQueryTerm.java View File

@@ -0,0 +1,36 @@
package org.apache.maven.repository.indexing.query;

/*
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed 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.
*/

/**
* A boolean NOT query term.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class NotQueryTerm
extends AbstractCompoundQueryTerm
{
public NotQueryTerm( Query query )
{
super( query );
}

public boolean isProhibited()
{
return true;
}
}

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RequiredQuery.java → maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/OrQueryTerm.java View File

@@ -1,14 +1,13 @@
package org.apache.maven.repository.indexing.query;

/*
* Copyright 2001-2005 The Apache Software Foundation.
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed 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,
@@ -18,9 +17,15 @@ package org.apache.maven.repository.indexing.query;
*/

/**
* @author Edwin Punzalan
* A boolean OR join term.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class RequiredQuery
extends AbstractCompoundQuery
public class OrQueryTerm
extends AbstractCompoundQueryTerm
{
public OrQueryTerm( Query query )
{
super( query );
}
}

+ 27
- 28
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java View File

@@ -23,9 +23,8 @@ import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.repository.digest.DefaultDigester;
import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.indexing.query.OptionalQuery;
import org.apache.maven.repository.indexing.query.CompoundQuery;
import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.RequiredQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
@@ -314,9 +313,9 @@ public class ArtifactRepositoryIndexingTest
// ex. artifactId=maven-artifact AND groupId=org.apache.maven
Query qry1 = new SinglePhraseQuery( ARTIFACTID, "maven-artifact" );
Query qry2 = new SinglePhraseQuery( GROUPID, "org.apache.maven" );
RequiredQuery rQry = new RequiredQuery();
rQry.add( qry1 );
rQry.add( qry2 );
CompoundQuery rQry = new CompoundQuery();
rQry.and( qry1 );
rQry.and( qry2 );

List artifacts = repoSearcher.search( rQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -330,9 +329,9 @@ public class ArtifactRepositoryIndexingTest
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
// version=2.0.3
Query qry3 = new SinglePhraseQuery( VERSION, "2.0.3" );
OptionalQuery oQry = new OptionalQuery();
oQry.add( rQry );
oQry.add( qry3 );
CompoundQuery oQry = new CompoundQuery();
oQry.or( rQry );
oQry.or( qry3 );

artifacts = repoSearcher.search( oQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -347,20 +346,20 @@ public class ArtifactRepositoryIndexingTest
// (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
Query qry4 = new SinglePhraseQuery( VERSION, "2.0.1" );
oQry = new OptionalQuery();
oQry.add( qry3 );
oQry.add( qry4 );
oQry = new CompoundQuery();
oQry.or( qry3 );
oQry.or( qry4 );

OptionalQuery oQry5 = new OptionalQuery();
CompoundQuery oQry5 = new CompoundQuery();
Query qry9 = new SinglePhraseQuery( NAME, "maven-artifact-2.0.1.jar" );
Query qry10 = new SinglePhraseQuery( NAME, "maven-artifact" );
oQry5.add( qry9 );
oQry5.add( qry10 );
oQry5.or( qry9 );
oQry5.or( qry10 );

RequiredQuery rQry2 = new RequiredQuery();
rQry2.add( oQry );
rQry2.add( rQry );
rQry2.add( oQry5 );
CompoundQuery rQry2 = new CompoundQuery();
rQry2.or( oQry );
rQry2.and( rQry );
rQry2.or( oQry5 );

artifacts = repoSearcher.search( rQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -376,14 +375,14 @@ public class ArtifactRepositoryIndexingTest
// (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
// OR [(artifactId=sample AND groupId=test)]
RequiredQuery rQry3 = new RequiredQuery();
CompoundQuery rQry3 = new CompoundQuery();
Query qry5 = new SinglePhraseQuery( ARTIFACTID, "sample" );
Query qry6 = new SinglePhraseQuery( GROUPID, "test" );
rQry3.add( qry5 );
rQry3.add( qry6 );
OptionalQuery oQry2 = new OptionalQuery();
oQry2.add( rQry2 );
oQry2.add( rQry3 );
rQry3.and( qry5 );
rQry3.and( qry6 );
CompoundQuery oQry2 = new CompoundQuery();
oQry2.and( rQry2 );
oQry2.and( rQry3 );

artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -400,12 +399,12 @@ public class ArtifactRepositoryIndexingTest
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
// [(artifactId=sample AND groupId=test)] OR
// [(artifactId=sample2 AND groupId=test)]
RequiredQuery rQry4 = new RequiredQuery();
CompoundQuery rQry4 = new CompoundQuery();
Query qry7 = new SinglePhraseQuery( ARTIFACTID, "sample2" );
Query qry8 = new SinglePhraseQuery( GROUPID, "test" );
rQry4.add( qry7 );
rQry4.add( qry8 );
oQry2.add( rQry4 );
rQry4.and( qry7 );
rQry4.and( qry8 );
oQry2.and( rQry4 );

artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )

+ 35
- 37
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java View File

@@ -29,9 +29,8 @@ import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.repository.digest.DefaultDigester;
import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.indexing.query.OptionalQuery;
import org.apache.maven.repository.indexing.query.CompoundQuery;
import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.RequiredQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
@@ -74,7 +73,6 @@ public class PomRepositoryIndexingTest
public void testIndexerExceptions()
throws Exception
{
PomRepositoryIndex indexer;
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );

try
@@ -101,7 +99,7 @@ public class PomRepositoryIndexingTest

Model pom = getPom( "test", "test-artifactId", "1.0" );

indexer = factory.createPomRepositoryIndex( indexPath, repository );
PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
indexer.close();

try
@@ -228,8 +226,8 @@ public class PomRepositoryIndexingTest
while ( dependencies.hasNext() )
{
Dependency dep = (Dependency) dependencies.next();
if ( ( dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion() ).equals(
"org.codehaus.plexus:plexus-utils:1.0.5" ) )
if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals(
dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion() ) )
{
depFound = true;
break;
@@ -251,8 +249,8 @@ public class PomRepositoryIndexingTest
while ( plugins.hasNext() )
{
Plugin plugin = (Plugin) plugins.next();
if ( ( plugin.getKey() + ":" + plugin.getVersion() ).equals(
"org.codehaus.modello:modello-maven-plugin:2.0" ) )
if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals(
plugin.getKey() + ":" + plugin.getVersion() ) )
{
found = true;
break;
@@ -274,8 +272,8 @@ public class PomRepositoryIndexingTest
while ( plugins.hasNext() )
{
ReportPlugin plugin = (ReportPlugin) plugins.next();
if ( ( plugin.getKey() + ":" + plugin.getVersion() ).equals(
"org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ) )
if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals(
plugin.getKey() + ":" + plugin.getVersion() ) )
{
found = true;
break;
@@ -295,7 +293,7 @@ public class PomRepositoryIndexingTest
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
Artifact artifact2 = (Artifact) artifacts.next();
String sha1Tmp = digester.createChecksum( getPomFile( artifact ), Digester.SHA1 );
String sha1Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.SHA1 );
assertEquals( sha1, sha1Tmp );
}

@@ -332,9 +330,9 @@ public class PomRepositoryIndexingTest
// ex. artifactId=maven-artifact AND groupId=org.apache.maven
Query qry1 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
Query qry2 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
RequiredQuery rQry = new RequiredQuery();
rQry.add( qry1 );
rQry.add( qry2 );
CompoundQuery rQry = new CompoundQuery();
rQry.and( qry1 );
rQry.and( qry2 );

List artifacts = repoSearcher.search( rQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -348,9 +346,9 @@ public class PomRepositoryIndexingTest
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
// version=2.0.3
Query qry3 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.3" );
OptionalQuery oQry = new OptionalQuery();
oQry.add( rQry );
oQry.add( qry3 );
CompoundQuery oQry = new CompoundQuery();
oQry.and( rQry );
oQry.or( qry3 );

artifacts = repoSearcher.search( oQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -365,22 +363,22 @@ public class PomRepositoryIndexingTest
// (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
Query qry4 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.1" );
oQry = new OptionalQuery();
oQry.add( qry3 );
oQry.add( qry4 );
oQry = new CompoundQuery();
oQry.or( qry3 );
oQry.or( qry4 );

OptionalQuery oQry5 = new OptionalQuery();
CompoundQuery oQry5 = new CompoundQuery();
Query qry9 =
new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
Query qry10 = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES,
"org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" );
oQry5.add( qry9 );
oQry5.add( qry10 );
oQry5.or( qry9 );
oQry5.or( qry10 );

RequiredQuery rQry2 = new RequiredQuery();
rQry2.add( oQry );
rQry2.add( rQry );
rQry2.add( oQry5 );
CompoundQuery rQry2 = new CompoundQuery();
rQry2.or( oQry );
rQry2.and( rQry );
rQry2.and( oQry5 );

artifacts = repoSearcher.search( rQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -396,14 +394,14 @@ public class PomRepositoryIndexingTest
// (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
// OR [(artifactId=sample AND groupId=test)]
RequiredQuery rQry3 = new RequiredQuery();
CompoundQuery rQry3 = new CompoundQuery();
Query qry5 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample" );
Query qry6 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" );
rQry3.add( qry5 );
rQry3.add( qry6 );
OptionalQuery oQry2 = new OptionalQuery();
oQry2.add( rQry2 );
oQry2.add( rQry3 );
rQry3.and( qry5 );
rQry3.and( qry6 );
CompoundQuery oQry2 = new CompoundQuery();
oQry2.and( rQry2 );
oQry2.and( rQry3 );

artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@@ -420,12 +418,12 @@ public class PomRepositoryIndexingTest
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
// [(artifactId=sample AND groupId=test)] OR
// [(artifactId=sample2 AND groupId=test)]
RequiredQuery rQry4 = new RequiredQuery();
CompoundQuery rQry4 = new CompoundQuery();
Query qry7 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample2" );
Query qry8 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" );
rQry4.add( qry7 );
rQry4.add( qry8 );
oQry2.add( rQry4 );
rQry4.and( qry7 );
rQry4.and( qry8 );
oQry2.and( rQry4 );

artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )

+ 35
- 43
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/query/QueryTest.java View File

@@ -28,57 +28,49 @@ public class QueryTest
public void testSinglePhraseQueryObject()
{
SinglePhraseQuery query = new SinglePhraseQuery( "Field", "Value" );
assertTrue( query instanceof Query );
assertEquals( "Field", query.getField() );
assertEquals( "Value", query.getValue() );
}

public void testCompoundQueries()
{
RequiredQuery rQuery = new RequiredQuery();
assertTrue( rQuery instanceof Query );
rQuery.add( new SinglePhraseQuery( "r1Field", "r1Value" ) );
rQuery.add( new SinglePhraseQuery( "r2Field", "r2Value" ) );
CompoundQuery rQuery = new CompoundQuery();
rQuery.and( new SinglePhraseQuery( "r1Field", "r1Value" ) );
rQuery.and( new SinglePhraseQuery( "r2Field", "r2Value" ) );

OptionalQuery oQuery = new OptionalQuery();
oQuery.add( new SinglePhraseQuery( "oField", "oValue" ) );
CompoundQuery oQuery = new CompoundQuery();
oQuery.or( new SinglePhraseQuery( "oField", "oValue" ) );

RequiredQuery all = new RequiredQuery();
all.add( rQuery );
all.add( oQuery );
assertEquals( 2, all.getQueryList().size() );
CompoundQuery all = new CompoundQuery();
all.and( rQuery );
all.or( oQuery );
assertEquals( 2, all.getQueries().size() );

CompoundQueryTerm queryTerm = (CompoundQueryTerm) all.getQueries().get( 0 );
assertTrue( queryTerm.getQuery() instanceof CompoundQuery );
rQuery = (CompoundQuery) queryTerm.getQuery();
assertEquals( 2, rQuery.getQueries().size() );
queryTerm = (CompoundQueryTerm) rQuery.getQueries().get( 0 );
assertTrue( queryTerm.getQuery() instanceof SinglePhraseQuery );
SinglePhraseQuery sQuery = (SinglePhraseQuery) queryTerm.getQuery();
assertEquals( "r1Field", sQuery.getField() );
assertEquals( "r1Value", sQuery.getValue() );
queryTerm = (CompoundQueryTerm) rQuery.getQueries().get( 1 );
assertTrue( queryTerm.getQuery() instanceof SinglePhraseQuery );
sQuery = (SinglePhraseQuery) queryTerm.getQuery();
assertEquals( "r2Field", sQuery.getField() );
assertEquals( "r2Value", sQuery.getValue() );

queryTerm = (CompoundQueryTerm) all.getQueries().get( 1 );
assertTrue( queryTerm.getQuery() instanceof CompoundQuery );
rQuery = (CompoundQuery) queryTerm.getQuery();
assertEquals( 1, rQuery.getQueries().size() );
queryTerm = (CompoundQueryTerm) rQuery.getQueries().get( 0 );
assertTrue( queryTerm.getQuery() instanceof SinglePhraseQuery );
sQuery = (SinglePhraseQuery) queryTerm.getQuery();
assertEquals( "oField", sQuery.getField() );
assertEquals( "oValue", sQuery.getValue() );

for ( int ctr = 0; ctr < all.getQueryList().size(); ctr++ )
{
Query query = (Query) all.getQueryList().get( ctr );
switch ( ctr )
{
case 0:
assertTrue( query instanceof RequiredQuery );
rQuery = (RequiredQuery) query;
assertEquals( 2, rQuery.getQueryList().size() );
query = (Query) rQuery.getQueryList().get( 0 );
assertTrue( query instanceof SinglePhraseQuery );
SinglePhraseQuery sQuery = (SinglePhraseQuery) query;
assertEquals( "r1Field", sQuery.getField() );
assertEquals( "r1Value", sQuery.getValue() );
query = (Query) rQuery.getQueryList().get( 1 );
assertTrue( query instanceof SinglePhraseQuery );
sQuery = (SinglePhraseQuery) query;
assertEquals( "r2Field", sQuery.getField() );
assertEquals( "r2Value", sQuery.getValue() );
break;
case 1:
assertTrue( query instanceof OptionalQuery );
oQuery = (OptionalQuery) query;
assertEquals( 1, oQuery.getQueryList().size() );
query = (Query) oQuery.getQueryList().get( 0 );
assertTrue( query instanceof SinglePhraseQuery );
sQuery = (SinglePhraseQuery) query;
assertEquals( "oField", sQuery.getField() );
assertEquals( "oValue", sQuery.getValue() );
break;
}
}
}
}


Loading…
Cancel
Save