[MRM-127] add index record classes

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@425379 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2006-07-25 12:02:08 +00:00
parent ef773988c8
commit c2939d7bd4
14 changed files with 1007 additions and 4 deletions

View File

@ -51,14 +51,30 @@ import java.util.zip.ZipOutputStream;
public class EclipseRepositoryIndex
extends AbstractRepositoryIndex
{
// TODO: change constants to an enumerated type
/**
* Field name for the JAR filename.
*/
private static final String JAR_NAME = "j";
/**
* Field name for the JAR file size.
*/
private static final String JAR_SIZE = "s";
/**
* Field name for the JAR last modified timestamp.
*/
private static final String JAR_DATE = "d";
/**
* Field name for the list of classes in the JAR.
*/
private static final String NAMES = "c";
/**
* Field name for the MD5 checksum of the JAR.
*/
private static final String MD5 = "m";
private Digester digester;

View File

@ -0,0 +1,80 @@
package org.apache.maven.repository.indexing.record;
/*
* 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.
*/
import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Base class for the index record factories.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public abstract class AbstractArtifactIndexRecordFactory
extends AbstractLogEnabled
implements RepositoryIndexRecordFactory
{
/**
* @plexus.requirement
*/
private Digester digester;
protected String readChecksum( File file, String algorithm )
{
String checksum;
try
{
checksum = digester.createChecksum( file, algorithm ).toLowerCase();
}
catch ( DigesterException e )
{
getLogger().error( "Error getting checksum for artifact file, leaving empty in index: " + e.getMessage() );
checksum = null;
}
return checksum;
}
protected List readFilesInArchive( File file )
throws IOException
{
ZipFile zipFile = new ZipFile( file );
List files = new ArrayList( zipFile.size() );
for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry) entries.nextElement();
files.add( entry.getName() );
}
return files;
}
protected static boolean isClass( String name )
{
// TODO: verify if class is public or protected (this might require the original ZipEntry)
return name.endsWith( ".class" ) && name.lastIndexOf( "$" ) < 0;
}
}

View File

@ -0,0 +1,162 @@
package org.apache.maven.repository.indexing.record;
import java.util.Date;
/*
* 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.
*/
/**
* The a record with the fields in the minimal index.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class MinimalArtifactIndexRecord
implements RepositoryIndexRecord
{
/**
* The classes in the archive for the artifact, if it is a JAR. The package name is <b>not</b> included.
*/
private String classes;
/**
* The MD5 checksum of the artifact file.
*/
private String md5Checksum;
/**
* The filename of the artifact file (no path).
*/
private String filename;
/**
* The timestamp that the artifact file was last modified.
*/
private long lastModified;
/**
* The size of the artifact file in bytes.
*/
private long size;
public void setClasses( String classes )
{
this.classes = classes;
}
public void setMd5Checksum( String md5Checksum )
{
this.md5Checksum = md5Checksum;
}
public void setFilename( String filename )
{
this.filename = filename;
}
public void setLastModified( long lastModified )
{
this.lastModified = lastModified;
}
public void setSize( long size )
{
this.size = size;
}
public String getClasses()
{
return classes;
}
public String getMd5Checksum()
{
return md5Checksum;
}
public String getFilename()
{
return filename;
}
public long getLastModified()
{
return lastModified;
}
public long getSize()
{
return size;
}
/**
* @noinspection RedundantIfStatement
*/
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
if ( obj == null || getClass() != obj.getClass() )
{
return false;
}
MinimalArtifactIndexRecord that = (MinimalArtifactIndexRecord) obj;
if ( lastModified != that.lastModified )
{
return false;
}
if ( size != that.size )
{
return false;
}
if ( classes != null ? !classes.equals( that.classes ) : that.classes != null )
{
return false;
}
if ( !filename.equals( that.filename ) )
{
return false;
}
if ( md5Checksum != null ? !md5Checksum.equals( that.md5Checksum ) : that.md5Checksum != null )
{
return false;
}
return true;
}
/**
* @noinspection UnnecessaryParentheses
*/
public int hashCode()
{
int result = classes != null ? classes.hashCode() : 0;
result = 31 * result + ( md5Checksum != null ? md5Checksum.hashCode() : 0 );
result = 31 * result + filename.hashCode();
result = 31 * result + (int) ( lastModified ^ ( lastModified >>> 32 ) );
result = 31 * result + (int) ( size ^ ( size >>> 32 ) );
return result;
}
public String toString()
{
return "Filename: " + filename + "; checksum: " + md5Checksum + "; size: " + size + "; lastModified: " +
new Date( lastModified ) + "; classes: " + classes;
}
}

