import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
-import java.io.File;
import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
@Inject
FileTypes filetypes;
- public static File getTestFile( String path )
+ public static Path getTestFile( String path )
{
- return new File( FileUtil.getBasedir(), path );
+ return Paths.get( FileUtil.getBasedir(), path );
}
protected <T> T lookup( Class<T> clazz, String hint )
public void testStoreConfiguration()
throws Exception
{
- File file = getTestFile( "target/test/test-file.xml" );
- file.delete();
- assertFalse( file.exists() );
+ Path file = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists(file);
+ assertFalse( Files.exists(file) );
// TODO: remove with commons-configuration 1.4
//file.getParentFile().mkdirs();
verify( listener );
- assertTrue( "Check file exists", file.exists() );
+ assertTrue( "Check file exists", Files.exists(file) );
// check it
configuration = archivaConfiguration.getConfiguration();
public void testStoreConfigurationUser()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists( baseFile );
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists( userFile );
+ assertFalse( Files.exists(userFile) );
- userFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.forName( "UTF-8" ) );
+ Files.createDirectories(userFile.getParent());
+ FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
archivaConfiguration.save( configuration );
- assertTrue( "Check file exists", userFile.exists() );
- assertFalse( "Check file not created", baseFile.exists() );
+ assertTrue( "Check file exists", Files.exists(userFile) );
+ assertFalse( "Check file not created", Files.exists(baseFile) );
// check it
configuration = archivaConfiguration.getConfiguration();
public void testStoreConfigurationLoadedFromDefaults()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.delete(baseFile);
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user-defaults" );
verify( listener );
- assertTrue( "Check file exists", userFile.exists() );
- assertFalse( "Check file not created", baseFile.exists() );
+ assertTrue( "Check file exists", Files.exists(userFile) );
+ assertFalse( "Check file not created", Files.exists(baseFile) );
// check it
configuration = archivaConfiguration.getConfiguration();
public void testStoreConfigurationFallback()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists(baseFile);
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
- baseFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( baseFile, "<configuration/>", Charset.forName( "UTF-8" ) );
+ Files.createDirectories( baseFile.getParent());
+ FileUtils.writeStringToFile( baseFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
ArchivaConfiguration archivaConfiguration =
(ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-fallback" );
archivaConfiguration.save( configuration );
- assertTrue( "Check file exists", baseFile.exists() );
- assertFalse( "Check file not created", userFile.exists() );
+ assertTrue( "Check file exists", Files.exists(baseFile) );
+ assertFalse( "Check file not created", Files.exists(userFile) );
// check it
configuration = archivaConfiguration.getConfiguration();
public void testStoreConfigurationFailsWhenReadFromBothLocationsNoLists()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists(baseFile);
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
- baseFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( baseFile, "<configuration/>", Charset.forName( "UTF-8" ) );
+ Files.createDirectories( baseFile.getParent() );
+ FileUtils.writeStringToFile( baseFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
- userFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.forName( "UTF-8" ) );
+ Files.createDirectories( userFile.getParent());
+ FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
archivaConfiguration.save( configuration );
- assertTrue( "Check file exists", baseFile.exists() );
+ assertTrue( "Check file exists", Files.exists(baseFile) );
assertEquals( "Check base file is unchanged", "<configuration/>",
- FileUtils.readFileToString( baseFile, Charset.forName( "UTF-8" ) ) );
- assertTrue( "Check file exists", userFile.exists() );
+ FileUtils.readFileToString( baseFile.toFile(), Charset.forName( "UTF-8" ) ) );
+ assertTrue( "Check file exists", Files.exists(userFile) );
assertFalse( "Check base file is changed",
- "<configuration/>".equals( FileUtils.readFileToString( userFile, Charset.forName( "UTF-8" ) ) ) );
+ "<configuration/>".equals( FileUtils.readFileToString( userFile.toFile(), Charset.forName( "UTF-8" ) ) ) );
// check it
configuration = archivaConfiguration.getConfiguration();
public void testStoreConfigurationFailsWhenReadFromBothLocationsUserHasLists()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists(baseFile);
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
- userFile.getParentFile().mkdirs();
- FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ), userFile );
+ Files.createDirectories( userFile.getParent() );
+ FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ).toFile(), userFile.toFile() );
- baseFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( baseFile, "<configuration/>", Charset.forName( "UTF-8" ) );
+ Files.createDirectories(baseFile.getParent());
+ FileUtils.writeStringToFile( baseFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
archivaConfiguration.save( configuration );
- assertTrue( "Check file exists", baseFile.exists() );
+ assertTrue( "Check file exists", Files.exists(baseFile) );
assertEquals( "Check base file is unchanged", "<configuration/>",
- FileUtils.readFileToString( baseFile, Charset.forName( "UTF-8" ) ) );
- assertTrue( "Check file exists", userFile.exists() );
+ FileUtils.readFileToString( baseFile.toFile(), Charset.forName( "UTF-8" ) ) );
+ assertTrue( "Check file exists", Files.exists(userFile) );
assertFalse( "Check base file is changed",
- "<configuration/>".equals( FileUtils.readFileToString( userFile, Charset.forName( "UTF-8" ) ) ) );
+ "<configuration/>".equals( FileUtils.readFileToString( userFile.toFile(), Charset.forName( "UTF-8" ) ) ) );
// check it
configuration = archivaConfiguration.getConfiguration();
public void testStoreConfigurationFailsWhenReadFromBothLocationsAppserverHasLists()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists(baseFile);
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
- baseFile.getParentFile().mkdirs();
- FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ), baseFile );
+ Files.createDirectories(baseFile.getParent());
+ FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ).toFile(), baseFile.toFile() );
- userFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.forName( "UTF-8" ) );
+ Files.createDirectories(userFile.getParent());
+ FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
public void testLoadConfigurationFromInvalidUserLocationOnDisk()
throws Exception
{
- File testConfDir = getTestFile( "target/test-appserver-base/conf/" );
- testConfDir.mkdirs();
+ Path testConfDir = getTestFile( "target/test-appserver-base/conf/" );
+ Files.createDirectories( testConfDir );
ArchivaConfiguration archivaConfiguration =
lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-user" );
throws Exception
{
// Setup the autodetect-v1.xml file in the target directory (so we can save/load it)
- File userFile = getTestFile( "target/test-autodetect-v1/archiva-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test-autodetect-v1/archiva-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
- userFile.getParentFile().mkdirs();
- FileUtils.copyFile( getTestFile( "src/test/conf/autodetect-v1.xml" ), userFile );
+ Files.createDirectories(userFile.getParent());
+ FileUtils.copyFile( getTestFile( "src/test/conf/autodetect-v1.xml" ).toFile(), userFile.toFile());
// Load the original (unconverted) archiva.xml
ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositoriesAsMap().size() );
assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
- String actualXML = FileUtils.readFileToString( userFile, Charset.forName( "UTF-8" ) );
+ String actualXML = FileUtils.readFileToString( userFile.toFile(), Charset.forName( "UTF-8" ) );
XMLAssert.assertXpathNotExists( "//configuration/repositories/repository", actualXML );
XMLAssert.assertXpathNotExists( "//configuration/repositories", actualXML );
}
public void testCronExpressionsWithComma()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists(baseFile);
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
- baseFile.getParentFile().mkdirs();
- FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ), baseFile );
+ Files.createDirectories(baseFile.getParent());
+ FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ).toFile(), baseFile.toFile() );
- userFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.defaultCharset() );
+ Files.createDirectories(userFile.getParent());
+ FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.defaultCharset() );
final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
public void testRemoveLastElements()
throws Exception
{
- File baseFile = getTestFile( "target/test/test-file.xml" );
- baseFile.delete();
- assertFalse( baseFile.exists() );
+ Path baseFile = getTestFile( "target/test/test-file.xml" );
+ Files.deleteIfExists(baseFile);
+ assertFalse( Files.exists(baseFile) );
- File userFile = getTestFile( "target/test/test-file-user.xml" );
- userFile.delete();
- assertFalse( userFile.exists() );
+ Path userFile = getTestFile( "target/test/test-file-user.xml" );
+ Files.deleteIfExists(userFile);
+ assertFalse( Files.exists(userFile) );
- baseFile.getParentFile().mkdirs();
- FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ), baseFile );
+ Files.createDirectories( baseFile.getParent() );
+ FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ).toFile(), baseFile.toFile() );
- userFile.getParentFile().mkdirs();
- FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.forName( "UTF-8" ) );
+ Files.createDirectories( userFile.getParent());
+ FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-remove-central" );