Author: tchemit Date: 2012-07-19 11:09:40 +0200 (Thu, 19 Jul 2012) New Revision: 320 Url: http://nuiton.org/repositories/revision/jredmine/320 Log: fixes #2034: Updates to helper-m-p 1.5 fixes #937: Maven test utilise les properties de test de la distribution (introduce RedmineConfigurationUtil + update docs) refs #2197: Can use api Key to connect to redmine (begin of it, but still not used by any redmine service, nor any mojo) Added: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java Modified: branches/jredmine-1.x/jredmine-client/pom.xml branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java branches/jredmine-1.x/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java branches/jredmine-1.x/jredmine-client/src/test/resources/test-config.properties branches/jredmine-1.x/jredmine-maven-plugin/pom.xml branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/RedmineFixtures.java branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java branches/jredmine-1.x/jredmine-maven-plugin/src/test/resources/test-config.properties branches/jredmine-1.x/pom.xml branches/jredmine-1.x/src/site/apt/tests.apt Modified: branches/jredmine-1.x/jredmine-client/pom.xml =================================================================== --- branches/jredmine-1.x/jredmine-client/pom.xml 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-client/pom.xml 2012-07-19 09:09:40 UTC (rev 320) @@ -79,6 +79,11 @@ </dependency> <dependency> + <groupId>commons-beanutils</groupId> + <artifactId>commons-beanutils</artifactId> + </dependency> + + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> Added: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java (rev 0) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java 2012-07-19 09:09:40 UTC (rev 320) @@ -0,0 +1,362 @@ +package org.nuiton.jredmine; +/* + * #%L + * JRedmine :: Client + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.beanutils.BeanUtilsBean; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.io.rest.RestException; +import org.nuiton.jredmine.rest.RedmineRestClient; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Properties; + +/** + * Helper methods about configuration. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class RedmineConfigurationUtil { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(RedmineConfigurationUtil.class); + + public static final String PROPERTY_PREFIX = "jredmine-test."; + + protected RedmineConfigurationUtil() { + // avoid instance + } + + public static RedmineServiceConfiguration obtainRedmineConfiguration(RedmineServiceConfiguration anoConf) throws IOException { + + RedmineServiceConfiguration conf = newLogguedConfiguration(anoConf); + + boolean ok = false; + + RedmineRestClient client = new RedmineRestClient(conf); + + try { + + client.open(); + ok = client.isOpen(); + } catch (Exception e) { + + if (log.isDebugEnabled()) { + log.debug("Could not connect to redmine with configuration: " + conf, e); + } + + } finally { + try { + client.close(); + } catch (RestException e) { + if (log.isErrorEnabled()) { + log.error("Could not close session", e); + } + } + } + + if (!ok) { + + // try to connect anonymous + conf = cloneConfiguration(anoConf); + + client = new RedmineRestClient(conf); + + try { + + client.open(); + ok = client.isOpen(); + } catch (Exception e) { + + if (log.isDebugEnabled()) { + log.debug("Could not connect to redmine with configuration: " + conf, e); + } + } finally { + try { + client.close(); + } catch (RestException e) { + if (log.isErrorEnabled()) { + log.error("Could not close session", e); + } + } + } + } + + if (!ok) { + + // no conf available + conf = null; + } + return conf; + } + + public static RedmineServiceConfiguration newAnonymousConfiguration(String propertiesFromEnv, + String classPathPropertiesLocation) + throws IOException { + + Properties props = new Properties(); + + InputStream in = null; + try { + String jredmineConfiguration = System.getenv(propertiesFromEnv); + if (jredmineConfiguration == null) { + if (log.isWarnEnabled()) { + log.warn("Could not find environement variable " + + "'jredmine-test.properties' will use " + + "default test configuration"); + } + + in = RedmineConfigurationUtil.class.getResourceAsStream(classPathPropertiesLocation); + } else { + + File file = new File(jredmineConfiguration); + + if (!file.exists()) { + throw new IllegalStateException("Could not find " + jredmineConfiguration + + " file"); + } + in = FileUtils.openInputStream(file); + } + props.load(in); + in.close(); + } finally { + IOUtils.closeQuietly(in); + } + + RedmineServiceConfiguration result = new SimpleRedmineServiceConfiguration(); + + overridePropertyFromProperties(result, "url", "restUrl", props); + overridePropertyFromProperties(result, "encoding", props); + overridePropertyFromProperties(result, "verbose", props); + + overridePropertyFromEnv(result, "url", "restUrl"); + overridePropertyFromEnv(result, "encoding"); + overridePropertyFromEnv(result, "verbose"); + result.setAnonymous(true); + return result; + } + + public static RedmineServiceConfiguration newLogguedConfiguration(RedmineServiceConfiguration anoConf) + throws IOException { + + // use anonymous configuration + + RedmineServiceConfiguration result = cloneConfiguration(anoConf); + + overridePropertyFromEnv(result, "login", "restUsername"); + overridePropertyFromEnv(result, "password", "restPassword"); + overridePropertyFromEnv(result, "apiKey", "apiKey"); + + result.setAnonymous(false); + + return result; + } + + public static RedmineServiceConfiguration cloneConfiguration( + RedmineServiceConfiguration src) { + RedmineServiceConfiguration dst = new SimpleRedmineServiceConfiguration(); + dst.setRestUrl(src.getRestUrl()); + dst.setRestUsername(src.getRestUsername()); + dst.setRestPassword(src.getRestPassword()); + dst.setApiKey(src.getApiKey()); + dst.setEncoding(src.getEncoding()); + dst.setVerbose(src.isVerbose()); + dst.setAnonymous(src.isAnonymous()); + return dst; + } + + public static void copyConfiguration(RedmineServiceConfiguration src, + RedmineServiceConfiguration dst) { + dst.setRestUrl(src.getRestUrl()); + dst.setRestUsername(src.getRestUsername()); + dst.setRestPassword(src.getRestPassword()); + dst.setApiKey(src.getApiKey()); + dst.setEncoding(src.getEncoding()); + dst.setVerbose(src.isVerbose()); + dst.setAnonymous(src.isAnonymous()); + } + + protected static void overridePropertyFromProperties(RedmineServiceConfiguration conf, + String prop, + Properties props) throws IOException { + overridePropertyFromProperties(conf, prop, prop, props); + } + + protected static void overridePropertyFromProperties(RedmineServiceConfiguration conf, + String prop, + String beanProp, + Properties props) throws IOException { + String value = props.getProperty(PROPERTY_PREFIX + prop); + if (StringUtils.isNotEmpty(value)) { + try { + BeanUtilsBean.getInstance().setProperty(conf, beanProp, value); + } catch (Exception e) { + throw new IOException( + "Could not set property '" + beanProp + + "' with value '" + value + "' to configuration"); + } + } + } + + protected static void overridePropertyFromEnv(RedmineServiceConfiguration conf, + String prop) throws IOException { + overridePropertyFromEnv(conf, prop, prop); + } + + protected static void overridePropertyFromEnv(RedmineServiceConfiguration conf, + String prop, + String beanProp) throws IOException { + String value = System.getenv(PROPERTY_PREFIX + prop); + if (StringUtils.isNotEmpty(value) && !"null".equals(value)) { + try { + BeanUtilsBean.getInstance().setProperty(conf, beanProp, value); + } catch (Exception e) { + throw new IOException( + "Could not set property '" + prop + + "' with value '" + value + "' to configuration"); + } + } + } + + /** + * Configuration of a redmine service for test purposes. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ + public static class SimpleRedmineServiceConfiguration implements RedmineServiceConfiguration { + + URL restUrl; + + String restUsername; + + String restPassword; + + boolean verbose; + + boolean anonymous; + + String encoding; + + String apiKey; + + @Override + public String getEncoding() { + return encoding; + } + + @Override + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + @Override + public String getRestPassword() { + return restPassword; + } + + @Override + public void setRestPassword(String restPassword) { + this.restPassword = restPassword; + } + + @Override + public URL getRestUrl() { + return restUrl; + } + + @Override + public void setRestUrl(URL restUrl) { + this.restUrl = restUrl; + } + + @Override + public String getRestUsername() { + return restUsername; + } + + @Override + public void setRestUsername(String restUsername) { + this.restUsername = restUsername; + } + + @Override + public boolean isVerbose() { + return verbose; + } + + @Override + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + @Override + public boolean isAnonymous() { + return anonymous; + } + + @Override + public void setAnonymous(boolean anonymous) { + this.anonymous = anonymous; + } + + @Override + public String toString() { + ToStringBuilder b = new ToStringBuilder( + this, + ToStringStyle.MULTI_LINE_STYLE + ); + b.append("redmineUrl", restUrl); + if (anonymous) { + b.append("anonymous", true); + } else { + b.append("redmineUsername", restUsername); + b.append("redminePassword", "***"); + } + b.append("encoding", encoding); + b.append("verbose", verbose); + return b.toString(); + } + + @Override + public String getApiKey() { + return apiKey; + } + + @Override + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + } +} Property changes on: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java 2012-07-19 09:09:40 UTC (rev 320) @@ -24,7 +24,6 @@ */ import com.google.common.base.Strings; -import com.google.common.collect.Maps; import org.nuiton.io.rest.AbstractRequestFactory; import org.nuiton.io.rest.RestMethod; import org.nuiton.io.rest.RestRequestBuilder; @@ -92,13 +91,13 @@ checkRequestArgs(1, "apiKey", args); } - @Override - public Map<String, String> getHeaders(Object... args) { - Map<String, String> headers = Maps.newHashMap(); - String apiKey = (String) args[0]; - headers.put("X-Redmine-API-Key", apiKey); - return headers; - } +// @Override +// public Map<String, String> getHeaders(Object... args) { +// Map<String, String> headers = Maps.newHashMap(); +// String apiKey = (String) args[0]; +// headers.put("X-Redmine-API-Key", apiKey); +// return headers; +// } }); // data with no scope requests Modified: branches/jredmine-1.x/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java 2012-07-19 09:09:40 UTC (rev 320) @@ -23,16 +23,10 @@ * #L% */ -import com.google.common.base.Charsets; import com.google.common.collect.ArrayListMultimap; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.io.rest.RestException; import org.nuiton.jredmine.model.Attachment; import org.nuiton.jredmine.model.Issue; import org.nuiton.jredmine.model.IssueCategory; @@ -45,15 +39,10 @@ import org.nuiton.jredmine.model.User; import org.nuiton.jredmine.model.Version; import org.nuiton.jredmine.model.io.xpp3.RedmineDataConverter; -import org.nuiton.jredmine.rest.RedmineRestClient; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.net.URL; import java.util.Date; import java.util.List; -import java.util.Properties; /** * TODO @@ -88,81 +77,21 @@ return "1925"; } + public RedmineServiceConfiguration obtainRedmineConfiguration() throws IOException { - protected RedmineServiceConfiguration obtainRedmineConfiguration() throws IOException { - - RedmineServiceConfiguration conf = newLogguedConfiguration(); - - boolean ok = false; - - RedmineRestClient client = new RedmineRestClient(conf); - - try { - - client.open(); - ok = client.isOpen(); - } catch (Exception e) { - - if (log.isDebugEnabled()) { - log.debug("Could not connect to redmine with configuration: " + conf, e); - } - - } finally { - try { - client.close(); - } catch (RestException e) { - if (log.isErrorEnabled()) { - log.error("Could not close session", e); - } - } - } - - if (!ok) { - - // try to connect anonymous - conf = newAnonymousConfiguration(); - - client = new RedmineRestClient(conf); - - try { - - client.open(); - ok = client.isOpen(); - } catch (Exception e) { - - if (log.isDebugEnabled()) { - log.debug("Could not connect to redmine with configuration: " + conf, e); - } - } finally { - try { - client.close(); - } catch (RestException e) { - if (log.isErrorEnabled()) { - log.error("Could not close session", e); - } - } - } - } - - if (!ok) { - - // no conf available - conf = null; - } - return conf; + return RedmineConfigurationUtil.obtainRedmineConfiguration( + newAnonymousConfiguration()); } public RedmineServiceConfiguration newAnonymousConfiguration() throws IOException { - RedmineServiceConfiguration conf = new FakeRedmineServiceConfiguration(); - copyConfiguration(getAnonymousConfiguration(), conf); + RedmineServiceConfiguration conf = RedmineConfigurationUtil.cloneConfiguration(getAnonymousConfiguration()); return conf; } public RedmineServiceConfiguration newLogguedConfiguration() throws IOException { - RedmineServiceConfiguration conf = new FakeRedmineServiceConfiguration(); - copyConfiguration(getLogguedConfiguration(), conf); + RedmineServiceConfiguration conf = RedmineConfigurationUtil.cloneConfiguration(getLogguedConfiguration()); return conf; } @@ -183,56 +112,11 @@ protected RedmineServiceConfiguration getAnonymousConfiguration() throws IOException { if (anonymousConfiguration == null) { + anonymousConfiguration = RedmineConfigurationUtil.newAnonymousConfiguration( + "jredmine-test.properties", + "/test-config.properties" + ); - Properties props = new Properties(); - - InputStream inputStream = null; - try { - String jredmineConfiguration = System.getenv("jredmine-test.properties"); - if (jredmineConfiguration == null) { - if (log.isWarnEnabled()) { - log.warn("Could not find environement variable " + - "'jredmine-test.properties' will use " + - "default test configuration"); - } - - inputStream = getClass().getResourceAsStream("/test-config.properties"); - } else { - - File file = new File(jredmineConfiguration); - - if (!file.exists()) { - throw new IllegalStateException("Could not find " + jredmineConfiguration + - " file"); - } - inputStream = FileUtils.openInputStream(file); - } - props.load(inputStream); - } finally { - if (inputStream != null) { - inputStream.close(); - } - } - anonymousConfiguration = new FakeRedmineServiceConfiguration(); - - String url = props.getProperty("test.redmineUrl"); - anonymousConfiguration.setRestUrl(new URL(url)); - - String e = props.getProperty("test.encoding"); - anonymousConfiguration.setEncoding(e); - - String verbose = props.getProperty("test.verbose"); - if (StringUtils.isNotEmpty(verbose)) { - anonymousConfiguration.setVerbose(Boolean.valueOf(verbose)); - } - anonymousConfiguration.setEncoding(Charsets.UTF_8.name()); - anonymousConfiguration.setAnonymous(true); - - verbose = System.getenv("jredmine-test.verbose"); - if (StringUtils.isNotEmpty(verbose)) { - anonymousConfiguration.setVerbose(Boolean.valueOf(verbose)); - } - } return anonymousConfiguration; } @@ -241,43 +125,12 @@ throws IOException { if (logguedConfiguration == null) { - // use anonymous configuration - RedmineServiceConfiguration anoConf = getAnonymousConfiguration(); - if (anoConf != null) { - logguedConfiguration = new FakeRedmineServiceConfiguration(); - copyConfiguration(anoConf, logguedConfiguration); - - // get system login password from env - String login = System.getenv("jredmine-test.login"); - String password = System.getenv("jredmine-test.password"); - String apiKey = System.getenv("jredmine-test.apiKey"); - if (!"null".equals(login)) { - logguedConfiguration.setRestUsername(login); - } - if (!"null".equals(password)) { - logguedConfiguration.setRestPassword(password); - } - if (!"null".equals(apiKey)) { - logguedConfiguration.setApiKey(apiKey); - } - logguedConfiguration.setAnonymous(false); - } + logguedConfiguration = RedmineConfigurationUtil.newLogguedConfiguration(anoConf); } return logguedConfiguration; } - protected void copyConfiguration(RedmineServiceConfiguration src, - RedmineServiceConfiguration dst) { - dst.setRestUrl(src.getRestUrl()); - dst.setRestUsername(src.getRestUsername()); - dst.setRestPassword(src.getRestPassword()); - dst.setApiKey(src.getApiKey()); - dst.setEncoding(src.getEncoding()); - dst.setVerbose(src.isVerbose()); - dst.setAnonymous(src.isAnonymous()); - } - public List<Attachment> getAttachments() { return get(Attachment.class); } @@ -785,115 +638,4 @@ attachmentToAdd.setDescription("Description"); return attachmentToAdd; } - - /** - * Configuration of a redmine service for test purposes. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.4 - */ - public static class FakeRedmineServiceConfiguration implements RedmineServiceConfiguration { - - URL restUrl; - - String restUsername; - - String restPassword; - - boolean verbose; - - boolean anonymous; - - String encoding; - - String apiKey; - - @Override - public String getEncoding() { - return encoding; - } - - @Override - public void setEncoding(String encoding) { - this.encoding = encoding; - } - - @Override - public String getRestPassword() { - return restPassword; - } - - @Override - public void setRestPassword(String restPassword) { - this.restPassword = restPassword; - } - - @Override - public URL getRestUrl() { - return restUrl; - } - - @Override - public void setRestUrl(URL restUrl) { - this.restUrl = restUrl; - } - - @Override - public String getRestUsername() { - return restUsername; - } - - @Override - public void setRestUsername(String restUsername) { - this.restUsername = restUsername; - } - - @Override - public boolean isVerbose() { - return verbose; - } - - @Override - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - @Override - public boolean isAnonymous() { - return anonymous; - } - - @Override - public void setAnonymous(boolean anonymous) { - this.anonymous = anonymous; - } - - @Override - public String toString() { - ToStringBuilder b = new ToStringBuilder( - this, - ToStringStyle.MULTI_LINE_STYLE - ); - b.append("redmineUrl", restUrl); - if (anonymous) { - b.append("anonymous", true); - } else { - b.append("redmineUsername", restUsername); - b.append("redminePassword", "***"); - } - b.append("encoding", encoding); - b.append("verbose", verbose); - return b.toString(); - } - - @Override - public String getApiKey() { - return apiKey; - } - - @Override - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - } } Modified: branches/jredmine-1.x/jredmine-client/src/test/resources/test-config.properties =================================================================== --- branches/jredmine-1.x/jredmine-client/src/test/resources/test-config.properties 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-client/src/test/resources/test-config.properties 2012-07-19 09:09:40 UTC (rev 320) @@ -23,6 +23,6 @@ # #L% ### # default values for test configuration (see org.nuiton.jredmine.TestHelper) -test.redmineUrl=http://nuiton.org -test.encoding=UTF-8 -test.verbose=false \ No newline at end of file +jredmine-test.url=http://nuiton.org +jredmine-test.encoding=UTF-8 +jredmine-test.verbose=false \ No newline at end of file Modified: branches/jredmine-1.x/jredmine-maven-plugin/pom.xml =================================================================== --- branches/jredmine-1.x/jredmine-maven-plugin/pom.xml 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-maven-plugin/pom.xml 2012-07-19 09:09:40 UTC (rev 320) @@ -63,11 +63,6 @@ </dependency> <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - </dependency> - - <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> Modified: branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/RedmineFixtures.java =================================================================== --- branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/RedmineFixtures.java 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/RedmineFixtures.java 2012-07-19 09:09:40 UTC (rev 320) @@ -23,21 +23,10 @@ * #L% */ -import com.google.common.base.Charsets; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.io.rest.RestException; -import org.nuiton.jredmine.rest.RedmineRestClient; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Properties; /** * TODO @@ -72,15 +61,13 @@ public RedmineServiceConfiguration newAnonymousConfiguration() throws IOException { - RedmineServiceConfiguration conf = new FakeRedmineServiceConfiguration(); - copyConfiguration(getAnonymousConfiguration(), conf); + RedmineServiceConfiguration conf = RedmineConfigurationUtil.cloneConfiguration(getAnonymousConfiguration()); return conf; } public RedmineServiceConfiguration newLogguedConfiguration() throws IOException { - RedmineServiceConfiguration conf = new FakeRedmineServiceConfiguration(); - copyConfiguration(getLogguedConfiguration(), conf); + RedmineServiceConfiguration conf = RedmineConfigurationUtil.cloneConfiguration(getLogguedConfiguration()); return conf; } @@ -98,132 +85,14 @@ return service; } - public void copyConfiguration(RedmineServiceConfiguration src, - RedmineServiceConfiguration dst) { - dst.setRestUrl(src.getRestUrl()); - dst.setRestUsername(src.getRestUsername()); - dst.setRestPassword(src.getRestPassword()); - dst.setEncoding(src.getEncoding()); - dst.setVerbose(src.isVerbose()); - dst.setAnonymous(src.isAnonymous()); - } - - public RedmineServiceConfiguration obtainRedmineConfiguration() throws IOException { - - RedmineServiceConfiguration conf = newLogguedConfiguration(); - - boolean ok = false; - - RedmineRestClient client = new RedmineRestClient(conf); - - try { - - client.open(); - ok = client.isOpen(); - } catch (Exception e) { - - if (log.isDebugEnabled()) { - log.debug("Could not connect to redmine with configuration: " + conf, e); - } - - } finally { - try { - client.close(); - } catch (RestException e) { - if (log.isErrorEnabled()) { - log.error("Could not close session", e); - } - } - } - - if (!ok) { - - // try to connect anonymous - conf = newAnonymousConfiguration(); - - client = new RedmineRestClient(conf); - - try { - - client.open(); - ok = client.isOpen(); - } catch (Exception e) { - - if (log.isDebugEnabled()) { - log.debug("Could not connect to redmine with configuration: " + conf, e); - } - } finally { - try { - client.close(); - } catch (RestException e) { - if (log.isErrorEnabled()) { - log.error("Could not close session", e); - } - } - } - } - - if (!ok) { - - // no conf available - conf = null; - } - return conf; - } - protected RedmineServiceConfiguration getAnonymousConfiguration() throws IOException { if (anonymousConfiguration == null) { + anonymousConfiguration = RedmineConfigurationUtil.newAnonymousConfiguration( + "jredmine-test.properties", + "/test-config.properties" + ); - Properties props = new Properties(); - - InputStream inputStream = null; - try { - String jredmineConfiguration = System.getenv("jredmine-test.properties"); - if (jredmineConfiguration == null) { - if (log.isWarnEnabled()) { - log.warn("Could not find environement variable " + - "'jredmine-test.properties' will use " + - "default test configuration"); - } - - inputStream = getClass().getResourceAsStream("/test-config.properties"); - } else { - - File file = new File(jredmineConfiguration); - - if (!file.exists()) { - throw new IllegalStateException("Could not find " + jredmineConfiguration + - " file"); - } - inputStream = FileUtils.openInputStream(file); - } - props.load(inputStream); - } finally { - if (inputStream != null) { - inputStream.close(); - } - } - anonymousConfiguration = new FakeRedmineServiceConfiguration(); - - String url = props.getProperty("test.redmineUrl"); - anonymousConfiguration.setRestUrl(new URL(url)); - - String e = props.getProperty("test.encoding"); - anonymousConfiguration.setEncoding(e); - - String verbose = props.getProperty("test.verbose"); - if (StringUtils.isNotEmpty(verbose)) { - anonymousConfiguration.setVerbose(Boolean.valueOf(verbose)); - } - anonymousConfiguration.setEncoding(Charsets.UTF_8.name()); - anonymousConfiguration.setAnonymous(true); - - verbose = System.getenv("jredmine-test.verbose"); - if (StringUtils.isNotEmpty(verbose)) { - anonymousConfiguration.setVerbose(Boolean.valueOf(verbose)); - } - } return anonymousConfiguration; } @@ -232,136 +101,16 @@ throws IOException { if (logguedConfiguration == null) { - // use anonymous configuration - RedmineServiceConfiguration anoConf = getAnonymousConfiguration(); - if (anoConf != null) { - logguedConfiguration = new FakeRedmineServiceConfiguration(); - copyConfiguration(anoConf, logguedConfiguration); - - // get system login password from env - String login = System.getenv("jredmine-test.login"); - String password = System.getenv("jredmine-test.password"); - if (!"null".equals(login)) { - logguedConfiguration.setRestUsername(login); - } - if (!"null".equals(password)) { - logguedConfiguration.setRestPassword(password); - } - logguedConfiguration.setAnonymous(false); - } + logguedConfiguration = RedmineConfigurationUtil.newLogguedConfiguration(anoConf); } return logguedConfiguration; } - /** - * Configuration of a redmine service for test purposes. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.4 - */ - public static class FakeRedmineServiceConfiguration implements RedmineServiceConfiguration { + public RedmineServiceConfiguration obtainRedmineConfiguration() throws IOException { - URL restUrl; - - String restUsername; - - String restPassword; - - boolean verbose; - - boolean anonymous; - - String encoding; - - String apiKey; - - @Override - public String getEncoding() { - return encoding; - } - - @Override - public void setEncoding(String encoding) { - this.encoding = encoding; - } - - @Override - public String getRestPassword() { - return restPassword; - } - - @Override - public void setRestPassword(String restPassword) { - this.restPassword = restPassword; - } - - @Override - public URL getRestUrl() { - return restUrl; - } - - @Override - public void setRestUrl(URL restUrl) { - this.restUrl = restUrl; - } - - @Override - public String getRestUsername() { - return restUsername; - } - - @Override - public void setRestUsername(String restUsername) { - this.restUsername = restUsername; - } - - @Override - public boolean isVerbose() { - return verbose; - } - - @Override - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - @Override - public boolean isAnonymous() { - return anonymous; - } - - @Override - public void setAnonymous(boolean anonymous) { - this.anonymous = anonymous; - } - - @Override - public String toString() { - ToStringBuilder b = new ToStringBuilder( - this, - ToStringStyle.MULTI_LINE_STYLE - ); - b.append("redmineUrl", restUrl); - if (anonymous) { - b.append("anonymous", true); - } else { - b.append("redmineUsername", restUsername); - b.append("redminePassword", "***"); - } - b.append("encoding", encoding); - b.append("verbose", verbose); - return b.toString(); - } - - @Override - public String getApiKey() { - return apiKey; - } - - @Override - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } + return RedmineConfigurationUtil.obtainRedmineConfiguration( + newAnonymousConfiguration()); } + } Modified: branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java =================================================================== --- branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java 2012-07-19 09:09:40 UTC (rev 320) @@ -30,6 +30,7 @@ import org.codehaus.plexus.util.StringUtils; import org.junit.After; import org.junit.Assert; +import org.nuiton.jredmine.RedmineConfigurationUtil; import org.nuiton.jredmine.RedmineFixtures; import org.nuiton.jredmine.RedmineServiceConfiguration; import org.nuiton.plugin.AbstractMojoTest; @@ -103,7 +104,7 @@ RedmineServiceConfiguration configuration = getConfiguration(); // copy redmine test server configuration - getFixtures().copyConfiguration(configuration, mojo); + RedmineConfigurationUtil.copyConfiguration(configuration, mojo); beforeMojoInit(mojo, pomFile); Modified: branches/jredmine-1.x/jredmine-maven-plugin/src/test/resources/test-config.properties =================================================================== --- branches/jredmine-1.x/jredmine-maven-plugin/src/test/resources/test-config.properties 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/jredmine-maven-plugin/src/test/resources/test-config.properties 2012-07-19 09:09:40 UTC (rev 320) @@ -23,6 +23,6 @@ # #L% ### # default values for test configuration (see org.nuiton.jredmine.TestHelper) -test.redmineUrl=http://nuiton.org -test.encoding=UTF-8 -test.verbose=false \ No newline at end of file +jredmine-test.url=http://nuiton.org +jredmine-test.encoding=UTF-8 +jredmine-test.verbose=false \ No newline at end of file Modified: branches/jredmine-1.x/pom.xml =================================================================== --- branches/jredmine-1.x/pom.xml 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/pom.xml 2012-07-19 09:09:40 UTC (rev 320) @@ -536,7 +536,8 @@ <projectId>jredmine</projectId> - <helperPluginVersion>1.5-SNAPSHOT</helperPluginVersion> + <!-- TODO Remove this when using mavenpom >= 3.3.5 --> + <helperPluginVersion>1.5</helperPluginVersion> <!-- must be on a fixed version, not on the snapshot to make possible release --> <jredminePluginVersion>1.3</jredminePluginVersion> @@ -557,16 +558,17 @@ <artifactId>maven-surefire-plugin</artifactId> <configuration> <environmentVariables> - <test.redmineUrl>${test.redmineUrl}</test.redmineUrl> - <jredmine-test.login> - ${jredmine-test.login} - </jredmine-test.login> + <jredmine-test.url>${jredmine-test.url}</jredmine-test.url> + <jredmine-test.login>${jredmine-test.login}</jredmine-test.login> <jredmine-test.password> ${jredmine-test.password} </jredmine-test.password> <jredmine-test.verbose> ${jredmine-test.verbose} </jredmine-test.verbose> + <jredmine-test.encoding> + ${jredmine-test.verbose} + </jredmine-test.encoding> </environmentVariables> </configuration> </plugin> Modified: branches/jredmine-1.x/src/site/apt/tests.apt =================================================================== --- branches/jredmine-1.x/src/site/apt/tests.apt 2012-07-19 06:50:02 UTC (rev 319) +++ branches/jredmine-1.x/src/site/apt/tests.apt 2012-07-19 09:09:40 UTC (rev 320) @@ -36,29 +36,45 @@ Configuration de base - Par défaut, les tests vont chercher dans un fichier dans le class-path - nommé <<test-config.properties>> les informations de base pour communiquer - avec un servuer redmine. + Par défaut, la configuration des tests est chargée à partir d'un fichier + properties (nommé <<test-config.properties>>) localisé dans le class-path. Voici la configuration par défaut actuelle : ------------------------------------------------------------------------------- -test.redmineUrl=http://nuiton.org -test.encoding=UTF-8 -test.verbose=false +jredmine-test.url=http://nuiton.org +jredmine-test.encoding=UTF-8 +jredmine-test.verbose=false ------------------------------------------------------------------------------- Cette configuration permet de tester uniquement les fonctionnalités non authentifiés de l'api. -Surcharger la configuration de base +* Changer la configuration de base + Il est possible d'indiquer le chemin d'un fichier de configuration à utiliser + au lieu de celui fournit via une variable d'environnement nommée + <<jredmine-test.properties>>. + + Exemple : + +------------------------------------------------------------------------------- +mvn install -Djredmine-test.properties=/tmp/maConfig.properties +------------------------------------------------------------------------------- + +* Surcharger la configuration de base + Il est possible de surcharger cette configuration de base en utilisant des variables d'environnement : - * <<test.redmineUrl>> : pour changer l'url du serveur redmine à utiliser - * <<jredmine-test.verbose>> : pour changer la verbosité dans les tests. + * <<jredmine-test.url>> : pour changer l'url du serveur redmine à utiliser + * <<jredmine-test.encoding>> : pour changer l'encoding dans les tests + + * <<jredmine-test.verbose>> : pour changer la verbosité dans les tests + + [] + Exemple : ------------------------------------------------------------------------------- @@ -70,8 +86,12 @@ Des tests ont aussi été écrits pour tester les fonctionnées authentifiés de l'api. - Pour cela il faut fournir aux tests un login + password et ceci peut être fait de plusieurs manières différentes + Pour cela il faut fournir aux tests un login + password et ceci peut être + fait de plusieurs manières différentes. + A noter que dans la version 2.0, on pourra aussi utiliser un <<apiKey>> (voir + {{{http://nuiton.org/issues/2197}évolution 2197}}). + * Via des variables d'environnement Renseigner les deux variables suivantes : @@ -80,6 +100,8 @@ * <<jredmine-test.password>> + [] + Exemple : ------------------------------------------------------------------------------- @@ -93,7 +115,7 @@ Le serveur à utiliser est nommé <<jredmine-test>> -Exemple : + Exemple : ------------------------------------------------------------------------------- <!-- login to jredmine test --> @@ -128,7 +150,7 @@ <!-- On crée un profile vide par défaut pour ne pas avoir de warning pour les autres builds maven... ... --> <profile> - <id>jredmine-server-test</id> + <id>jredmine-test-profile</id> </profile> <!-- ... --> @@ -136,12 +158,14 @@ <activeProfiles> <!-- ... --> - <activeProfile>jredmine-server-test</activeProfile> + <activeProfile>jredmine-test-profile</activeProfile> </activeProfiles> ------------------------------------------------------------------------------- Jeu de test - En version <<2.0>> le jeu de test sera configurable et clairement spécifié. + En version <<2.0>> le jeu de test sera clairement spécifié pour permettre + une meilleure testabilité du projet + (voir {{{http://nuiton.org/issues/2201}évolution 2201}}). Pour le moment, le projet testé est <<jredmine>> en version<<1.3>>.