aboutsummaryrefslogtreecommitdiffstats
path: root/archiva-modules/archiva-base
diff options
context:
space:
mode:
authorMartin Stockhammer <martin_s@apache.org>2021-05-30 19:14:03 +0200
committerMartin Stockhammer <martin_s@apache.org>2021-05-30 19:14:03 +0200
commit79c61a7c6a63be33a2221a0bd75fdfea7f6b1bf6 (patch)
tree5a12b8ad5d8755f5d0f0b6c189bd6a69ceee2bc3 /archiva-modules/archiva-base
parentfe117fcc4be288a37db07788a2fd3cc857beeb28 (diff)
downloadarchiva-79c61a7c6a63be33a2221a0bd75fdfea7f6b1bf6.tar.gz
archiva-79c61a7c6a63be33a2221a0bd75fdfea7f6b1bf6.zip
Improving storage handling with assets
Diffstat (limited to 'archiva-modules/archiva-base')
-rw-r--r--archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java24
-rw-r--r--archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java12
-rw-r--r--archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java22
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/pom.xml28
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetType.java29
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/StorageAsset.java69
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/util/StorageUtil.java127
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockAsset.java85
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockStorage.java24
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/AbstractStorageUtilTest.java234
-rw-r--r--archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/StorageUtilTest.java158
-rw-r--r--archiva-modules/archiva-base/archiva-storage-fs/pom.xml9
-rw-r--r--archiva-modules/archiva-base/archiva-storage-fs/src/main/java/org/apache/archiva/repository/storage/fs/FilesystemAsset.java49
-rw-r--r--archiva-modules/archiva-base/archiva-storage-fs/src/test/java/org/apache/archiva/repository/storage/fs/FileSystemStorageUtilTest.java120
14 files changed, 806 insertions, 184 deletions
diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java
index 46e32e7f7..61778d2c8 100644
--- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java
+++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java
@@ -91,6 +91,30 @@ public interface ManagedRepositoryContent extends RepositoryContent
void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException;
/**
+ * Copies the given item to the destination repository. The destination repository must be of the same type
+ * as this repository. Metadata is updated only if there is no metadata scan consumer is active.
+ *
+ * @param item the item to copy
+ * @param destinationRepository the destination repository
+ * @throws ItemNotFoundException if the given item does not exist
+ * @throws ContentAccessException if an error occurred during the copy process
+ */
+ void copyItem(ContentItem item, ManagedRepository destinationRepository) throws ItemNotFoundException, ContentAccessException;
+
+ /**
+ * Copies the given item to the destination repository. The destination repository must be of the same type
+ * as this repository.
+ *
+ * @param item the item to copy
+ * @param destinationRepository the destination repository
+ * @param updateMetadata <code>true</code>, if the metadata will be updated immediately after copying. <code>false</code>
+ * if metadata is not updated after copying, but it may be updated by the metadata scan consumer, if it is configured.
+ * @throws ItemNotFoundException if the given item does not exist
+ * @throws ContentAccessException if an error occurred during the copy process
+ */
+ void copyItem(ContentItem item, ManagedRepository destinationRepository, boolean updateMetadata) throws ItemNotFoundException, ContentAccessException;
+
+ /**
* Returns a item for the given selector. The type of the returned item depends on the
* selector.
*
diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java
index a989942d4..c0aefcbdd 100644
--- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java
+++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java
@@ -115,6 +115,18 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout
}
@Override
+ public void copyItem( ContentItem item, ManagedRepository destinationRepository ) throws ItemNotFoundException, ContentAccessException
+ {
+
+ }
+
+ @Override
+ public void copyItem( ContentItem item, ManagedRepository destinationRepository, boolean updateMetadata ) throws ItemNotFoundException, ContentAccessException
+ {
+
+ }
+
+ @Override
public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
diff --git a/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java
index aeee40b5d..9280bd747 100644
--- a/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java
+++ b/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java
@@ -125,7 +125,19 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout
}
@Override
- public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
+ public void deleteItem( ContentItem item ) throws ContentAccessException
+ {
+
+ }
+
+ @Override
+ public void copyItem( ContentItem item, ManagedRepository destinationRepository ) throws ContentAccessException
+ {
+
+ }
+
+ @Override
+ public void copyItem( ContentItem item, ManagedRepository destinationRepository, boolean updateMetadata ) throws ContentAccessException
{
}
@@ -161,7 +173,7 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout
}
@Override
- public Artifact getArtifact( String path ) throws LayoutException, ContentAccessException
+ public Artifact getArtifact( String path ) throws ContentAccessException
{
return null;
}
@@ -252,13 +264,13 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout
}
@Override
- public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
+ public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item )
{
return null;
}
@Override
- public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
+ public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz )
{
return null;
}
@@ -296,7 +308,7 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout
@Override
- public ContentItem toItem( String path ) throws LayoutException
+ public ContentItem toItem( String path )
{
return null;
}
diff --git a/archiva-modules/archiva-base/archiva-storage-api/pom.xml b/archiva-modules/archiva-base/archiva-storage-api/pom.xml
index 9085887fe..3bc8be6b0 100644
--- a/archiva-modules/archiva-base/archiva-storage-api/pom.xml
+++ b/archiva-modules/archiva-base/archiva-storage-api/pom.xml
@@ -49,11 +49,39 @@
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
+ <!-- We need the abstract test class for other projects and are creating a jar file with test classifier -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetType.java b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetType.java
new file mode 100644
index 000000000..e38c8a976
--- /dev/null
+++ b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetType.java
@@ -0,0 +1,29 @@
+package org.apache.archiva.repository.storage;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * The asset type is only used for creating new assets.
+ * FILE type contains data and provides input and output stream
+ * CONTAINER type contains only further assets and not data
+ * @author Martin Stockhammer <martin_s@apache.org>
+ */
+public enum AssetType
+{
+ FILE,CONTAINER
+}
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/StorageAsset.java b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/StorageAsset.java
index eb7074c0f..12f425fed 100644
--- a/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/StorageAsset.java
+++ b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/StorageAsset.java
@@ -43,13 +43,14 @@ public interface StorageAsset
{
/**
- * Returns the storage this asset belongs to.
- * @return
+ * Returns the storage this asset belongs to. Each asset belongs to exactly one storage instance.
+ *
+ * @return the storage instance
*/
RepositoryStorage getStorage();
/**
- * Returns the complete path relative to the repository to the given asset.
+ * Returns the complete path relative to the base path to the given asset.
*
* @return A path starting with '/' that uniquely identifies the asset in the repository.
*/
@@ -81,7 +82,7 @@ public interface StorageAsset
boolean isLeaf();
/**
- * List the child assets.
+ * List the child assets. Implementations should return a ordered list of children.
*
* @return The list of children. If there are no children and if the asset is not a container, a empty list will be returned.
*/
@@ -96,40 +97,55 @@ public interface StorageAsset
/**
* Returns the input stream of the artifact content.
- * It will throw a IOException, if the stream could not be created.
+ * This method will throw a IOException, if the stream could not be created.
+ * Assets of type {@link AssetType#CONTAINER} will throw an IOException because they have no data attached.
* Implementations should create a new stream instance for each invocation and make sure that the
* stream is proper closed after usage.
*
* @return The InputStream representing the content of the artifact.
- * @throws IOException
+ * @throws IOException if the stream could not be created, either because of a problem accessing the storage, or because
+ * the asset is not capable to provide a data stream
*/
InputStream getReadStream() throws IOException;
/**
* Returns a NIO representation of the data.
- *
+ * This method will throw a IOException, if the stream could not be created.
+ * Assets of type {@link AssetType#CONTAINER} will throw an IOException because they have no data attached.
+ * Implementations should create a new channel instance for each invocation and make sure that the
+ * channel is proper closed after usage.
+
* @return A channel to the asset data.
- * @throws IOException
+ * @throws IOException if the channel could not be created, either because of a problem accessing the storage, or because
+ * the asset is not capable to provide a data stream
*/
ReadableByteChannel getReadChannel() throws IOException;
/**
*
* Returns an output stream where you can write data to the asset. The operation is not locked or synchronized.
+ * This method will throw a IOException, if the stream could not be created.
+ * Assets of type {@link AssetType#CONTAINER} will throw an IOException because they have no data attached.
* User of this method have to make sure, that the stream is proper closed after usage.
*
* @param replace If true, the original data will be replaced, otherwise the data will be appended.
* @return The OutputStream where the data can be written.
- * @throws IOException
+ * @throws IOException if the stream could not be created, either because of a problem accessing the storage, or because
+ * the asset is not capable to provide a data stream
*/
OutputStream getWriteStream( boolean replace) throws IOException;
/**
* Returns a NIO representation of the asset where you can write the data.
+ * This method will throw a IOException, if the stream could not be created.
+ * Assets of type {@link AssetType#CONTAINER} will throw an IOException because they have no data attached.
+ * Implementations should create a new channel instance for each invocation and make sure that the
+ * channel is proper closed after usage.
*
* @param replace True, if the content should be replaced by the data written to the stream.
* @return The Channel for writing the data.
- * @throws IOException
+ * @throws IOException if the channel could not be created, either because of a problem accessing the storage, or because
+ * the asset is not capable to provide a data stream
*/
WritableByteChannel getWriteChannel( boolean replace) throws IOException;
@@ -140,6 +156,7 @@ public interface StorageAsset
* The original file may be deleted, if the storage was successful.
*
* @param newData Replaces the data by the content of the given file.
+ * @throws IOException if the access to the storage failed
*/
boolean replaceDataFromFile( Path newData) throws IOException;
@@ -156,6 +173,13 @@ public interface StorageAsset
void create() throws IOException;
/**
+ * Creates the asset as the given type
+ * @param type the type to create, if the asset does not exist
+ * @throws IOException if the asset could not be created
+ */
+ void create(AssetType type) throws IOException;
+
+ /**
* Returns the real path to the asset, if it exist. Not all implementations may implement this method.
* The method throws {@link UnsupportedOperationException}, if and only if {@link #isFileBased()} returns false.
*
@@ -166,7 +190,7 @@ public interface StorageAsset
/**
* Returns true, if the asset can return a file path for the given asset. If this is true, the {@link #getFilePath()}
- * will not throw a {@link UnsupportedOperationException}
+ * must not throw a {@link UnsupportedOperationException}
*
* @return
*/
@@ -174,20 +198,33 @@ public interface StorageAsset
/**
* Returns true, if there is a parent to this asset.
- * @return
+ * @return True, if this asset is a descendant of a parent. False, if this is the root asset of the storage.
*/
boolean hasParent();
/**
- * Returns the parent of this asset.
+ * Returns the parent of this asset. If this is the root asset of the underlying storage,
+ * <code>null</code> will be returned.
+ *
* @return The asset, or <code>null</code>, if it does not exist.
*/
StorageAsset getParent();
/**
- * Returns the asset relative to the given path
- * @param toPath
- * @return
+ * Returns the asset instance relative to the given path. The returned asset may not persisted yet.
+ *
+ * @param toPath the path relative to the current asset
+ * @return the asset representing the given path
*/
StorageAsset resolve(String toPath);
+
+ /**
+ * Returns the relative path from <code>this</code> asset to the given asset.
+ * If the given asset is from a different storage implementation, than this asset, the
+ * result is undefined.
+ *
+ * @param asset the asset that should be a descendant of <code>this</code>
+ * @return the relative path
+ */
+ String relativize(StorageAsset asset );
}
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/util/StorageUtil.java b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/util/StorageUtil.java
index bd52e4b6d..5f64d4d76 100644
--- a/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/util/StorageUtil.java
+++ b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/util/StorageUtil.java
@@ -18,6 +18,7 @@ package org.apache.archiva.repository.storage.util;
* under the License.
*/
+import org.apache.archiva.repository.storage.AssetType;
import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
@@ -31,8 +32,14 @@ import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.CopyOption;
import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.List;
+import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@@ -135,42 +142,118 @@ public class StorageUtil
final RepositoryStorage storage = baseDir.getStorage( );
try(Stream<StorageAsset> stream = newAssetStream( baseDir ))
{
- if ( stopOnError )
+ try
{
// Return true, if no exception occurred
// anyMatch is short-circuiting, that means it stops if the condition matches
- return !stream.map( a -> {
- try
- {
- storage.removeAsset( a );
- // Returning false, if OK
- return Boolean.FALSE;
- }
- catch ( IOException e )
- {
- LOG.error( "Could not delete asset {}: {}", a.getPath( ), e.getMessage( ), e );
- // Returning true, if exception
- return Boolean.TRUE;
- }
- } ).anyMatch( r -> r );
- } else {
- // Return true, if all removals were OK
- // We want to consume all, so we use allMatch
return stream.map( a -> {
try
{
storage.removeAsset( a );
- // Returning true, if OK
return Boolean.TRUE;
}
catch ( IOException e )
{
LOG.error( "Could not delete asset {}: {}", a.getPath( ), e.getMessage( ), e );
- // Returning false, if exception
- return Boolean.FALSE;
+ if (stopOnError) {
+ throw new RuntimeException( e );
+ } else
+ {
+ return Boolean.FALSE;
+ }
+ }
+ } ).reduce( (a,b)->Boolean.logicalAnd( a,b ) ).orElse( Boolean.FALSE );
+ } catch ( RuntimeException e ) {
+ return false;
+ }
+ }
+ }
+
+ /**
+ * Deletes the given asset and all child assets recursively.
+ * @param srcAsset The source directory
+ * @param destAsset The destination directory
+ * @param stopOnError if <code>true</code> the traversal stops, if an exception is encountered
+ * @return returns <code>true</code>, if every item was removed. If an IOException was encountered during
+ * traversal it returns <code>false</code>
+ */
+ public static final boolean copyRecursively(final StorageAsset srcAsset, final StorageAsset destAsset, final boolean stopOnError) throws IOException
+ {
+ try
+ {
+ if ( srcAsset.isFileBased( ) && destAsset.isFileBased( ) )
+ {
+ Path src = srcAsset.getFilePath( );
+ Path dest = destAsset.getFilePath( );
+ return Files.walk( src )
+ .map( source -> {
+ try
+ {
+ Files.copy( source, dest.resolve( src.relativize( source ) ),
+ StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES );
+ return Boolean.TRUE;
+ }
+ catch ( IOException e )
+ {
+ if ( stopOnError )
+ {
+ throw new RuntimeException( e );
+ }
+ else
+ {
+ return Boolean.FALSE;
+ }
+ }
+ } ).reduce( ( a, b ) -> Boolean.logicalAnd( a, b ) ).get();
+ }
+ else
+ {
+ try ( Stream<StorageAsset> stream = newAssetStream( srcAsset ) )
+ {
+
+ if (!destAsset.exists() && srcAsset.isContainer()) {
+ destAsset.create( AssetType.CONTAINER );
}
- } ).allMatch( r -> r );
+ return stream.map( a -> {
+ try
+ {
+ String relativePath = destAsset.relativize( a );
+ System.out.println( "Destination relative: " + relativePath );
+ StorageAsset destFile = destAsset.resolve( relativePath );
+ assert destFile != null;
+ System.out.println( "Destination " + destFile.getPath( ) + " " + a.isContainer() );
+ if (a.isContainer()) {
+ destFile.create( AssetType.CONTAINER);
+ } else {
+ if (!destFile.getParent( ).exists() ) {
+ System.out.println( "Creating parent " + destFile.getParent( ) );
+ destFile.getParent().create( AssetType.CONTAINER );
+ }
+ System.out.println( "Copying " + a.getPath( ) + "->" + destFile.getPath( ) );
+ copy( a.getReadChannel( ), destFile.getWriteChannel( true ) );
+ }
+ return Boolean.TRUE;
+ }
+ catch ( IOException e )
+ {
+ LOG.error( "Could not copy asset {}: {}", a.getPath( ), e.getMessage( ), e );
+ // Returning true, if exception
+ if ( stopOnError )
+ {
+ throw new RuntimeException( e );
+ }
+ else
+ {
+ return Boolean.FALSE;
+ }
+ }
+ } ).reduce( ( a, b ) -> Boolean.logicalAnd( a, b ) ).orElse(Boolean.FALSE);
+ }
}
+ } catch (RuntimeException e) {
+ System.err.println( "Exception " + e.getMessage( ) );
+ e.printStackTrace( );
+ return false;
}
}
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockAsset.java b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockAsset.java
index 0e19459f0..c2d38338e 100644
--- a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockAsset.java
+++ b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockAsset.java
@@ -19,21 +19,29 @@ package org.apache.archiva.repository.storage.mock;
* under the License.
*/
+import org.apache.archiva.repository.storage.AssetType;
import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.storage.StorageAsset;
+import org.apache.archiva.repository.storage.util.StorageUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.mockito.ArgumentMatcher;
+import org.mockito.Mock;
+import org.mockito.Mockito;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.time.Instant;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
+
+import static org.mockito.Matchers.any;
public class MockAsset implements StorageAsset
{
@@ -43,24 +51,37 @@ public class MockAsset implements StorageAsset
private LinkedHashMap<String, MockAsset> children = new LinkedHashMap<>( );
private boolean container = false;
private RepositoryStorage storage;
-
+ private boolean exists = false;
private boolean throwException;
public MockAsset( String name ) {
+ this.parent = null;
this.name = name;
this.path = "/";
}
public MockAsset( MockAsset parent, String name ) {
+ if (parent!=null && "".equals(name)) {
+ throw new RuntimeException( "Bad asset creation with empty name and parent" );
+ }
this.parent = parent;
- this.path = (parent.hasParent()?parent.getPath( ):"") + "/" + name;
+ this.path = getPath( parent, name );
this.name = name;
this.storage = parent.getStorage( );
parent.registerChild( this );
}
+ private String getPath(MockAsset parent, String name) {
+
+ if (parent.hasParent() && !parent.getPath( ).equals( "/" )) {
+ return parent.getPath( ) + "/" + name;
+ } else {
+ return "/" + name;
+ }
+ }
+
public void registerChild(MockAsset child) {
children.putIfAbsent( child.getName(), child );
this.container = true;
@@ -136,25 +157,28 @@ public class MockAsset implements StorageAsset
@Override
public InputStream getReadStream( ) throws IOException
{
- return null;
+ return Mockito.mock( InputStream.class );
}
@Override
public ReadableByteChannel getReadChannel( ) throws IOException
{
- return null;
+ ReadableByteChannel channel = Mockito.mock( ReadableByteChannel.class );
+ Mockito.when( channel.read( any( ByteBuffer.class ) ) ).thenReturn( -1 );
+ return channel;
}
@Override
public OutputStream getWriteStream( boolean replace ) throws IOException
{
- return null;
+ return Mockito.mock( OutputStream.class );
}
@Override
public WritableByteChannel getWriteChannel( boolean replace ) throws IOException
{
- return null;
+ this.exists = true;
+ return Mockito.mock( WritableByteChannel.class );
}
@Override
@@ -166,13 +190,22 @@ public class MockAsset implements StorageAsset
@Override
public boolean exists( )
{
- return false;
+ return exists;
}
@Override
public void create( ) throws IOException
{
+ this.exists = true;
+ }
+ @Override
+ public void create( AssetType type ) throws IOException
+ {
+ if (type.equals( AssetType.CONTAINER )) {
+ this.container = true;
+ }
+ this.exists = true;
}
@Override
@@ -205,7 +238,39 @@ public class MockAsset implements StorageAsset
if (children.containsKey( toPath )) {
return children.get( toPath );
} else {
- return null;
+ if (toPath.startsWith( "/" )) {
+ toPath = StringUtils.removeStart( toPath, "/" );
+ }
+ if ( "".equals( toPath ) )
+ {
+ return this;
+ }
+ String[] destPath = toPath.split( "/" );
+ StringBuilder destPathStr = new StringBuilder( );
+ MockAsset destParent = this;
+ for (int i=0; i<destPath.length; i++) {
+ destPathStr.append( "/" ).append( destPath[i] );
+ StorageAsset child = storage.getAsset( destPathStr.toString( ) );
+ if (child!=null) {
+ destParent = (MockAsset) child;
+ } else
+ {
+ System.out.println( "Resolve " + destParent.getPath( ) + " -- " + destPath[i] );
+ destParent = new MockAsset( destParent, destPath[i] );
+ }
+ }
+ return destParent;
+ }
+ }
+
+ @Override
+ public String relativize( StorageAsset asset )
+ {
+ System.out.println( "relativize this " + this.getPath( ) + " -> other " + asset.getPath( ) );
+ if (asset.isFileBased()) {
+ return Paths.get( getPath( ) ).relativize( asset.getFilePath( ) ).toString();
+ } else {
+ return StringUtils.removeStart( asset.getPath( ), this.getPath( ) );
}
}
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockStorage.java b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockStorage.java
index e024caefb..23acc89ca 100644
--- a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockStorage.java
+++ b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockStorage.java
@@ -18,6 +18,7 @@ package org.apache.archiva.repository.storage.mock;
* under the License.
*/
+import org.apache.archiva.repository.storage.AssetType;
import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.repository.storage.util.VisitStatus;
@@ -48,12 +49,30 @@ public class MockStorage implements RepositoryStorage
public MockStorage( MockAsset root )
{
this.root = root;
+ try
+ {
+ this.root.create( AssetType.CONTAINER );
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace( );
+ }
root.setStorage( this );
+ assets.put( "/", root );
}
public MockStorage() {
- this.root = new MockAsset( "" );
+ this.root = new MockAsset( "/" );
+ try
+ {
+ this.root.create( AssetType.CONTAINER );
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace( );
+ }
this.root.setStorage( this );
+ assets.put( "/", this.root );
}
public VisitStatus getStatus() {
@@ -85,8 +104,9 @@ public class MockStorage implements RepositoryStorage
}
@Override
- public StorageAsset getAsset( String path )
+ public StorageAsset getAsset( final String requestedPath )
{
+ String path = requestedPath.startsWith( "/" ) ? requestedPath : "/"+requestedPath;
if (assets.containsKey( path )) {
return assets.get( path );
}
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/AbstractStorageUtilTest.java b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/AbstractStorageUtilTest.java
new file mode 100644
index 000000000..9a15a08be
--- /dev/null
+++ b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/AbstractStorageUtilTest.java
@@ -0,0 +1,234 @@
+package org.apache.archiva.repository.storage.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.repository.storage.AssetType;
+import org.apache.archiva.repository.storage.RepositoryStorage;
+import org.apache.archiva.repository.storage.StorageAsset;
+import org.apache.archiva.repository.storage.mock.MockStorage;
+import org.apache.archiva.repository.storage.util.StorageUtil;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Abstract test class for the storage utility to test for different storage implementations.
+ *
+ * @author Martin Stockhammer <martin_s@apache.org>
+ */
+public abstract class AbstractStorageUtilTest
+{
+ private static final int LEVEL1 = 12;
+ private static final int LEVEL2 = 13;
+ private static final int LEVEL3 = 6;
+
+ /**
+ * A subclass must override this method. This method returns a new asset instance with the given parent.
+ *
+ * @param parent the parent asset for the newly created asset
+ * @param name the name of the new asset
+ * @return the asset
+ */
+ protected abstract StorageAsset createAsset( StorageAsset parent, String name, AssetType type );
+
+ protected StorageAsset createAsset(StorageAsset parent, String name) {
+ return createAsset( parent, name, AssetType.FILE );
+ }
+
+ /**
+ * A subclass must override this method. This method returns a new root asset instance without parent.
+ * @return the newly created asset instance
+ */
+ protected abstract StorageAsset createRootAsset( );
+
+ /**
+ * Activates a exception on a certain asset in the storage
+ * @param root the root asset
+ */
+ protected abstract void activateException( StorageAsset root );
+
+
+
+ /**
+ * A subclass should override this method. This method creates a new storage instance with the given root element.
+ *
+ * @param root the root asset
+ * @return the storage instance
+ */
+ protected abstract RepositoryStorage createStorage( StorageAsset root );
+
+
+ protected StorageAsset createTree( )
+ {
+ return createTree( LEVEL1, LEVEL2, LEVEL3 );
+ }
+
+ protected StorageAsset createTree( int... levelElements )
+ {
+ StorageAsset root = createRootAsset( );
+ recurseSubTree( root, 0, levelElements );
+ return root;
+ }
+
+ private void recurseSubTree( StorageAsset parent, int level, int[] levelElements )
+ {
+ if ( level < levelElements.length )
+ {
+ AssetType type = ( level == levelElements.length - 1 ) ? AssetType.FILE : AssetType.CONTAINER;
+ for ( int k = 0; k < levelElements[level]; k++ )
+ {
+ String name = parent.getName( ) + String.format( "%03d", k );
+ StorageAsset asset = createAsset( parent, name, type );
+ recurseSubTree( asset, level + 1, levelElements );
+ }
+ }
+ }
+
+ @Test
+ void testWalkFromRoot( )
+ {
+ StorageAsset root = createTree( );
+ ConsumeVisitStatus status = new ConsumeVisitStatus( );
+
+ StorageUtil.walk( root, status );
+ int expected = LEVEL1 * LEVEL2 * LEVEL3 + LEVEL1 * LEVEL2 + LEVEL1 + 1;
+ Assertions.assertEquals( expected, status.size( ) );
+ StorageAsset first = root.list( ).get( 0 ).list( ).get( 0 ).list( ).get( 0 );
+ Assertions.assertEquals( first, status.getFirst( ) );
+ Assertions.assertEquals( root, status.getLast( ) );
+ }
+
+ @Test
+ void testWalkFromChild( )
+ {
+ StorageAsset root = createTree( );
+ ConsumeVisitStatus status = new ConsumeVisitStatus( );
+ StorageAsset testRoot = root.list( ).get( 3 );
+
+ StorageUtil.walk( testRoot, status );
+ int expected = LEVEL2 * LEVEL3 + LEVEL2 + 1;
+ Assertions.assertEquals( expected, status.size( ) );
+ StorageAsset first = root.list( ).get( 3 ).list( ).get( 0 ).list( ).get( 0 );
+ Assertions.assertEquals( first, status.getFirst( ) );
+ Assertions.assertEquals( testRoot, status.getLast( ) );
+ }
+
+ @Test
+ void testWalkFromRootWithCondition( )
+ {
+ StorageAsset root = createTree( );
+ StopVisitStatus status = new StopVisitStatus( );
+ status.setStopCondition( a -> a.getName( ).equals( "001002003" ) );
+
+ StorageUtil.walk( root, status );
+ Assertions.assertEquals( "001002003", status.getLast( ).getName( ) );
+ int expected = LEVEL2 * LEVEL3 + LEVEL2 + 2 * LEVEL3 + 1 + 1 + 1 + 4;
+ Assertions.assertEquals( expected, status.size( ) );
+ }
+
+ @Test
+ void testStream( )
+ {
+ StorageAsset root = createTree( );
+ List<StorageAsset> result;
+ try ( Stream<StorageAsset> stream = StorageUtil.newAssetStream( root, false ) )
+ {
+ result = stream.filter( a -> a.getName( ).startsWith( "001" ) ).collect( Collectors.toList( ) );
+ }
+ int expected = LEVEL2 * LEVEL3 + LEVEL2 + 1;
+ Assertions.assertEquals( expected, result.size( ) );
+ Assertions.assertEquals( "001", result.get( result.size( ) - 1 ).getName( ) );
+ Assertions.assertEquals( "001012", result.get( result.size( ) - 2 ).getName( ) );
+ }
+
+ @Test
+ void testStreamParallel( )
+ {
+ StorageAsset root = createTree( );
+ List<StorageAsset> result;
+ try ( Stream<StorageAsset> stream = StorageUtil.newAssetStream( root, true ) )
+ {
+ result = stream.filter( a -> a.getName( ).startsWith( "001" ) ).collect( Collectors.toList( ) );
+ }
+ int expected = LEVEL2 * LEVEL3 + LEVEL2 + 1;
+ Assertions.assertEquals( expected, result.size( ) );
+ }
+
+ @Test
+ void testDelete( )
+ {
+ StorageAsset root = createTree( );
+ RepositoryStorage storage = createStorage( root );
+
+ StorageUtil.deleteRecursively( root );
+ int expected = LEVEL1 * LEVEL2 * LEVEL3 + LEVEL1 * LEVEL2 + LEVEL1 + 1;
+ testDeletionStatus( expected, storage );
+
+ }
+
+ protected abstract void testDeletionStatus( int expected, RepositoryStorage storage );
+
+ @Test
+ void testDeleteWithException( )
+ {
+ StorageAsset root = createTree( );
+ RepositoryStorage storage = createStorage( root );
+ activateException( root );
+
+ StorageUtil.deleteRecursively( root );
+ int expected = LEVEL1 * LEVEL2 * LEVEL3 + LEVEL1 * LEVEL2 + LEVEL1 + 1;
+ testDeletionStatus( expected, storage );
+ }
+
+ @Test
+ void testDeleteWithExceptionFailFast( )
+ {
+ StorageAsset root = createTree( );
+ RepositoryStorage storage = createStorage( root );
+ activateException( root );
+
+ StorageUtil.deleteRecursively( root, true );
+ int expected = 113;
+ testDeletionStatus( expected, storage );
+ }
+
+ @Test
+ void testCopyRecursive( ) throws IOException
+ {
+ StorageAsset root = createTree( );
+ createStorage( root );
+ StorageAsset destinationRoot = createRootAsset( );
+ RepositoryStorage destinationStorage = createStorage( destinationRoot );
+ StorageAsset destination = destinationStorage.getAsset( "" );
+ boolean result = StorageUtil.copyRecursively( root, destination, false );
+ Assertions.assertTrue( result );
+ Assertions.assertTrue( destination.exists( ) );
+ Assertions.assertTrue( destination.resolve( "000/000000/000000000" ).exists( ) );
+ Assertions.assertTrue( destination.resolve( "011/011000/011000000" ).exists( ) );
+ Assertions.assertTrue( destination.resolve( "010/010000/010000000" ).exists( ) );
+ Assertions.assertTrue( destination.resolve( "000/000000/000000000" ).exists( ) );
+
+ }
+}
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/StorageUtilTest.java b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/StorageUtilTest.java
index c1c53c7e2..8d6fbef72 100644
--- a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/StorageUtilTest.java
+++ b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/util/StorageUtilTest.java
@@ -19,157 +19,71 @@ package org.apache.archiva.repository.storage.util;
*/
+import org.apache.archiva.repository.storage.AssetType;
+import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.repository.storage.mock.MockAsset;
import org.apache.archiva.repository.storage.mock.MockStorage;
-import org.junit.jupiter.api.Test;
-
-import java.io.IOException;
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Assertions;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
-class StorageUtilTest
+class StorageUtilTest extends AbstractStorageUtilTest
{
- private static int LEVEL1 = 12;
- private static int LEVEL2 = 13;
- private static int LEVEL3 = 6;
-
- private MockAsset createTree() {
- return createTree( LEVEL1, LEVEL2, LEVEL3 );
- }
-
- private MockAsset createTree(int... levelElements) {
- MockAsset root = new MockAsset( "" );
- recurseSubTree( root, 0, levelElements );
- return root;
+ private MockStorage createStorage(MockAsset root) {
+ return new MockStorage( root );
}
- private void recurseSubTree(MockAsset parent, int level, int[] levelElements) {
- if (level < levelElements.length)
+ private MockAsset createAsset( MockAsset parent, String name, AssetType type ) {
+ if (parent==null) {
+ return new MockAsset( name );
+ } else
{
- for ( int k = 0; k < levelElements[level]; k++ )
- {
- String name = parent.getName( ) + String.format( "%03d", k );
- MockAsset asset = new MockAsset( parent, name );
- recurseSubTree( asset, level + 1, levelElements );
- }
+ return new MockAsset( parent, name );
}
}
- @Test
- void testWalkFromRoot() {
- StorageAsset root = createTree( );
- ConsumeVisitStatus status = new ConsumeVisitStatus( );
- StorageUtil.walk( root, status );
- int expected = LEVEL1 * LEVEL2 * LEVEL3 + LEVEL1 * LEVEL2 + LEVEL1 + 1;
- assertEquals( expected, status.size() );
- StorageAsset first = root.list( ).get( 0 ).list( ).get( 0 ).list().get(0);
- assertEquals( first, status.getFirst( ) );
- assertEquals( root, status.getLast( ) );
- }
-
- @Test
- void testWalkFromChild() {
- StorageAsset root = createTree( );
- ConsumeVisitStatus status = new ConsumeVisitStatus( );
- StorageAsset testRoot = root.list( ).get( 3 );
-
- StorageUtil.walk( testRoot, status );
- int expected = LEVEL2 * LEVEL3 + LEVEL2 + 1;
- assertEquals( expected, status.size() );
- StorageAsset first = root.list( ).get( 3 ).list( ).get( 0 ).list().get(0);
- assertEquals( first, status.getFirst( ) );
- assertEquals( testRoot, status.getLast( ) );
- }
-
-
- @Test
- void testWalkFromRootWithCondition() {
- StorageAsset root = createTree( );
- StopVisitStatus status = new StopVisitStatus( );
- status.setStopCondition( a -> a.getName().equals("001002003") );
-
- StorageUtil.walk( root, status );
- assertEquals( "001002003", status.getLast( ).getName() );
- int expected = LEVEL2 * LEVEL3 + LEVEL2 + 2 * LEVEL3 + 1 + 1 + 1 + 4;
- assertEquals( expected, status.size() );
+ protected void activateException(MockAsset root) {
+ root.list( ).get( 1 ).list( ).get( 2 ).setThrowException( true );
}
- @Test
- void testStream() {
- StorageAsset root = createTree( );
- ConsumeVisitStatus status = new ConsumeVisitStatus( );
-
- List<StorageAsset> result;
- try ( Stream<StorageAsset> stream = StorageUtil.newAssetStream( root, false ) )
- {
- result = stream.filter( a -> a.getName( ).startsWith( "001" ) ).collect( Collectors.toList());
- }
- int expected = LEVEL2 * LEVEL3 + LEVEL2 + 1;
- assertEquals( expected, result.size( ) );
- assertEquals( "001", result.get( result.size( ) - 1 ).getName() );
- assertEquals( "001012", result.get( result.size( ) - 2 ).getName() );
+ @Override
+ protected StorageAsset createAsset( StorageAsset parent, String name, AssetType type )
+ {
+ return createAsset( (MockAsset) parent, name, type);
}
- @Test
- void testStreamParallel() {
- StorageAsset root = createTree( );
- ConsumeVisitStatus status = new ConsumeVisitStatus( );
-
- List<StorageAsset> result;
- try ( Stream<StorageAsset> stream = StorageUtil.newAssetStream( root, true ) )
- {
- result = stream.filter( a -> a.getName( ).startsWith( "001" ) ).collect( Collectors.toList());
- }
- int expected = LEVEL2 * LEVEL3 + LEVEL2 + 1;
- assertEquals( expected, result.size( ) );
+ @Override
+ protected StorageAsset createRootAsset( )
+ {
+ return new MockAsset( "" );
}
-
- @Test
- void testDelete() throws IOException
+ @Override
+ protected void activateException( StorageAsset root )
{
- MockAsset root = createTree( );
- MockStorage storage = new MockStorage( root );
-
- StorageUtil.deleteRecursively( root );
- int expected = LEVEL1 * LEVEL2 * LEVEL3 + LEVEL1 * LEVEL2 + LEVEL1 + 1;
- assertEquals( expected, storage.getStatus( ).size( MockStorage.REMOVE ) );
-
+ activateException( (MockAsset)root );
}
- @Test
- void testDeleteWithException() throws IOException
+ @Override
+ protected RepositoryStorage createStorage( StorageAsset root )
{
- MockAsset root = createTree( );
- MockStorage storage = new MockStorage( root );
- root.list( ).get( 1 ).list( ).get( 2 ).setThrowException( true );
-
- StorageUtil.deleteRecursively( root );
- int expected = LEVEL1 * LEVEL2 * LEVEL3 + LEVEL1 * LEVEL2 + LEVEL1 + 1;
- assertEquals( expected, storage.getStatus( ).size( MockStorage.REMOVE ) );
-
+ return new MockStorage( (MockAsset) root );
}
- @Test
- void testDeleteWithExceptionFailFast() throws IOException
+ protected void testDeletionStatus( int expected, RepositoryStorage storage )
{
- MockAsset root = createTree( );
- MockStorage storage = new MockStorage( root );
- root.list( ).get( 1 ).list( ).get( 2 ).setThrowException( true );
-
- StorageUtil.deleteRecursively( root, true );
- int expected = 113;
- assertEquals( expected, storage.getStatus( ).size( MockStorage.REMOVE ) );
-
+ if ( storage instanceof MockStorage )
+ {
+ Assertions.assertEquals( expected, ( (MockStorage) storage ).getStatus( ).size( MockStorage.REMOVE ) );
+ }
+ else
+ {
+ Assertions.fail( "Deletion status not implemented for this storage " + storage.getClass( ).getName( ) );
+ }
}
} \ No newline at end of file
diff --git a/archiva-modules/archiva-base/archiva-storage-fs/pom.xml b/archiva-modules/archiva-base/archiva-storage-fs/pom.xml
index f47d9d7aa..c277b6382 100644
--- a/archiva-modules/archiva-base/archiva-storage-fs/pom.xml
+++ b/archiva-modules/archiva-base/archiva-storage-fs/pom.xml
@@ -65,6 +65,15 @@
<artifactId>slf4j-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.archiva</groupId>
+ <artifactId>archiva-storage-api</artifactId>
+ <classifier>tests</classifier>
+ <type>test-jar</type>
+ <scope>test</scope>
+ <version>${project.version}</version>
+ </dependency>
+
</dependencies>
<build>
diff --git a/archiva-modules/archiva-base/archiva-storage-fs/src/main/java/org/apache/archiva/repository/storage/fs/FilesystemAsset.java b/archiva-modules/archiva-base/archiva-storage-fs/src/main/java/org/apache/archiva/repository/storage/fs/FilesystemAsset.java
index fb118a85c..1bbbe5b72 100644
--- a/archiva-modules/archiva-base/archiva-storage-fs/src/main/java/org/apache/archiva/repository/storage/fs/FilesystemAsset.java
+++ b/archiva-modules/archiva-base/archiva-storage-fs/src/main/java/org/apache/archiva/repository/storage/fs/FilesystemAsset.java
@@ -18,12 +18,14 @@ package org.apache.archiva.repository.storage.fs;
* under the License.
*/
+import org.apache.archiva.repository.storage.AssetType;
import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -89,7 +91,7 @@ public class FilesystemAsset implements StorageAsset, Comparable {
boolean supportsAcl = false;
boolean supportsPosix = false;
final boolean setPermissionsForNew;
- final RepositoryStorage storage;
+ final FilesystemStorage storage;
boolean directoryHint = false;
@@ -97,7 +99,7 @@ public class FilesystemAsset implements StorageAsset, Comparable {
private static final OpenOption[] APPEND_OPTIONS = new OpenOption[]{StandardOpenOption.APPEND};
- FilesystemAsset(RepositoryStorage storage, String path, Path assetPath, Path basePath) {
+ FilesystemAsset(FilesystemStorage storage, String path, Path assetPath, Path basePath) {
this.assetPath = assetPath;
this.relativePath = normalizePath(path);
this.setPermissionsForNew=false;
@@ -113,7 +115,7 @@ public class FilesystemAsset implements StorageAsset, Comparable {
* @param path The logical path for the asset relative to the repository.
* @param assetPath The asset path.
*/
- public FilesystemAsset(RepositoryStorage storage, String path, Path assetPath) {
+ public FilesystemAsset(FilesystemStorage storage, String path, Path assetPath) {
this.assetPath = assetPath;
this.relativePath = normalizePath(path);
this.setPermissionsForNew = false;
@@ -135,7 +137,7 @@ public class FilesystemAsset implements StorageAsset, Comparable {
* @param directory This is only relevant, if the represented file or directory does not exist yet and
* is a hint.
*/
- public FilesystemAsset(RepositoryStorage storage, String path, Path assetPath, Path basePath, boolean directory) {
+ public FilesystemAsset(FilesystemStorage storage, String path, Path assetPath, Path basePath, boolean directory) {
this.assetPath = assetPath;
this.relativePath = normalizePath(path);
this.directoryHint = directory;
@@ -154,7 +156,7 @@ public class FilesystemAsset implements StorageAsset, Comparable {
* @param directory This is only relevant, if the represented file or directory does not exist yet and
* is a hint.
*/
- public FilesystemAsset(RepositoryStorage storage, String path, Path assetPath, Path basePath, boolean directory, boolean setPermissionsForNew) {
+ public FilesystemAsset(FilesystemStorage storage, String path, Path assetPath, Path basePath, boolean directory, boolean setPermissionsForNew) {
this.assetPath = assetPath;
this.relativePath = normalizePath(path);
this.directoryHint = directory;
@@ -231,7 +233,7 @@ public class FilesystemAsset implements StorageAsset, Comparable {
@Override
public String getName() {
- return assetPath.getFileName().toString();
+ return hasParent( ) ? assetPath.getFileName( ).toString( ) : "";
}
@Override
@@ -277,7 +279,9 @@ public class FilesystemAsset implements StorageAsset, Comparable {
@Override
public List<StorageAsset> list() {
try {
- return Files.list(assetPath).map(p -> new FilesystemAsset(storage, relativePath + "/" + p.getFileName().toString(), assetPath.resolve(p), this.basePath))
+ return Files.list(assetPath)
+ .sorted()
+ .map(p -> new FilesystemAsset(storage, relativePath + "/" + p.getFileName().toString(), assetPath.resolve(p), this.basePath))
.collect(Collectors.toList());
} catch (IOException e) {
return Collections.EMPTY_LIST;
@@ -506,6 +510,37 @@ public class FilesystemAsset implements StorageAsset, Comparable {
}
@Override
+ public void create( AssetType type ) throws IOException {
+ if (!Files.exists(assetPath)) {
+ if (type.equals( AssetType.CONTAINER ) || directoryHint) {
+ Files.createDirectories(assetPath);
+ } else {
+ if (!Files.exists( assetPath.getParent() )) {
+ Files.createDirectories( assetPath.getParent( ) );
+ }
+ Files.createFile(assetPath);
+ }
+ if (setPermissionsForNew) {
+ applyDefaultPermissions(assetPath);
+ }
+ }
+ }
+
+ @Override
+ public String relativize( StorageAsset asset )
+ {
+ if (asset instanceof FilesystemAsset ) {
+ return this.relativize( (FilesystemAsset) asset );
+ } else {
+ return StringUtils.removeStart( asset.getPath( ), this.getPath( ) );
+ }
+ }
+
+ public String relativize( FilesystemAsset asset) {
+ return this.getFilePath( ).relativize( asset.getFilePath( ) ).toString();
+ }
+
+ @Override
public String toString() {
return relativePath+":"+assetPath;
}
diff --git a/archiva-modules/archiva-base/archiva-storage-fs/src/test/java/org/apache/archiva/repository/storage/fs/FileSystemStorageUtilTest.java b/archiva-modules/archiva-base/archiva-storage-fs/src/test/java/org/apache/archiva/repository/storage/fs/FileSystemStorageUtilTest.java
new file mode 100644
index 000000000..f403489dd
--- /dev/null
+++ b/archiva-modules/archiva-base/archiva-storage-fs/src/test/java/org/apache/archiva/repository/storage/fs/FileSystemStorageUtilTest.java
@@ -0,0 +1,120 @@
+package org.apache.archiva.repository.storage.fs;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.common.filelock.DefaultFileLockManager;
+import org.apache.archiva.repository.storage.AssetType;
+import org.apache.archiva.repository.storage.RepositoryStorage;
+import org.apache.archiva.repository.storage.StorageAsset;
+import org.apache.archiva.repository.storage.util.AbstractStorageUtilTest;
+import org.apache.commons.io.FileUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.TestInstance;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/**
+ * @author Martin Stockhammer <martin_s@apache.org>
+ */
+@TestInstance( TestInstance.Lifecycle.PER_CLASS)
+public class FileSystemStorageUtilTest extends AbstractStorageUtilTest
+{
+
+ List<Path> tmpDirs = new ArrayList<>( );
+
+ @Override
+ protected StorageAsset createAsset( StorageAsset parent, String name, AssetType type )
+ {
+ if (parent instanceof FilesystemAsset) {
+ return createAsset( (FilesystemAsset) parent, name, type );
+ } else {
+ fail( "Bad asset instance type" );
+ return null;
+ }
+ }
+
+ @AfterAll
+ private void cleanup() {
+ for( Path dir : tmpDirs) {
+ if (Files.exists( dir )) {
+ FileUtils.deleteQuietly( dir.toFile( ) );
+ }
+ }
+ }
+
+ private FilesystemAsset createAsset(FilesystemAsset parent, String name, AssetType type) {
+ FilesystemAsset asset = (FilesystemAsset) parent.resolve( name );
+ try
+ {
+ asset.create(type);
+ return asset;
+ }
+ catch ( IOException e )
+ {
+ fail( "Could not create asset " + e.getMessage( ) );
+ return null;
+ }
+ }
+
+ @Override
+ protected StorageAsset createRootAsset( )
+ {
+ try
+ {
+ Path tmpDir = Files.createTempDirectory( "testfs" );
+ tmpDirs.add( tmpDir );
+ FilesystemStorage storage = new FilesystemStorage( tmpDir, new DefaultFileLockManager( ) );
+ return storage.getRoot( );
+ }
+ catch ( IOException e )
+ {
+ fail( "Could not create storage" );
+ return null;
+ }
+ }
+
+ @Override
+ protected void activateException( StorageAsset root )
+ {
+ // Not done here
+ }
+
+ @Override
+ protected RepositoryStorage createStorage( StorageAsset root )
+ {
+ if (root instanceof FilesystemAsset) {
+ return root.getStorage( );
+ } else {
+ fail( "Wrong asset implementation " + root.getClass( ).getName( ) );
+ return null;
+ }
+ }
+
+ @Override
+ protected void testDeletionStatus( int expected, RepositoryStorage storage )
+ {
+ assertFalse( Files.exists( storage.getRoot( ).getFilePath( ) ) );
+ }
+}