diff options
author | Martin Stockhammer <martin_s@apache.org> | 2020-02-28 09:18:58 +0100 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2020-02-28 09:18:58 +0100 |
commit | 59a21521d2478e845e97a43d98dbc83af348a28a (patch) | |
tree | f8ba2666674e5ebdc0279375b5ee2af3cec3aacd /archiva-modules/archiva-base | |
parent | 4ab1e3733c2b98bf7d0f1c314911ed5e88ff9bf2 (diff) | |
download | archiva-59a21521d2478e845e97a43d98dbc83af348a28a.tar.gz archiva-59a21521d2478e845e97a43d98dbc83af348a28a.zip |
Adding test
Diffstat (limited to 'archiva-modules/archiva-base')
3 files changed, 305 insertions, 2 deletions
diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetSpliterator.java b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetSpliterator.java index 312615b7f..8195de714 100644 --- a/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetSpliterator.java +++ b/archiva-modules/archiva-base/archiva-storage-api/src/main/java/org/apache/archiva/repository/storage/AssetSpliterator.java @@ -28,6 +28,9 @@ import java.util.Set; import java.util.Spliterator; import java.util.function.Consumer; import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; /** * @@ -147,12 +150,20 @@ public class AssetSpliterator implements Spliterator<StorageAsset>, Closeable } } + // In reverse order List<StorageAsset> getChildContainers( StorageAsset parent) { - return parent.list( ).stream( ).filter( StorageAsset::isContainer ).collect( Collectors.toList( ) ); + final List<StorageAsset> children = parent.list( ); + final int len = children.size( ); + return IntStream.range( 0, children.size( ) ).mapToObj( i -> + children.get(len - i - 1)).filter( StorageAsset::isContainer ).collect( Collectors.toList( ) ); } + // In reverse order List<StorageAsset> getChildFiles(StorageAsset parent) { - return parent.list( ).stream( ).filter( StorageAsset::isLeaf ).collect( Collectors.toList( ) ); + final List<StorageAsset> children = parent.list( ); + final int len = children.size( ); + return IntStream.range( 0, children.size( ) ).mapToObj( i -> + children.get(len - i - 1)).filter( StorageAsset::isLeaf ).collect( Collectors.toList( ) ); } diff --git a/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/AssetSpliteratorTest.java b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/AssetSpliteratorTest.java new file mode 100644 index 000000000..7dea3538f --- /dev/null +++ b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/AssetSpliteratorTest.java @@ -0,0 +1,100 @@ +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. + */ + +import org.apache.archiva.repository.storage.mock.MockAsset; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test the AssetSpliterator class + * + * @author Martin Stockhammer <martin_s@apache.org> + */ +class AssetSpliteratorTest +{ + + private StorageAsset createTree() { + MockAsset root = new MockAsset( "" ); + for (int i=0; i<10; i++) { + String name1 = "a" + String.format("%03d",i); + MockAsset parent1 = new MockAsset( root, name1 ); + for (int k=0; k<15; k++) { + String name2 = name1 + String.format("%03d", k); + MockAsset parent2 = new MockAsset( parent1, name2 ); + for (int u=0; u<5; u++) { + String name3 = name2 + String.format("%03d", u); + MockAsset parent3 = new MockAsset( parent2, name3 ); + } + } + } + return root; + } + + private class Status { + LinkedList<StorageAsset> visited = new LinkedList<>( ); + + Status() { + + } + + public void add(StorageAsset asset) { + visited.addLast( asset ); + } + + public StorageAsset getLast() { + return visited.getLast( ); + } + + public List<StorageAsset> getVisited() { + return visited; + } + + public int size() { + return visited.size( ); + } + } + + @Test + void tryAdvance( ) + { + StorageAsset root = createTree( ); + AssetSpliterator spliterator = new AssetSpliterator( root ); + final StorageAsset expectedTarget = root.list( ).get( 0 ).list( ).get( 0 ).list( ).get( 0 ); + final Status status = new Status( ); + spliterator.tryAdvance( a -> status.add( a ) ); + assertEquals( expectedTarget, status.getLast( ) ); + } + + @Test + void forEachRemaining( ) + { + } + + @Test + void trySplit( ) + { + } +}
\ No newline at end of file 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 new file mode 100644 index 000000000..0d1764f94 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-storage-api/src/test/java/org/apache/archiva/repository/storage/mock/MockAsset.java @@ -0,0 +1,192 @@ +package org.apache.archiva.repository.storage.mock; + +/* + * 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.RepositoryStorage; +import org.apache.archiva.repository.storage.StorageAsset; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; +import java.nio.file.Path; +import java.time.Instant; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class MockAsset implements StorageAsset +{ + private StorageAsset parent; + private String path; + private String name; + private LinkedHashMap<String, StorageAsset> children = new LinkedHashMap<>( ); + private boolean container = false; + + public MockAsset( String name ) { + this.name = name; + this.path = ""; + } + + public MockAsset( MockAsset parent, String name ) { + this.parent = parent; + this.path = parent.getPath( ) + "/" + name; + this.name = name; + parent.registerChild( this ); + } + + public void registerChild(StorageAsset child) { + children.putIfAbsent( child.getName(), child ); + this.container = true; + } + + @Override + public RepositoryStorage getStorage( ) + { + return null; + } + + @Override + public String getPath( ) + { + return this.path; + } + + @Override + public String getName( ) + { + return this.name; + } + + @Override + public Instant getModificationTime( ) + { + return Instant.now(); + } + + @Override + public boolean isContainer( ) + { + return this.container; + } + + @Override + public boolean isLeaf( ) + { + return !this.container; + } + + @Override + public List<StorageAsset> list( ) + { + return new ArrayList( children.values( ) ); + } + + @Override + public long getSize( ) + { + return 0; + } + + @Override + public InputStream getReadStream( ) throws IOException + { + return null; + } + + @Override + public ReadableByteChannel getReadChannel( ) throws IOException + { + return null; + } + + @Override + public OutputStream getWriteStream( boolean replace ) throws IOException + { + return null; + } + + @Override + public WritableByteChannel getWriteChannel( boolean replace ) throws IOException + { + return null; + } + + @Override + public boolean replaceDataFromFile( Path newData ) throws IOException + { + return false; + } + + @Override + public boolean exists( ) + { + return false; + } + + @Override + public void create( ) throws IOException + { + + } + + @Override + public Path getFilePath( ) throws UnsupportedOperationException + { + return null; + } + + @Override + public boolean isFileBased( ) + { + return false; + } + + @Override + public boolean hasParent( ) + { + return this.parent != null; + } + + @Override + public StorageAsset getParent( ) + { + return this.parent; + } + + @Override + public StorageAsset resolve( String toPath ) + { + if (children.containsKey( toPath )) { + return children.get( toPath ); + } else { + return null; + } + } + + @Override + public String toString( ) + { + return getPath(); + } +} |