Skip to content

Commit 69c16e7

Browse files
committed
Fix a quality flaw
1 parent fe693d7 commit 69c16e7

4 files changed

Lines changed: 186 additions & 7 deletions

File tree

sslr-python-toolkit/pom.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@
2525
<groupId>ch.qos.logback</groupId>
2626
<artifactId>logback-classic</artifactId>
2727
</dependency>
28+
29+
<dependency>
30+
<groupId>org.easytesting</groupId>
31+
<artifactId>fest-assert</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.mockito</groupId>
36+
<artifactId>mockito-all</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<scope>test</scope>
43+
</dependency>
2844
</dependencies>
2945

3046
<build>
@@ -58,7 +74,6 @@
5874
<include>jaxen:jaxen</include>
5975
<include>org.codehaus.sonar.sslr:sslr-toolkit</include>
6076
<include>org.codehaus.sonar:sonar-colorizer</include>
61-
<include>org.codehaus.sonar:sonar-squid</include>
6277
<include>org.codehaus.sonar:sonar-channel</include>
6378
<include>org.slf4j:slf4j-api</include>
6479
<include>org.slf4j:jcl-over-slf4j</include>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011 SonarSource and Waleri Enns
4+
5+
*
6+
* This program 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+
* This program 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 this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonar.python.toolkit;
21+
22+
import com.google.common.annotations.VisibleForTesting;
23+
import com.google.common.collect.ImmutableList;
24+
import com.sonar.sslr.api.Grammar;
25+
import com.sonar.sslr.impl.Parser;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
import org.sonar.colorizer.KeywordsTokenizer;
29+
import org.sonar.colorizer.Tokenizer;
30+
import org.sonar.python.PythonConfiguration;
31+
import org.sonar.python.api.PythonKeyword;
32+
import org.sonar.python.parser.PythonParser;
33+
import org.sonar.sslr.toolkit.AbstractConfigurationModel;
34+
import org.sonar.sslr.toolkit.ConfigurationProperty;
35+
import org.sonar.sslr.toolkit.Validators;
36+
37+
import java.nio.charset.Charset;
38+
import java.util.List;
39+
40+
public class PythonConfigurationModel extends AbstractConfigurationModel {
41+
42+
private static final Logger LOG = LoggerFactory.getLogger(PythonConfigurationModel.class);
43+
44+
private static final String CHARSET_PROPERTY_KEY = "sonar.sourceEncoding";
45+
46+
@VisibleForTesting
47+
ConfigurationProperty charsetProperty = new ConfigurationProperty("Charset", CHARSET_PROPERTY_KEY,
48+
getPropertyOrDefaultValue(CHARSET_PROPERTY_KEY, "UTF-8"),
49+
Validators.charsetValidator());
50+
51+
@Override
52+
public Charset getCharset() {
53+
return Charset.forName(charsetProperty.getValue());
54+
}
55+
56+
@Override
57+
public List<ConfigurationProperty> getProperties() {
58+
return ImmutableList.of(charsetProperty);
59+
}
60+
61+
@Override
62+
public Parser<Grammar> doGetParser() {
63+
return PythonParser.create(getConfiguration());
64+
}
65+
66+
@Override
67+
public List<Tokenizer> doGetTokenizers() {
68+
return ImmutableList.of(
69+
(Tokenizer) new KeywordsTokenizer("<span class=\"k\">", "</span>", PythonKeyword.keywordValues()));
70+
}
71+
72+
@VisibleForTesting
73+
PythonConfiguration getConfiguration() {
74+
return new PythonConfiguration(Charset.forName(charsetProperty.getValue()));
75+
}
76+
77+
@VisibleForTesting
78+
static String getPropertyOrDefaultValue(String propertyKey, String defaultValue) {
79+
String propertyValue = System.getProperty(propertyKey);
80+
81+
if (propertyValue == null) {
82+
LOG.info("The property \"" + propertyKey + "\" is not set, using the default value \"" + defaultValue + "\".");
83+
return defaultValue;
84+
} else {
85+
LOG.info("The property \"" + propertyKey + "\" is set, using its value \"" + propertyValue + "\".");
86+
return propertyValue;
87+
}
88+
}
89+
90+
}

