use of org.apache.maven.settings.Settings in project maven-plugins by apache.
the class EvaluateMojo method handleResponse.
/**
* @param expr the user expression asked.
* @param output the file where to write the result, or <code>null</code> to print in standard output.
* @throws MojoExecutionException if any
* @throws MojoFailureException if any reflection exceptions occur or missing components.
*/
private void handleResponse(String expr, File output) throws MojoExecutionException, MojoFailureException {
StringBuilder response = new StringBuilder();
Object obj;
try {
obj = getEvaluator().evaluate(expr);
} catch (ExpressionEvaluationException e) {
throw new MojoExecutionException("Error when evaluating the Maven expression", e);
}
if (obj != null && expr.equals(obj.toString())) {
getLog().warn("The Maven expression was invalid. Please use a valid expression.");
return;
}
// handle null
if (obj == null) {
response.append("null object or invalid expression");
} else // handle primitives objects
if (obj instanceof String) {
response.append(obj.toString());
} else if (obj instanceof Boolean) {
response.append(obj.toString());
} else if (obj instanceof Byte) {
response.append(obj.toString());
} else if (obj instanceof Character) {
response.append(obj.toString());
} else if (obj instanceof Double) {
response.append(obj.toString());
} else if (obj instanceof Float) {
response.append(obj.toString());
} else if (obj instanceof Integer) {
response.append(obj.toString());
} else if (obj instanceof Long) {
response.append(obj.toString());
} else if (obj instanceof Short) {
response.append(obj.toString());
} else // handle specific objects
if (obj instanceof File) {
File f = (File) obj;
response.append(f.getAbsolutePath());
} else // handle Maven pom object
if (obj instanceof MavenProject) {
MavenProject projectAsked = (MavenProject) obj;
StringWriter sWriter = new StringWriter();
MavenXpp3Writer pomWriter = new MavenXpp3Writer();
try {
pomWriter.write(sWriter, projectAsked.getModel());
} catch (IOException e) {
throw new MojoExecutionException("Error when writing pom", e);
}
response.append(sWriter.toString());
} else // handle Maven Settings object
if (obj instanceof Settings) {
Settings settingsAsked = (Settings) obj;
StringWriter sWriter = new StringWriter();
SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
try {
settingsWriter.write(sWriter, settingsAsked);
} catch (IOException e) {
throw new MojoExecutionException("Error when writing settings", e);
}
response.append(sWriter.toString());
} else {
// others Maven objects
response.append(toXML(expr, obj));
}
if (output != null) {
try {
writeFile(output, response);
} catch (IOException e) {
throw new MojoExecutionException("Cannot write evaluation of expression to output: " + output, e);
}
getLog().info("Result of evaluation written to: " + output);
} else {
getLog().info(LS + response.toString());
}
}
use of org.apache.maven.settings.Settings in project maven-plugins by apache.
the class GitHubDownloaderTestCase method testConfigureAuthenticationWithProblems.
public void testConfigureAuthenticationWithProblems() throws Exception {
IssueManagement issueManagement = newGitHubIssueManagement();
GitHubDownloader gitHubDownloader = newGitHubDownloader(issueManagement);
Settings settings = new Settings();
Server server = newServer("github-server");
settings.addServer(server);
SettingsDecrypter decrypter = mock(SettingsDecrypter.class);
SettingsDecryptionResult result = mock(SettingsDecryptionResult.class);
Log log = mock(Log.class);
when(result.getProblems()).thenReturn(Arrays.<SettingsProblem>asList(new DefaultSettingsProblem("Ups " + server.getId(), Severity.ERROR, null, -1, -1, null)));
when(result.getServer()).thenReturn(server);
when(decrypter.decrypt(any(SettingsDecryptionRequest.class))).thenReturn(result);
gitHubDownloader.configureAuthentication(decrypter, "github-server", settings, log);
verify(log).error("Ups github-server", null);
ArgumentCaptor<SettingsDecryptionRequest> argument = ArgumentCaptor.forClass(SettingsDecryptionRequest.class);
verify(decrypter).decrypt(argument.capture());
List<Server> servers = ((DefaultSettingsDecryptionRequest) argument.getValue()).getServers();
assertEquals(1, servers.size());
assertSame(server, servers.get(0));
}
use of org.apache.maven.settings.Settings in project maven-plugins by apache.
the class JavadocReportTest method testProxy.
/**
* Method to test proxy support in the javadoc
*
* @throws Exception if any
*/
public void testProxy() throws Exception {
Settings settings = new Settings();
Proxy proxy = new Proxy();
// dummy proxy
proxy.setActive(true);
proxy.setHost("127.0.0.1");
proxy.setPort(80);
proxy.setProtocol("http");
proxy.setUsername("toto");
proxy.setPassword("toto");
proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
settings.addProxy(proxy);
File testPom = new File(getBasedir(), "src/test/resources/unit/proxy-test/proxy-test-plugin-config.xml");
JavadocReport mojo = lookupMojo(testPom);
MavenSession session = spy(newMavenSession(mojo.project));
ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
when(session.getRepositorySession()).thenReturn(repositorySession);
LegacySupport legacySupport = lookup(LegacySupport.class);
legacySupport.setSession(session);
setVariableValueToObject(mojo, "settings", settings);
setVariableValueToObject(mojo, "session", session);
mojo.execute();
File commandLine = new File(getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/javadoc." + (SystemUtils.IS_OS_WINDOWS ? "bat" : "sh"));
assertTrue(FileUtils.fileExists(commandLine.getAbsolutePath()));
String readed = readFile(commandLine);
assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
assertTrue(readed.contains("-J-Dhttp.proxyHost=127.0.0.1"));
assertTrue(readed.contains("-J-Dhttp.proxyPort=80"));
assertTrue(readed.contains("-J-Dhttp.proxyUser=\\\"toto\\\""));
assertTrue(readed.contains("-J-Dhttp.proxyPassword=\\\"toto\\\""));
assertTrue(readed.contains("-J-Dhttp.nonProxyHosts=\\\"www.google.com|*.somewhere.com\\\""));
File options = new File(getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/options");
assertTrue(FileUtils.fileExists(options.getAbsolutePath()));
String optionsContent = readFile(options);
// NO -link expected
assertFalse(optionsContent.contains("-link"));
// real proxy
ProxyServer proxyServer = null;
AuthAsyncProxyServlet proxyServlet;
try {
proxyServlet = new AuthAsyncProxyServlet();
proxyServer = new ProxyServer(proxyServlet);
proxyServer.start();
settings = new Settings();
proxy = new Proxy();
proxy.setActive(true);
proxy.setHost(proxyServer.getHostName());
proxy.setPort(proxyServer.getPort());
proxy.setProtocol("http");
settings.addProxy(proxy);
mojo = lookupMojo(testPom);
setVariableValueToObject(mojo, "settings", settings);
setVariableValueToObject(mojo, "session", session);
mojo.execute();
readed = readFile(commandLine);
assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
assertTrue(readed.contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName()));
assertTrue(readed.contains("-J-Dhttp.proxyPort=" + proxyServer.getPort()));
optionsContent = readFile(options);
// -link expected
// TODO: This got disabled for now!
// This test fails since the last commit but I actually think it only ever worked by accident.
// It did rely on a commons-logging-1.0.4.pom which got resolved by a test which did run previously.
// But after updating to commons-logging.1.1.1 there is no pre-resolved artifact available in
// target/local-repo anymore, thus the javadoc link info cannot get built and the test fails
// I'll for now just disable this line of code, because the test as far as I can see _never_
// did go upstream. The remoteRepository list used is always empty!.
//
// assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
} finally {
if (proxyServer != null) {
proxyServer.stop();
}
}
// auth proxy
Map<String, String> authentications = new HashMap<String, String>();
authentications.put("foo", "bar");
try {
proxyServlet = new AuthAsyncProxyServlet(authentications);
proxyServer = new ProxyServer(proxyServlet);
proxyServer.start();
settings = new Settings();
proxy = new Proxy();
proxy.setActive(true);
proxy.setHost(proxyServer.getHostName());
proxy.setPort(proxyServer.getPort());
proxy.setProtocol("http");
proxy.setUsername("foo");
proxy.setPassword("bar");
settings.addProxy(proxy);
mojo = lookupMojo(testPom);
setVariableValueToObject(mojo, "settings", settings);
setVariableValueToObject(mojo, "session", session);
mojo.execute();
readed = readFile(commandLine);
assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
assertTrue(readed.contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName()));
assertTrue(readed.contains("-J-Dhttp.proxyPort=" + proxyServer.getPort()));
assertTrue(readed.contains("-J-Dhttp.proxyUser=\\\"foo\\\""));
assertTrue(readed.contains("-J-Dhttp.proxyPassword=\\\"bar\\\""));
optionsContent = readFile(options);
// -link expected
// see comment above (line 829)
// assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
} finally {
if (proxyServer != null) {
proxyServer.stop();
}
}
}
use of org.apache.maven.settings.Settings in project maven-plugins by apache.
the class AbstractSiteDeployWebDavTest method davDeployThruProxyWithoutAuthzInProxy.
@Test
public void davDeployThruProxyWithoutAuthzInProxy() throws Exception {
FileUtils.cleanDirectory(siteTargetPath);
SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);
try {
File pluginXmlFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
AbstractMojo mojo = getMojo(pluginXmlFile);
assertNotNull(mojo);
SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub("deploy-dav");
// olamy, Note : toto is something like foo or bar for french folks :-)
String siteUrl = "dav:http://toto.com/site/";
siteMavenProjectStub.getDistributionManagement().getSite().setUrl(siteUrl);
setVariableValueToObject(mojo, "project", siteMavenProjectStub);
Settings settings = new Settings();
Proxy proxy = new Proxy();
//dummy proxy
proxy.setActive(true);
proxy.setHost("localhost");
proxy.setPort(simpleDavServerHandler.getPort());
proxy.setProtocol("http");
proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
settings.addProxy(proxy);
setVariableValueToObject(mojo, "settings", settings);
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setProxies(Arrays.asList(proxy));
MavenSession mavenSession = new MavenSession(getContainer(), null, request, null);
setVariableValueToObject(mojo, "mavenSession", mavenSession);
File inputDirectory = new File("src/test/resources/unit/deploy-dav/target/site");
setVariableValueToObject(mojo, "inputDirectory", inputDirectory);
mojo.execute();
assertContentInFiles();
assertTrue(requestsContainsProxyUse(simpleDavServerHandler.httpRequests));
for (HttpRequest rq : simpleDavServerHandler.httpRequests) {
log.info(rq.toString());
}
} finally {
simpleDavServerHandler.stop();
}
}
use of org.apache.maven.settings.Settings in project maven-plugins by apache.
the class AbstractSiteDeployWebDavTest method noAuthzDavDeploy.
@Test
public void noAuthzDavDeploy() throws Exception {
FileUtils.cleanDirectory(siteTargetPath);
SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);
try {
File pomFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
AbstractMojo mojo = getMojo(pomFile);
assertNotNull(mojo);
SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub("deploy-dav");
assertTrue("dav server port not available: " + simpleDavServerHandler.getPort(), simpleDavServerHandler.getPort() > 0);
siteMavenProjectStub.getDistributionManagement().getSite().setUrl("dav:http://localhost:" + simpleDavServerHandler.getPort() + "/site/");
setVariableValueToObject(mojo, "project", siteMavenProjectStub);
Settings settings = new Settings();
setVariableValueToObject(mojo, "settings", settings);
File inputDirectory = new File("src/test/resources/unit/deploy-dav/target/site");
setVariableValueToObject(mojo, "inputDirectory", inputDirectory);
mojo.execute();
assertContentInFiles();
assertFalse(requestsContainsProxyUse(simpleDavServerHandler.httpRequests));
} finally {
simpleDavServerHandler.stop();
}
}
Aggregations