View File

@ -0,0 +1,87 @@
package org.apache.maven.repository.indexing.record;
/*
* 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.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.repository.digest.Digester;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
/**
* An index record type for the minimal index.
*
* @author Edwin Punzalan
* @author Brett Porter
* @plexus.component role="org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory" role-hint="minimal"
*/
public class MinimalArtifactIndexRecordFactory
extends AbstractArtifactIndexRecordFactory
{
public RepositoryIndexRecord createRecord( Artifact artifact )
{
MinimalArtifactIndexRecord record = null;
File file = artifact.getFile();
if ( file != null && "jar".equals( artifact.getType() ) && file.exists() )
{
String md5 = readChecksum( file, Digester.MD5 );
List files = null;
try
{
files = readFilesInArchive( file );
}
catch ( IOException e )
{
getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() );
}
if ( files != null )
{
record = new MinimalArtifactIndexRecord();
record.setMd5Checksum( md5 );
record.setFilename( file.getName() );
record.setLastModified( file.lastModified() );
record.setSize( file.length() );
record.setClasses( getClassesFromFiles( files ) );
}
}
return record;
}
private String getClassesFromFiles( List files )
{
StringBuffer classes = new StringBuffer();
for ( Iterator i = files.iterator(); i.hasNext(); )
{
String name = (String) i.next();
if ( isClass( name ) )
{
int idx = name.lastIndexOf( '/' );
String classname = name.substring( idx + 1, name.length() - 6 );
classes.append( classname ).append( "\n" );
}
}
return classes.toString();
}
}

View File

@ -0,0 +1,26 @@
package org.apache.maven.repository.indexing.record;
/*
* 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 repository index record.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public interface RepositoryIndexRecord
{
}

View File

@ -0,0 +1,40 @@
package org.apache.maven.repository.indexing.record;
import org.apache.maven.artifact.Artifact;
/*
* 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.
*/
/**
* The layout of a record in a repository index.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public interface RepositoryIndexRecordFactory
{
/**
* The Plexus role.
*/
String ROLE = RepositoryIndexRecordFactory.class.getName();
/**
* Create an index record from an artifact.
*
* @param artifact the artifact
* @return the index record
*/
RepositoryIndexRecord createRecord( Artifact artifact );
}

View File

