Package org.sonatype.nexus.proxy.maven.maven2

Examples of org.sonatype.nexus.proxy.maven.maven2.M2Repository


  @Test
  public void testPoliciesWithStore()
      throws Exception
  {
    M2Repository repository = (M2Repository) getResourceStore();

    // a "release"
    repository.setRepositoryPolicy(RepositoryPolicy.RELEASE);
    repository.getCurrentCoreConfiguration().commitChanges();

    DefaultStorageFileItem item = new DefaultStorageFileItem(
        repository, new ResourceStoreRequest(SPOOF_RELEASE), true, true, new StringContentLocator(SPOOF_RELEASE)
    );

    repository.storeItem(false, item);

    try {
      item = new DefaultStorageFileItem(
          repository, new ResourceStoreRequest(SPOOF_SNAPSHOT), true, true, new StringContentLocator(SPOOF_SNAPSHOT)
      );

      repository.storeItem(false, item);

      assertThat("Should not be able to store snapshot to release repo", false);
    }
    catch (UnsupportedStorageOperationException e) {
      // good
    }

    // reset NFC
    repository.expireCaches(new ResourceStoreRequest(RepositoryItemUid.PATH_ROOT, true));

    // a "snapshot"
    repository.setRepositoryPolicy(RepositoryPolicy.SNAPSHOT);
    repository.getCurrentCoreConfiguration().commitChanges();

    item = new DefaultStorageFileItem(
        repository, new ResourceStoreRequest(SPOOF_SNAPSHOT), true, true, new StringContentLocator(SPOOF_SNAPSHOT)
    );

    repository.storeItem(false, item);

    try {
      item = new DefaultStorageFileItem(
          repository, new ResourceStoreRequest(SPOOF_RELEASE), true, true, new StringContentLocator(SPOOF_RELEASE)
      );

      repository.storeItem(false, item);

      assertThat("Should not be able to store release to snapshot repo", false);
    }
    catch (UnsupportedStorageOperationException e) {
      // good
View Full Code Here


  @Test
  public void testShouldServeByPolicies()
      throws Exception
  {
    M2Repository repository = (M2Repository) getResourceStore();

    String releasePom =
        "/org/codehaus/plexus/plexus-container-default/1.0-alpha-40/plexus-container-default-1.0-alpha-40.pom";
    String releaseArtifact =
        "/org/codehaus/plexus/plexus-container-default/1.0-alpha-40/plexus-container-default-1.0-alpha-40.jar";
    String snapshotPom =
        "/org/codehaus/plexus/plexus-container-default/1.0-alpha-41-SNAPSHOT/plexus-container-default-1.0-alpha-41-20071205.190351-1.pom";
    String snapshotArtifact =
        "/org/codehaus/plexus/plexus-container-default/1.0-alpha-41-SNAPSHOT/plexus-container-default-1.0-alpha-41-20071205.190351-1.jar";
    String metadata1 = "/org/codehaus/plexus/plexus-container-default/maven-metadata.xml";
    String metadataR = "/org/codehaus/plexus/plexus-container-default/1.0-alpha-40/maven-metadata.xml";
    String metadataS = "/org/codehaus/plexus/plexus-container-default/1.0-alpha-41-SNAPSHOT/maven-metadata.xml";
    String someDirectory = "/classworlds/";
    String anyNonArtifactFile = "/any/file.txt";

    ResourceStoreRequest request = new ResourceStoreRequest("");

    // it is equiv of repo type: RELEASE
    repository.setRepositoryPolicy(RepositoryPolicy.RELEASE);
    repository.getCurrentCoreConfiguration().commitChanges();

    request.setRequestPath(releasePom);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(releaseArtifact);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(snapshotPom);
    assertThat(repository.shouldServeByPolicies(request), is(false));
    request.setRequestPath(snapshotArtifact);
    assertThat(repository.shouldServeByPolicies(request), is(false));
    request.setRequestPath(metadata1);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(metadataR);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(metadataS);
    assertThat(repository.shouldServeByPolicies(request), is(false));
    request.setRequestPath(someDirectory);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(anyNonArtifactFile);
    assertThat(repository.shouldServeByPolicies(request), is(true));

    // it is equiv of repo type: SNAPSHOT
    repository.setRepositoryPolicy(RepositoryPolicy.SNAPSHOT);
    repository.getCurrentCoreConfiguration().commitChanges();

    request.setRequestPath(releasePom);
    assertThat(repository.shouldServeByPolicies(request), is(false));
    request.setRequestPath(releaseArtifact);
    assertThat(repository.shouldServeByPolicies(request), is(false));
    request.setRequestPath(snapshotPom);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(snapshotArtifact);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(metadata1);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(metadataR);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(metadataS);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(someDirectory);
    assertThat(repository.shouldServeByPolicies(request), is(true));
    request.setRequestPath(anyNonArtifactFile);
    assertThat(repository.shouldServeByPolicies(request), is(true));
  }
View Full Code Here

  @Test
  public void testGetLatestVersionSimple()
      throws Exception
  {
    M2Repository repository = (M2Repository) getResourceStore();

    List<String> versions = new ArrayList<String>();
    versions.add("1.0.0");
    versions.add("1.0.1");
    versions.add("1.0.2");
    versions.add("1.1.2");
    assertThat(repository.getLatestVersion(versions), is("1.1.2"));
  }
View Full Code Here

  @Test
  public void testGetLatestVersionClassifiers()
      throws Exception
  {
    M2Repository repository = (M2Repository) getResourceStore();

    List<String> versions = new ArrayList<String>();
    versions.add("1.0-alpha-19");
    versions.add("1.0-alpha-9-stable-1");
    versions.add("1.0-alpha-20");
    versions.add("1.0-alpha-21");
    versions.add("1.0-alpha-22");
    versions.add("1.0-alpha-40");
    assertThat(repository.getLatestVersion(versions), is("1.0-alpha-40"));
  }
View Full Code Here

  private void doTestExpiration(String path, final int age, final int... expectedHits)
      throws Exception
  {
    CounterListener ch = new CounterListener();

    M2Repository repository = (M2Repository) getResourceStore();

    eventBus().register(ch);

    File mdFile = new File(new File(getBasedir()), "target/test-classes/repo1" + path);
    long fileTimestamp = mdFile.lastModified();

    assertThat(mdFile, exists());

    try {
      repository.deleteItem(new ResourceStoreRequest("/spoof", true));
    }
    catch (ItemNotFoundException e) {
      // ignore
    }

    repository.setMetadataMaxAge(age);
    repository.setArtifactMaxAge(age);
    repository.getCurrentCoreConfiguration().commitChanges();

    for (int i = 0; i < 10 && !mdFile.setLastModified(System.currentTimeMillis() - (3L * A_DAY)); i++) {
      System.gc(); // helps with FS sync'ing on Windows
      Thread.sleep(500); // wait for FS
    }


    assertThat("File timestamp did not change, first pass", mdFile.lastModified(), not(equalTo(fileTimestamp)));
    fileTimestamp = mdFile.lastModified();

    // We need to wait a bit to avoid the check that last remote check = current time
    Thread.sleep(500);
    final StorageItem item = repository.retrieveItem(new ResourceStoreRequest(path, false));
    getLogger().info(
        path + " -> BEFORE assert 1 requestCount=" + expectedHits[0] + " at (" + System.currentTimeMillis() + ")");
    assertThat("Remote hits count fail after first request at (" + System.currentTimeMillis() + ")",
        ch.getRequestCount(), equalTo(expectedHits[0]));
    getLogger().info(
        path + " -> AFTER assert 1 requestCount=" + expectedHits[0] + " at (" + System.currentTimeMillis() + ")");

    for (int i = 0; i < 10 && !mdFile.setLastModified(System.currentTimeMillis() - (2L * A_DAY)); i++) {
      System.gc(); // helps with FS sync'ing on Windows
      Thread.sleep(500); // wait for FS
    }

    assertThat("File timestamp did not change, second pass", mdFile.lastModified(), not(equalTo(fileTimestamp)));
    fileTimestamp = mdFile.lastModified();

    // We need to wait a bit to avoid the check that last remote check = current time
    Thread.sleep(500);
    // this goes remote depending on age setting
    repository.retrieveItem(new ResourceStoreRequest(path, false));
    getLogger().info(
        path + " -> BEFORE assert 2 requestCount=" + expectedHits[1] + " at (" + System.currentTimeMillis() + ")");
    assertThat("Remote hits count fail after second request at (" + System.currentTimeMillis() + ")",
        ch.getRequestCount(), equalTo(expectedHits[1]));
    getLogger().info(
        path + " -> AFTER assert 2 requestCount=" + expectedHits[1] + " at (" + System.currentTimeMillis() + ")");

    for (int i = 0; i < 10 && !mdFile.setLastModified(System.currentTimeMillis() - (1L * A_DAY)); i++) {
      System.gc(); // helps with FS sync'ing on Windows
      Thread.sleep(500); // wait for FS
    }

    assertThat("File timestamp did not change, third pass", mdFile.lastModified(), not(equalTo(fileTimestamp)));
    fileTimestamp = mdFile.lastModified();

    // set up last checked timestamp so that nexus should go remote
    final RepositoryItemUid uid = item.getRepositoryItemUid();
    final AttributeStorage storage = uid.getRepository().getAttributesHandler().getAttributeStorage();
    final Attributes attributes = item.getRepositoryItemAttributes();
    attributes.setCheckedRemotely(System.currentTimeMillis() - ((Math.abs(age) + 1) * 60 * 1000));
    storage.putAttributes(uid, attributes);

    // We need to wait a bit to avoid the check that last remote check = current time
    Thread.sleep(500);
    repository.retrieveItem(new ResourceStoreRequest(path, false));
    getLogger().info(
        path + " -> BEFORE assert 3 requestCount=" + expectedHits[2] + " at (" + System.currentTimeMillis() + ")");
    assertThat("Remote hits count fail after third request at (" + System.currentTimeMillis() + ")",
        ch.getRequestCount(), equalTo(expectedHits[2]));
    getLogger().info(
View Full Code Here

  @Test
  public void testLocalStorageChanges()
      throws Exception
  {
    M2Repository repository = (M2Repository) getResourceStore();

    String changedUrl = repository.getLocalUrl() + "foo";

    repository.setLocalUrl(changedUrl);

    assertFalse("Should not be the same!", changedUrl.equals(repository.getLocalUrl()));

    repository.getCurrentCoreConfiguration().commitChanges();

    assertTrue("Should be the same!", changedUrl.equals(repository.getLocalUrl()));
  }
View Full Code Here

  @Test
  public void testRemoteStorageChanges()
      throws Exception
  {
    M2Repository repository = (M2Repository) getResourceStore();

    String changedUrl = repository.getRemoteUrl() + "/foo/";

    repository.setRemoteUrl(changedUrl);

    assertFalse("Should not be the same!", changedUrl.equals(repository.getRemoteUrl()));

    repository.getCurrentCoreConfiguration().commitChanges();

    assertTrue("Should be the same!", changedUrl.equals(repository.getRemoteUrl()));
  }
View Full Code Here

  @Test
  public void testProxyLastRequestedAttribute()
      throws Exception
  {
    M2Repository repository = (M2Repository) this.getRepositoryRegistry().getRepository("repo1");

    String item = "/org/slf4j/slf4j-api/1.4.3/slf4j-api-1.4.3.pom";
    ResourceStoreRequest request = new ResourceStoreRequest(item);
    request.getRequestContext().put(AccessManager.REQUEST_REMOTE_ADDRESS, "127.0.0.1");
    StorageItem storageItem = repository.retrieveItem(request);
    long lastRequest = System.currentTimeMillis() - 10 * A_DAY;
    storageItem.setLastRequested(lastRequest);
    repository.storeItem(false, storageItem);

    // now request the object, the lastRequested timestamp should be updated
    StorageItem resultItem = repository.retrieveItem(request);
    Assert.assertTrue(resultItem.getLastRequested() > lastRequest);

    // check the shadow attributes
    Attributes shadowStorageItem =
        repository.getAttributesHandler().getAttributeStorage().getAttributes(
            repository.createUid(request.getRequestPath()));
    assertThat(shadowStorageItem.getLastRequested(), is(resultItem.getLastRequested()));
  }
View Full Code Here

  public void testHostedLastRequestedAttribute()
      throws Exception
  {
    String itemPath = "/org/test/foo.junk";

    M2Repository repository = (M2Repository) this.getRepositoryRegistry().getRepository("inhouse");
    File inhouseLocalStorageDir =
        new File(
            new URL(((CRepositoryCoreConfiguration) repository.getCurrentCoreConfiguration()).getConfiguration(
                false).getLocalStorage().getUrl()).getFile());

    File artifactFile = new File(inhouseLocalStorageDir, itemPath);
    artifactFile.getParentFile().mkdirs();

    FileUtils.write(artifactFile, "Some Text so the file is not empty");

    ResourceStoreRequest request = new ResourceStoreRequest(itemPath);
    request.getRequestContext().put(AccessManager.REQUEST_REMOTE_ADDRESS, "127.0.0.1");
    StorageItem storageItem = repository.retrieveItem(request);
    long lastRequest = System.currentTimeMillis() - 10 * A_DAY;
    storageItem.setLastRequested(lastRequest);
    repository.storeItem(false, storageItem);

    // now request the object, the lastRequested timestamp should be updated
    StorageItem resultItem = repository.retrieveItem(request);
    Assert.assertTrue(resultItem.getLastRequested() > lastRequest);

    // check the shadow attributes
    Attributes shadowStorageItem =
        repository.getAttributesHandler().getAttributeStorage().getAttributes(
            repository.createUid(request.getRequestPath()));
    assertThat(shadowStorageItem.getLastRequested(), is(resultItem.getLastRequested()));
  }
View Full Code Here

  }

  public void doTestNEXUS4218(final String path, final String userAgent, final String remoteAddress)
      throws Exception
  {
    final M2Repository repository = (M2Repository) getResourceStore();
    // we check our expectation agains repository: it has to be Maven2 proxy repository
    Assert.assertTrue(repository.getRepositoryKind().isFacetAvailable(MavenProxyRepository.class));
    Assert.assertTrue(Maven2ContentClass.ID.equals(repository.getRepositoryContentClass().getId()));
    File repositoryLocalStorageDir =
        new File(
            new URL(((CRepositoryCoreConfiguration) repository.getCurrentCoreConfiguration()).getConfiguration(
                false).getLocalStorage().getUrl()).getFile());

    // create a request and equip it as needed
    final ResourceStoreRequest request = new ResourceStoreRequest(path);
    if (userAgent != null) {
      request.getRequestContext().put(AccessManager.REQUEST_AGENT, userAgent);
    }
    if (remoteAddress != null) {
      request.getRequestContext().put(AccessManager.REQUEST_REMOTE_ADDRESS, remoteAddress);
    }

    // invoke expire caches
    repository.expireCaches(request);

    // NO file should be pulled down
    {
      File artifactFile;
      if (!M2ArtifactRecognizer.isChecksum(path)) {
        artifactFile = new File(repositoryLocalStorageDir, path.substring(1));
      }
      else {
        artifactFile =
            new File(repositoryLocalStorageDir, path.substring(1, path.length() - ".sha1".length()));
      }
      Assert.assertFalse(artifactFile.exists());
    }

    // create a request and equip it as needed (requests should NOT be reused!)
    final ResourceStoreRequest retrieveRequest = new ResourceStoreRequest(path);
    if (userAgent != null) {
      retrieveRequest.getRequestContext().put(AccessManager.REQUEST_AGENT, userAgent);
    }
    if (remoteAddress != null) {
      retrieveRequest.getRequestContext().put(AccessManager.REQUEST_REMOTE_ADDRESS, remoteAddress);
    }

    // now do a fetch
    // this verifies that an assertion ("path in question does not exists in proxy cache but does exists on remote")
    // also verifies that cstamas did not mistype the paths in @Test method ;)
    repository.retrieveItem(retrieveRequest);

    // files SHOULD be pulled down
    {
      File artifactFile;
      if (!M2ArtifactRecognizer.isChecksum(path)) {
View Full Code Here

TOP

Related Classes of org.sonatype.nexus.proxy.maven.maven2.M2Repository

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.