]> source.dussan.org Git - sonarqube.git/blob
bae346a718ff3577bcf9e6fbdda86d63fb29e2ec
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2011 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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
19  */
20 package org.sonar.api.checks.templates;
21
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;
27
28 import java.util.Iterator;
29 import java.util.Locale;
30
31 import static junit.framework.Assert.assertNotNull;
32 import static org.hamcrest.Matchers.is;
33 import static org.junit.Assert.*;
34
35 public class AnnotationCheckTemplateFactoryTest {
36
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;
40
41   private static final Locale JVM_LOCALE = Locale.getDefault();
42
43   @BeforeClass
44   public static void beforeAll() {
45     Locale.setDefault(Locale.ENGLISH);
46   }
47
48   @AfterClass
49   public static void afterAll() {
50     Locale.setDefault(JVM_LOCALE);
51   }
52
53   @Test
54   public void checkWithDefaultValues() {
55     BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(SimpleAnnotatedCheck.class);
56     assertNotNull(check);
57
58     assertThat(check.getKey(), is("org.sonar.api.checks.samples.SimpleAnnotatedCheck"));
59
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"));
63
64     assertThat(check.getDescription(DEFAULT_LOCALE), is(""));
65     assertThat(check.getDescription(ALTERNATIVE_LOCALE), is(""));
66     assertThat(check.getDescription(UNKNOWN_LOCALE), is(""));
67
68     assertEquals(IsoCategory.Efficiency, check.getIsoCategory());
69
70     assertThat(check.getProperties().size(), is(2));
71     Iterator<CheckTemplateProperty> it = check.getProperties().iterator();
72
73     CheckTemplateProperty maxTemplateProperty = it.next();
74     assertThat(maxTemplateProperty.getKey(), is("max"));
75
76     assertThat(maxTemplateProperty.getDescription(DEFAULT_LOCALE), is(""));
77     assertThat(maxTemplateProperty.getDescription(ALTERNATIVE_LOCALE), is(""));
78     assertThat(maxTemplateProperty.getDescription(UNKNOWN_LOCALE), is(""));
79
80     CheckTemplateProperty minTemplateProperty = it.next();
81     assertThat(minTemplateProperty.getKey(), is("min"));
82   }
83
84   @Test
85   public void failOnNonCheckClass() {
86     assertNull(new AnnotationCheckTemplateFactory(null).create(String.class));
87   }
88
89   @Test
90   public void checkWithDetailedMessages() {
91     BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(DetailedAnnotatedCheck.class);
92     assertNotNull(check);
93
94     assertThat(check.getKey(), is("org.sonar.api.checks.samples.DetailedAnnotatedCheck"));
95
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"));
99
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"));
103
104     assertThat(check.getIsoCategory(), is(IsoCategory.Reliability));
105
106     assertThat(check.getProperties().size(), is(2));
107     Iterator<CheckTemplateProperty> it = check.getProperties().iterator();
108
109     CheckTemplateProperty maxTemplateProperty = it.next();
110     assertThat(maxTemplateProperty.getKey(), is("max"));
111
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"));
115   }
116
117   @Test
118   public void checkWithInternationalizedMessages() {
119     BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(AnnotatedCheckWithBundles.class);
120     assertNotNull(check);
121
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"));
126
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"));
130
131     assertThat(check.getProperties().size(), is(2));
132     Iterator<CheckTemplateProperty> it = check.getProperties().iterator();
133
134     CheckTemplateProperty maxTemplateProperty = it.next();
135     assertThat(maxTemplateProperty.getKey(), is("max"));
136
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"));
140   }
141
142   @Test
143   public void loadBundlesFromAlternativePath() {
144     BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(I18nCheckWithAlternativeBundle.class);
145     assertNotNull(check);
146
147     assertThat(check.getKey(), is("new_key"));
148     assertThat(check.getTitle(DEFAULT_LOCALE), is("Alternative Path to Bundle"));
149   }
150
151   @Test
152   public void loadFromAnnotationIfNoDefaultLocale() {
153     BundleCheckTemplate check = new AnnotationCheckTemplateFactory(null).create(I18nCheckWithoutDefaultLocale.class);
154     assertNotNull(check);
155
156     assertThat(check.getTitle(DEFAULT_LOCALE), is("Title from annotation"));
157     assertThat(check.getTitle(ALTERNATIVE_LOCALE), is("Titre depuis le bundle"));
158   }
159 }