@ -0,0 +1,191 @@
package org.apache.maven.repository.indexing.record;
/*
* 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.
*/
/**
* The a record with the fields in the standard index.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class StandardArtifactIndexRecord
extends MinimalArtifactIndexRecord
{
/**
* The SHA-1 checksum of the artifact file.
*/
private String sha1Checksum;
/**
* The artifact's group.
*/
private String groupId;
/**
* The artifact's identifier within the group.
*/
private String artifactId;
/**
* The artifact's version.
*/
private String version;
/**
* The classifier, if there is one.
*/
private String classifier;
/**
* The artifact type (from the file).
*/
private String type;
/**
* A list of packages (separated by '\n') in the artifact if it contains Java classes.
*/
private String packages;
/**
* A list of files (separated by '\n') in the artifact if it is an archive.
*/
private String files;
/**
* The identifier of the repository that the artifact came from.
*/
private String repository;
public void setSha1Checksum( String sha1Checksum )
{
this.sha1Checksum = sha1Checksum;
}
public void setGroupId( String groupId )
{
this.groupId = groupId;
}
public void setArtifactId( String artifactId )
{
this.artifactId = artifactId;
}
public void setVersion( String version )
{
this.version = version;
}
public void setClassifier( String classifier )
{
this.classifier = classifier;
}
public void setType( String type )
{
this.type = type;
}
public void setPackages( String packages )
{
this.packages = packages;
}
public void setFiles( String files )
{
this.files = files;
}
public void setRepository( String repository )
{
this.repository = repository;
}
/**
* @noinspection RedundantIfStatement
*/
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
if ( obj == null || getClass() != obj.getClass() )
{
return false;
}
if ( !super.equals( obj ) )
{
return false;
}
StandardArtifactIndexRecord that = (StandardArtifactIndexRecord) obj;
if ( !artifactId.equals( that.artifactId ) )
{
return false;
}
if ( classifier != null ? !classifier.equals( that.classifier ) : that.classifier != null )
{
return false;
}
if ( files != null ? !files.equals( that.files ) : that.files != null )
{
return false;
}
if ( !groupId.equals( that.groupId ) )
{
return false;
}
if ( packages != null ? !packages.equals( that.packages ) : that.packages != null )
{
return false;
}
if ( repository != null ? !repository.equals( that.repository ) : that.repository != null )
{
return false;
}
if ( sha1Checksum != null ? !sha1Checksum.equals( that.sha1Checksum ) : that.sha1Checksum != null )
{
return false;
}
if ( type != null ? !type.equals( that.type ) : that.type != null )
{
return false;
}
if ( !version.equals( that.version ) )
{
return false;
}
return true;
}
public int hashCode()
{
int result = super.hashCode();
result = 31 * result + ( sha1Checksum != null ? sha1Checksum.hashCode() : 0 );
result = 31 * result + groupId.hashCode();
result = 31 * result + artifactId.hashCode();
result = 31 * result + version.hashCode();
result = 31 * result + ( classifier != null ? classifier.hashCode() : 0 );
result = 31 * result + ( type != null ? type.hashCode() : 0 );
result = 31 * result + ( packages != null ? packages.hashCode() : 0 );
result = 31 * result + ( files != null ? files.hashCode() : 0 );
result = 31 * result + ( repository != null ? repository.hashCode() : 0 );
return result;
}
}

View File

