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

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


  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

  }

  private boolean cachedHashItem(final String itemPath, String suffix)
      throws Exception
  {
    final M2Repository repository = getRepository();
    try {
      AbstractStorageItem item =
          repository.getLocalStorage().retrieveItem(repository, new ResourceStoreRequest(itemPath, true, false));

      String attrname;
      if (ChecksumContentValidator.SUFFIX_SHA1.equals(suffix)) {
        attrname = ChecksumContentValidator.ATTR_REMOTE_SHA1;
      }
View Full Code Here

    assertFalse("repo is not proxied", patient.getRepositoryKind().isFacetAvailable(ProxyRepository.class));
    assertFalse("repo is not proxied", patient.getRepositoryKind().isFacetAvailable(MavenProxyRepository.class));

    // do the conversion
    // forcing cast
    M2Repository repoToBeTreated = (M2Repository) patient;

    repoToBeTreated.setRemoteStorage(remoteRepositoryStorage);

    repoToBeTreated.setRemoteUrl("http://repo1.maven.org/maven2/");

    getApplicationConfiguration().saveConfiguration();

    // check
    assertFalse("repo is not hosted", patient.getRepositoryKind().isFacetAvailable(HostedRepository.class));
    assertFalse("repo is not hosted", patient.getRepositoryKind().isFacetAvailable(MavenHostedRepository.class));
    assertTrue("repo is proxied", patient.getRepositoryKind().isFacetAvailable(ProxyRepository.class));
    assertTrue("repo is proxied", patient.getRepositoryKind().isFacetAvailable(MavenProxyRepository.class));

    // now we just walk in, like nothing of above happened :)
    M2Repository afterTreatment =
        (M2Repository) getRepositoryRegistry().getRepositoryWithFacet(patient.getId(), MavenProxyRepository.class);

    assertNotNull("It should exists (heh, but NoSuchRepo exception should be thrown anyway)", afterTreatment);

    assertEquals("This should match, since they should be the same!", remoteRepositoryStorage.getProviderId(),
        afterTreatment.getRemoteStorage().getProviderId());

    // before NEXUS-5258, this test used to check that the provider was set. Nexus does not set the provider anymore
    // if it is the default provider, to let repos pick up the system default.

    assertEquals("Config should state the same as object is", afterTreatment.getRemoteUrl(),
        (((CRepositoryCoreConfiguration) afterTreatment.getCurrentCoreConfiguration())
            .getConfiguration(false)).getRemoteStorage().getUrl()
    );
  }
View Full Code Here

      throws Exception
  {
    List<String> reposes = new ArrayList<String>();
    for (String remoteRepoId : repoIds()) {
      // create proxy for remote
      M2Repository repo = (M2Repository) env.lookup(Repository.class, "maven2");
      CRepository repoConf = new DefaultCRepository();
      repoConf.setProviderRole(Repository.class.getName());
      repoConf.setProviderHint("maven2");
      repoConf.setId(remoteRepoId);
      repoConf.setName(remoteRepoId);
      repoConf.setNotFoundCacheActive(true);

      repoConf.setLocalStorage(new CLocalStorage());
      repoConf.getLocalStorage().setProvider("file");
      repoConf.getLocalStorage().setUrl(
          env
              .getApplicationConfiguration().getWorkingDirectory("proxy/store/" + remoteRepoId).toURI()
              .toURL().toString()
      );

      Xpp3Dom ex = new Xpp3Dom("externalConfiguration");
      repoConf.setExternalConfiguration(ex);
      M2RepositoryConfiguration exConf = new M2RepositoryConfiguration(ex);
      if (remoteRepoId.endsWith("-snapshot")) {
        exConf.setRepositoryPolicy(RepositoryPolicy.SNAPSHOT);
      }
      else {
        exConf.setRepositoryPolicy(RepositoryPolicy.RELEASE);
      }
      exConf.setChecksumPolicy(ChecksumPolicy.STRICT_IF_EXISTS);
      exConf.setRoutingDiscoveryEnabled(false);

      repoConf.setRemoteStorage(new CRemoteStorage());
      repoConf.getRemoteStorage().setProvider(env.getRemoteProviderHintFactory().getDefaultHttpRoleHint());
      repoConf.getRemoteStorage().setUrl(server().getUrl() + "/" + remoteRepoId + "/");

      repo.configure(repoConf);

      // repo.setCacheManager( env.getCacheManager() );
      reposes.add(repo.getId());

      env.getApplicationConfiguration().getConfigurationModel().addRepository(repoConf);

      env.getRepositoryRegistry().addRepository(repo);
    }

    // ading one hosted only
    M2Repository repo = (M2Repository) env.lookup(Repository.class, "maven2");

    CRepository repoConf = new DefaultCRepository();

    repoConf.setProviderRole(Repository.class.getName());
    repoConf.setProviderHint("maven2");
    repoConf.setId("inhouse");

    repoConf.setLocalStorage(new CLocalStorage());
    repoConf.getLocalStorage().setProvider("file");
    repoConf.getLocalStorage().setUrl(
        env.getApplicationConfiguration().getWorkingDirectory("proxy/store/inhouse").toURI().toURL().toString());

    Xpp3Dom exRepo = new Xpp3Dom("externalConfiguration");
    repoConf.setExternalConfiguration(exRepo);
    M2RepositoryConfiguration exRepoConf = new M2RepositoryConfiguration(exRepo);
    exRepoConf.setRepositoryPolicy(RepositoryPolicy.RELEASE);
    exRepoConf.setChecksumPolicy(ChecksumPolicy.STRICT_IF_EXISTS);

    repo.configure(repoConf);

    reposes.add(repo.getId());

    env.getApplicationConfiguration().getConfigurationModel().addRepository(repoConf);

    env.getRepositoryRegistry().addRepository(repo);

    // add a hosted snapshot repo
    M2Repository repoSnapshot = (M2Repository) env.lookup(Repository.class, "maven2");

    CRepository repoSnapshotConf = new DefaultCRepository();

    repoSnapshotConf.setProviderRole(Repository.class.getName());
    repoSnapshotConf.setProviderHint("maven2");
    repoSnapshotConf.setId("inhouse-snapshot");

    repoSnapshotConf.setLocalStorage(new CLocalStorage());
    repoSnapshotConf.getLocalStorage().setProvider("file");
    repoSnapshotConf.getLocalStorage().setUrl(
        env
            .getApplicationConfiguration().getWorkingDirectory("proxy/store/inhouse-snapshot").toURI().toURL()
            .toString()
    );

    Xpp3Dom exSnapRepo = new Xpp3Dom("externalConfiguration");
    repoSnapshotConf.setExternalConfiguration(exSnapRepo);
    M2RepositoryConfiguration exSnapRepoConf = new M2RepositoryConfiguration(exSnapRepo);
    exSnapRepoConf.setRepositoryPolicy(RepositoryPolicy.SNAPSHOT);
    exSnapRepoConf.setChecksumPolicy(ChecksumPolicy.STRICT_IF_EXISTS);

    repoSnapshot.configure(repoSnapshotConf);

    reposes.add(repoSnapshot.getId());

    env.getApplicationConfiguration().getConfigurationModel().addRepository(repoSnapshotConf);

    env.getRepositoryRegistry().addRepository(repoSnapshot);
View Full Code Here

      public void buildEnvironment(AbstractProxyTestEnvironment env)
          throws Exception
      {
        {
          // adding one proxy
          final M2Repository repo = (M2Repository) env.lookup(Repository.class, "maven2");
          CRepository repoConf = new DefaultCRepository();
          repoConf.setProviderRole(Repository.class.getName());
          repoConf.setProviderHint("maven2");
          repoConf.setId(PROXY_REPO_ID);
          repoConf.setName(PROXY_REPO_ID);
          repoConf.setNotFoundCacheActive(true);
          repoConf.setLocalStorage(new CLocalStorage());
          repoConf.getLocalStorage().setProvider("file");
          repoConf.getLocalStorage().setUrl(
              env.getApplicationConfiguration().getWorkingDirectory("proxy/store/" + PROXY_REPO_ID).toURI().toURL()
                  .toString()
          );
          Xpp3Dom ex = new Xpp3Dom("externalConfiguration");
          repoConf.setExternalConfiguration(ex);
          M2RepositoryConfiguration exConf = new M2RepositoryConfiguration(ex);
          exConf.setRepositoryPolicy(RepositoryPolicy.RELEASE);
          exConf.setChecksumPolicy(ChecksumPolicy.STRICT_IF_EXISTS);
          repoConf.setRemoteStorage(new CRemoteStorage());
          repoConf.getRemoteStorage().setProvider(
              env.getRemoteProviderHintFactory().getDefaultHttpRoleHint());
          repoConf.getRemoteStorage().setUrl("http://localhost:" + server.getPort() + "/");
          repo.configure(repoConf);
          env.getApplicationConfiguration().getConfigurationModel().addRepository(repoConf);
          env.getRepositoryRegistry().addRepository(repo);
        }
      }
    };
View Full Code Here

TOP

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

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.