1 package org.apache.archiva.repository.content;
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
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.filelock.DefaultFileLockManager;
23 import org.apache.commons.io.IOUtils;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
34 import static org.junit.Assert.*;
36 public class FilesystemStorageTest {
38 private FilesystemStorage fsStorage;
39 private FilesystemAsset file1Asset;
40 private FilesystemAsset dir1Asset;
46 public void init() throws IOException {
47 baseDir = Files.createTempDirectory("FsStorageTest");
48 DefaultFileLockManager fl = new DefaultFileLockManager();
49 fsStorage = new FilesystemStorage(baseDir,fl);
50 Files.createDirectories(baseDir.resolve("dir1"));
51 Files.createDirectories(baseDir.resolve("dir2"));
52 file1 = Files.createFile(baseDir.resolve("dir1/testfile1.dat"));
53 dir1 = Files.createDirectories(baseDir.resolve("dir1/testdir"));
54 file1Asset = new FilesystemAsset("/dir1/testfile1.dat", file1);
55 dir1Asset = new FilesystemAsset("/dir1/testdir", dir1);
58 private class StringResult {
59 public String getData() {
63 public void setData(String data) {
72 public void cleanup() {
74 Files.deleteIfExists(file1);
75 } catch (IOException e) {
79 Files.deleteIfExists(dir1);
80 } catch (IOException e) {
84 Files.deleteIfExists(baseDir.resolve("dir1"));
85 } catch (IOException e) {
89 Files.deleteIfExists(baseDir.resolve("dir2"));
90 } catch (IOException e) {
94 Files.deleteIfExists(baseDir);
95 } catch (IOException e) {
104 public void consumeData() throws IOException {
105 try(OutputStream os = Files.newOutputStream(file1)) {
106 IOUtils.write("abcdefghijkl", os, "ASCII");
108 StringResult result = new StringResult();
109 fsStorage.consumeData(file1Asset, is -> consume(is, result), false );
110 assertEquals("abcdefghijkl" ,result.getData());
113 private void consume(InputStream is, StringResult result) {
115 result.setData(IOUtils.toString(is, "ASCII"));
116 } catch (IOException e) {
123 public void getAsset() {
124 StorageAsset asset = fsStorage.getAsset("/dir1/testfile1.dat");
125 assertEquals(file1, asset.getFilePath());
129 public void addAsset() {
130 StorageAsset newAsset = fsStorage.addAsset("dir2/test", false);
131 assertNotNull(newAsset);
132 assertFalse(newAsset.isContainer());
133 assertFalse(newAsset.exists());
135 StorageAsset newDirAsset = fsStorage.addAsset("/dir2/testdir2", true);
136 assertNotNull(newDirAsset);
137 assertTrue(newDirAsset.isContainer());
138 assertFalse(newDirAsset.exists());
142 public void removeAsset() throws IOException {
143 assertTrue(Files.exists(file1));
144 fsStorage.removeAsset(file1Asset);
145 assertFalse(Files.exists(file1));
147 assertTrue(Files.exists(dir1));
148 fsStorage.removeAsset(dir1Asset);
149 assertFalse(Files.exists(dir1));
153 public void moveAsset() throws IOException {
157 assertTrue(Files.exists(file1));
158 try (OutputStream os = Files.newOutputStream(file1)) {
159 IOUtils.write("testakdkkdkdkdk", os, "ASCII");
161 long fileSize = Files.size(file1);
162 fsStorage.moveAsset(file1Asset, "/dir2/testfile2.dat");
163 assertFalse(Files.exists(file1));
164 newFile = baseDir.resolve("dir2/testfile2.dat");
165 assertTrue(Files.exists(newFile));
166 assertEquals(fileSize, Files.size(newFile));
169 assertTrue(Files.exists(dir1));
170 newDir = baseDir.resolve("dir2/testdir2");
171 fsStorage.moveAsset(dir1Asset, "dir2/testdir2");
172 assertFalse(Files.exists(dir1));
173 assertTrue(Files.exists(newDir));
175 if (newFile!=null) Files.deleteIfExists(newFile);
176 if (newDir!=null) Files.deleteIfExists(newDir);
181 public void copyAsset() throws IOException {
185 assertTrue(Files.exists(file1));
186 try (OutputStream os = Files.newOutputStream(file1)) {
187 IOUtils.write("testakdkkdkdkdk", os, "ASCII");
189 long fileSize = Files.size(file1);
190 fsStorage.copyAsset(file1Asset, "/dir2/testfile2.dat");
191 assertTrue(Files.exists(file1));
192 assertEquals(fileSize, Files.size(file1));
193 newFile = baseDir.resolve("dir2/testfile2.dat");
194 assertTrue(Files.exists(newFile));
195 assertEquals(fileSize, Files.size(newFile));
198 assertTrue(Files.exists(dir1));
199 newDir = baseDir.resolve("dir2/testdir2");
200 fsStorage.copyAsset(dir1Asset, "dir2/testdir2");
201 assertTrue(Files.exists(dir1));
202 assertTrue(Files.exists(newDir));
204 if (newFile!=null) Files.deleteIfExists(newFile);
205 if (newDir!=null) Files.deleteIfExists(newDir);