1 package org.apache.archiva.repository.storage.mock;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.repository.storage.RepositoryStorage;
22 import org.apache.archiva.repository.storage.StorageAsset;
23 import org.apache.archiva.repository.storage.util.VisitStatus;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
29 import java.nio.channels.ReadableByteChannel;
30 import java.nio.channels.WritableByteChannel;
31 import java.nio.file.CopyOption;
32 import java.util.LinkedHashMap;
33 import java.util.Optional;
34 import java.util.function.Consumer;
37 * @author Martin Stockhammer <martin_s@apache.org>
39 public class MockStorage implements RepositoryStorage
41 public static final String ADD = "ADD";
42 public static final String REMOVE = "REMOVE";
43 private MockAsset root;
44 private LinkedHashMap<String, MockAsset> assets = new LinkedHashMap<>( );
46 private VisitStatus status = new VisitStatus( );
48 public MockStorage( MockAsset root )
51 root.setStorage( this );
54 public MockStorage() {
55 this.root = new MockAsset( "" );
56 this.root.setStorage( this );
59 public VisitStatus getStatus() {
64 public URI getLocation( )
70 public void updateLocation( URI newLocation ) throws IOException
75 private String[] splitPath(String path) {
76 if (path.equals("/")) {
80 if (path.startsWith( "/" )) {
81 return path.substring( 1, path.length( ) ).split( "/" );
83 return path.split( "/" );
88 public StorageAsset getAsset( String path )
90 if (assets.containsKey( path )) {
91 return assets.get( path );
93 String[] pathArr = splitPath( path );
94 StorageAsset parent = root;
95 for (String pathElement : pathArr) {
96 Optional<? extends StorageAsset> next = parent.list( ).stream( ).filter( a -> a.getName( ).equals( pathElement ) ).findFirst( );
97 if (next.isPresent()) {
100 MockAsset asset = new MockAsset( (MockAsset)parent, pathElement );
101 assets.put( asset.getPath( ), asset );
109 public StorageAsset getRoot( )
115 public void consumeData( StorageAsset asset, Consumer<InputStream> consumerFunction, boolean readLock ) throws IOException
121 public void consumeDataFromChannel( StorageAsset asset, Consumer<ReadableByteChannel> consumerFunction, boolean readLock ) throws IOException
127 public void writeData( StorageAsset asset, Consumer<OutputStream> consumerFunction, boolean writeLock ) throws IOException
133 public void writeDataToChannel( StorageAsset asset, Consumer<WritableByteChannel> consumerFunction, boolean writeLock ) throws IOException
139 public StorageAsset addAsset( String path, boolean container )
141 String[] pathArr = splitPath( path );
142 StorageAsset parent = root;
143 for (String pathElement : pathArr) {
144 Optional<? extends StorageAsset> next = parent.list( ).stream( ).filter( a -> a.getName( ).equals( pathElement ) ).findFirst( );
145 if (next.isPresent()) {
146 parent = next.get( );
148 MockAsset asset = new MockAsset( (MockAsset)parent, pathElement );
149 assets.put( asset.getPath( ), asset );
153 status.add( ADD, parent );
158 public void removeAsset( StorageAsset assetArg ) throws IOException
160 MockAsset asset = (MockAsset) assetArg;
161 if (asset.hasParent())
163 asset.getParent( ).unregisterChild( asset );
165 assets.remove( asset.getPath( ) );
166 status.add( REMOVE, asset );
167 if (asset.isThrowException()) {
168 throw new IOException( "Mocked IOException for " + asset.getPath( ) );
173 public StorageAsset moveAsset( StorageAsset origin, String destination, CopyOption... copyOptions ) throws IOException
179 public void moveAsset( StorageAsset origin, StorageAsset destination, CopyOption... copyOptions ) throws IOException
185 public StorageAsset copyAsset( StorageAsset origin, String destination, CopyOption... copyOptions ) throws IOException
191 public void copyAsset( StorageAsset origin, StorageAsset destination, CopyOption... copyOptions ) throws IOException