1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.charset;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.MessageException;
import static com.google.common.collect.Sets.immutableEnumSet;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.db.charset.DatabaseCharsetChecker.Flag.AUTO_REPAIR_COLLATION;
import static org.sonar.db.charset.DatabaseCharsetChecker.Flag.ENFORCE_UTF8;
public class MysqlCharsetHandlerTest {
private static final String TABLE_ISSUES = "issues";
private static final String TABLE_PROJECTS = "projects";
private static final String COLUMN_KEE = "kee";
private static final String COLUMN_NAME = "name";
@Rule
public ExpectedException expectedException = ExpectedException.none();
SqlExecutor selectExecutor = mock(SqlExecutor.class);
MysqlCharsetHandler underTest = new MysqlCharsetHandler(selectExecutor);
@Test
public void do_not_fail_if_charsets_of_all_columns_are_utf8_and_case_sensitive() throws Exception {
answerColumnDef(asList(
new ColumnDef(TABLE_ISSUES, COLUMN_KEE, "utf8", "utf8_bin", "varchar", 10, false),
new ColumnDef(TABLE_PROJECTS, COLUMN_NAME, "utf8", "utf8_bin", "varchar", 10, false)));
// all columns are utf8
underTest.handle(mock(Connection.class), immutableEnumSet(ENFORCE_UTF8));
}
@Test
public void fail_if_charsets_of_a_column_is_utf8_but_case_insensitive() throws Exception {
answerColumnDef(asList(
new ColumnDef(TABLE_ISSUES, COLUMN_KEE, "utf8", "utf8_bin", "varchar", 10, false),
new ColumnDef(TABLE_PROJECTS, COLUMN_NAME, "utf8", "utf8_general_ci", "varchar", 10, false)));
expectedException.expect(MessageException.class);
expectedException.expectMessage("UTF8 case-sensitive collation is required for database columns [projects.name]");
underTest.handle(mock(Connection.class), immutableEnumSet(ENFORCE_UTF8));
}
@Test
public void fail_if_not_utf8() throws Exception {
answerColumnDef(asList(
new ColumnDef(TABLE_ISSUES, COLUMN_KEE, "utf8", "utf8_bin", "varchar", 10, false),
new ColumnDef(TABLE_PROJECTS, COLUMN_KEE, "latin1", "latin1_german1_ci", "varchar", 10, false),
new ColumnDef(TABLE_PROJECTS, COLUMN_NAME, "latin1", "latin1_swedish_ci", "varchar", 20, false)));
expectedException.expect(MessageException.class);
expectedException.expectMessage("UTF8 case-sensitive collation is required for database columns [projects.kee, projects.name]");
underTest.handle(mock(Connection.class), immutableEnumSet(ENFORCE_UTF8));
}
@Test
public void repair_case_insensitive_column() throws Exception {
answerColumnDef(asList(
new ColumnDef(TABLE_ISSUES, COLUMN_KEE, "utf8", "utf8_bin", "varchar", 10, false),
new ColumnDef(TABLE_PROJECTS, COLUMN_NAME, "latin1", "latin1_swedish_ci", "varchar", 10, false)));
Connection connection = mock(Connection.class);
underTest.handle(connection, immutableEnumSet(AUTO_REPAIR_COLLATION));
verify(selectExecutor).executeUpdate(connection, "ALTER TABLE projects MODIFY name varchar(10) CHARACTER SET 'latin1' COLLATE 'latin1_bin' NOT NULL");
}
@Test
public void size_should_be_ignored_on_longtext_column() throws Exception {
answerColumnDef(asList(new ColumnDef(TABLE_ISSUES, COLUMN_KEE, "latin1", "latin1_german1_ci", "longtext", 4_294_967_295L, false)));
Connection connection = mock(Connection.class);
underTest.handle(connection, immutableEnumSet(AUTO_REPAIR_COLLATION));
verify(selectExecutor).executeUpdate(connection, "ALTER TABLE " + TABLE_ISSUES + " MODIFY " + COLUMN_KEE + " longtext CHARACTER SET 'latin1' COLLATE 'latin1_bin' NOT NULL");
}
@Test
public void tests_toCaseSensitive() {
assertThat(MysqlCharsetHandler.toCaseSensitive("big5_chinese_ci")).isEqualTo("big5_bin");
}
private void answerColumnDef(List<ColumnDef> columnDefs) throws SQLException {
when(selectExecutor.executeSelect(any(Connection.class), anyString(), eq(ColumnDef.ColumnDefRowConverter.INSTANCE))).thenReturn(columnDefs);
}
}
|