2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.api.checks.templates;
22 import org.junit.AfterClass;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.sonar.api.checks.samples.*;
26 import org.sonar.check.IsoCategory;
28 import java.util.Iterator;
29 import java.util.Locale;
31 import static junit.framework.Assert.assertNotNull;
32 import static org.hamcrest.Matchers.is;
33 import static org.junit.Assert.*;
35 public class AnnotationCheckTemplateFactoryTest {
37 private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
38 private static final Locale ALTERNATIVE_LOCALE = Locale.FRENCH;
39 private static final Locale UNKNOWN_LOCALE = Locale.CHINESE;
41 private static final Locale JVM_LOCALE = Locale.getDefault();
44 public static void beforeAll() {
45 Locale.setDefault(Locale.ENGLISH);
49 public static void afterAll() {
50 Locale.setDefault(JVM_LOCALE);
54 public void checkWithDefaultValues() {
55 BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(SimpleAnnotatedCheck.class);
58 assertThat(check.getKey(), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck"));
60 assertThat(check.getTitle(DEFAULT_LOCALE), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck"));
61 assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck"));
62 assertThat(check.getTitle(UNKNOWN_LOCALE), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck"));
64 assertThat(check.getDescription(DEFAULT_LOCALE), is(""));
65 assertThat(check.getDescription(ALTERNATIVE_LOCALE), is(""));
66 assertThat(check.getDescription(UNKNOWN_LOCALE), is(""));
68 assertEquals(IsoCategory.Efficiency, check.getIsoCategory());
70 assertThat(check.getProperties().size(), is(2));
71 Iterator<CheckTemplateProperty> it = check.getProperties().iterator();
73 CheckTemplateProperty maxTemplateProperty = it.next();
74 assertThat(maxTemplateProperty.getKey(), is("max"));
76 assertThat(maxTemplateProperty.getDescription(DEFAULT_LOCALE), is(""));
77 assertThat(maxTemplateProperty.getDescription(ALTERNATIVE_LOCALE), is(""));
78 assertThat(maxTemplateProperty.getDescription(UNKNOWN_LOCALE), is(""));
80 CheckTemplateProperty minTemplateProperty = it.next();
81 assertThat(minTemplateProperty.getKey(), is("min"));
85 public void failOnNonCheckClass() {
86 assertNull(new AnnotationCheckTemplateFactory(null).create(String.class));
90 public void checkWithDetailedMessages() {
91 BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(DetailedAnnotatedCheck.class);
94 assertThat(check.getKey(), is("org.sonar.api.checks.samples.DetailedAnnotatedCheck"));
96 assertThat(check.getTitle(DEFAULT_LOCALE), is("Detailed Check"));
97 assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("Detailed Check"));
98 assertThat(check.getTitle(UNKNOWN_LOCALE), is("Detailed Check"));
100 assertThat(check.getDescription(DEFAULT_LOCALE), is("Detailed description"));
101 assertThat(check.getDescription(ALTERNATIVE_LOCALE), is("Detailed description"));
102 assertThat(check.getDescription(UNKNOWN_LOCALE), is("Detailed description"));
104 assertThat(check.getIsoCategory(), is(IsoCategory.Reliability));
106 assertThat(check.getProperties().size(), is(2));
107 Iterator<CheckTemplateProperty> it = check.getProperties().iterator();
109 CheckTemplateProperty maxTemplateProperty = it.next();
110 assertThat(maxTemplateProperty.getKey(), is("max"));
112 assertThat(maxTemplateProperty.getDescription(DEFAULT_LOCALE), is("Maximum value"));
113 assertThat(maxTemplateProperty.getDescription(ALTERNATIVE_LOCALE), is("Maximum value"));
114 assertThat(maxTemplateProperty.getDescription(UNKNOWN_LOCALE), is("Maximum value"));
118 public void checkWithInternationalizedMessages() {
119 BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(AnnotatedCheckWithBundles.class);
120 assertNotNull(check);
122 assertThat(check.getKey(), is("org.sonar.api.checks.samples.AnnotatedCheckWithBundles"));
123 assertThat(check.getTitle(DEFAULT_LOCALE), is("I18n Check"));
124 assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("Règle d'internationalisation"));
125 assertThat(check.getTitle(UNKNOWN_LOCALE), is("I18n Check"));
127 assertThat(check.getDescription(DEFAULT_LOCALE), is("Description in english"));
128 assertThat(check.getDescription(ALTERNATIVE_LOCALE), is("Description en Français"));
129 assertThat(check.getDescription(UNKNOWN_LOCALE), is("Description in english"));
131 assertThat(check.getProperties().size(), is(2));
132 Iterator<CheckTemplateProperty> it = check.getProperties().iterator();
134 CheckTemplateProperty maxTemplateProperty = it.next();
135 assertThat(maxTemplateProperty.getKey(), is("max"));
137 assertThat(maxTemplateProperty.getDescription(DEFAULT_LOCALE), is("Description in english of the maximum value"));
138 assertThat(maxTemplateProperty.getDescription(ALTERNATIVE_LOCALE), is("Description en Français de la valeur maximale"));
139 assertThat(maxTemplateProperty.getDescription(UNKNOWN_LOCALE), is("Description in english of the maximum value"));
143 public void loadBundlesFromAlternativePath() {
144 BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(I18nCheckWithAlternativeBundle.class);
145 assertNotNull(check);
147 assertThat(check.getKey(), is("new_key"));
148 assertThat(check.getTitle(DEFAULT_LOCALE), is("Alternative Path to Bundle"));
152 public void loadFromAnnotationIfNoDefaultLocale() {
153 BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(I18nCheckWithoutDefaultLocale.class);
154 assertNotNull(check);
156 assertThat(check.getTitle(DEFAULT_LOCALE), is("Title from annotation"));
157 assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("Titre depuis le bundle"));