You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractProxyTestCase.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. package org.apache.archiva.proxy;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import net.sf.ehcache.CacheManager;
  21. import org.apache.archiva.configuration.ArchivaConfiguration;
  22. import org.apache.archiva.configuration.ArchivaRuntimeConfiguration;
  23. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  24. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  25. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  26. import org.apache.archiva.policies.CachedFailuresPolicy;
  27. import org.apache.archiva.policies.ChecksumPolicy;
  28. import org.apache.archiva.policies.PolicyOption;
  29. import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
  30. import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
  31. import org.apache.archiva.policies.ReleasesPolicy;
  32. import org.apache.archiva.policies.SnapshotsPolicy;
  33. import org.apache.archiva.proxy.model.RepositoryProxyHandler;
  34. import org.apache.archiva.repository.ManagedRepository;
  35. import org.apache.archiva.repository.ManagedRepositoryContent;
  36. import org.apache.archiva.repository.RepositoryRegistry;
  37. import org.apache.archiva.repository.RepositoryType;
  38. import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
  39. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  40. import org.apache.archiva.repository.storage.StorageAsset;
  41. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  42. import org.apache.maven.wagon.Wagon;
  43. import org.easymock.EasyMock;
  44. import org.easymock.IMocksControl;
  45. import org.junit.Before;
  46. import org.junit.runner.RunWith;
  47. import org.slf4j.Logger;
  48. import org.slf4j.LoggerFactory;
  49. import org.springframework.context.ApplicationContext;
  50. import org.springframework.test.context.ContextConfiguration;
  51. import javax.inject.Inject;
  52. import java.io.BufferedReader;
  53. import java.io.FileReader;
  54. import java.io.IOException;
  55. import java.nio.charset.Charset;
  56. import java.nio.file.Files;
  57. import java.nio.file.Path;
  58. import java.nio.file.Paths;
  59. import java.nio.file.attribute.FileTime;
  60. import java.text.ParseException;
  61. import java.text.SimpleDateFormat;
  62. import java.util.ArrayList;
  63. import java.util.Calendar;
  64. import java.util.Collection;
  65. import java.util.Date;
  66. import java.util.List;
  67. import java.util.Locale;
  68. import java.util.concurrent.TimeUnit;
  69. import java.util.stream.Collectors;
  70. import static org.junit.Assert.*;
  71. /**
  72. * AbstractProxyTestCase
  73. */
  74. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  75. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  76. public abstract class AbstractProxyTestCase
  77. {
  78. @Inject
  79. protected ApplicationContext applicationContext;
  80. @Inject
  81. private ProxyRegistry proxyRegistry;
  82. @Inject
  83. RepositoryRegistry repositoryRegistry;
  84. @SuppressWarnings( "unused" )
  85. @Inject
  86. RepositoryHandlerDependencies repositoryHandlerDependencies;
  87. protected static final String ID_PROXIED1 = "proxied1";
  88. protected static final String ID_PROXIED1_TARGET = "proxied1-target";
  89. protected static final String ID_PROXIED2 = "proxied2";
  90. protected static final String ID_PROXIED2_TARGET = "proxied2-target";
  91. protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
  92. protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
  93. protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
  94. protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
  95. protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
  96. protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
  97. // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
  98. protected IMocksControl wagonMockControl;
  99. protected Wagon wagonMock;
  100. protected RepositoryProxyHandler proxyHandler;
  101. protected ManagedRepositoryContent managedDefaultRepository;
  102. protected Path managedDefaultDir;
  103. protected MockConfiguration config;
  104. protected Logger log = LoggerFactory.getLogger( getClass() );
  105. WagonDelegate delegate;
  106. @Before
  107. public void setUp()
  108. throws Exception
  109. {
  110. config =
  111. (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
  112. config.getConfiguration().setManagedRepositories( new ArrayList<ManagedRepositoryConfiguration>() );
  113. config.getConfiguration().setRemoteRepositories( new ArrayList<RemoteRepositoryConfiguration>() );
  114. config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
  115. ArchivaRuntimeConfiguration runtimeConfiguration = new ArchivaRuntimeConfiguration();
  116. List<String> checksumTypes = new ArrayList<>();
  117. checksumTypes.add("md5");
  118. checksumTypes.add("sha256");
  119. checksumTypes.add("sha1");
  120. checksumTypes.add("asc");
  121. runtimeConfiguration.setChecksumTypes(checksumTypes);
  122. config.getConfiguration().setArchivaRuntimeConfiguration(runtimeConfiguration);
  123. repositoryRegistry.setArchivaConfiguration( config );
  124. // Setup source repository (using default layout)
  125. String name = getClass().getSimpleName();
  126. Path repoPath = Paths.get("target/test-repository/managed/" + name);
  127. managedDefaultRepository =
  128. createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath.toString(), "default" );
  129. managedDefaultDir = repoPath.resolve(ID_DEFAULT_MANAGED) ;
  130. org.apache.archiva.repository.ManagedRepository repoConfig = repositoryRegistry.getManagedRepository(ID_DEFAULT_MANAGED);
  131. // Setup target (proxied to) repository.
  132. saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
  133. Paths.get( REPOPATH_PROXIED1 ).toUri().toURL().toExternalForm(), "default" );
  134. // Setup target (proxied to) repository.
  135. saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
  136. Paths.get( REPOPATH_PROXIED2 ).toUri().toURL().toExternalForm(), "default" );
  137. repositoryRegistry.reload();
  138. repositoryRegistry.putRepository(repoConfig);
  139. // Setup the proxy handler.
  140. //proxyHandler = applicationContext.getBean (RepositoryProxyHandler) lookup( RepositoryProxyHandler.class.getName() );
  141. proxyHandler = applicationContext.getBean( "repositoryProxyHandler#test", RepositoryProxyHandler.class );
  142. assertNotNull( proxyRegistry );
  143. assertTrue(proxyRegistry.getAllHandler( ).get( RepositoryType.MAVEN).contains( proxyHandler ));
  144. // Setup the wagon mock.
  145. wagonMockControl = EasyMock.createNiceControl();
  146. wagonMock = wagonMockControl.createMock( Wagon.class );
  147. delegate = (WagonDelegate) applicationContext.getBean( "wagon#http", Wagon.class );
  148. delegate.setDelegate( wagonMock );
  149. CacheManager.getInstance().clearAll();
  150. log.info( "\n.\\ {}() \\._________________________________________\n", name );
  151. }
  152. protected void assertChecksums( Path expectedFile, String expectedSha1Contents, String expectedMd5Contents )
  153. throws Exception
  154. {
  155. Path sha1File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString()+ ".sha1" );
  156. Path md5File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString() + ".md5" );
  157. if ( expectedSha1Contents == null )
  158. {
  159. assertFalse( "SHA1 File should NOT exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
  160. }
  161. else
  162. {
  163. assertTrue( "SHA1 File should exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
  164. String actualSha1Contents = readChecksumFile( sha1File );
  165. assertEquals( "SHA1 File contents: " + sha1File.toAbsolutePath(), expectedSha1Contents, actualSha1Contents );
  166. }
  167. if ( expectedMd5Contents == null )
  168. {
  169. assertFalse( "MD5 File should NOT exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
  170. }
  171. else
  172. {
  173. assertTrue( "MD5 File should exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
  174. String actualMd5Contents = readChecksumFile( md5File );
  175. assertEquals( "MD5 File contents: " + md5File.toAbsolutePath(), expectedMd5Contents, actualMd5Contents );
  176. }
  177. }
  178. protected void assertFileEquals( Path expectedFile, Path actualFile, Path sourceFile )
  179. throws Exception
  180. {
  181. assertNotNull( "Expected File should not be null.", expectedFile );
  182. assertNotNull( "Actual File should not be null.", actualFile );
  183. assertTrue( "Check actual file exists.", Files.exists(actualFile) );
  184. assertTrue("Check expected file exists", Files.exists(expectedFile));
  185. assertTrue( "Check file is the same.", Files.isSameFile( expectedFile,
  186. actualFile));
  187. String expectedContents =
  188. org.apache.commons.io.FileUtils.readFileToString( sourceFile.toFile(), Charset.defaultCharset() );
  189. String actualContents =
  190. org.apache.commons.io.FileUtils.readFileToString( actualFile.toFile(), Charset.defaultCharset() );
  191. assertEquals( "Check file contents.", expectedContents, actualContents );
  192. }
  193. protected void assertNotDownloaded( StorageAsset downloadedFile )
  194. {
  195. assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
  196. }
  197. @SuppressWarnings( "unchecked" )
  198. protected void assertNoTempFiles( Path expectedFile )
  199. {
  200. Path workingDir = expectedFile.getParent();
  201. if ( ( workingDir == null ) || !Files.isDirectory( workingDir) )
  202. {
  203. return;
  204. }
  205. Collection<Path> tmpFiles = null;
  206. try {
  207. tmpFiles = Files.list(workingDir).filter(path -> Files.isRegularFile(path) && path.getFileName().toString().endsWith(".tmp")).collect(Collectors.toList());
  208. } catch (IOException e) {
  209. log.error("Could not retrieve tmpFiles {}", workingDir);
  210. }
  211. if ( tmpFiles!=null && !tmpFiles.isEmpty() )
  212. {
  213. StringBuilder emsg = new StringBuilder();
  214. emsg.append( "Found Temp Files in dir: " ).append( workingDir.toString() );
  215. for ( Path tfile : tmpFiles )
  216. {
  217. emsg.append( "\n " ).append( tfile.getFileName().toString());
  218. }
  219. fail( emsg.toString() );
  220. }
  221. }
  222. /**
  223. * A faster recursive copy that omits .svn directories.
  224. *
  225. * @param sourceDirectory the source directory to copy
  226. * @param destDirectory the target location
  227. * @throws java.io.IOException if there is a copying problem
  228. * @todo get back into plexus-utils, share with converter module
  229. */
  230. protected void copyDirectoryStructure( Path sourceDirectory, Path destDirectory )
  231. throws IOException
  232. {
  233. if ( !Files.exists(sourceDirectory) )
  234. {
  235. throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
  236. }
  237. Path[] files = Files.list(sourceDirectory).filter(path -> Files.isRegularFile(path)).toArray(Path[]::new);
  238. String sourcePath = sourceDirectory.toAbsolutePath().toString();
  239. for ( int i = 0; i < files.length; i++ )
  240. {
  241. Path file = files[i];
  242. String dest = file.toAbsolutePath().toString();
  243. dest = dest.substring( sourcePath.length() + 1 );
  244. Path destination = destDirectory.resolve( dest );
  245. if ( Files.isRegularFile(file) )
  246. {
  247. destination = destination.getParent();
  248. org.apache.commons.io.FileUtils.copyFile( file.toFile(), destination.resolve( file.getFileName() ).toFile(), false );
  249. // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
  250. //FileUtils.copyFileToDirectory( file, destination );
  251. }
  252. else if ( Files.isDirectory(file) )
  253. {
  254. if ( !".svn".equals( file.getFileName().toString() ) )
  255. {
  256. if ( !Files.exists(destination))
  257. {
  258. Files.createDirectories(destination);
  259. }
  260. copyDirectoryStructure( file, destination );
  261. }
  262. }
  263. else
  264. {
  265. throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
  266. }
  267. }
  268. }
  269. protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
  270. throws Exception
  271. {
  272. ManagedRepository repo = BasicManagedRepository.newFilesystemInstance(id, name, Paths.get(path).resolve(id));
  273. repositoryRegistry.putRepository(repo);
  274. return repositoryRegistry.getManagedRepository(id).getContent();
  275. }
  276. /**
  277. * Read the first line from the checksum file, and return it (trimmed).
  278. */
  279. protected String readChecksumFile( Path checksumFile )
  280. throws Exception
  281. {
  282. FileReader freader = null;
  283. BufferedReader buf = null;
  284. try
  285. {
  286. freader = new FileReader( checksumFile.toFile() );
  287. buf = new BufferedReader( freader );
  288. return buf.readLine();
  289. }
  290. finally
  291. {
  292. if ( buf != null )
  293. {
  294. buf.close();
  295. }
  296. if ( freader != null )
  297. {
  298. freader.close();
  299. }
  300. }
  301. }
  302. protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
  303. {
  304. saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
  305. CachedFailuresPolicy.NO, disabled );
  306. }
  307. protected void saveConnector( String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
  308. PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, boolean disabled )
  309. {
  310. saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
  311. PropagateErrorsDownloadPolicy.QUEUE, disabled );
  312. }
  313. protected void saveConnector( String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
  314. PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, PolicyOption errorPolicy,
  315. boolean disabled )
  316. {
  317. saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
  318. errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
  319. }
  320. protected void saveConnector(String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
  321. PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, PolicyOption errorPolicy,
  322. PolicyOption errorOnUpdatePolicy, boolean disabled )
  323. {
  324. ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
  325. connectorConfig.setSourceRepoId( sourceRepoId );
  326. connectorConfig.setTargetRepoId( targetRepoId );
  327. connectorConfig.setProxyId(sourceRepoId);
  328. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy.getId() );
  329. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy.getId() );
  330. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy.getId() );
  331. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy.getId());
  332. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy.getId() );
  333. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy.getId() );
  334. connectorConfig.setDisabled( disabled );
  335. int count = config.getConfiguration().getProxyConnectors().size();
  336. config.getConfiguration().addProxyConnector( connectorConfig );
  337. // Proper Triggering ...
  338. String prefix = "proxyConnectors.proxyConnector(" + count + ")";
  339. config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
  340. config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
  341. config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
  342. config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
  343. config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
  344. config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
  345. config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
  346. config.triggerChange( prefix + ".policies.propagate-errors",
  347. connectorConfig.getPolicy( "propagate-errors", "" ) );
  348. config.triggerChange( prefix + ".policies.propagate-errors-on-update",
  349. connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
  350. }
  351. protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
  352. {
  353. ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
  354. repoConfig.setId( id );
  355. repoConfig.setName( name );
  356. repoConfig.setLayout( layout );
  357. repoConfig.setLocation( path );
  358. int count = config.getConfiguration().getManagedRepositories().size();
  359. config.getConfiguration().addManagedRepository( repoConfig );
  360. String prefix = "managedRepositories.managedRepository(" + count + ")";
  361. config.triggerChange( prefix + ".id", repoConfig.getId() );
  362. config.triggerChange( prefix + ".name", repoConfig.getName() );
  363. config.triggerChange( prefix + ".location", repoConfig.getLocation() );
  364. config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
  365. }
  366. protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
  367. {
  368. RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
  369. repoConfig.setId( id );
  370. repoConfig.setName( name );
  371. repoConfig.setLayout( layout );
  372. repoConfig.setUrl( url );
  373. int count = config.getConfiguration().getRemoteRepositories().size();
  374. config.getConfiguration().addRemoteRepository( repoConfig );
  375. String prefix = "remoteRepositories.remoteRepository(" + count + ")";
  376. config.triggerChange( prefix + ".id", repoConfig.getId() );
  377. config.triggerChange( prefix + ".name", repoConfig.getName() );
  378. config.triggerChange( prefix + ".url", repoConfig.getUrl() );
  379. config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
  380. repositoryRegistry.reload();
  381. }
  382. protected Path saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
  383. throws IOException
  384. {
  385. Path repoLocation = Paths.get( targetPath );
  386. org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoLocation );
  387. copyDirectoryStructure( Paths.get(originalPath) , repoLocation );
  388. saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
  389. return repoLocation;
  390. }
  391. /**
  392. * Copy the specified resource directory from the src/test/repository/managed/ to
  393. * the testable directory under target/test-repository/managed/${testName}/
  394. *
  395. * @param resourcePath
  396. * @throws IOException
  397. */
  398. protected void setupTestableManagedRepository( String resourcePath )
  399. throws IOException
  400. {
  401. String resourceDir = resourcePath;
  402. if ( !resourcePath.endsWith( "/" ) )
  403. {
  404. int idx = resourcePath.lastIndexOf( '/' );
  405. resourceDir = resourcePath.substring( 0, idx );
  406. }
  407. Path sourceRepoDir = Paths.get( REPOPATH_DEFAULT_MANAGED );
  408. Path sourceDir = sourceRepoDir.resolve(resourceDir );
  409. Path destRepoDir = managedDefaultDir;
  410. Path destDir = destRepoDir.resolve(resourceDir );
  411. // Cleanout destination dirs.
  412. if ( Files.exists(destDir))
  413. {
  414. org.apache.archiva.common.utils.FileUtils.deleteDirectory( destDir );
  415. }
  416. // Make the destination dir.
  417. Files.createDirectories(destDir);
  418. // Test the source dir.
  419. if ( !Files.exists(sourceDir) )
  420. {
  421. // This is just a warning.
  422. log.error( "[WARN] Skipping setup of testable managed repository, source dir does not exist: {}", //
  423. sourceDir );
  424. }
  425. else
  426. {
  427. // Test that the source is a dir.
  428. if ( !Files.isDirectory( sourceDir) )
  429. {
  430. fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
  431. }
  432. // Copy directory structure.
  433. copyDirectoryStructure( sourceDir, destDir );
  434. }
  435. }
  436. protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile )
  437. {
  438. setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
  439. }
  440. protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile, long time )
  441. {
  442. assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
  443. assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
  444. try
  445. {
  446. Files.setLastModifiedTime( managedFile,
  447. FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() + time, TimeUnit.MILLISECONDS ));
  448. }
  449. catch ( IOException e )
  450. {
  451. e.printStackTrace( );
  452. }
  453. try
  454. {
  455. assertTrue( Files.getLastModifiedTime( managedFile).compareTo( Files.getLastModifiedTime( remoteFile )) > 0);
  456. }
  457. catch ( IOException e )
  458. {
  459. e.printStackTrace( );
  460. }
  461. }
  462. protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile )
  463. {
  464. setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
  465. }
  466. protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile, long time )
  467. {
  468. assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
  469. assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
  470. try
  471. {
  472. Files.setLastModifiedTime( managedFile,
  473. FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() - time, TimeUnit.MILLISECONDS ));
  474. }
  475. catch ( IOException e )
  476. {
  477. e.printStackTrace( );
  478. }
  479. try
  480. {
  481. assertTrue( Files.getLastModifiedTime( managedFile ).compareTo(Files.getLastModifiedTime( remoteFile )) < 0 );
  482. }
  483. catch ( IOException e )
  484. {
  485. e.printStackTrace( );
  486. }
  487. }
  488. protected void assertNotModified( Path file, long expectedModificationTime )
  489. {
  490. try
  491. {
  492. assertEquals( "File <" + file.toAbsolutePath() + "> not have been modified.", expectedModificationTime,
  493. Files.getLastModifiedTime( file ).toMillis());
  494. }
  495. catch ( IOException e )
  496. {
  497. e.printStackTrace( );
  498. }
  499. }
  500. protected void assertNotExistsInManagedDefaultRepo( Path testFile )
  501. throws Exception
  502. {
  503. Path managedDefaultPath = managedDefaultDir;
  504. assertTrue( "Unit Test Failure: File <" + testFile
  505. + "> should be have been defined within the managed default path of <" + managedDefaultPath
  506. + ">", testFile.startsWith( managedDefaultPath ) );
  507. assertFalse( "File < " + testFile + "> should not exist in managed default repository.", Files.exists(testFile) );
  508. }
  509. protected static Date getFutureDate()
  510. throws ParseException
  511. {
  512. Calendar cal = Calendar.getInstance();
  513. cal.add( Calendar.YEAR, 1 );
  514. return cal.getTime();
  515. }
  516. protected static Date getPastDate()
  517. throws ParseException
  518. {
  519. return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );
  520. }
  521. }