123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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.common.filelock.DefaultFileLockManager;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import org.junit.After;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.time.Instant;
-
- public class FilesystemAssetTest {
-
- Path assetPathFile;
- Path assetPathDir;
- FilesystemStorage filesystemStorage;
-
- @Before
- public void init() throws IOException {
- assetPathDir = Files.createTempDirectory("assetDir");
- assetPathFile = Files.createTempFile(assetPathDir,"assetFile", "dat");
- filesystemStorage = new FilesystemStorage(assetPathDir, new DefaultFileLockManager());
- }
-
- @After
- public void cleanup() {
-
- try {
- Files.deleteIfExists(assetPathFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- FileUtils.deleteQuietly(assetPathDir.toFile());
- }
-
-
- @Test
- public void getPath() {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, assetPathFile.getFileName().toString(), assetPathFile);
- Assert.assertEquals("/"+assetPathFile.getFileName().toString(), asset.getPath());
- }
-
- @Test
- public void getName() {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/"+assetPathFile.getFileName().toString(), assetPathFile);
- Assert.assertEquals(assetPathFile.getFileName().toString(), asset.getName());
-
- }
-
- @Test
- public void getModificationTime() throws IOException {
- Instant modTime = Files.getLastModifiedTime(assetPathFile).toInstant();
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test123", assetPathFile);
- Assert.assertTrue(modTime.equals(asset.getModificationTime()));
- }
-
- @Test
- public void isContainer() {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1323", assetPathFile);
- Assert.assertFalse(asset.isContainer());
- FilesystemAsset asset2 = new FilesystemAsset(filesystemStorage, "/test1234", assetPathDir);
- Assert.assertTrue(asset2.isContainer());
- }
-
- @Test
- public void list() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Assert.assertEquals(0, asset.list().size());
-
- FilesystemAsset asset2 = new FilesystemAsset(filesystemStorage, "/test1235", assetPathDir);
- Assert.assertEquals(1, asset2.list().size());
- Path f1 = Files.createTempFile(assetPathDir, "testfile", "dat");
- Path f2 = Files.createTempFile(assetPathDir, "testfile", "dat");
- Path d1 = Files.createTempDirectory(assetPathDir, "testdir");
- Assert.assertEquals(4, asset2.list().size());
- Assert.assertTrue(asset2.list().stream().anyMatch(p -> p.getName().equals(f1.getFileName().toString())));
- Assert.assertTrue(asset2.list().stream().anyMatch(p -> p.getName().equals(f2.getFileName().toString())));
- Assert.assertTrue(asset2.list().stream().anyMatch(p -> p.getName().equals(d1.getFileName().toString())));
- Files.deleteIfExists(f1);
- Files.deleteIfExists(f2);
- Files.deleteIfExists(d1);
-
-
- }
-
- @Test
- public void getSize() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Assert.assertEquals(0, asset.getSize());
-
- Files.write(assetPathFile, new String("abcdef").getBytes("ASCII"));
- Assert.assertTrue(asset.getSize()>=6);
-
-
- }
-
- @Test
- public void getData() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
- try(InputStream is = asset.getReadStream()) {
- Assert.assertEquals("abcdef", IOUtils.toString(is, "ASCII"));
- }
-
- }
-
- @Test
- public void getDataExceptionOnDir() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathDir);
- Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
- try {
- InputStream is = asset.getReadStream();
- Assert.assertFalse("Exception expected for data on dir", true);
- } catch (IOException e) {
- // fine
- }
-
- }
-
- @Test
- public void writeData() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
- try(OutputStream os = asset.getWriteStream(true)) {
- IOUtils.write("test12345", os, "ASCII");
- }
- Assert.assertEquals("test12345", IOUtils.toString(assetPathFile.toUri().toURL(), "ASCII"));
- }
-
- @Test
- public void writeDataAppend() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
- try(OutputStream os = asset.getWriteStream(false)) {
- IOUtils.write("test12345", os, "ASCII");
- }
- Assert.assertEquals("abcdeftest12345", IOUtils.toString(assetPathFile.toUri().toURL(), "ASCII"));
- }
-
- @Test
- public void writeDataExceptionOnDir() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathDir);
- try {
-
- OutputStream os = asset.getWriteStream(true);
- Assert.assertTrue("Writing to a directory should throw a IOException", false);
- } catch (IOException e) {
- // Fine
- }
- }
-
- @Test
- public void storeDataFile() throws IOException {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Path dataFile = Files.createTempFile("testdata", "dat");
- try(OutputStream os = Files.newOutputStream(dataFile)) {
- IOUtils.write("testkdkdkd", os, "ASCII");
- }
- asset.replaceDataFromFile(dataFile);
- Assert.assertEquals("testkdkdkd", IOUtils.toString(assetPathFile.toUri().toURL(), "ASCII"));
- }
-
- @Test
- public void exists() {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Assert.assertTrue(asset.exists());
- FilesystemAsset asset2 = new FilesystemAsset(filesystemStorage, "/test1234", Paths.get("abcdefgkdkdk"));
- Assert.assertFalse(asset2.exists());
-
- }
-
- @Test
- public void getFilePath() {
- FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
- Assert.assertEquals(assetPathFile, asset.getFilePath());
- }
- }
|