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

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