protected void setUp() throws Exception {
    // prepare plexus environment
    super.setUp();

    ajcMojo.project = project;
    String temp = new File(".").getAbsolutePath();
    basedir = temp.substring(0, temp.length() - 2) + "/src/test/projects/" + getProjectName() + "/";
    project.getBuild().setDirectory(basedir + "/target");
    project.getBuild().setOutputDirectory(basedir + "/target/classes");
    project.getBuild().setTestOutputDirectory(basedir + "/target/test-classes");
    project.getBuild().setSourceDirectory(basedir + "/src/main/java");
    project.getBuild().setTestSourceDirectory(basedir + "/src/test/java");
    project.addCompileSourceRoot(project.getBuild().getSourceDirectory());
    project.addTestCompileSourceRoot(project.getBuild().getTestSourceDirectory());
    ajcMojo.basedir = new File(basedir);

    setVariableValueToObject(
        ajcMojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()));

    ArtifactHandler artifactHandler = new MockArtifactHandler();
    Artifact artifact = new MockArtifact("dill", "dall");
    artifact.setArtifactHandler(artifactHandler);
    project.setArtifact(artifact);
    project.setDependencyArtifacts(Collections.EMPTY_SET);
  }
Esempio n. 2
0
 MavenProject readProject(File pomFile) throws IOException {
   MavenXpp3Reader mavenReader = new MavenXpp3Reader();
   FileReader fileReader = null;
   try {
     fileReader = new FileReader(pomFile);
     Model model = mavenReader.read(fileReader);
     model.setPomFile(pomFile);
     MavenProject project = new MavenProject(model);
     project.setFile(pomFile);
     project.setArtifact(
         createArtifact(
             pomFile,
             model.getGroupId(),
             model.getArtifactId(),
             model.getVersion(),
             "compile",
             model.getPackaging(),
             ""));
     return project;
   } catch (Exception e) {
     throw new RuntimeException(e);
   } finally {
     if (fileReader != null) {
       fileReader.close();
     }
   }
 }
  private MavenProject buildProjectStub(final Artifact depArtifact) {
    final Model model = new Model();
    model.setGroupId(depArtifact.getGroupId());
    model.setArtifactId(depArtifact.getArtifactId());
    model.setVersion(depArtifact.getBaseVersion());
    model.setPackaging(depArtifact.getType());

    model.setDescription("Stub for " + depArtifact.getId());

    final MavenProject project = new MavenProject(model);
    project.setArtifact(depArtifact);

    return project;
  }
  /**
   * Resolve maven URI into maven project.
   *
   * <p>Provides only model and artifact.
   */
  public static MavenProject newProject(String mavenURI) throws Exception {

    final Artifact artifact = newArtifact(mavenURI);

    final File input = artifact.getFile();

    final ModelReader reader = new DefaultModelReader();

    final Model model = reader.read(input, null);

    final MavenProject project = new MavenProject(model);

    project.setArtifact(RepositoryUtils.toArtifact(artifact));

    return project;
  }
  @Test
  public void autoDiscoverWithOneCompleteLocationFromSnapshotPOMDistMgmt()
      throws NexusDiscoveryException {
    Settings settings = new Settings();

    String url = "http://nexus.somewhere.com/";
    String id = "some-mirror";
    String user = "******";
    String password = "******";

    testClientManager.testUrl = url;
    testClientManager.testUser = user;
    testClientManager.testPassword = password;

    Server server = new Server();
    server.setId(id);
    server.setUsername(user);
    server.setPassword(password);

    settings.addServer(server);

    Model model = new Model();
    model.setModelVersion("4.0.0");
    model.setGroupId("group.id");
    model.setArtifactId("artifact-id");
    model.setVersion("1-SNAPSHOT");

    DistributionManagement dm = new DistributionManagement();

    DeploymentRepository repo = new DeploymentRepository();
    repo.setId(id);
    repo.setUrl(url);

    dm.setSnapshotRepository(repo);

    model.setDistributionManagement(dm);

    MavenProject project = new MavenProject(model);

    project.setArtifact(factory.create(project));

    discovery.discover(settings, project, "blah", true);
  }
  /**
   * Tests if a Filter is installed correctly, also if createSourcesJar is set to true.
   *
   * @throws Exception
   */
  public void testShadeWithFilter() throws Exception {
    ShadeMojo mojo = new ShadeMojo();

    // set createSourcesJar = true
    Field createSourcesJar = ShadeMojo.class.getDeclaredField("createSourcesJar");
    createSourcesJar.setAccessible(true);
    createSourcesJar.set(mojo, Boolean.TRUE);

    // configure artifactFactory for mojo
    ArtifactFactory artifactFactory = (ArtifactFactory) lookup(ArtifactFactory.ROLE);
    Field artifactFactoryField = ShadeMojo.class.getDeclaredField("artifactFactory");
    artifactFactoryField.setAccessible(true);
    artifactFactoryField.set(mojo, artifactFactory);

    // configure artifactResolver (mocked) for mojo
    ArtifactResolver mockArtifactResolver =
        new DefaultArtifactResolver() {

          public void resolve(
              Artifact artifact, List<ArtifactRepository> remoteRepos, ArtifactRepository repo)
              throws ArtifactResolutionException, ArtifactNotFoundException {
            // artifact is resolved
            artifact.setResolved(true);

            // set file
            artifact.setFile(
                new File(
                    artifact.getArtifactId()
                        + "-"
                        + artifact.getVersion()
                        + (artifact.getClassifier() != null ? "-" + artifact.getClassifier() : "")
                        + ".jar"));
          }
        };
    Field artifactResolverField = ShadeMojo.class.getDeclaredField("artifactResolver");
    artifactResolverField.setAccessible(true);
    artifactResolverField.set(mojo, mockArtifactResolver);

    // create and configure MavenProject
    MavenProject project = new MavenProject();
    ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE);
    Artifact artifact =
        new DefaultArtifact(
            "org.apache.myfaces.core",
            "myfaces-impl",
            VersionRange.createFromVersion("2.0.1-SNAPSHOT"),
            "compile",
            "jar",
            null,
            artifactHandler);
    mockArtifactResolver.resolve(artifact, null, null); // setFile and setResolved
    project.setArtifact(artifact);
    Field projectField = ShadeMojo.class.getDeclaredField("project");
    projectField.setAccessible(true);
    projectField.set(mojo, project);

    // create and configure the ArchiveFilter
    ArchiveFilter archiveFilter = new ArchiveFilter();
    Field archiveFilterArtifact = ArchiveFilter.class.getDeclaredField("artifact");
    archiveFilterArtifact.setAccessible(true);
    archiveFilterArtifact.set(archiveFilter, "org.apache.myfaces.core:myfaces-impl");

    // add ArchiveFilter to mojo
    Field filtersField = ShadeMojo.class.getDeclaredField("filters");
    filtersField.setAccessible(true);
    filtersField.set(mojo, new ArchiveFilter[] {archiveFilter});

    // invoke getFilters()
    Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", new Class[0]);
    getFilters.setAccessible(true);
    List<Filter> filters = (List<Filter>) getFilters.invoke(mojo);

    // assertions - there must be one filter
    assertEquals(1, filters.size());

    // the filter must be able to filter the binary and the sources jar
    Filter filter = filters.get(0);
    assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT.jar"))); // binary jar
    assertTrue(
        filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT-sources.jar"))); // sources jar
  }