@ -0,0 +1,143 @@
package org.apache.maven.repository.indexing.record;
/*
* 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.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.repository.digest.Digester;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* An index record type for the standard index.
*
* @author Edwin Punzalan
* @author Brett Porter
* @plexus.component role="org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory" role-hint="standard"
*/
public class StandardArtifactIndexRecordFactory
extends AbstractArtifactIndexRecordFactory
{
/**
* A list of artifact types to treat as a zip archive.
*
* @todo this should be smarter (perhaps use plexus archiver to look for an unarchiver, and make the ones for zip configurable since sar, par, etc can be added at random.
*/
private static final Set ARCHIVE_TYPES =
new HashSet( Arrays.asList( new String[]{"jar", "zip", "ejb", "par", "sar", "war", "ear"} ) );
public RepositoryIndexRecord createRecord( Artifact artifact )
{
StandardArtifactIndexRecord record = null;
File file = artifact.getFile();
if ( file != null && file.exists() )
{
String md5 = readChecksum( file, Digester.MD5 );
String sha1 = readChecksum( file, Digester.SHA1 );
List files = null;
try
{
if ( ARCHIVE_TYPES.contains( artifact.getType() ) )
{
files = readFilesInArchive( file );
}
else
{
files = Collections.EMPTY_LIST;
}
}
catch ( IOException e )
{
getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() );
}
if ( files != null )
{
record = new StandardArtifactIndexRecord();
record.setGroupId( artifact.getGroupId() );
record.setArtifactId( artifact.getArtifactId() );
record.setVersion( artifact.getVersion() );
record.setClassifier( artifact.getClassifier() );
record.setType( artifact.getType() );
record.setMd5Checksum( md5 );
record.setSha1Checksum( sha1 );
record.setFilename( file.getName() );
record.setLastModified( file.lastModified() );
record.setSize( file.length() );
record.setRepository( artifact.getRepository().getId() );
/* TODO! these come from the POM and metadata, so probably part of an update record method instead
record.setPluginPrefix( pluginPrefix );
record.setPackaging( packaging );
indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() );
indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() );
record.setDependencies( dependencies );
record.setLicenses( licenses );
*/
populateArchiveEntries( files, record );
}
}
return record;
}
private void populateArchiveEntries( List files, StandardArtifactIndexRecord record )
{
StringBuffer classes = new StringBuffer();
StringBuffer packages = new StringBuffer();
StringBuffer fileBuffer = new StringBuffer();
for ( Iterator i = files.iterator(); i.hasNext(); )
{
String name = (String) i.next();
// ignore directories
if ( !name.endsWith( "/" ) )
{
fileBuffer.append( name ).append( "\n" );
if ( isClass( name ) )
{
int idx = name.lastIndexOf( '/' );
String classname = name.substring( idx + 1, name.length() - 6 );
classes.append( classname ).append( "\n" );
if ( idx > 0 )
{
String packageName = name.substring( 0, idx ).replace( '/', '.' );
if ( packages.indexOf( packageName ) < 0 )
{
packages.append( packageName ).append( "\n" );
}
}
}
}
}
record.setClasses( classes.toString() );
record.setPackages( packages.toString() );
record.setFiles( fileBuffer.toString() );
}
}

View File

@ -66,10 +66,10 @@ Indexer Design
* Reduced Size Index
An additional index is maintained by the repository manager in the
{{{../apidocs/org/apache/maven/repository/indexing/MinimalIndex.html} MinimalIndex}} class. This indexes all of the
same artifacts as the first index, but stores them with shorter field names and less information to maintain a smaller
size. This index is appropriate for use by certain clients such as IDE integration for fast searching. For a fuller
interface to the repository information, the integration should use the XMLRPC interface.
{{{../apidocs/org/apache/maven/repository/indexing/MinimalArtifactIndexRecord.html} MinimalIndex}} class. This
indexes all of the same artifacts as the first index, but stores them with shorter field names and less information to
maintain a smaller size. This index is appropriate for use by certain clients such as IDE integration for fast
searching. For a fuller interface to the repository information, the integration should use the XMLRPC interface.
The following fields are in the reduced index:

View File

