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

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