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 24KB

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