@ -0,0 +1,117 @@
package org.apache.maven.repository.indexing.record;
/*
* 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.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.codehaus.plexus.PlexusTestCase;
import java.io.File;
/**
* Test the minimal artifact index record.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class MinimalArtifactIndexRecordFactoryTest
extends PlexusTestCase
{
private RepositoryIndexRecordFactory factory;
private ArtifactRepository repository;
private ArtifactFactory artifactFactory;
private static final String TEST_GROUP_ID = "org.apache.maven.repository.record";
protected void setUp()
throws Exception
{
super.setUp();
factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
ArtifactRepositoryFactory repositoryFactory =
(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
File file = getTestFile( "src/test/managed-repository" );
repository =
repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
}
public void testIndexedJar()
{
Artifact artifact = createArtifact( "test-jar" );
RepositoryIndexRecord record = factory.createRecord( artifact );
MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
expectedRecord.setFilename( "test-jar-1.0.jar" );
expectedRecord.setLastModified( artifact.getFile().lastModified() );
expectedRecord.setSize( artifact.getFile().length() );
expectedRecord.setClasses( "A\nB\nC\n" );
assertEquals( "check record", expectedRecord, record );
}
public void testCorruptJar()
{
Artifact artifact = createArtifact( "test-corrupt-jar" );
RepositoryIndexRecord record = factory.createRecord( artifact );
assertNull( "Confirm no record is returned", record );
}
public void testNonJar()
{
Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
RepositoryIndexRecord record = factory.createRecord( artifact );
assertNull( "Confirm no record is returned", record );
}
public void testMissingFile()
{
Artifact artifact = createArtifact( "test-foo" );
RepositoryIndexRecord record = factory.createRecord( artifact );
assertNull( "Confirm no record is returned", record );
}
private Artifact createArtifact( String artifactId )
{
return createArtifact( artifactId, "1.0", "jar" );
}
private Artifact createArtifact( String artifactId, String version, String type )
{
Artifact artifact = artifactFactory.createBuildArtifact( TEST_GROUP_ID, artifactId, version, type );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
return artifact;
}
}

View File

@ -0,0 +1,141 @@
package org.apache.maven.repository.indexing.record;
/*
* 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.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.codehaus.plexus.PlexusTestCase;
import java.io.File;
/**
* Test the minimal artifact index record.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class StandardArtifactIndexRecordFactoryTest
extends PlexusTestCase
{
private RepositoryIndexRecordFactory factory;
private ArtifactRepository repository;
private ArtifactFactory artifactFactory;
private static final String TEST_GROUP_ID = "org.apache.maven.repository.record";
protected void setUp()
throws Exception
{
super.setUp();
factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
ArtifactRepositoryFactory repositoryFactory =
(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
File file = getTestFile( "src/test/managed-repository" );
repository =
repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
}
public void testIndexedJar()
{
Artifact artifact = createArtifact( "test-jar" );
RepositoryIndexRecord record = factory.createRecord( artifact );
StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
expectedRecord.setFilename( "test-jar-1.0.jar" );
expectedRecord.setLastModified( artifact.getFile().lastModified() );
expectedRecord.setSize( artifact.getFile().length() );
expectedRecord.setClasses( "A\nB\nC\n" );
expectedRecord.setArtifactId( "test-jar" );
expectedRecord.setGroupId( TEST_GROUP_ID );
expectedRecord.setVersion( "1.0" );
expectedRecord.setFiles( "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class\n" );
expectedRecord.setPackages( "b\nb.c\n" );
expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
expectedRecord.setType( "jar" );
expectedRecord.setRepository( "test" );
assertEquals( "check record", expectedRecord, record );
}
public void testCorruptJar()
{
Artifact artifact = createArtifact( "test-corrupt-jar" );
RepositoryIndexRecord record = factory.createRecord( artifact );
assertNull( "Confirm no record is returned", record );
}
public void testDll()
{
Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
RepositoryIndexRecord record = factory.createRecord( artifact );
StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
expectedRecord.setMd5Checksum( "d41d8cd98f00b204e9800998ecf8427e" );
expectedRecord.setFilename( "test-dll-1.0.1.34.dll" );
expectedRecord.setLastModified( artifact.getFile().lastModified() );
expectedRecord.setSize( artifact.getFile().length() );
expectedRecord.setArtifactId( "test-dll" );
expectedRecord.setGroupId( TEST_GROUP_ID );
expectedRecord.setVersion( "1.0.1.34" );
expectedRecord.setSha1Checksum( "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
expectedRecord.setType( "dll" );
expectedRecord.setRepository( "test" );
expectedRecord.setClasses( "" );
expectedRecord.setPackages( "" );
expectedRecord.setFiles( "" );
assertEquals( "check record", expectedRecord, record );
}
public void testMissingFile()
{
Artifact artifact = createArtifact( "test-foo" );
RepositoryIndexRecord record = factory.createRecord( artifact );
assertNull( "Confirm no record is returned", record );
}
private Artifact createArtifact( String artifactId )
{
return createArtifact( artifactId, "1.0", "jar" );
}
private Artifact createArtifact( String artifactId, String version, String type )
{
Artifact artifact = artifactFactory.createBuildArtifact( TEST_GROUP_ID, artifactId, version, type );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
artifact.setRepository( repository );
return artifact;
}
}