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

import org.apache.lucene.search.Hits; import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery; 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.Query;
import org.apache.maven.repository.indexing.query.RequiredQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery; import org.apache.maven.repository.indexing.query.SinglePhraseQuery;


import java.io.IOException; import java.io.IOException;
{ {
protected RepositoryIndex index; protected RepositoryIndex index;


private BooleanQuery bQry;

private BooleanQuery mainQry;

private boolean isRequired = true;

/** /**
* Constructor * Constructor
* *
* @param index the index object * @param index the index object
*/ */
public AbstractRepositoryIndexSearcher( RepositoryIndex index )
protected AbstractRepositoryIndexSearcher( RepositoryIndex index )
{ {
this.index = index; this.index = index;
} }
public List search( Query query ) public List search( Query query )
throws RepositoryIndexSearchException throws RepositoryIndexSearchException
{ {
List artifactList = null;
IndexSearcher searcher = null;
Hits hits = null;
IndexSearcher searcher;


try try
{ {
throw new RepositoryIndexSearchException( e.getMessage(), e ); 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 try
{ {
artifactList = buildList( hits ); artifactList = buildList( hits );
return artifactList; 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 throws ParseException
{ {
org.apache.lucene.search.Query qry; org.apache.lucene.search.Query qry;
QueryParser parser = new QueryParser( field, index.getAnalyzer() ); QueryParser parser = new QueryParser( field, index.getAnalyzer() );
qry = parser.parse( value ); 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 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

public class PomRepositoryIndexSearcher public class PomRepositoryIndexSearcher
extends AbstractRepositoryIndexSearcher 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; private ArtifactFactory factory;


public PomRepositoryIndexSearcher( RepositoryIndex index, ArtifactFactory artifactFactory ) public PomRepositoryIndexSearcher( RepositoryIndex index, ArtifactFactory artifactFactory )

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

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

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

package org.apache.maven.repository.indexing.query; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
/** /**
* @author Edwin Punzalan * @author Edwin Punzalan
*/ */
public abstract class AbstractCompoundQuery
public class CompoundQuery
implements Query implements Query
{ {
protected List queries; protected List queries;


public AbstractCompoundQuery()
public CompoundQuery()
{ {
queries = new ArrayList(); 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; 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

package org.apache.maven.repository.indexing.query; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
*/ */


/** /**
* @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

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

package org.apache.maven.repository.indexing.query; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
*/ */


/** /**
* @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

import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.repository.digest.DefaultDigester; import org.apache.maven.repository.digest.DefaultDigester;
import org.apache.maven.repository.digest.Digester; 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.Query;
import org.apache.maven.repository.indexing.query.RequiredQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery; import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.PlexusTestCase; import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
// ex. artifactId=maven-artifact AND groupId=org.apache.maven // ex. artifactId=maven-artifact AND groupId=org.apache.maven
Query qry1 = new SinglePhraseQuery( ARTIFACTID, "maven-artifact" ); Query qry1 = new SinglePhraseQuery( ARTIFACTID, "maven-artifact" );
Query qry2 = new SinglePhraseQuery( GROUPID, "org.apache.maven" ); 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 ); List artifacts = repoSearcher.search( rQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
// version=2.0.3 // version=2.0.3
Query qry3 = new SinglePhraseQuery( 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 ); artifacts = repoSearcher.search( oQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// (version=2.0.3 OR version=2.0.1) // (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact) // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
Query qry4 = new SinglePhraseQuery( VERSION, "2.0.1" ); 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 qry9 = new SinglePhraseQuery( NAME, "maven-artifact-2.0.1.jar" );
Query qry10 = new SinglePhraseQuery( NAME, "maven-artifact" ); 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 ); artifacts = repoSearcher.search( rQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// (version=2.0.3 OR version=2.0.1) // (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
// OR [(artifactId=sample AND groupId=test)] // OR [(artifactId=sample AND groupId=test)]
RequiredQuery rQry3 = new RequiredQuery();
CompoundQuery rQry3 = new CompoundQuery();
Query qry5 = new SinglePhraseQuery( ARTIFACTID, "sample" ); Query qry5 = new SinglePhraseQuery( ARTIFACTID, "sample" );
Query qry6 = new SinglePhraseQuery( GROUPID, "test" ); 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 ); artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
// [(artifactId=sample AND groupId=test)] OR // [(artifactId=sample AND groupId=test)] OR
// [(artifactId=sample2 AND groupId=test)] // [(artifactId=sample2 AND groupId=test)]
RequiredQuery rQry4 = new RequiredQuery();
CompoundQuery rQry4 = new CompoundQuery();
Query qry7 = new SinglePhraseQuery( ARTIFACTID, "sample2" ); Query qry7 = new SinglePhraseQuery( ARTIFACTID, "sample2" );
Query qry8 = new SinglePhraseQuery( GROUPID, "test" ); 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 ); artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )

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

import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.repository.digest.DefaultDigester; import org.apache.maven.repository.digest.DefaultDigester;
import org.apache.maven.repository.digest.Digester; 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.Query;
import org.apache.maven.repository.indexing.query.RequiredQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery; import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.PlexusTestCase; import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
public void testIndexerExceptions() public void testIndexerExceptions()
throws Exception throws Exception
{ {
PomRepositoryIndex indexer;
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );


try try


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


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


try try
while ( dependencies.hasNext() ) while ( dependencies.hasNext() )
{ {
Dependency dep = (Dependency) dependencies.next(); 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; depFound = true;
break; break;
while ( plugins.hasNext() ) while ( plugins.hasNext() )
{ {
Plugin plugin = (Plugin) plugins.next(); 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; found = true;
break; break;
while ( plugins.hasNext() ) while ( plugins.hasNext() )
{ {
ReportPlugin plugin = (ReportPlugin) plugins.next(); 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; found = true;
break; break;
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{ {
Artifact artifact2 = (Artifact) artifacts.next(); Artifact artifact2 = (Artifact) artifacts.next();
String sha1Tmp = digester.createChecksum( getPomFile( artifact ), Digester.SHA1 );
String sha1Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.SHA1 );
assertEquals( sha1, sha1Tmp ); assertEquals( sha1, sha1Tmp );
} }


// ex. artifactId=maven-artifact AND groupId=org.apache.maven // ex. artifactId=maven-artifact AND groupId=org.apache.maven
Query qry1 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); Query qry1 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
Query qry2 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" ); 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 ); List artifacts = repoSearcher.search( rQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
// version=2.0.3 // version=2.0.3
Query qry3 = new SinglePhraseQuery( PomRepositoryIndex.FLD_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 ); artifacts = repoSearcher.search( oQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// (version=2.0.3 OR version=2.0.1) // (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact) // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
Query qry4 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.1" ); 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 = Query qry9 =
new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
Query qry10 = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, Query qry10 = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES,
"org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" ); "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 ); artifacts = repoSearcher.search( rQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// (version=2.0.3 OR version=2.0.1) // (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
// OR [(artifactId=sample AND groupId=test)] // OR [(artifactId=sample AND groupId=test)]
RequiredQuery rQry3 = new RequiredQuery();
CompoundQuery rQry3 = new CompoundQuery();
Query qry5 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample" ); Query qry5 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample" );
Query qry6 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" ); 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 ); artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
// [(artifactId=sample AND groupId=test)] OR // [(artifactId=sample AND groupId=test)] OR
// [(artifactId=sample2 AND groupId=test)] // [(artifactId=sample2 AND groupId=test)]
RequiredQuery rQry4 = new RequiredQuery();
CompoundQuery rQry4 = new CompoundQuery();
Query qry7 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample2" ); Query qry7 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample2" );
Query qry8 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" ); 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 ); artifacts = repoSearcher.search( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) 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

public void testSinglePhraseQueryObject() public void testSinglePhraseQueryObject()
{ {
SinglePhraseQuery query = new SinglePhraseQuery( "Field", "Value" ); SinglePhraseQuery query = new SinglePhraseQuery( "Field", "Value" );
assertTrue( query instanceof Query );
assertEquals( "Field", query.getField() ); assertEquals( "Field", query.getField() );
assertEquals( "Value", query.getValue() ); assertEquals( "Value", query.getValue() );
} }


public void testCompoundQueries() 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