diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 09:59:18 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 09:59:18 +0000 |
commit | 91b4e663c59c2fd03b64ce3746617e44aeab2bdd (patch) | |
tree | e6f5ca160de886d4ade86642d2f572d18de024ff /sonar-deprecated/src | |
parent | 3a268c327d4cd7dc1e1e2530d2733d42a62e0d09 (diff) | |
download | sonarqube-91b4e663c59c2fd03b64ce3746617e44aeab2bdd.tar.gz sonarqube-91b4e663c59c2fd03b64ce3746617e44aeab2bdd.zip |
move deprecated check api to sonar-deprecated
Diffstat (limited to 'sonar-deprecated/src')
46 files changed, 3045 insertions, 0 deletions
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/AnnotationCheckerFactory.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/AnnotationCheckerFactory.java new file mode 100644 index 00000000000..9f8574d9e83 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/AnnotationCheckerFactory.java @@ -0,0 +1,160 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.checkers; + +import org.apache.commons.lang.StringUtils; +import org.sonar.api.checks.profiles.Check; +import org.sonar.api.checks.profiles.CheckProfile; +import org.sonar.check.AnnotationIntrospector; +import org.sonar.check.CheckProperty; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.Map; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated + +public class AnnotationCheckerFactory<CHECKER> extends CheckerFactory<CHECKER> { + + private CheckProfile profile; + private String repositoryKey; + private Collection<Class<CHECKER>> checkerClasses; + + public AnnotationCheckerFactory(CheckProfile profile, String repositoryKey, Collection<Class<CHECKER>> checkerClasses) { + this.profile = profile; + this.repositoryKey = repositoryKey; + this.checkerClasses = checkerClasses; + } + + public Map<Check, CHECKER> create() { + Map<String, Class<CHECKER>> classesByKey = getClassesByKey(checkerClasses); + + Map<Check, CHECKER> map = new IdentityHashMap<Check, CHECKER>(); + for (Check check : profile.getChecks(repositoryKey)) { + Class<CHECKER> clazz = classesByKey.get(check.getTemplateKey()); + if (clazz != null) { + CHECKER checker = instantiate(check, clazz); + if (checker != null) { + map.put(check, checker); + } + } + } + return map; + } + + CHECKER instantiate(Check check, Class<CHECKER> clazz) { + try { + CHECKER checker = clazz.newInstance(); + configureFields(check, checker); + return checker; + + } catch (UnvalidCheckerException e) { + throw e; + + } catch (Exception e) { + throw new UnvalidCheckerException("The checker " + clazz.getCanonicalName() + " can not be created", e); + } + } + + private void configureFields(Check check, CHECKER checker) throws IllegalAccessException { + for (Map.Entry<String, String> entry : check.getProperties().entrySet()) { + Field field = getField(checker, entry.getKey()); + if (field == null) { + throw new UnvalidCheckerException("The field " + entry.getKey() + " does not exist or is not annotated with @CheckProperty"); + } + if (StringUtils.isNotBlank(entry.getValue())) { + configureField(checker, field, entry); + } + } + + } + + private void configureField(Object checker, Field field, Map.Entry<String, String> parameter) throws IllegalAccessException { + field.setAccessible(true); + + if (field.getType().equals(String.class)) { + field.set(checker, parameter.getValue()); + + } else if (field.getType().getSimpleName().equals("int")) { + field.setInt(checker, Integer.parseInt(parameter.getValue())); + + } else if (field.getType().getSimpleName().equals("short")) { + field.setShort(checker, Short.parseShort(parameter.getValue())); + + } else if (field.getType().getSimpleName().equals("long")) { + field.setLong(checker, Long.parseLong(parameter.getValue())); + + } else if (field.getType().getSimpleName().equals("double")) { + field.setDouble(checker, Double.parseDouble(parameter.getValue())); + + } else if (field.getType().getSimpleName().equals("boolean")) { + field.setBoolean(checker, Boolean.parseBoolean(parameter.getValue())); + + } else if (field.getType().getSimpleName().equals("byte")) { + field.setByte(checker, Byte.parseByte(parameter.getValue())); + + } else if (field.getType().equals(Integer.class)) { + field.set(checker, new Integer(Integer.parseInt(parameter.getValue()))); + + } else if (field.getType().equals(Long.class)) { + field.set(checker, new Long(Long.parseLong(parameter.getValue()))); + + } else if (field.getType().equals(Double.class)) { + field.set(checker, new Double(Double.parseDouble(parameter.getValue()))); + + } else if (field.getType().equals(Boolean.class)) { + field.set(checker, Boolean.valueOf(Boolean.parseBoolean(parameter.getValue()))); + + } else { + throw new UnvalidCheckerException("The type of the field " + field + " is not supported: " + field.getType()); + } + } + + private Field getField(Object checker, String key) { + Field[] fields = checker.getClass().getDeclaredFields(); + for (Field field : fields) { + CheckProperty annotation = field.getAnnotation(CheckProperty.class); + if (annotation != null) { + if (key.equals(field.getName()) || key.equals(annotation.key())) { + return field; + } + } + } + return null; + } + + private Map<String, Class<CHECKER>> getClassesByKey(Collection<Class<CHECKER>> checkerClasses) { + Map<String, Class<CHECKER>> result = new HashMap<String, Class<CHECKER>>(); + for (Class<CHECKER> checkerClass : checkerClasses) { + String key = AnnotationIntrospector.getCheckKey(checkerClass); + if (key != null) { + result.put(key, checkerClass); + } + } + return result; + } + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/CheckerFactory.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/CheckerFactory.java new file mode 100644 index 00000000000..de0a12a738c --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/CheckerFactory.java @@ -0,0 +1,33 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.checkers; + +import org.sonar.api.checks.profiles.Check; + +import java.util.Map; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public abstract class CheckerFactory<CHECKER> { + public abstract Map<Check, CHECKER> create(); +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/MessageDispatcher.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/MessageDispatcher.java new file mode 100644 index 00000000000..7f563920825 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/MessageDispatcher.java @@ -0,0 +1,111 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.checkers; + +import com.google.common.collect.Maps; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.checks.profiles.Check; +import org.sonar.api.checks.profiles.CheckProfile; +import org.sonar.api.resources.Resource; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RulePriority; +import org.sonar.api.rules.Violation; +import org.sonar.check.Message; + +import java.util.Collection; +import java.util.Locale; +import java.util.Map; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class MessageDispatcher { + + private Map<Check, Object> checkersByCheck; + private Map<Object, Check> checksByChecker; + private SensorContext context; + + public MessageDispatcher(SensorContext context) { + this.context = context; + checkersByCheck = Maps.newIdentityHashMap(); + checksByChecker = Maps.newIdentityHashMap(); + } + + public void registerChecker(Check check, Object checker) { + checkersByCheck.put(check, checker); + checksByChecker.put(checker, check); + } + + public void registerCheckers(CheckerFactory factory) { + Map<Check, Object> map = factory.create(); + for (Map.Entry<Check, Object> entry : map.entrySet()) { + registerChecker(entry.getKey(), entry.getValue()); + } + } + + public void registerCheckers(CheckProfile profile) { + for (Check check : profile.getChecks()) { + registerChecker(check, check); + } + } + + public Object getChecker(Check check) { + return checkersByCheck.get(check); + } + + public Check getCheck(Object checker) { + return checksByChecker.get(checker); + } + + public Collection getCheckers() { + return checkersByCheck.values(); + } + + public void unregisterCheck(Check check) { + Object checker = checkersByCheck.remove(check); + if (checker != null) { + checksByChecker.remove(checker); + } + } + + public void unregisterChecks(CheckProfile profile) { + for (Check check : profile.getChecks()) { + unregisterCheck(check); + } + } + + public void log(Resource resource, Message message) { + Object checker = message.getChecker(); + Check check = getCheck(checker); + Violation violation = new Violation(new Rule(check.getRepositoryKey(), check.getTemplateKey()), resource); + violation.setLineId(message.getLine()); + violation.setMessage(message.getText(Locale.ENGLISH)); + violation.setPriority(RulePriority.fromCheckPriority(check.getPriority())); + context.saveViolation(violation); + } + + public void clear() { + checkersByCheck.clear(); + checksByChecker.clear(); + } + +}
\ No newline at end of file diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/UnvalidCheckerException.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/UnvalidCheckerException.java new file mode 100644 index 00000000000..0c98a1fece3 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/checkers/UnvalidCheckerException.java @@ -0,0 +1,42 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.checkers; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class UnvalidCheckerException extends RuntimeException { + public UnvalidCheckerException() { + } + + public UnvalidCheckerException(String message) { + super(message); + } + + public UnvalidCheckerException(String message, Throwable cause) { + super(message, cause); + } + + public UnvalidCheckerException(Throwable cause) { + super(cause); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/AnnotationCheckProfileFactory.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/AnnotationCheckProfileFactory.java new file mode 100644 index 00000000000..e5084965d42 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/AnnotationCheckProfileFactory.java @@ -0,0 +1,72 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import org.sonar.check.AnnotationIntrospector; +import org.sonar.check.BelongsToProfile; +import org.sonar.check.BelongsToProfiles; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public final class AnnotationCheckProfileFactory { + + private AnnotationCheckProfileFactory() { + } + + public static Collection<CheckProfile> create(String repositoryKey, String language, Collection<Class> checkClasses) { + Map<String, CheckProfile> profilesByTitle = new HashMap<String, CheckProfile>(); + + if (checkClasses != null) { + for (Class aClass : checkClasses) { + BelongsToProfiles belongsToProfiles = (BelongsToProfiles) aClass.getAnnotation(BelongsToProfiles.class); + if (belongsToProfiles != null) { + for (BelongsToProfile belongsToProfile : belongsToProfiles.value()) { + registerProfile(profilesByTitle, aClass, belongsToProfile, repositoryKey, language); + } + } + BelongsToProfile belongsToProfile = (BelongsToProfile) aClass.getAnnotation(BelongsToProfile.class); + registerProfile(profilesByTitle, aClass, belongsToProfile, repositoryKey, language); + } + } + + return profilesByTitle.values(); + } + + private static void registerProfile(Map<String, CheckProfile> profilesByTitle, Class aClass, BelongsToProfile belongsToProfile, String repositoryKey, String language) { + if (belongsToProfile != null) { + String title = belongsToProfile.title(); + CheckProfile profile = profilesByTitle.get(title); + if (profile == null) { + profile = new CheckProfile(title, language); + profilesByTitle.put(title, profile); + } + Check check = new Check(repositoryKey, AnnotationIntrospector.getCheckKey(aClass)); + check.setPriority(belongsToProfile.priority()); + profile.addCheck(check); + } + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/Check.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/Check.java new file mode 100644 index 00000000000..b33c3a11ddc --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/Check.java @@ -0,0 +1,111 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import org.apache.commons.lang.builder.ToStringBuilder; +import org.sonar.check.Priority; + +import java.util.HashMap; +import java.util.Map; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class Check { + + private String repositoryKey; + private String templateKey; + private Priority priority = null; + private final Map<String, String> properties = new HashMap<String,String>(); + + public Check() { + } + + public Check(String repositoryKey, String templateKey) { + this.repositoryKey = repositoryKey; + this.templateKey = templateKey; + } + + public String getTemplateKey() { + return templateKey; + } + + public String getRepositoryKey() { + return repositoryKey; + } + + public void setRepositoryKey(String repositoryKey) { + this.repositoryKey = repositoryKey; + } + + public void setTemplateKey(String templateKey) { + this.templateKey = templateKey; + } + + public Priority getPriority() { + return priority; + } + + public void setPriority(Priority priority) { + this.priority = priority; + } + + public Map<String, String> getProperties() { + return properties; + } + + public String getProperty(String key) { + return properties.get(key); + } + + public void addProperty(String key, Object value) { + properties.put(key, value.toString()); + } + + public void addProperties(Map<String, String> properties) { + this.properties.putAll(properties); + } + + public void setProperties(Map<String, String> properties) { + this.properties.clear(); + this.properties.putAll(properties); + } + + @Override + public boolean equals(Object o) { + return o == this; + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("repository", repositoryKey) + .append("template", templateKey) + .append("priority", priority) + .toString(); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfile.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfile.java new file mode 100644 index 00000000000..2a47f800bdc --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfile.java @@ -0,0 +1,136 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import org.sonar.api.BatchExtension; +import org.sonar.api.ServerExtension; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class CheckProfile implements BatchExtension, ServerExtension { + + private String name; + private String language; + private List<Check> checks = new ArrayList<Check>(); + + public CheckProfile(String name, String language) { + if (name == null) { + throw new IllegalArgumentException("Name can not be null"); + } + if (language == null) { + throw new IllegalArgumentException("Language can not be null"); + } + this.name = name; + this.language = language; + } + + public String getName() { + return name; + } + + public String getLanguage() { + return language; + } + + public List<Check> getChecks() { + return checks; + } + + public List<Check> getChecks(String repositoryKey) { + List<Check> result = new ArrayList<Check>(); + for (Check check : getChecks()) { + if (check.getRepositoryKey().equals(repositoryKey)) { + result.add(check); + } + } + return result; + } + + public List<Check> getChecks(String repositoryKey, String templateKey) { + List<Check> result = new ArrayList<Check>(); + List<Check> repoChecks = getChecks(repositoryKey); + for (Check repoCheck : repoChecks) { + if (repoCheck.getTemplateKey().equals(templateKey)) { + result.add(repoCheck); + } + } + return result; + } + + /** + * We assume there is only one check for this template + */ + public Check getCheck(String repositoryKey, String templateKey) { + List<Check> repoChecks = getChecks(repositoryKey); + for (Check repoCheck : repoChecks) { + if (repoCheck.getTemplateKey().equals(templateKey)) { + return repoCheck; + } + } + return null; + } + + public void addCheck(Check check) { + checks.add(check); + } + + public void setChecks(Collection<Check> list) { + checks.clear(); + checks.addAll(list); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + CheckProfile profile = (CheckProfile) o; + if (!language.equals(profile.language)) { + return false; + } + if (!name.equals(profile.name)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int result = name.hashCode(); + result = 31 * result + language.hashCode(); + return result; + } + + @Override + public String toString() { + return name; + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfileProvider.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfileProvider.java new file mode 100644 index 00000000000..0482cf447fd --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfileProvider.java @@ -0,0 +1,35 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import org.sonar.api.ServerExtension; + +import java.util.Collection; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public abstract class CheckProfileProvider implements ServerExtension { + + public abstract Collection<CheckProfile> provide(); + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfileXmlMarshaller.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfileXmlMarshaller.java new file mode 100644 index 00000000000..4e7d359643a --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/profiles/CheckProfileXmlMarshaller.java @@ -0,0 +1,156 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.MarshallingContext; +import com.thoughtworks.xstream.converters.UnmarshallingContext; +import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; +import com.thoughtworks.xstream.io.xml.CompactWriter; +import com.thoughtworks.xstream.io.xml.XppDriver; +import org.apache.commons.io.IOUtils; +import org.sonar.check.Priority; + +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.Writer; +import java.util.Map; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public final class CheckProfileXmlMarshaller { + + public static void toXml(CheckProfile profile, Writer writer) { + getXStream().toXML(profile, writer); + } + + public static CheckProfile fromXml(Reader xml) { + return (CheckProfile) getXStream().fromXML(xml); + } + + public static CheckProfile fromXmlInClasspath(String pathToXml) { + return fromXmlInClasspath(pathToXml, CheckProfileXmlMarshaller.class); + } + + public static CheckProfile fromXmlInClasspath(String pathToXml, Class clazz) { + Reader reader = new InputStreamReader(clazz.getResourceAsStream(pathToXml)); + try { + return fromXml(reader); + } finally { + IOUtils.closeQuietly(reader); + } + } + + private static XStream getXStream() { + XStream xstream = new XStream(new CompactDriver()); + xstream.setClassLoader(CheckProfileXmlMarshaller.class.getClassLoader()); + xstream.registerConverter(new CheckConverter()); + xstream.alias("profile", CheckProfile.class); + xstream.alias("check", Check.class); + xstream.addImplicitCollection(CheckProfile.class, "checks"); + return xstream; + } + + private static class CompactDriver extends XppDriver { + @Override + public HierarchicalStreamWriter createWriter(Writer out) { + return new XDataPrintWriter(out); + } + } + + + private static class XDataPrintWriter extends CompactWriter { + public XDataPrintWriter(Writer writer) { + super(writer, XML_1_0); + } + } + + private static class CheckConverter implements Converter { + + public boolean canConvert(Class clazz) { + return clazz.equals(Check.class); + } + + public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { + Check check = (Check) value; + writer.startNode("repository"); + writer.setValue(check.getRepositoryKey()); + writer.endNode(); + writer.startNode("template"); + writer.setValue(check.getTemplateKey()); + writer.endNode(); + writer.startNode("priority"); + writer.setValue(check.getPriority().toString()); + writer.endNode(); + for (Map.Entry<String, String> entry : check.getProperties().entrySet()) { + if (entry.getValue() != null) { + writer.startNode("property"); + writer.startNode("key"); + writer.setValue(entry.getKey()); + writer.endNode(); + writer.startNode("value"); + // TODO is escaping automatically supported by xstream ? + writer.setValue(entry.getValue()); + writer.endNode(); + writer.endNode(); + } + } + } + + public Object unmarshal(HierarchicalStreamReader reader, + UnmarshallingContext context) { + Check check = new Check(); + while (reader.hasMoreChildren()) { + reader.moveDown(); + readValue(reader, check); + reader.moveUp(); + } + return check; + } + + private void readValue(HierarchicalStreamReader reader, Check check) { + if (reader.getNodeName().equals("repository")) { + check.setRepositoryKey(reader.getValue()); + + } else if (reader.getNodeName().equals("template")) { + check.setTemplateKey(reader.getValue()); + + } else if (reader.getNodeName().equals("priority")) { + check.setPriority(Priority.valueOf(reader.getValue())); + + } else if (reader.getNodeName().equals("property")) { + reader.moveDown(); + String key = reader.getValue(); + reader.moveUp(); + + reader.moveDown(); + String value = reader.getValue(); + reader.moveUp(); + check.addProperty(key, value); + } + } + + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/AnnotationCheckTemplateFactory.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/AnnotationCheckTemplateFactory.java new file mode 100644 index 00000000000..08d21bb6fe3 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/AnnotationCheckTemplateFactory.java @@ -0,0 +1,114 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.check.AnnotationIntrospector; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class AnnotationCheckTemplateFactory { + + private static final Logger LOG = LoggerFactory.getLogger(AnnotationCheckTemplateFactory.class); + + private Collection<Class> annotatedClasses; + + public AnnotationCheckTemplateFactory(Collection<Class> annotatedClasses) { + this.annotatedClasses = annotatedClasses; + } + + public List<CheckTemplate> create() { + List<CheckTemplate> templates = new ArrayList<CheckTemplate>(); + for (Class annotatedClass : annotatedClasses) { + BundleCheckTemplate template = create(annotatedClass); + if (template != null) { + templates.add(template); + } + } + return templates; + } + + + protected BundleCheckTemplate create(Class annotatedClass) { + org.sonar.check.Check checkAnnotation = AnnotationIntrospector.getCheckAnnotation(annotatedClass); + if (checkAnnotation == null) { + LOG.warn("The class " + annotatedClass.getCanonicalName() + " is not a check template. It should be annotated with " + CheckTemplate.class); + return null; + } + + BundleCheckTemplate check = toTemplate(annotatedClass, checkAnnotation); + Field[] fields = annotatedClass.getDeclaredFields(); + if (fields != null) { + for (Field field : fields) { + BundleCheckTemplateProperty property = toProperty(check, field); + if (property != null) { + check.addProperty(property); + } + } + } + return check; + } + + private static BundleCheckTemplate toTemplate(Class annotatedClass, org.sonar.check.Check checkAnnotation) { + String key = AnnotationIntrospector.getCheckKey(annotatedClass); + String bundle = getBundleBaseName(checkAnnotation, annotatedClass); + + BundleCheckTemplate check = new BundleCheckTemplate(key, bundle); + check.setDefaultDescription(checkAnnotation.description()); + check.setDefaultTitle(checkAnnotation.title()); + check.setIsoCategory(checkAnnotation.isoCategory()); + check.setPriority(checkAnnotation.priority()); + + return check; + } + + private static String getBundleBaseName(org.sonar.check.Check checkAnnotation, Class annotatedClass) { + String bundle = checkAnnotation.bundle(); + if (StringUtils.isBlank(bundle)) { + bundle = annotatedClass.getCanonicalName(); + } + return bundle; + } + + private static BundleCheckTemplateProperty toProperty(BundleCheckTemplate check, Field field) { + org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class); + if (propertyAnnotation != null) { + String fieldKey = propertyAnnotation.key(); + if (fieldKey==null || "".equals(fieldKey)) { + fieldKey = field.getName(); + } + BundleCheckTemplateProperty property = new BundleCheckTemplateProperty(check, fieldKey); + property.setDefaultTitle(propertyAnnotation.title()); + property.setDefaultDescription(propertyAnnotation.description()); + return property; + } + return null; + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/BundleCheckTemplate.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/BundleCheckTemplate.java new file mode 100644 index 00000000000..e54554b84cc --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/BundleCheckTemplate.java @@ -0,0 +1,110 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class BundleCheckTemplate extends CheckTemplate { + private static final Logger LOG = LoggerFactory.getLogger(BundleCheckTemplate.class); + + private String bundleBaseName; + private String defaultTitle; + private String defaultDescription; + + protected BundleCheckTemplate(String key, String bundleBaseName) { + super(key); + this.bundleBaseName = bundleBaseName; + } + + protected BundleCheckTemplate(String key, Class bundleClass) { + this(key, bundleClass.getCanonicalName()); + } + + protected String getDefaultTitle() { + if (defaultTitle == null || "".equals(defaultTitle)) { + return getKey(); + } + return defaultTitle; + } + + protected void setDefaultTitle(String defaultTitle) { + this.defaultTitle = defaultTitle; + } + + protected String getDefaultDescription() { + return defaultDescription; + } + + protected void setDefaultDescription(String defaultDescription) { + this.defaultDescription = defaultDescription; + } + + @Override + public String getTitle(Locale locale) { + return getText("title", locale, getDefaultTitle()); + } + + @Override + public String getDescription(Locale locale) { + return getText("description", locale, getDefaultDescription()); + } + + @Override + public String getMessage(Locale locale, String key, Object... params) { + return null; + } + + protected String getText(String key, Locale locale, String defaultValue) { + String result = null; + ResourceBundle bundle = getBundle(locale); + if (bundle != null) { + try { + result = bundle.getString(key); + } catch (MissingResourceException e) { + LOG.debug(e.getMessage()); + } + } + if (result == null) { + result = defaultValue; + } + return result; + } + + protected ResourceBundle getBundle(Locale locale) { + try { + if (locale != null) { + return ResourceBundle.getBundle(bundleBaseName, locale); + } + } catch (MissingResourceException e) { + // do nothing : use the default values + } + return null; + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/BundleCheckTemplateProperty.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/BundleCheckTemplateProperty.java new file mode 100644 index 00000000000..ac1ebf6795a --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/BundleCheckTemplateProperty.java @@ -0,0 +1,69 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import java.util.Locale; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class BundleCheckTemplateProperty extends CheckTemplateProperty { + + private BundleCheckTemplate check; + private String defaultTitle; + private String defaultDescription; + + public BundleCheckTemplateProperty(BundleCheckTemplate check, String key) { + setKey(key); + this.check = check; + } + + public String getDefaultTitle() { + if (defaultTitle == null || "".equals(defaultTitle)) { + return getKey(); + } + return defaultTitle; + } + + public void setDefaultTitle(String s) { + this.defaultTitle = s; + } + + + @Override + public String getTitle(Locale locale) { + return check.getText("property." + getKey() + ".title", locale, getDefaultTitle()); + } + + public String getDefaultDescription() { + return defaultDescription; + } + + public void setDefaultDescription(String s) { + this.defaultDescription = s; + } + + @Override + public String getDescription(Locale locale) { + return check.getText("property." + getKey() + ".description", locale, getDefaultDescription()); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplate.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplate.java new file mode 100644 index 00000000000..a6df4a7e73f --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplate.java @@ -0,0 +1,134 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public abstract class CheckTemplate { + + protected String key; + protected String configKey; + protected Priority priority; + protected IsoCategory isoCategory; + protected List<CheckTemplateProperty> properties; + + public CheckTemplate(String key) { + this.key = key; + } + + public CheckTemplate() { + } + + public String getKey() { + return key; + } + + public void setKey(String s) { + this.key = s; + } + + public Priority getPriority() { + return priority; + } + + public void setPriority(Priority p) { + this.priority = p; + } + + public IsoCategory getIsoCategory() { + return isoCategory; + } + + public void setIsoCategory(IsoCategory c) { + this.isoCategory = c; + } + + public String getConfigKey() { + return configKey; + } + + public void setConfigKey(String configKey) { + this.configKey = configKey; + } + + public abstract String getTitle(Locale locale); + + public abstract String getDescription(Locale locale); + + public abstract String getMessage(Locale locale, String key, Object... params); + + public List<CheckTemplateProperty> getProperties() { + if (properties==null) { + return Collections.emptyList(); + } + return properties; + } + + public void addProperty(CheckTemplateProperty p) { + if (properties==null) { + properties = new ArrayList<CheckTemplateProperty>(); + } + properties.add(p); + } + + public CheckTemplateProperty getProperty(String key) { + if (properties!=null) { + for (CheckTemplateProperty property : properties) { + if (property.getKey().equals(key)) { + return property; + } + } + } + return null; + } + + /** + * Checks are equal within the same plugin. Two plugins can have two different checks with the same key. + */ + @Override + public final boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof CheckTemplate)) { + return false; + } + + CheckTemplate checkTemplate = (CheckTemplate) o; + return key.equals(checkTemplate.key); + } + + @Override + public final int hashCode() { + return key.hashCode(); + } + +}
\ No newline at end of file diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateFactory.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateFactory.java new file mode 100644 index 00000000000..d72dc1db31c --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateFactory.java @@ -0,0 +1,33 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import java.util.Collection; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public abstract class CheckTemplateFactory { + + public abstract Collection<CheckTemplate> create(); + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateProperty.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateProperty.java new file mode 100644 index 00000000000..34dce342635 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateProperty.java @@ -0,0 +1,71 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import java.util.Locale; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public abstract class CheckTemplateProperty implements Comparable<CheckTemplateProperty> { + + protected String key; + + public String getKey() { + return key; + } + + public void setKey(String s) { + this.key = s; + } + + public abstract String getTitle(Locale locale); + + public String getDescription() { + return getDescription(Locale.ENGLISH); + } + + + public abstract String getDescription(Locale locale); + + @Override + public final boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof CheckTemplateProperty)) { + return false; + } + + CheckTemplateProperty that = (CheckTemplateProperty) o; + return key.equals(that.key); + } + + @Override + public final int hashCode() { + return key.hashCode(); + } + + public int compareTo(CheckTemplateProperty o) { + return getKey().compareTo(o.getKey()); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateRepositories.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateRepositories.java new file mode 100644 index 00000000000..6a159a9bd27 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateRepositories.java @@ -0,0 +1,64 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.sonar.api.ServerExtension; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class CheckTemplateRepositories implements ServerExtension { + + private Map<String, CheckTemplateRepository> repositoriesByKey = new HashMap<String, CheckTemplateRepository>(); + + public CheckTemplateRepositories(CheckTemplateRepository[] repositories) { + if (repositories != null) { + for (CheckTemplateRepository templateRepository : repositories) { + repositoriesByKey.put(templateRepository.getKey(), templateRepository); + } + } + } + + public CheckTemplateRepositories() { + // DO NOT REMOVE THIS CONSTRUCTOR. It is used by Picocontainer when no repositories are available. + } + + public CheckTemplateRepository getRepository(String key) { + return repositoriesByKey.get(key); + } + + public Collection<CheckTemplateRepository> getRepositories() { + return repositoriesByKey.values(); + } + + public CheckTemplate getTemplate(String repositoryKey, String templateKey) { + CheckTemplateRepository repo = getRepository(repositoryKey); + if (repo != null) { + return repo.getTemplate(templateKey); + } + return null; + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateRepository.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateRepository.java new file mode 100644 index 00000000000..fa96311c8db --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/CheckTemplateRepository.java @@ -0,0 +1,203 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.apache.commons.io.IOUtils; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Language; +import org.sonar.api.rules.*; +import org.sonar.check.IsoCategory; + +import java.io.InputStream; +import java.util.*; + +/** + * @since 2.1 (experimental) + * @deprecated since 2.3 + */ +@Deprecated +public class CheckTemplateRepository implements RulesRepository { + + private String key; + private Language language; + private List<CheckTemplate> templates; + private Map<String, CheckTemplate> templatesByKey; + + + public CheckTemplateRepository() { + } + + public CheckTemplateRepository(String key) { + if (key == null) { + throw new IllegalArgumentException("Key can not be null"); + } + this.key = key; + } + + public String getKey() { + return key; + } + + public CheckTemplateRepository setKey(String key) { + this.key = key; + return this; + } + + public Language getLanguage() { + return language; + } + + public CheckTemplateRepository setLanguage(Language l) { + this.language = l; + return this; + } + + public List<CheckTemplate> getTemplates() { + if (templates == null) { + return Collections.emptyList(); + } + return templates; + } + + public CheckTemplateRepository setTemplates(List<CheckTemplate> c) { + this.templates = c; + return this; + } + + public CheckTemplate getTemplate(String key) { + if (templatesByKey == null || templatesByKey.isEmpty()) { + templatesByKey = new HashMap<String, CheckTemplate>(); + for (CheckTemplate template : templates) { + templatesByKey.put(template.getKey(), template); + } + } + return templatesByKey.get(key); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + CheckTemplateRepository that = (CheckTemplateRepository) o; + return key.equals(that.key); + + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + + public static CheckTemplateRepository createFromXml(String repositoryKey, Language language, String pathToXml) { + InputStream input = CheckTemplateRepository.class.getResourceAsStream(pathToXml); + try { + List<CheckTemplate> templates = new XmlCheckTemplateFactory().parse(input); + CheckTemplateRepository repository = new CheckTemplateRepository(repositoryKey); + repository.setTemplates(templates); + repository.setLanguage(language); + return repository; + + } finally { + IOUtils.closeQuietly(input); + } + } + + public static CheckTemplateRepository createFromAnnotatedClasses(String repositoryKey, Language language, Collection<Class> classes) { + AnnotationCheckTemplateFactory factory = new AnnotationCheckTemplateFactory(classes); + CheckTemplateRepository repository = new CheckTemplateRepository(repositoryKey); + repository.setTemplates(factory.create()); + repository.setLanguage(language); + return repository; + } + + + + + + + + + + + /* + + CODE FOR BACKWARD COMPATIBLITY + This class should not extend RulesRepository in next versions + + */ + + + public List<Rule> getInitialReferential() { + List<Rule> rules = new ArrayList<Rule>(); + for (CheckTemplate checkTemplate : getTemplates()) { + rules.add(toRule(checkTemplate)); + } + return rules; + } + + private Rule toRule(CheckTemplate checkTemplate) { + Rule rule = new Rule(getKey(), checkTemplate.getKey()); + rule.setDescription(checkTemplate.getDescription(Locale.ENGLISH)); + rule.setName(checkTemplate.getTitle(Locale.ENGLISH)); + rule.setPriority(RulePriority.fromCheckPriority(checkTemplate.getPriority())); + rule.setRulesCategory(toRuleCategory(checkTemplate.getIsoCategory())); + for (CheckTemplateProperty checkTemplateProperty : checkTemplate.getProperties()) { + RuleParam param = rule.createParameter(checkTemplateProperty.getKey()); + param.setDescription(checkTemplateProperty.getDescription(Locale.ENGLISH)); + param.setType("s"); + } + + return rule; + } + + private RulesCategory toRuleCategory(IsoCategory isoCategory) { + if (isoCategory == IsoCategory.Reliability) { + return Iso9126RulesCategories.RELIABILITY; + } + if (isoCategory == IsoCategory.Efficiency) { + return Iso9126RulesCategories.EFFICIENCY; + } + if (isoCategory == IsoCategory.Maintainability) { + return Iso9126RulesCategories.MAINTAINABILITY; + } + if (isoCategory == IsoCategory.Portability) { + return Iso9126RulesCategories.PORTABILITY; + } + if (isoCategory == IsoCategory.Usability) { + return Iso9126RulesCategories.USABILITY; + } + return null; + } + + + public List<Rule> parseReferential(String fileContent) { + return Collections.emptyList(); + } + + public List<RulesProfile> getProvidedProfiles() { + return Collections.emptyList(); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/DefaultCheckTemplate.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/DefaultCheckTemplate.java new file mode 100644 index 00000000000..76f6fb6ca4e --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/DefaultCheckTemplate.java @@ -0,0 +1,81 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.apache.commons.lang.builder.ToStringBuilder; + +import java.util.Locale; + +/** + * EXPERIMENTAL - will be used in version 2.2 + * + * Non-internationalized check + * + * @since 2.1 + */ +public class DefaultCheckTemplate extends CheckTemplate { + + private String title; + private String description; + + public DefaultCheckTemplate() { + } + + public DefaultCheckTemplate(String key) { + super(key); + } + + public void setTitle(String title) { + this.title = title; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String getTitle(Locale locale) { + if (title == null || "".equals(title)) { + return getKey(); + } + return title; + } + + @Override + public String getDescription(Locale locale) { + return description; + } + + @Override + public String getMessage(Locale locale, String key, Object... params) { + return null; + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("key", key) + .append("title", title) + .append("configKey", configKey) + .append("priority", priority) + .append("isoCategory", isoCategory) + .toString(); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/DefaultCheckTemplateProperty.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/DefaultCheckTemplateProperty.java new file mode 100644 index 00000000000..142942b8ced --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/DefaultCheckTemplateProperty.java @@ -0,0 +1,62 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.sonar.api.checks.templates.CheckTemplateProperty; + +import java.util.Locale; + +/** + * @since 2.1 + */ +public class DefaultCheckTemplateProperty extends CheckTemplateProperty { + + private String title; + private String description; + + public String getTitle() { + if (title == null || "".equals(title)) { + return getKey(); + } + return title; + } + + @Override + public String getTitle(Locale locale) { + return getTitle(); + } + + public void setTitle(String s) { + this.title = s; + } + + public String getDescription() { + return description; + } + + public void setDescription(String s) { + this.description = s; + } + + @Override + public String getDescription(Locale locale) { + return getDescription(); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/XmlCheckTemplateFactory.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/XmlCheckTemplateFactory.java new file mode 100644 index 00000000000..0b51222c53d --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/templates/XmlCheckTemplateFactory.java @@ -0,0 +1,100 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.CharEncoding; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleParam; +import org.sonar.api.rules.StandardRulesXmlParser; +import org.sonar.api.utils.SonarException; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + +/** + * EXPERIMENTAL - will be used in version 2.2 + * @since 2.1 + */ +public class XmlCheckTemplateFactory { + + public List<CheckTemplate> parseXml(String xml) { + InputStream input = null; + try { + input = IOUtils.toInputStream(xml, CharEncoding.UTF_8); + return parse(input); + + } catch (IOException e) { + throw new SonarException("Can't parse xml file", e); + + } finally { + IOUtils.closeQuietly(input); + } + } + + public List<CheckTemplate> parse(Reader reader) { + StandardRulesXmlParser parser = new StandardRulesXmlParser(); + List<Rule> rules = parser.parse(reader); + return toCheckTemplates(rules); + + } + + public List<CheckTemplate> parse(InputStream input) { + StandardRulesXmlParser parser = new StandardRulesXmlParser(); + List<Rule> rules = parser.parse(input); + return toCheckTemplates(rules); + + } + + private List<CheckTemplate> toCheckTemplates(List<Rule> rules) { + List<CheckTemplate> templates = new ArrayList<CheckTemplate>(); + if (rules != null) { + for (Rule rule : rules) { + DefaultCheckTemplate template = new DefaultCheckTemplate(rule.getKey()); + templates.add(template); + + template.setConfigKey(rule.getConfigKey()); + template.setDescription(rule.getDescription()); + template.setIsoCategory(rule.getRulesCategory().toIsoCategory()); + template.setPriority(rule.getPriority().toCheckPriority()); + template.setTitle(rule.getName()); + + if (rule.getParams() != null) { + for (RuleParam param : rule.getParams()) { + template.addProperty(toProperty(param)); + } + } + } + } + return templates; + } + + private CheckTemplateProperty toProperty(RuleParam param) { + DefaultCheckTemplateProperty property = new DefaultCheckTemplateProperty(); + property.setKey(param.getKey()); + property.setTitle(param.getKey()); + property.setDescription(param.getDescription()); + return property; + } + +}
\ No newline at end of file diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/AnnotationCheckerFactoryTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/AnnotationCheckerFactoryTest.java new file mode 100644 index 00000000000..8235725ac56 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/AnnotationCheckerFactoryTest.java @@ -0,0 +1,148 @@ +package org.sonar.api.checks.checkers; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.sonar.api.checks.profiles.CheckProfile; +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AnnotationCheckerFactoryTest { + + private AnnotationCheckerFactory factory = null; + + @Before + public void before() { + CheckProfile profile = new CheckProfile("test", "java"); + factory = new AnnotationCheckerFactory(profile, "repository", Arrays.asList( + CheckerWithoutProperties.class, + CheckerWithStringProperty.class, + CheckerWithPrimitiveProperties.class)); + + } + + @Test + public void createCheckerWithoutProperties() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckerWithoutProperties.class); + CheckerWithoutProperties checker = (CheckerWithoutProperties) factory.instantiate(check, CheckerWithoutProperties.class); + assertNotNull(checker); + } + + @Test + public void createCheckerWithStringProperty() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckerWithStringProperty.class); + + Map map = new HashMap(); + map.put("max", "foo"); + when(check.getProperties()).thenReturn(map); + + CheckerWithStringProperty checker = (CheckerWithStringProperty) factory.instantiate(check, CheckerWithStringProperty.class); + assertNotNull(checker); + assertThat(checker.getMax(), is("foo")); + } + + @Test(expected = UnvalidCheckerException.class) + public void failIfMissingProperty() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckerWithStringProperty.class); + + Map map = new HashMap(); + map.put("max", "foo"); + map.put("missing", "bar"); + when(check.getProperties()).thenReturn(map); + + factory.instantiate(check, CheckerWithStringProperty.class); + } + + @Test + public void createCheckerWithPrimitiveProperties() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckerWithPrimitiveProperties.class); + + Map map = new HashMap(); + map.put("max", "300"); + map.put("active", "true"); + when(check.getProperties()).thenReturn(map); + + CheckerWithPrimitiveProperties checker = (CheckerWithPrimitiveProperties) factory.instantiate(check, CheckerWithPrimitiveProperties.class); + assertNotNull(checker); + assertThat(checker.getMax(), is(300)); + assertThat(checker.isActive(), is(true)); + } + + @Test + public void createCheckerWithIntegerProperty() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckerWithIntegerProperty.class); + + Map map = new HashMap(); + map.put("max", "300"); + when(check.getProperties()).thenReturn(map); + + CheckerWithIntegerProperty checker = (CheckerWithIntegerProperty) factory.instantiate(check, CheckerWithIntegerProperty.class); + assertNotNull(checker); + assertThat(checker.getMax(), is(300)); + } + + @Test + public void createCheckerWithDefaultValues() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckerWithPrimitiveProperties.class); + when(check.getProperties()).thenReturn(new HashMap<String, String>()); + + CheckerWithPrimitiveProperties checker = (CheckerWithPrimitiveProperties) factory.instantiate(check, CheckerWithPrimitiveProperties.class); + assertNotNull(checker); + assertThat(checker.getMax(), is(50)); + assertThat(checker.isActive(), is(false)); + } + + @Test(expected=UnvalidCheckerException.class) + public void checkWithUnsupportedPropertyType() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckWithUnsupportedPropertyType.class); + Map map = new HashMap(); + map.put("max", "300"); + when(check.getProperties()).thenReturn(map); + + factory.instantiate(check, CheckWithUnsupportedPropertyType.class); + } + + @Test + @Ignore("Key is not used as i18n is not managed on properties.") + public void createCheckerWithOverridenPropertyKey() { + org.sonar.api.checks.profiles.Check check = mockCheck(CheckWithOverridenPropertyKey.class); + Map map = new HashMap(); + map.put("maximum", "300"); + when(check.getProperties()).thenReturn(map); + + CheckWithOverridenPropertyKey checker = (CheckWithOverridenPropertyKey) factory.instantiate(check, CheckWithOverridenPropertyKey.class); + assertNotNull(checker); + assertThat(checker.getMax(), is(300)); + } + + + private org.sonar.api.checks.profiles.Check mockCheck(Class checkerClass) { + org.sonar.api.checks.profiles.Check check = mock(org.sonar.api.checks.profiles.Check.class); + when(check.getRepositoryKey()).thenReturn("repository"); + when(check.getTemplateKey()).thenReturn(checkerClass.getCanonicalName()); + return check; + } +} + +@Check(isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class CheckWithOverridenPropertyKey{ + + @CheckProperty(key = "maximum") + private int max = 50; + + public int getMax() { + return max; + } +}
\ No newline at end of file diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckWithUnsupportedPropertyType.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckWithUnsupportedPropertyType.java new file mode 100644 index 00000000000..b5b9f1bacbb --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckWithUnsupportedPropertyType.java @@ -0,0 +1,21 @@ +package org.sonar.api.checks.checkers; + +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +/** + * Created by IntelliJ IDEA. + * User: simonbrandhof + * Date: Sep 14, 2010 + * Time: 11:20:57 AM + * To change this template use File | Settings | File Templates. + */ +@Check(isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class CheckWithUnsupportedPropertyType { + + @CheckProperty + private StringBuilder max = null; + +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithIntegerProperty.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithIntegerProperty.java new file mode 100644 index 00000000000..4ae71746cf3 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithIntegerProperty.java @@ -0,0 +1,17 @@ +package org.sonar.api.checks.checkers; + +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +@Check(isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class CheckerWithIntegerProperty { + + @CheckProperty + private Integer max; + + public Integer getMax() { + return max; + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithPrimitiveProperties.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithPrimitiveProperties.java new file mode 100644 index 00000000000..15af2ade92f --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithPrimitiveProperties.java @@ -0,0 +1,31 @@ +package org.sonar.api.checks.checkers; + +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +/** + * Created by IntelliJ IDEA. + * User: simonbrandhof + * Date: Sep 14, 2010 + * Time: 11:20:57 AM + * To change this template use File | Settings | File Templates. + */ +@Check(isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class CheckerWithPrimitiveProperties { + + @CheckProperty(description = "Maximum threshold") + private int max = 50; + + @CheckProperty + private boolean active; + + public int getMax() { + return max; + } + + public boolean isActive() { + return active; + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithStringProperty.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithStringProperty.java new file mode 100644 index 00000000000..a7f62a248e3 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithStringProperty.java @@ -0,0 +1,24 @@ +package org.sonar.api.checks.checkers; + +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +/** + * Created by IntelliJ IDEA. + * User: simonbrandhof + * Date: Sep 14, 2010 + * Time: 11:20:57 AM + * To change this template use File | Settings | File Templates. + */ +@Check(isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class CheckerWithStringProperty { + + @CheckProperty(key = "maiximum") + private String max; + + public String getMax() { + return max; + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithoutProperties.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithoutProperties.java new file mode 100644 index 00000000000..a3a72999cfd --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/checkers/CheckerWithoutProperties.java @@ -0,0 +1,29 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.checkers; + +import org.sonar.check.Check; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +@Check(isoCategory = IsoCategory.Efficiency, priority = Priority.CRITICAL) +class CheckerWithoutProperties { + +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/AnnotationCheckProfileFactoryTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/AnnotationCheckProfileFactoryTest.java new file mode 100644 index 00000000000..48ead0f7d7f --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/AnnotationCheckProfileFactoryTest.java @@ -0,0 +1,68 @@ +package org.sonar.api.checks.profiles; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.Test; +import org.sonar.check.Priority; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.junit.internal.matchers.IsCollectionContaining.hasItem; + +/** + * Created by IntelliJ IDEA. + * User: simonbrandhof + * Date: Sep 14, 2010 + * Time: 11:02:28 AM + * To change this template use File | Settings | File Templates. + */ +public class AnnotationCheckProfileFactoryTest { + + @Test + public void noChecks() { + assertThat(AnnotationCheckProfileFactory.create("plugin_foo", "java", null).size(), is(0)); + assertThat(AnnotationCheckProfileFactory.create("plugin_foo", "java", Collections.<Class>emptyList()).size(), is(0)); + } + + @Test + public void create() { + Collection<CheckProfile> profiles = AnnotationCheckProfileFactory.create("plugin_foo", "java", Arrays.<Class>asList(FakeCheckOne.class)); + assertThat(profiles.size(), is(1)); + + CheckProfile profile = profiles.iterator().next(); + assertThat(profile.getName(), is("profile one")); + assertThat(profile.getChecks().size(), is(1)); + assertThat(profile.getChecks().get(0).getPriority(), is(Priority.MINOR)); + } + + @Test + public void provideManyProfiles() { + Collection<CheckProfile> profiles = AnnotationCheckProfileFactory.create("plugin_foo", "java", Arrays.<Class>asList(FakeCheckOne.class, FakeCheckTwo.class)); + assertThat(profiles.size(), is(2)); + + assertThat(profiles, hasItem(new CheckProfileMatcher("profile one", 2))); + assertThat(profiles, hasItem(new CheckProfileMatcher("profile two", 1))); + } +} + +class CheckProfileMatcher extends BaseMatcher<CheckProfile> { + private String name; + private int numberOfChecks; + + CheckProfileMatcher(String name, int numberOfChecks) { + this.name = name; + this.numberOfChecks = numberOfChecks; + } + + public boolean matches(Object o) { + CheckProfile profile = (CheckProfile) o; + return profile.getName().equals(name) && profile.getChecks().size() == numberOfChecks; + } + + public void describeTo(Description description) { + } +}
\ No newline at end of file diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckProfileTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckProfileTest.java new file mode 100644 index 00000000000..20d7ffc6910 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckProfileTest.java @@ -0,0 +1,60 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import org.junit.Test; + +import static junit.framework.Assert.*; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class CheckProfileTest { + + @Test + public void testCheck() { + CheckProfile profile = new CheckProfile("fake", "java"); + assertThat(profile.getName(), is("fake")); + assertThat(profile.getLanguage(), is("java")); + assertThat(profile.getChecks().size(), is(0)); + } + + @Test + public void equalsByNameAndLanguage() { + CheckProfile profile1 = new CheckProfile("fake1", "java"); + CheckProfile profile1Clone = new CheckProfile("fake1", "java"); + CheckProfile profile2 = new CheckProfile("fake1", "php"); + + assertTrue(profile1.equals(profile1)); + assertTrue(profile1.equals(profile1Clone)); + assertFalse(profile1.equals(profile2)); + + assertEquals(profile1.hashCode(), profile1Clone.hashCode()); + } + + @Test + public void addChecks() { + CheckProfile profile = new CheckProfile("fake", "java"); + profile.addCheck(new Check("repo", "template")); + + assertThat(profile.getChecks().size(), is(1)); + assertThat(profile.getChecks("repo").size(), is(1)); + assertThat(profile.getChecks("other repo").size(), is(0)); + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest.java new file mode 100644 index 00000000000..bf6c488d237 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest.java @@ -0,0 +1,95 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import org.apache.commons.io.IOUtils; +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Test; +import org.sonar.check.Priority; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.io.StringWriter; + +import static junit.framework.Assert.assertTrue; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class CheckProfileXmlMarshallerTest { + + private String expectedXml; + + @Before + public void loadExpectedXml() throws IOException { + InputStream input = getClass().getResourceAsStream("/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest/profile.xml"); + try { + expectedXml = IOUtils.toString(input); + } finally { + IOUtils.closeQuietly(input); + } + } + + + @Test + public void toXml() throws IOException, SAXException { + CheckProfile profile = new CheckProfile("one", "java"); + Check check1 = new Check("checkstyle", "C1"); + check1.setPriority(Priority.MINOR); + check1.addProperty("min", "1"); + check1.addProperty("max", "3"); + profile.addCheck(check1); + + StringWriter writer = new StringWriter(); + CheckProfileXmlMarshaller.toXml(profile, writer); + + XMLUnit.setIgnoreWhitespace(true); + Diff diff = XMLUnit.compareXML(expectedXml, writer.toString()); + assertTrue(diff.similar()); + } + + + @Test + public void fromXml() throws IOException, SAXException { + CheckProfile profile = CheckProfileXmlMarshaller.fromXml(new StringReader(expectedXml)); + assertThat(profile.getName(), is("one")); + assertThat(profile.getLanguage(), is("java")); + assertThat(profile.getChecks("checkstyle").size(), is(1)); + assertThat(profile.getChecks("unknown").size(), is(0)); + + Check check = profile.getChecks("checkstyle").get(0); + assertThat(check.getRepositoryKey(), is("checkstyle")); + assertThat(check.getTemplateKey(), is("C1")); + assertThat(check.getPriority(), is(Priority.MINOR)); + + assertThat(check.getProperties().size(), is(2)); + assertThat(check.getProperty("min"), is("1")); + assertThat(check.getProperty("max"), is("3")); + } + + @Test + public void fromXmlInClasspath() { + CheckProfile profile = CheckProfileXmlMarshaller.fromXmlInClasspath("/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest/profile.xml"); + assertThat(profile.getName(), is("one")); + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckTest.java new file mode 100644 index 00000000000..3d59ff3cb52 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/CheckTest.java @@ -0,0 +1,61 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.profiles; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertThat; +import static org.hamcrest.core.Is.is; + +public class CheckTest { + + @Test + public void testParameters() { + Check check = new Check("fake_plugin", "fake_key"); + assertThat(check.getProperties().size(), is(0)); + + check.addProperty("foo", "bar"); + assertThat(check.getProperties().size(), is(1)); + assertThat(check.getProperties().get("foo"), is("bar")); + + Map<String, String> newParams = new HashMap<String, String>(); + newParams.put("foo", "new foo"); + newParams.put("hello", "world"); + + check.setProperties(newParams); + assertThat(check.getProperties().size(), is(2)); + assertThat(check.getProperties().get("foo"), is("new foo")); + assertThat(check.getProperties().get("hello"), is("world")); + } + + @Test + public void equalsByReference() { + Check check1 = new Check("fake_plugin", "fake_key"); + Check check2 = new Check("fake_plugin", "fake_key"); + + assertTrue(check1.equals(check1)); + assertFalse(check1.equals(check2)); + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/FakeCheckOne.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/FakeCheckOne.java new file mode 100644 index 00000000000..ec10e7d5f9f --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/FakeCheckOne.java @@ -0,0 +1,18 @@ +package org.sonar.api.checks.profiles; + +import org.sonar.check.BelongsToProfile; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +/** + * Created by IntelliJ IDEA. + * User: simonbrandhof + * Date: Sep 14, 2010 + * Time: 11:02:28 AM + * To change this template use File | Settings | File Templates. + */ +@org.sonar.check.Check(priority = Priority.BLOCKER, isoCategory = IsoCategory.Maintainability) +@BelongsToProfile(title = "profile one", priority = Priority.MINOR) +class FakeCheckOne { + +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/FakeCheckTwo.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/FakeCheckTwo.java new file mode 100644 index 00000000000..1e65c3e1f5b --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/profiles/FakeCheckTwo.java @@ -0,0 +1,22 @@ +package org.sonar.api.checks.profiles; + +import org.sonar.check.BelongsToProfile; +import org.sonar.check.BelongsToProfiles; +import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; + +/** + * Created by IntelliJ IDEA. + * User: simonbrandhof + * Date: Sep 14, 2010 + * Time: 11:02:28 AM + * To change this template use File | Settings | File Templates. + */ +@org.sonar.check.Check(priority = Priority.BLOCKER, isoCategory = IsoCategory.Maintainability) +@BelongsToProfiles({ + @BelongsToProfile(title = "profile two", priority = Priority.INFO), + @BelongsToProfile(title = "profile one", priority = Priority.MINOR) +}) +class FakeCheckTwo { + +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/AnnotatedCheckWithBundles.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/AnnotatedCheckWithBundles.java new file mode 100644 index 00000000000..5618932fda5 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/AnnotatedCheckWithBundles.java @@ -0,0 +1,43 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.samples; + +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; + +@Check(isoCategory = IsoCategory.Efficiency) +public final class AnnotatedCheckWithBundles { + @CheckProperty + private String max; + + @CheckProperty + protected String min; + + private int nonConfigurableProperty; + + public String getMax() { + return max; + } + + public void setMax(String max) { + this.max = max; + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/DetailedAnnotatedCheck.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/DetailedAnnotatedCheck.java new file mode 100644 index 00000000000..7e461ea1248 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/DetailedAnnotatedCheck.java @@ -0,0 +1,44 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.samples; + +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; + +@Check(title ="Detailed Check", description = "Detailed description", isoCategory = IsoCategory.Reliability) +public class DetailedAnnotatedCheck { + + @CheckProperty(description ="Maximum value") + private String max; + + @CheckProperty(description ="Minimum value") + protected String min; + + private int nonConfigurableProperty; + + public String getMax() { + return max; + } + + public void setMax(String max) { + this.max = max; + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/I18nCheckWithAlternativeBundle.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/I18nCheckWithAlternativeBundle.java new file mode 100644 index 00000000000..50dc9a78df2 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/I18nCheckWithAlternativeBundle.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.samples; + +import org.sonar.check.Check; +import org.sonar.check.IsoCategory; + +@Check( + key = "new_key", + bundle = "org.sonar.api.checks.samples.alternative.path.AlternativeBundle", + isoCategory = IsoCategory.Efficiency) +public class I18nCheckWithAlternativeBundle { +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/I18nCheckWithoutDefaultLocale.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/I18nCheckWithoutDefaultLocale.java new file mode 100644 index 00000000000..7828d928cb1 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/I18nCheckWithoutDefaultLocale.java @@ -0,0 +1,27 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.samples; + +import org.sonar.check.Check; +import org.sonar.check.IsoCategory; + +@Check(title = "Title from annotation", isoCategory = IsoCategory.Efficiency) +public class I18nCheckWithoutDefaultLocale { +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/SimpleAnnotatedCheck.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/SimpleAnnotatedCheck.java new file mode 100644 index 00000000000..67e99868bbb --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/samples/SimpleAnnotatedCheck.java @@ -0,0 +1,45 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.samples; + +import org.sonar.check.Check; +import org.sonar.check.CheckProperty; +import org.sonar.check.IsoCategory; + +@Check(isoCategory = IsoCategory.Efficiency) +public class SimpleAnnotatedCheck { + + @CheckProperty + private String max; + + @CheckProperty + protected String min; + + private int nonConfigurableProperty; + + public String getMax() { + return max; + } + + public void setMax(String max) { + this.max = max; + } +} + diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/AnnotationCheckTemplateFactoryTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/AnnotationCheckTemplateFactoryTest.java new file mode 100644 index 00000000000..5f8f5de48d6 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/AnnotationCheckTemplateFactoryTest.java @@ -0,0 +1,159 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.sonar.api.checks.samples.*; +import org.sonar.check.IsoCategory; + +import java.util.Iterator; +import java.util.Locale; + +import static junit.framework.Assert.assertNotNull; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.*; + +public class AnnotationCheckTemplateFactoryTest { + + private static final Locale DEFAULT_LOCALE = Locale.ENGLISH; + private static final Locale ALTERNATIVE_LOCALE = Locale.FRENCH; + private static final Locale UNKNOWN_LOCALE = Locale.CHINESE; + + private static final Locale JVM_LOCALE = Locale.getDefault(); + + @BeforeClass + public static void beforeAll() { + Locale.setDefault(Locale.ENGLISH); + } + + @AfterClass + public static void afterAll() { + Locale.setDefault(JVM_LOCALE); + } + + @Test + public void checkWithDefaultValues() { + BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(SimpleAnnotatedCheck.class); + assertNotNull(check); + + assertThat(check.getKey(), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck")); + + assertThat(check.getTitle(DEFAULT_LOCALE), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck")); + assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck")); + assertThat(check.getTitle(UNKNOWN_LOCALE), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck")); + + assertThat(check.getDescription(DEFAULT_LOCALE), is("")); + assertThat(check.getDescription(ALTERNATIVE_LOCALE), is("")); + assertThat(check.getDescription(UNKNOWN_LOCALE), is("")); + + assertEquals(IsoCategory.Efficiency, check.getIsoCategory()); + + assertThat(check.getProperties().size(), is(2)); + Iterator<CheckTemplateProperty> it = check.getProperties().iterator(); + + CheckTemplateProperty maxTemplateProperty = it.next(); + assertThat(maxTemplateProperty.getKey(), is("max")); + + assertThat(maxTemplateProperty.getDescription(DEFAULT_LOCALE), is("")); + assertThat(maxTemplateProperty.getDescription(ALTERNATIVE_LOCALE), is("")); + assertThat(maxTemplateProperty.getDescription(UNKNOWN_LOCALE), is("")); + + CheckTemplateProperty minTemplateProperty = it.next(); + assertThat(minTemplateProperty.getKey(), is("min")); + } + + @Test + public void failOnNonCheckClass() { + assertNull(new AnnotationCheckTemplateFactory(null).create(String.class)); + } + + @Test + public void checkWithDetailedMessages() { + BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(DetailedAnnotatedCheck.class); + assertNotNull(check); + + assertThat(check.getKey(), is("org.sonar.api.checks.samples.DetailedAnnotatedCheck")); + + assertThat(check.getTitle(DEFAULT_LOCALE), is("Detailed Check")); + assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("Detailed Check")); + assertThat(check.getTitle(UNKNOWN_LOCALE), is("Detailed Check")); + + assertThat(check.getDescription(DEFAULT_LOCALE), is("Detailed description")); + assertThat(check.getDescription(ALTERNATIVE_LOCALE), is("Detailed description")); + assertThat(check.getDescription(UNKNOWN_LOCALE), is("Detailed description")); + + assertThat(check.getIsoCategory(), is(IsoCategory.Reliability)); + + assertThat(check.getProperties().size(), is(2)); + Iterator<CheckTemplateProperty> it = check.getProperties().iterator(); + + CheckTemplateProperty maxTemplateProperty = it.next(); + assertThat(maxTemplateProperty.getKey(), is("max")); + + assertThat(maxTemplateProperty.getDescription(DEFAULT_LOCALE), is("Maximum value")); + assertThat(maxTemplateProperty.getDescription(ALTERNATIVE_LOCALE), is("Maximum value")); + assertThat(maxTemplateProperty.getDescription(UNKNOWN_LOCALE), is("Maximum value")); + } + + @Test + public void checkWithInternationalizedMessages() { + BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(AnnotatedCheckWithBundles.class); + assertNotNull(check); + + assertThat(check.getKey(), is("org.sonar.api.checks.samples.AnnotatedCheckWithBundles")); + assertThat(check.getTitle(DEFAULT_LOCALE), is("I18n Check")); + assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("Règle d'internationalisation")); + assertThat(check.getTitle(UNKNOWN_LOCALE), is("I18n Check")); + + assertThat(check.getDescription(DEFAULT_LOCALE), is("Description in english")); + assertThat(check.getDescription(ALTERNATIVE_LOCALE), is("Description en Français")); + assertThat(check.getDescription(UNKNOWN_LOCALE), is("Description in english")); + + assertThat(check.getProperties().size(), is(2)); + Iterator<CheckTemplateProperty> it = check.getProperties().iterator(); + + CheckTemplateProperty maxTemplateProperty = it.next(); + assertThat(maxTemplateProperty.getKey(), is("max")); + + assertThat(maxTemplateProperty.getDescription(DEFAULT_LOCALE), is("Description in english of the maximum value")); + assertThat(maxTemplateProperty.getDescription(ALTERNATIVE_LOCALE), is("Description en Français de la valeur maximale")); + assertThat(maxTemplateProperty.getDescription(UNKNOWN_LOCALE), is("Description in english of the maximum value")); + } + + @Test + public void loadBundlesFromAlternativePath() { + BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(I18nCheckWithAlternativeBundle.class); + assertNotNull(check); + + assertThat(check.getKey(), is("new_key")); + assertThat(check.getTitle(DEFAULT_LOCALE), is("Alternative Path to Bundle")); + } + + @Test + public void loadFromAnnotationIfNoDefaultLocale() { + BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(I18nCheckWithoutDefaultLocale.class); + assertNotNull(check); + + assertThat(check.getTitle(DEFAULT_LOCALE), is("Title from annotation")); + assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("Titre depuis le bundle")); + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/BundleCheckTemplateTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/BundleCheckTemplateTest.java new file mode 100644 index 00000000000..905ba0399b8 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/BundleCheckTemplateTest.java @@ -0,0 +1,74 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.sonar.api.checks.samples.AnnotatedCheckWithBundles; +import org.sonar.api.checks.samples.SimpleAnnotatedCheck; + +import java.util.Locale; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +public class BundleCheckTemplateTest { + + private static final Locale DEFAULT_LOCALE = Locale.getDefault(); + + @BeforeClass + public static void beforeAll() { + Locale.setDefault(Locale.ENGLISH); + } + + @AfterClass + public static void afterAll() { + Locale.setDefault(DEFAULT_LOCALE); + } + + @Test + public void loadBundlesFromClass() { + BundleCheckTemplate check = new BundleCheckTemplate("key", AnnotatedCheckWithBundles.class); + + assertNotNull(check.getBundle(Locale.ENGLISH)); + assertNotNull(check.getBundle(Locale.FRENCH)); + assertNotNull(check.getBundle(Locale.CHINESE)); // use the english bundle + + assertThat(check.getBundle(Locale.ENGLISH).getString("title"), is("I18n Check")); + assertThat(check.getBundle(Locale.CHINESE).getString("title"), is("I18n Check")); + assertThat(check.getBundle(Locale.FRENCH).getString("title"), is("Règle d'internationalisation")); + } + + @Test + public void useDefaultValuesWhenNoBundles() { + BundleCheckTemplate check = new BundleCheckTemplate("key", SimpleAnnotatedCheck.class); + check.setDefaultTitle("default title"); + check.setDefaultDescription("default desc"); + + assertThat(check.getTitle(null), is("default title")); + assertThat(check.getTitle(Locale.ENGLISH), is("default title")); + assertThat(check.getTitle(Locale.CHINESE), is("default title")); + + assertThat(check.getDescription(Locale.ENGLISH), is("default desc")); + assertThat(check.getDescription(Locale.CHINESE), is("default desc")); + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/CheckTemplateRepositoriesTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/CheckTemplateRepositoriesTest.java new file mode 100644 index 00000000000..691a2084eaf --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/CheckTemplateRepositoriesTest.java @@ -0,0 +1,57 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.junit.Test; +import org.sonar.api.checks.templates.CheckTemplateRepositories; +import org.sonar.api.checks.templates.CheckTemplateRepository; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CheckTemplateRepositoriesTest { + + @Test + public void noRepositories() { + CheckTemplateRepositories templateRepositories = new CheckTemplateRepositories(); + assertNull(templateRepositories.getRepository("foo")); + assertThat(templateRepositories.getRepositories().size(), is(0)); + } + + @Test + public void getRepositoryByKey() { + CheckTemplateRepository repo1 = mock(CheckTemplateRepository.class); + when(repo1.getKey()).thenReturn("one"); + + CheckTemplateRepository repo2 = mock(CheckTemplateRepository.class); + when(repo2.getKey()).thenReturn("two"); + + CheckTemplateRepositories templateRepositories = new CheckTemplateRepositories(new CheckTemplateRepository[]{repo1, repo2}); + + assertThat(templateRepositories.getRepositories().size(), is(2)); + assertEquals(repo1, templateRepositories.getRepository("one")); + assertEquals(repo2, templateRepositories.getRepository("two")); + assertNull(templateRepositories.getRepository("foo")); + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/DefaultCheckTemplateTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/DefaultCheckTemplateTest.java new file mode 100644 index 00000000000..e83896a6197 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/templates/DefaultCheckTemplateTest.java @@ -0,0 +1,42 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.checks.templates; + +import org.junit.Test; + +import java.util.Locale; + +import static org.junit.Assert.assertEquals; + +public class DefaultCheckTemplateTest { + + @Test + public void isNotInternationalized() { + DefaultCheckTemplate check = new DefaultCheckTemplate("key1"); + check.setTitle("title"); + check.setDescription("desc"); + + assertEquals("title", check.getTitle(Locale.ENGLISH)); + assertEquals(check.getTitle(Locale.ENGLISH), check.getTitle(Locale.FRENCH)); + + assertEquals("desc", check.getDescription(Locale.ENGLISH)); + assertEquals(check.getDescription(Locale.ENGLISH), check.getDescription(Locale.FRENCH)); + } +} diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest/profile.xml b/sonar-deprecated/src/test/resources/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest/profile.xml new file mode 100644 index 00000000000..bcbba0f82ba --- /dev/null +++ b/sonar-deprecated/src/test/resources/org/sonar/api/checks/profiles/CheckProfileXmlMarshallerTest/profile.xml @@ -0,0 +1,17 @@ +<profile> + <name>one</name> + <language>java</language> + <check> + <repository>checkstyle</repository> + <template>C1</template> + <priority>MINOR</priority> + <property> + <key>min</key> + <value>1</value> + </property> + <property> + <key>max</key> + <value>3</value> + </property> + </check> +</profile>
\ No newline at end of file diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/AnnotatedCheckWithBundles.properties b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/AnnotatedCheckWithBundles.properties new file mode 100644 index 00000000000..e1d764b21b4 --- /dev/null +++ b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/AnnotatedCheckWithBundles.properties @@ -0,0 +1,6 @@ +title: I18n Check +description: Description in english +property.max.title: Maximum value +property.max.description: Description in english of the maximum value +property.min.title: Minimum value +property.min.description: Description in english of the minimum value
\ No newline at end of file diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/AnnotatedCheckWithBundles_fr.properties b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/AnnotatedCheckWithBundles_fr.properties new file mode 100644 index 00000000000..14e2f2dbbcf --- /dev/null +++ b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/AnnotatedCheckWithBundles_fr.properties @@ -0,0 +1,6 @@ +title: Règle d'internationalisation +description: Description en Français +property.max.title: Valeur maximale +property.max.description: Description en Français de la valeur maximale +property.min.title: Valeur minimale +property.min.description: Description en Français de la valeur minimale diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/I18nCheckWithoutDefaultLocale_fr.properties b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/I18nCheckWithoutDefaultLocale_fr.properties new file mode 100644 index 00000000000..d020f8d393b --- /dev/null +++ b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/I18nCheckWithoutDefaultLocale_fr.properties @@ -0,0 +1,2 @@ +title: Titre depuis le bundle +description: Seul le francais est disponible dans les bundles
\ No newline at end of file diff --git a/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/alternative/path/AlternativeBundle.properties b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/alternative/path/AlternativeBundle.properties new file mode 100644 index 00000000000..ceae9d0375d --- /dev/null +++ b/sonar-deprecated/src/test/resources/org/sonar/api/checks/samples/alternative/path/AlternativeBundle.properties @@ -0,0 +1,2 @@ +title : Alternative Path to Bundle +description : description of Alternative Path to Bundle
\ No newline at end of file |