sslr-python-toolkit/src/main/java/org/sonar/python/toolkit/PythonToolkit.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@
2222
import com.google.common.collect.ImmutableList;
2323
import org.sonar.colorizer.KeywordsTokenizer;
2424
import org.sonar.colorizer.Tokenizer;
25-
import org.sonar.python.PythonConfiguration;
2625
import org.sonar.python.api.PythonKeyword;
27-
import org.sonar.python.parser.PythonParser;
2826
import org.sonar.sslr.toolkit.Toolkit;
2927

30-
import java.nio.charset.Charset;
3128
import java.util.List;
3229

3330
public final class PythonToolkit {
@@ -36,13 +33,13 @@ private PythonToolkit() {
3633
}
3734

3835
public static void main(String[] args) {
39-
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "SSDK");
40-
new Toolkit(PythonParser.create(new PythonConfiguration(Charset.defaultCharset())), getPythonTokenizers(), "SSLR Python Toolkit").run();
36+
Toolkit toolkit = new Toolkit("SSLR :: Python :: Toolkit", new PythonConfigurationModel());
37+
toolkit.run();
4138
}
4239

4340
public static List<Tokenizer> getPythonTokenizers() {
4441
return ImmutableList.of(
45-
(Tokenizer) new KeywordsTokenizer("<span class=\"k\">", "</span>", PythonKeyword.keywordValues()));
42+
(Tokenizer) new KeywordsTokenizer("<span class=\"k\">", "</span>", PythonKeyword.keywordValues()));
4643
}
4744

4845
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011 SonarSource and Waleri Enns
4+
5+
*
6+
* This program 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+
* This program 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 this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonar.python.toolkit;
21+
22+
import com.google.common.base.Charsets;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
26+
27+
import static org.fest.assertions.Assertions.assertThat;
28+
29+
public class PythonConfigurationModelTest {
30+
31+
@Rule
32+
public ExpectedException thrown = ExpectedException.none();
33+
34+
@Test
35+
public void getConfiguration_charset() {
36+
PythonConfigurationModel model = new PythonConfigurationModel();
37+
model.charsetProperty.setValue("UTF-8");
38+
assertThat(model.getCharset()).isEqualTo(Charsets.UTF_8);
39+
assertThat(model.getConfiguration().getCharset()).isEqualTo(Charsets.UTF_8);
40+
model.charsetProperty.setValue("ISO-8859-1");
41+
assertThat(model.getCharset()).isEqualTo(Charsets.ISO_8859_1);
42+
assertThat(model.getConfiguration().getCharset()).isEqualTo(Charsets.ISO_8859_1);
43+
}
44+
45+
@Test
46+
public void getPropertyOrDefaultValue_with_property_set() {
47+
String oldValue = System.getProperty("foo");
48+
49+
try {
50+
System.setProperty("foo", "bar");
51+
assertThat(PythonConfigurationModel.getPropertyOrDefaultValue("foo", "baz")).isEqualTo("bar");
52+
} finally {
53+
if (oldValue == null) {
54+
System.clearProperty("foo");
55+
} else {
56+
System.setProperty("foo", oldValue);
57+
}
58+
}
59+
}
60+
61+
@Test
62+
public void getPropertyOrDefaultValue_with_property_not_set() {
63+
String oldValue = System.getProperty("foo");
64+
65+
try {
66+
System.clearProperty("foo");
67+
assertThat(PythonConfigurationModel.getPropertyOrDefaultValue("foo", "baz")).isEqualTo("baz");
68+
} finally {
69+
if (oldValue == null) {
70+
System.clearProperty("foo");
71+
} else {
72+
System.setProperty("foo", oldValue);
73+
}
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)