├── .github ├── FUNDING.yml └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── LICENSE ├── META-INF └── plugin.xml ├── README.md ├── dbunit-extractor.iml ├── dbunit-extractor.png ├── in-place.gif ├── src ├── intentionDescriptions │ └── QueryToXMLConverter │ │ └── description.html └── li │ └── ktt │ ├── CopyToDbUnit.java │ ├── QueryToXMLConverter.java │ ├── SQLInjector.java │ ├── datagrid │ ├── DataGridHelper.java │ ├── DataHelper.java │ └── ResultSetHelper.java │ ├── settings │ ├── ExcludedColumns.java │ ├── ExtractorProperties.java │ ├── ProjectSettings.java │ ├── ProjectSettingsPage.form │ └── ProjectSettingsPage.java │ └── xml │ ├── XmlGenerator.java │ └── XmlOutput.java └── test └── li └── ktt ├── datagrid └── DataGridHelperTest.java ├── settings ├── ExcludedColumnsTest.java └── ProjectSettingsPageTest.java └── xml └── XmlGeneratorTest.java /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=43EEBTD2V4L6E 13 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | 14 | # Intellij 15 | .idea/ 16 | *.iml 17 | *.ipr 18 | *.iws 19 | 20 | /out/ 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2018 Pawel Mozdzonek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | li.ktt 3 | DbUnit Extractor 4 | 1.9 5 | Pawel Mozdzonek 6 | 7 |
9 |

Authors

10 | 17 |

Source code and documentation

18 | 23 | ]]>
24 | 25 | 27 |
  • 1.9 - fixes compatibility with 2022.2
  • 28 |
  • 1.8 - fixes connection issues on latest IntelliJ versions and moves configuration under Tools section
  • 29 |
  • 1.7 - removes deprecated code
  • 30 |
  • 1.6 - makes timestamps as zoned UTC
  • 31 |
  • 1.5 - fallback values if schema cannot be determined
  • 32 |
  • 1.4 - fixes NPE when extracting column with null value
  • 33 |
  • 1.3 - adds xml escaping, fixes timestamp and varchar(max) columns extraction
  • 34 |
  • 1.2 - fixes @NotNull exception
  • 35 |
  • 1.1 - fixes NPE in some situations
  • 36 |
  • 1.0 - adds support for IntelliJ 2016.3
  • 37 |
  • 0.9 - adds in-place convert without text selection
  • 38 |
  • 0.8 - adds in-place convert for dataset files
  • 39 |
  • 0.7 - adds additional check for table name and schema
  • 40 |
  • 0.6 - fixes compatibility with Intellij IDEA 15, adds popup with information if copy action
  • 41 |
  • 0.5 - adds regular expressions validation for excluded columns, reset configuration
  • 42 |
  • 0.4 - adds include schema name option to include schema in exported xml lines
  • 43 |
  • 0.3 - adds skip null, skip empty value and exclude columns patterns
  • 44 |
  • 0.2 - fixes table name on jTds connection
  • 45 |
  • 0.1 - initial version
  • 46 | 47 | ]]> 48 |
    49 | 50 | 51 | 52 | com.intellij.modules.ultimate 53 | com.intellij.database 54 | 55 | 56 | 57 | 58 | 59 | li.ktt.QueryToXMLConverter 60 | XML 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 74 | 75 | 76 | 77 | 78 |
    79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DbUnit Extractor 2 | 3 | Export database data to XML DbUnit dataset. 4 | 5 | [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=43EEBTD2V4L6E) 6 | 7 | # Download 8 | 9 | [Jetbrains DbUnit Extractor plugin page](https://plugins.jetbrains.com/plugin/7958?pr=idea) 10 | 11 | # Usage 12 | 13 | **Copy query results** 14 | 15 | 1. Install plugin and set up database connection 16 | 2. Write SQL query and execute it 17 | 3. Select rows and columns you want to export 18 | 4. Right click on selection 19 | 5. Select "Copy to clipboard as DbUnit XML" from context menu 20 | 6. Paste result in your .xml file 21 | 22 | **In-place convert** 23 | 24 | 1. Install plugin and set up database connection 25 | 2. Open xml file for dbunit sample data (it has to starts with "\") 26 | 3. Write query you want to convert 27 | 4. Select query, press ALT+ENTER and choose "Convert query to XML". 28 | 29 | Copy selected rows from SQL Console as DbUnit XML. 30 | ![](https://raw.githubusercontent.com/kTT/dbunit-extractor/master/dbunit-extractor.png) 31 | Change SQL query to XML in-place. 32 | ![](https://raw.githubusercontent.com/kTT/dbunit-extractor/master/in-place.gif) 33 | 34 | Inspired by [@rakk](https://github.com/rakk) web tool for generating DbUnit XML lines. 35 | 36 | # Known issues 37 | 38 | * Schema name is empty when using "Copy to clipboard as DbUnit XML". 39 | 40 | Please synchronize your database. Intellij doesn't return metadata if datasource isn't synchronized. 41 | 42 | * "Only one table queries are supported" error when using jTds. 43 | 44 | This driver doesn't return metadata by default (issue: https://sourceforge.net/p/jtds/bugs/546/). Please add **useCursors=true** parameter to your database connection url. 45 | 46 | Example: `jdbc:jtds:sqlserver://localhost/db;instance=TEST;useCursors=true` 47 | 48 | * "Only one table queries are supported" error when using Microsoft driver. 49 | 50 | Please add **selectMethod=cursor** parameter to your database connection url. 51 | 52 | Example: `jdbc:sqlserver://localhost/db;instance=TEST;selectMethod=cursor` 53 | 54 | # License 55 | 56 | This project is licensed under the MIT License. 57 | -------------------------------------------------------------------------------- /dbunit-extractor.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /dbunit-extractor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kTT/dbunit-extractor/5adf7b604bb63035e8e0f1271ca3160acf9c0b8d/dbunit-extractor.png -------------------------------------------------------------------------------- /in-place.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kTT/dbunit-extractor/5adf7b604bb63035e8e0f1271ca3160acf9c0b8d/in-place.gif -------------------------------------------------------------------------------- /src/intentionDescriptions/QueryToXMLConverter/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Convert query to XML 4 | 5 | -------------------------------------------------------------------------------- /src/li/ktt/CopyToDbUnit.java: -------------------------------------------------------------------------------- 1 | package li.ktt; 2 | 3 | import com.intellij.database.DatabaseDataKeys; 4 | import com.intellij.database.datagrid.DataGrid; 5 | import com.intellij.openapi.actionSystem.AnAction; 6 | import com.intellij.openapi.actionSystem.AnActionEvent; 7 | import com.intellij.openapi.actionSystem.DataContext; 8 | import com.intellij.openapi.ide.CopyPasteManager; 9 | import com.intellij.openapi.ui.MessageType; 10 | import com.intellij.openapi.ui.popup.Balloon; 11 | import com.intellij.openapi.ui.popup.JBPopupFactory; 12 | import com.intellij.util.ui.TextTransferable; 13 | import li.ktt.datagrid.DataGridHelper; 14 | import li.ktt.settings.ExtractorProperties; 15 | import li.ktt.settings.ProjectSettings; 16 | import li.ktt.xml.XmlGenerator; 17 | import li.ktt.xml.XmlOutput; 18 | import org.jetbrains.annotations.NotNull; 19 | 20 | public class CopyToDbUnit extends AnAction { 21 | 22 | public void actionPerformed(@NotNull AnActionEvent e) { 23 | DataContext dataContext = e.getDataContext(); 24 | DataGrid dataGrid = DatabaseDataKeys.DATA_GRID_KEY.getData(dataContext); 25 | ExtractorProperties extractorProperties = ProjectSettings.getExtractorProperties(dataContext); 26 | XmlOutput xmlOutput = actionPerformed(extractorProperties, dataGrid, dataContext); 27 | copyOutput(xmlOutput); 28 | showPopup(dataContext, xmlOutput); 29 | } 30 | 31 | public XmlOutput actionPerformed(ExtractorProperties extractorProperties, DataGrid dataGrid, DataContext dataContext) { 32 | XmlOutput result = null; 33 | if (dataGrid != null) { 34 | DataGridHelper data = new DataGridHelper(extractorProperties, dataGrid); 35 | XmlGenerator xmlGenerator = new XmlGenerator(extractorProperties, data); 36 | xmlGenerator.appendRows(); 37 | result = xmlGenerator.getOutput(); 38 | } 39 | return result; 40 | } 41 | 42 | private void copyOutput(XmlOutput xmlOutput) { 43 | CopyPasteManager.getInstance().setContents(new TextTransferable(xmlOutput.getText())); 44 | } 45 | 46 | private void showPopup(DataContext dataContext, XmlOutput xmlOutput) { 47 | MessageType messageType = MessageType.INFO; 48 | String htmlMessage = ""; 49 | if (xmlOutput != null && xmlOutput.getRowSize() > 0 && xmlOutput.getColumnsSize() > 0) { 50 | if (xmlOutput.getTableName() == null || xmlOutput.getTableName().isEmpty()) { 51 | messageType = MessageType.WARNING; 52 | htmlMessage += "Table name is missing. Please try to synchronize database connection.
    "; 53 | } 54 | htmlMessage += "Copied: " + xmlOutput.getRowSize() + " entries (selected " + xmlOutput.getColumnsSize() + " columns)"; 55 | } else { 56 | messageType = MessageType.ERROR; 57 | if (xmlOutput == null) { 58 | htmlMessage = "Failed to copy entries. No grid available."; 59 | } else if (xmlOutput.getRowSize() <= 0) { 60 | htmlMessage = "No rows selected."; 61 | } else if (xmlOutput.getColumnsSize() <= 0) { 62 | htmlMessage = "No columns selected."; 63 | } 64 | } 65 | JBPopupFactory.getInstance() 66 | .createHtmlTextBalloonBuilder(htmlMessage, messageType, null) 67 | .setFadeoutTime(7500) 68 | .createBalloon().show(JBPopupFactory.getInstance().guessBestPopupLocation(dataContext), Balloon.Position.atRight); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/li/ktt/QueryToXMLConverter.java: -------------------------------------------------------------------------------- 1 | package li.ktt; 2 | 3 | import com.intellij.codeInsight.intention.IntentionAction; 4 | import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; 5 | import com.intellij.database.dataSource.DatabaseConnection; 6 | import com.intellij.database.dataSource.connection.DGDepartment; 7 | import com.intellij.database.datagrid.DataConsumer.Column; 8 | import com.intellij.database.datagrid.DataConsumer.Row; 9 | import com.intellij.database.datagrid.GridColumn; 10 | import com.intellij.database.datagrid.GridRow; 11 | import com.intellij.database.psi.DbDataSource; 12 | import com.intellij.database.psi.DbPsiFacade; 13 | import com.intellij.database.remote.jdbc.RemoteDatabaseMetaData; 14 | import com.intellij.database.remote.jdbc.RemoteResultSet; 15 | import com.intellij.database.remote.jdbc.RemoteResultSetMetaData; 16 | import com.intellij.database.remote.jdbc.RemoteStatement; 17 | import com.intellij.database.util.DbImplUtil; 18 | import com.intellij.database.util.GuardedRef; 19 | import com.intellij.injected.editor.EditorWindow; 20 | import com.intellij.openapi.actionSystem.impl.SimpleDataContext; 21 | import com.intellij.openapi.application.ApplicationManager; 22 | import com.intellij.openapi.command.WriteCommandAction; 23 | import com.intellij.openapi.editor.Editor; 24 | import com.intellij.openapi.editor.SelectionModel; 25 | import com.intellij.openapi.project.Project; 26 | import com.intellij.openapi.ui.MessageType; 27 | import com.intellij.openapi.ui.popup.Balloon; 28 | import com.intellij.openapi.ui.popup.JBPopupFactory; 29 | import com.intellij.openapi.util.TextRange; 30 | import com.intellij.openapi.util.text.StringUtil; 31 | import com.intellij.psi.PsiElement; 32 | import com.intellij.psi.SmartPointerManager; 33 | import com.intellij.psi.SmartPsiElementPointer; 34 | import com.intellij.psi.util.PsiTreeUtil; 35 | import com.intellij.sql.psi.SqlSelectStatement; 36 | import com.intellij.util.IncorrectOperationException; 37 | import li.ktt.datagrid.ResultSetHelper; 38 | import li.ktt.settings.ExtractorProperties; 39 | import li.ktt.settings.ProjectSettings; 40 | import li.ktt.xml.XmlGenerator; 41 | import li.ktt.xml.XmlOutput; 42 | import org.jetbrains.annotations.Nls; 43 | import org.jetbrains.annotations.NotNull; 44 | import org.jetbrains.annotations.Nullable; 45 | 46 | import java.rmi.RemoteException; 47 | import java.sql.SQLException; 48 | import java.util.LinkedHashSet; 49 | import java.util.LinkedList; 50 | import java.util.List; 51 | import java.util.Set; 52 | 53 | public class QueryToXMLConverter extends PsiElementBaseIntentionAction implements IntentionAction { 54 | 55 | private int TABLE_SCHEME_INDEX = 2; 56 | 57 | @NotNull 58 | public String getText() { 59 | return "Convert query to XML"; 60 | } 61 | 62 | @Nls 63 | @NotNull 64 | @Override 65 | public String getFamilyName() { 66 | return getText(); 67 | } 68 | 69 | @Override 70 | public void invoke(@NotNull final Project project, 71 | final Editor editor, 72 | @NotNull final PsiElement psiElement) throws IncorrectOperationException { 73 | 74 | final ExtractorProperties extractorProperties = 75 | ProjectSettings.getExtractorProperties(SimpleDataContext.getProjectContext(project)); 76 | 77 | final List dataSources = DbPsiFacade.getInstance(project).getDataSources(); 78 | 79 | if (dataSources.isEmpty()) { 80 | showPopup(editor, MessageType.ERROR, "Could not find datasource."); 81 | return; 82 | } 83 | 84 | final String selectedDataSourceName = extractorProperties.getSelectedDataSourceName(); 85 | 86 | final DbDataSource dataSource = getDataSource(editor, dataSources, selectedDataSourceName); 87 | 88 | final String query; 89 | if (editor.getSelectionModel().hasSelection()) { 90 | query = StringUtil.trim(editor.getSelectionModel().getSelectedText()); 91 | } else { 92 | final SmartPsiElementPointer pointer = getNearestPointer(project, psiElement); 93 | if (pointer != null) { 94 | query = pointer.getElement().getText(); 95 | 96 | final int startOffset = pointer.getRange().getStartOffset(); 97 | int endOffset = pointer.getRange().getEndOffset(); 98 | 99 | if (editor.getDocument().getText(TextRange.create(endOffset, endOffset + 1)).equals(";")) { 100 | endOffset += 1; // take semicolon after query 101 | } 102 | editor.getSelectionModel().setSelection(startOffset, endOffset); 103 | } else { 104 | query = null; 105 | } 106 | } 107 | 108 | ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { 109 | @Override 110 | public void run() { 111 | applySelectionChange(project, editor, extractorProperties, dataSource, query); 112 | } 113 | }); 114 | } 115 | 116 | @Nullable 117 | private DbDataSource getDataSource(final Editor editor, 118 | final List dataSources, 119 | final String selectedDataSourceName) { 120 | DbDataSource dataSource = null; 121 | 122 | if (StringUtil.isNotEmpty(selectedDataSourceName)) { 123 | for (final DbDataSource source : dataSources) { 124 | if (source.getName().equals(selectedDataSourceName)) { 125 | dataSource = source; 126 | break; 127 | } 128 | } 129 | } 130 | 131 | if (dataSource == null || StringUtil.isEmpty(selectedDataSourceName)) { 132 | dataSource = dataSources.get(0); 133 | showPopup(editor, 134 | MessageType.INFO, 135 | "Using first found datasource: " + dataSource.getName() + ". Please change default one in options."); 136 | } 137 | return dataSource; 138 | } 139 | 140 | private void applySelectionChange(final Project project, final Editor editor, 141 | final ExtractorProperties extractorProperties, 142 | final DbDataSource dataSource, final String query) { 143 | final String cleanedQuery = query.replaceAll(";$", ""); 144 | RemoteStatement statement = null; 145 | RemoteResultSet resultSet = null; 146 | try (final GuardedRef connection = DbImplUtil.getDatabaseConnection(dataSource, DGDepartment.CODE_GENERATION)) { 147 | statement = connection == null ? null : connection.get().getRemoteConnection().createStatement(); 148 | resultSet = statement == null ? null : statement.executeQuery(cleanedQuery); 149 | 150 | if (resultSet == null) { 151 | showPopup(editor, MessageType.ERROR, "Connection error"); 152 | return; 153 | } 154 | 155 | final RemoteResultSetMetaData metaData = resultSet.getMetaData(); 156 | 157 | Set tableNames = getTablesNamesFromQuery(metaData); 158 | if (tableNames.size() != 1) { 159 | tableNames = getTablesNamesFromQuery(cleanedQuery); 160 | if (tableNames.size() != 1) { 161 | showPopup(editor, MessageType.ERROR, "Only one table queries are supported."); 162 | return; 163 | } 164 | } 165 | 166 | final List columns = constructColumns(metaData); 167 | final List rows = constructRows(metaData, resultSet); 168 | final String tableName = tableNames.iterator().next(); 169 | final String schema = StringUtil.isNotEmpty(metaData.getSchemaName(1)) 170 | ? metaData.getSchemaName(1) 171 | : getSchemaName(connection.get(), tableName); 172 | 173 | final ResultSetHelper resultSetHelper = 174 | new ResultSetHelper(extractorProperties, 175 | schema, 176 | tableName, 177 | columns, 178 | rows); 179 | final XmlGenerator xmlGenerator = 180 | new XmlGenerator(extractorProperties, resultSetHelper); 181 | xmlGenerator.appendRows(); 182 | 183 | WriteCommandAction.runWriteCommandAction(project, new Runnable() { 184 | @Override 185 | public void run() { 186 | replaceSelection(editor, xmlGenerator.getOutput()); 187 | } 188 | }); 189 | } catch (Exception e) { 190 | showPopup(editor, MessageType.ERROR, e.getLocalizedMessage()); 191 | } finally { 192 | if (resultSet != null) { 193 | try { 194 | resultSet.close(); 195 | } catch (Exception e) { 196 | showPopup(editor, MessageType.ERROR, "Failed to close connection."); 197 | } 198 | } 199 | if (statement != null) { 200 | try { 201 | statement.close(); 202 | } catch (Exception e) { 203 | showPopup(editor, MessageType.ERROR, "Failed to close connection."); 204 | } 205 | } 206 | } 207 | } 208 | 209 | @NotNull 210 | private Set getTablesNamesFromQuery(final RemoteResultSetMetaData metaData) 211 | throws SQLException, RemoteException { 212 | Set tableNames = new LinkedHashSet<>(); 213 | for (int i = 1; i <= metaData.getColumnCount(); i++) { 214 | if (StringUtil.isNotEmpty(metaData.getTableName(i))) { 215 | tableNames.add(metaData.getTableName(i)); 216 | } 217 | } 218 | return tableNames; 219 | } 220 | 221 | @NotNull 222 | private Set getTablesNamesFromQuery(final String query) { 223 | Set tableNames = new LinkedHashSet<>(); 224 | 225 | int startIndex = query.indexOf("FROM") + 5; 226 | int endIndex = query.indexOf("WHERE"); 227 | 228 | String querySubstring = null; 229 | if (endIndex > startIndex) { 230 | querySubstring = query.substring(startIndex, endIndex); 231 | } else { 232 | querySubstring = query.substring(startIndex); 233 | } 234 | 235 | String[] tables = querySubstring.split(","); 236 | for (String tableName : tables) { 237 | if (tableName.contains(".")) 238 | tableNames.add(tableName.substring(tableName.indexOf(".") + 1).trim()); 239 | else 240 | tableNames.add(tableName.trim()); 241 | } 242 | 243 | return tableNames; 244 | } 245 | 246 | private String getSchemaName(final DatabaseConnection connection, final String tableName) throws SQLException, RemoteException { 247 | String[] tableType = {"TABLE"}; 248 | final RemoteDatabaseMetaData connectionMetaData = connection.getRemoteMetaData(); 249 | RemoteResultSet result = null; 250 | try { 251 | result = connectionMetaData.getTables(null, null, tableName, tableType); 252 | while (result.next()) { 253 | return result.getString(TABLE_SCHEME_INDEX); 254 | } 255 | } finally { 256 | result.close(); 257 | } 258 | 259 | // Fallback: Try to take schema from tableName. 260 | if (tableName.contains(".")) { 261 | return tableName.substring(0, tableName.indexOf(".")); 262 | } 263 | 264 | return null; 265 | } 266 | 267 | @NotNull 268 | private List constructRows(final RemoteResultSetMetaData metaData, 269 | final RemoteResultSet resultSet) throws SQLException, RemoteException { 270 | final List rows = new LinkedList<>(); 271 | int rowNum = 0; 272 | while (resultSet.next()) { 273 | List values = new LinkedList<>(); 274 | for (int i = 1; i <= metaData.getColumnCount(); i++) { 275 | values.add(resultSet.getString(i)); 276 | } 277 | rows.add(Row.create(rowNum - 1, values.toArray())); 278 | ++rowNum; 279 | } 280 | return rows; 281 | } 282 | 283 | @NotNull 284 | private List constructColumns(final RemoteResultSetMetaData metaData) 285 | throws SQLException, RemoteException { 286 | final List columns = new LinkedList<>(); 287 | 288 | for (int i = 1; i <= metaData.getColumnCount(); i++) { 289 | columns.add(new Column(i - 1, 290 | metaData.getColumnName(i), 291 | metaData.getColumnType(i), 292 | metaData.getColumnTypeName(i), 293 | metaData.getColumnClassName(i))); 294 | } 295 | return columns; 296 | } 297 | 298 | private void replaceSelection(final Editor editor, final XmlOutput xmlOutput) { 299 | final SelectionModel selectionModel = editor.getSelectionModel(); 300 | editor.getDocument() 301 | .replaceString(selectionModel.getSelectionStart(), 302 | selectionModel.getSelectionEnd(), 303 | xmlOutput.getText()); 304 | editor.getSelectionModel().removeSelection(); 305 | } 306 | 307 | @Override 308 | public boolean isAvailable(@NotNull final Project project, 309 | final Editor editor, 310 | @NotNull final PsiElement psiElement) { 311 | final boolean isDataSetFile; 312 | if (editor instanceof EditorWindow) { 313 | isDataSetFile = ((EditorWindow) editor).getDelegate() 314 | .getDocument() 315 | .getText() 316 | .startsWith(""); 317 | } else { 318 | isDataSetFile = editor.getDocument().getText().startsWith(""); 319 | } 320 | SmartPsiElementPointer pointer = getNearestPointer(project, psiElement); 321 | final String selectedText = editor.getSelectionModel().getSelectedText(); 322 | final boolean hasSelectedQuery = editor.getSelectionModel().hasSelection() && selectedText.trim().toUpperCase().startsWith("SELECT"); 323 | return isDataSetFile && (hasSelectedQuery || pointer != null); 324 | } 325 | 326 | private SmartPsiElementPointer getNearestPointer(final @NotNull Project project, 327 | final @NotNull PsiElement psiElement) { 328 | SmartPsiElementPointer pointer = getStatementPointer(project, psiElement); 329 | if (pointer == null && psiElement.getPrevSibling() != null) { 330 | pointer = getStatementPointer(project, psiElement.getPrevSibling()); 331 | } 332 | if (pointer == null && psiElement.getPrevSibling() != null) { 333 | final String prevText = psiElement.getPrevSibling().getText(); 334 | if ((prevText.equals(";") || prevText.isEmpty()) && psiElement.getPrevSibling().getPrevSibling() != null) { 335 | pointer = getStatementPointer(project, psiElement.getPrevSibling().getPrevSibling()); 336 | } 337 | } 338 | return pointer; 339 | } 340 | 341 | @Nullable 342 | private SmartPsiElementPointer getStatementPointer(final @NotNull Project project, 343 | final @NotNull PsiElement psiElement) { 344 | final SqlSelectStatement sqlSelectStatement = 345 | PsiTreeUtil.getParentOfType(psiElement.getContainingFile().findElementAt(psiElement.getTextOffset()), 346 | SqlSelectStatement.class); 347 | SmartPsiElementPointer pointer = null; 348 | if (sqlSelectStatement != null) { 349 | pointer = SmartPointerManager.getInstance(project) 350 | .createSmartPsiElementPointer(sqlSelectStatement); 351 | } 352 | return pointer; 353 | } 354 | 355 | private void showPopup(final Editor editor, 356 | final MessageType messageType, 357 | final String message) { 358 | ApplicationManager.getApplication().invokeLater(new Runnable() { 359 | @Override 360 | public void run() { 361 | JBPopupFactory.getInstance() 362 | .createHtmlTextBalloonBuilder(message, messageType, null) 363 | .setFadeoutTime(7500) 364 | .createBalloon() 365 | .show(JBPopupFactory.getInstance().guessBestPopupLocation(editor), Balloon.Position.atRight); 366 | } 367 | }); 368 | } 369 | } 370 | -------------------------------------------------------------------------------- /src/li/ktt/SQLInjector.java: -------------------------------------------------------------------------------- 1 | package li.ktt; 2 | 3 | import com.intellij.lang.Language; 4 | import com.intellij.openapi.util.TextRange; 5 | import com.intellij.psi.InjectedLanguagePlaces; 6 | import com.intellij.psi.LanguageInjector; 7 | import com.intellij.psi.PsiLanguageInjectionHost; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | public class SQLInjector implements LanguageInjector { 11 | @Override 12 | public void getLanguagesToInject(@NotNull final PsiLanguageInjectionHost host, 13 | @NotNull final InjectedLanguagePlaces places) { 14 | final boolean isSelectQuery = host.getText().trim().toUpperCase().startsWith("SELECT"); 15 | final boolean isDataSetFile = host.getContainingFile().getText().startsWith(""); 16 | if (isDataSetFile && isSelectQuery) { 17 | final Language language = Language.findLanguageByID("SQL"); 18 | if (language != null) { 19 | places.addPlace(language, TextRange.from(0, host.getTextLength()), null, null); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/li/ktt/datagrid/DataGridHelper.java: -------------------------------------------------------------------------------- 1 | package li.ktt.datagrid; 2 | 3 | import com.intellij.database.datagrid.DataGrid; 4 | import com.intellij.database.datagrid.DataGridUtil; 5 | import com.intellij.database.datagrid.GridColumn; 6 | import com.intellij.database.datagrid.GridModel; 7 | import com.intellij.database.datagrid.GridRow; 8 | import com.intellij.database.datagrid.JdbcGridColumn; 9 | import com.intellij.database.model.DasTable; 10 | import com.intellij.database.run.ui.DataAccessType; 11 | import com.intellij.openapi.util.text.StringUtil; 12 | import li.ktt.settings.ExcludedColumns; 13 | import li.ktt.settings.ExtractorProperties; 14 | import org.jetbrains.annotations.NotNull; 15 | import org.jetbrains.annotations.Nullable; 16 | 17 | import java.util.LinkedList; 18 | import java.util.List; 19 | 20 | public class DataGridHelper implements DataHelper { 21 | 22 | private final String schemaName; 23 | 24 | private final String tableName; 25 | 26 | private final List filteredColumns; 27 | 28 | private final List rows; 29 | 30 | private final ExcludedColumns excludedColumns; 31 | 32 | public DataGridHelper(ExtractorProperties extractorProperties, final DataGrid dataGrid) { 33 | this.schemaName = initSchemaName(dataGrid); 34 | this.tableName = initTableName(dataGrid); 35 | String excludedColumnsString = extractorProperties.getExcludeColumns(); 36 | excludedColumns = new ExcludedColumns(excludedColumnsString); 37 | this.filteredColumns = initFilteredColumns(getSelectedColumns(dataGrid)); 38 | this.rows = getSelectedRows(dataGrid); 39 | } 40 | 41 | 42 | public DataGridHelper(String schemaName, String tableName, List filteredColumns, List rows) { 43 | this.schemaName = schemaName; 44 | this.tableName = tableName; 45 | this.excludedColumns = new ExcludedColumns(""); 46 | this.filteredColumns = filteredColumns; 47 | this.rows = rows; 48 | } 49 | 50 | @Override 51 | public String getSchemaName() { 52 | return schemaName; 53 | } 54 | 55 | @Override 56 | public String getTableName() { 57 | return tableName; 58 | } 59 | 60 | @Override 61 | public List getFilteredColumns() { 62 | return filteredColumns; 63 | } 64 | 65 | @Override 66 | public List getRows() { 67 | return rows; 68 | } 69 | 70 | @NotNull 71 | private List getSelectedRows(final DataGrid dataGrid) { 72 | return getDataModel(dataGrid).getRows(dataGrid.getSelectionModel().getSelectedRows()); 73 | } 74 | 75 | @NotNull 76 | private GridModel getDataModel(final DataGrid dataGrid) { 77 | return dataGrid.getDataModel(DataAccessType.DATABASE_DATA); 78 | } 79 | 80 | @NotNull 81 | private List getSelectedColumns(final DataGrid dataGrid) { 82 | return getDataModel(dataGrid).getColumns(dataGrid.getSelectionModel().getSelectedColumns()); 83 | } 84 | 85 | @Nullable 86 | private String initTableName(final DataGrid dataGrid) { 87 | DasTable table = DataGridUtil.getDatabaseTable(dataGrid); 88 | final List columns = getDataModel(dataGrid).getColumns(); 89 | String name = columns.isEmpty() || !(columns.get(0) instanceof JdbcGridColumn) ? null : ((JdbcGridColumn) columns.get(0)).getTable(); 90 | if ((name == null || name.isEmpty()) && table != null) { 91 | return table.getName(); 92 | } 93 | return name; 94 | } 95 | 96 | @Nullable 97 | private String initSchemaName(final DataGrid dataGrid) { 98 | DasTable table = DataGridUtil.getDatabaseTable(dataGrid); 99 | final List columns = getDataModel(dataGrid).getColumns(); 100 | String name = columns.isEmpty() || !(columns.get(0) instanceof JdbcGridColumn) ? null : ((JdbcGridColumn) columns.get(0)).getSchema(); 101 | if (StringUtil.isEmpty(name) && table != null && table.getDasParent() != null) { 102 | name = table.getDasParent().getName(); 103 | } 104 | return name; 105 | } 106 | 107 | private List initFilteredColumns(final List allColumns) { 108 | List filtered = new LinkedList<>(); 109 | for (final GridColumn column : allColumns) { 110 | if (this.excludedColumns.canBeAdded(this.tableName + "." + column.getName())) { 111 | filtered.add(column); 112 | } 113 | } 114 | return filtered; 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/li/ktt/datagrid/DataHelper.java: -------------------------------------------------------------------------------- 1 | package li.ktt.datagrid; 2 | 3 | import com.intellij.database.datagrid.DataConsumer.Column; 4 | import com.intellij.database.datagrid.DataConsumer.Row; 5 | import com.intellij.database.datagrid.GridColumn; 6 | import com.intellij.database.datagrid.GridRow; 7 | 8 | import java.util.List; 9 | 10 | public interface DataHelper { 11 | String getSchemaName(); 12 | 13 | String getTableName(); 14 | 15 | List getFilteredColumns(); 16 | 17 | List getRows(); 18 | } 19 | -------------------------------------------------------------------------------- /src/li/ktt/datagrid/ResultSetHelper.java: -------------------------------------------------------------------------------- 1 | package li.ktt.datagrid; 2 | 3 | import com.intellij.database.datagrid.DataConsumer.Column; 4 | import com.intellij.database.datagrid.DataConsumer.Row; 5 | import com.intellij.database.datagrid.GridColumn; 6 | import com.intellij.database.datagrid.GridRow; 7 | import li.ktt.settings.ExcludedColumns; 8 | import li.ktt.settings.ExtractorProperties; 9 | import net.miginfocom.layout.Grid; 10 | 11 | import java.util.LinkedList; 12 | import java.util.List; 13 | 14 | public class ResultSetHelper implements DataHelper { 15 | 16 | private final String schemaName; 17 | 18 | private final String tableName; 19 | 20 | private final List filteredColumns; 21 | 22 | private final List rows; 23 | 24 | private final ExcludedColumns excludedColumns; 25 | 26 | public ResultSetHelper(ExtractorProperties extractorProperties, 27 | final String schemaName, 28 | final String tableName, 29 | final List columns, 30 | final List rows) { 31 | this.schemaName = schemaName; 32 | this.tableName = tableName; 33 | String excludedColumnsString = extractorProperties.getExcludeColumns(); 34 | excludedColumns = new ExcludedColumns(excludedColumnsString); 35 | this.filteredColumns = initFilteredColumns(columns); 36 | this.rows = rows; 37 | } 38 | 39 | @Override 40 | public String getSchemaName() { 41 | return schemaName; 42 | } 43 | 44 | @Override 45 | public String getTableName() { 46 | return tableName; 47 | } 48 | 49 | @Override 50 | public List getFilteredColumns() { 51 | return filteredColumns; 52 | } 53 | 54 | @Override 55 | public List getRows() { 56 | return rows; 57 | } 58 | 59 | private List initFilteredColumns(final List allColumns) { 60 | List filtered = new LinkedList<>(); 61 | for (final GridColumn column : allColumns) { 62 | if (this.excludedColumns.canBeAdded(this.tableName + "." + column.getName())) { 63 | filtered.add(column); 64 | } 65 | } 66 | return filtered; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/li/ktt/settings/ExcludedColumns.java: -------------------------------------------------------------------------------- 1 | package li.ktt.settings; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.regex.Pattern; 7 | import java.util.regex.PatternSyntaxException; 8 | 9 | public class ExcludedColumns { 10 | 11 | private final String excludedString; 12 | 13 | private final List patterns; 14 | 15 | private final List invalidLines = new ArrayList<>(); 16 | 17 | public ExcludedColumns(String excludedString) { 18 | this.excludedString = excludedString; 19 | this.patterns = initPatterns(this.excludedString); 20 | } 21 | 22 | public boolean isValid() { 23 | return this.invalidLines.isEmpty(); 24 | } 25 | public List getInvalidLines() { 26 | return invalidLines; 27 | } 28 | 29 | public boolean canBeAdded(String fullColumnName) { 30 | boolean result = true; 31 | for (final Pattern pattern : this.patterns) { 32 | if (pattern.matcher(fullColumnName).matches()) { 33 | result = false; 34 | break; 35 | } 36 | } 37 | return result; 38 | } 39 | 40 | protected List getPatterns() { 41 | return this.patterns; 42 | } 43 | 44 | private List initPatterns(String excludedColumns) { 45 | List patterns = new LinkedList(); 46 | if (excludedColumns != null && !excludedColumns.isEmpty()) { 47 | String[] lines = excludedColumns.split("\n"); 48 | for (int i = 0; i < lines.length; i++) { 49 | String line = lines[i]; 50 | try { 51 | Pattern pattern = Pattern.compile(line); 52 | patterns.add(pattern); 53 | } catch (PatternSyntaxException pse) { 54 | invalidLines.add(i + 1); 55 | } 56 | } 57 | } 58 | return patterns; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/li/ktt/settings/ExtractorProperties.java: -------------------------------------------------------------------------------- 1 | package li.ktt.settings; 2 | 3 | public class ExtractorProperties { 4 | 5 | private boolean includeSchema; 6 | 7 | private boolean skipNull; 8 | 9 | private boolean skipEmpty; 10 | 11 | private String excludeColumns; 12 | 13 | private String selectedDataSourceName; 14 | 15 | public ExtractorProperties() { 16 | } 17 | 18 | public ExtractorProperties(boolean includeSchema, boolean skipNull, boolean skipEmpty, String excludeColumns, String selectedDataSourceName) { 19 | this.includeSchema = includeSchema; 20 | this.skipNull = skipNull; 21 | this.skipEmpty = skipEmpty; 22 | this.excludeColumns = excludeColumns; 23 | this.selectedDataSourceName = selectedDataSourceName; 24 | } 25 | 26 | public boolean isSkipNull() { 27 | return skipNull; 28 | } 29 | 30 | public boolean isSkipEmpty() { 31 | return skipEmpty; 32 | } 33 | 34 | public boolean isIncludeSchema() { 35 | return includeSchema; 36 | } 37 | 38 | public String getExcludeColumns() { 39 | return excludeColumns; 40 | } 41 | 42 | public String getSelectedDataSourceName() { 43 | return selectedDataSourceName; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/li/ktt/settings/ProjectSettings.java: -------------------------------------------------------------------------------- 1 | package li.ktt.settings; 2 | 3 | import com.intellij.ide.util.PropertiesComponent; 4 | import com.intellij.openapi.actionSystem.DataContext; 5 | import com.intellij.openapi.actionSystem.PlatformDataKeys; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.openapi.util.Key; 8 | 9 | public class ProjectSettings { 10 | 11 | private static final String DBUNIT_EXTRACTOR_SKIP_NULL_PROPERTY = "DbUnitExtractorSkipNull"; 12 | 13 | private static final String DBUNIT_EXTRACTOR_SKIP_EMPTY_PROPERTY = "DbUnitExtractorSkipEmpty"; 14 | 15 | private static final String DBUNIT_EXTRACTOR_INCLUDE_SCHEMA_PROPERTY = "DbUnitExtractorIncludeSchema"; 16 | 17 | private static final String DBUNIT_EXTRACTOR_EXCLUDE_COLUMNS_PROPERTY = "DbUnitExtractorExcludeColumns"; 18 | 19 | private static final String DBUNIT_EXTRACTOR_SELECTED_DATASOURCE_PROPERTY = "DbUnitExtractorSelectedDataSource"; 20 | 21 | private static final Key DB_UNIT_PROPERTIES_KEY = Key.create( 22 | "DbUnitProperties"); 23 | 24 | private static ProjectSettings instance; 25 | 26 | private final Project project; 27 | 28 | public ProjectSettings(Project project) { 29 | this.project = project; 30 | } 31 | 32 | synchronized public static ProjectSettings getInstance(Project project) { 33 | if (instance == null) { 34 | instance = new ProjectSettings(project); 35 | } 36 | return instance; 37 | } 38 | 39 | public ExtractorProperties setProperties(Boolean includeSchema, Boolean skipNull, Boolean skipEmpty, 40 | String excludeColumns, String selectedDataSourceName) { 41 | final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project); 42 | 43 | propertiesComponent.setValue(DBUNIT_EXTRACTOR_SKIP_NULL_PROPERTY, String.valueOf(skipNull)); 44 | propertiesComponent.setValue(DBUNIT_EXTRACTOR_SKIP_EMPTY_PROPERTY, 45 | String.valueOf(skipEmpty)); 46 | propertiesComponent.setValue(DBUNIT_EXTRACTOR_INCLUDE_SCHEMA_PROPERTY, 47 | String.valueOf(includeSchema)); 48 | propertiesComponent.setValue(DBUNIT_EXTRACTOR_EXCLUDE_COLUMNS_PROPERTY, excludeColumns); 49 | propertiesComponent.setValue(DBUNIT_EXTRACTOR_SELECTED_DATASOURCE_PROPERTY, selectedDataSourceName); 50 | 51 | ExtractorProperties dbUnitProperties = new ExtractorProperties(includeSchema, skipNull, skipEmpty, excludeColumns, selectedDataSourceName); 52 | 53 | project.putUserData(DB_UNIT_PROPERTIES_KEY, dbUnitProperties); 54 | 55 | return dbUnitProperties; 56 | } 57 | 58 | public static ExtractorProperties getExtractorProperties(DataContext dataContext) { 59 | final Project project = PlatformDataKeys.PROJECT.getData(dataContext); 60 | return getExtractorProperties(project); 61 | } 62 | 63 | public ExtractorProperties getExtractorProperties() { 64 | return getExtractorProperties(this.project); 65 | } 66 | 67 | private static ExtractorProperties getExtractorProperties(final Project project) { 68 | PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project); 69 | boolean includeSchema = propertiesComponent.getBoolean(DBUNIT_EXTRACTOR_INCLUDE_SCHEMA_PROPERTY, false); 70 | boolean skipNull = propertiesComponent.getBoolean(DBUNIT_EXTRACTOR_SKIP_NULL_PROPERTY, true); 71 | boolean skipEmpty = propertiesComponent.getBoolean(DBUNIT_EXTRACTOR_SKIP_EMPTY_PROPERTY, true); 72 | String excludedColumns = propertiesComponent.getValue(DBUNIT_EXTRACTOR_EXCLUDE_COLUMNS_PROPERTY, ""); 73 | String selectedDataSource = propertiesComponent.getValue(DBUNIT_EXTRACTOR_SELECTED_DATASOURCE_PROPERTY, ""); 74 | return new ExtractorProperties(includeSchema, skipNull, skipEmpty, excludedColumns, selectedDataSource); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/li/ktt/settings/ProjectSettingsPage.form: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/li/ktt/settings/ProjectSettingsPage.java: -------------------------------------------------------------------------------- 1 | package li.ktt.settings; 2 | 3 | import com.intellij.database.util.DbUtil; 4 | import com.intellij.openapi.options.Configurable; 5 | import com.intellij.openapi.options.ConfigurationException; 6 | import com.intellij.openapi.options.SearchableConfigurable; 7 | import com.intellij.openapi.project.Project; 8 | import org.jetbrains.annotations.Nls; 9 | import org.jetbrains.annotations.NotNull; 10 | import org.jetbrains.annotations.Nullable; 11 | 12 | import javax.swing.*; 13 | 14 | public class ProjectSettingsPage implements SearchableConfigurable, Configurable.NoScroll { 15 | private JCheckBox skipNullValues; 16 | private JCheckBox skipEmptyValues; 17 | private JCheckBox includeSchema; 18 | private JTextArea excludedColumns; 19 | private JPanel panel; 20 | private JLabel selectedDataSource; 21 | private JComboBox selectedDataSourceValue; 22 | 23 | private final ProjectSettings projectSettings; 24 | private ExtractorProperties extractorProperties; 25 | 26 | private Project project; 27 | 28 | public ProjectSettingsPage(final Project project) { 29 | this(ProjectSettings.getInstance(project)); 30 | this.project = project; 31 | } 32 | 33 | protected ProjectSettingsPage(final ProjectSettings projectSettings) { 34 | this.projectSettings = projectSettings; 35 | this.extractorProperties = this.projectSettings.getExtractorProperties(); 36 | } 37 | 38 | @NotNull 39 | @Override 40 | public String getId() { 41 | return getDisplayName(); 42 | } 43 | 44 | @Nullable 45 | @Override 46 | public Runnable enableSearch(final String s) { 47 | return null; 48 | } 49 | 50 | @Nls 51 | @Override 52 | public String getDisplayName() { 53 | return "DbUnit Extractor"; 54 | } 55 | 56 | @Nullable 57 | @Override 58 | public String getHelpTopic() { 59 | return null; 60 | } 61 | 62 | @Nullable 63 | @Override 64 | public JComponent createComponent() { 65 | includeSchema.setSelected(extractorProperties.isIncludeSchema()); 66 | skipNullValues.setSelected(extractorProperties.isSkipNull()); 67 | skipEmptyValues.setSelected(extractorProperties.isSkipEmpty()); 68 | excludedColumns.setText(extractorProperties.getExcludeColumns()); 69 | 70 | selectedDataSourceValue.addItem(""); 71 | for (final String datasourceName : DbUtil.getExistingDataSourceNames(project)) { 72 | selectedDataSourceValue.addItem(datasourceName); 73 | 74 | if (datasourceName.equals(extractorProperties.getSelectedDataSourceName())) { 75 | selectedDataSourceValue.setSelectedIndex(selectedDataSourceValue.getItemCount() - 1); 76 | } 77 | } 78 | return panel; 79 | } 80 | 81 | @Override 82 | public boolean isModified() { 83 | return includeSchema.isSelected() != extractorProperties.isIncludeSchema() 84 | || skipNullValues.isSelected() != extractorProperties.isSkipNull() 85 | || skipEmptyValues.isSelected() != extractorProperties.isSkipEmpty() 86 | || !excludedColumns.getText().equals(extractorProperties.getExcludeColumns()) 87 | || !selectedDataSourceValue.getSelectedItem().equals(extractorProperties.getSelectedDataSourceName()); 88 | } 89 | 90 | @Override 91 | public void apply() throws ConfigurationException { 92 | ExcludedColumns excludeValidator = new ExcludedColumns(excludedColumns.getText()); 93 | if (excludeValidator.isValid()) { 94 | this.extractorProperties = projectSettings.setProperties( 95 | includeSchema.isSelected(), 96 | skipNullValues.isSelected(), 97 | skipEmptyValues.isSelected(), 98 | excludedColumns.getText(), 99 | (String) selectedDataSourceValue.getSelectedItem()); 100 | } else { 101 | String message = invalidRegularExpressionsMessage(excludeValidator); 102 | throw new ConfigurationException(message); 103 | } 104 | } 105 | 106 | @NotNull 107 | private String invalidRegularExpressionsMessage(ExcludedColumns excludeValidator) { 108 | String lines = "Invalid regular expressions in lines: "; 109 | int size = excludeValidator.getInvalidLines().size(); 110 | for (int i = 0; i < size; i++) { 111 | int lineNumber = excludeValidator.getInvalidLines().get(i); 112 | lines += lineNumber; 113 | if (i < size - 1) { 114 | lines += ", "; 115 | } 116 | } 117 | return lines; 118 | } 119 | 120 | @Override 121 | public void reset() { 122 | skipNullValues.setSelected(extractorProperties.isSkipNull()); 123 | skipEmptyValues.setSelected(extractorProperties.isSkipEmpty()); 124 | includeSchema.setSelected(extractorProperties.isIncludeSchema()); 125 | excludedColumns.setText(extractorProperties.getExcludeColumns()); 126 | } 127 | 128 | @Override 129 | public void disposeUIResources() { 130 | } 131 | 132 | protected JCheckBox getIncludeSchema() { 133 | return includeSchema; 134 | } 135 | 136 | protected JCheckBox getSkipNullValues() { 137 | return skipNullValues; 138 | } 139 | 140 | protected JCheckBox getSkipEmptyValues() { 141 | return skipEmptyValues; 142 | } 143 | 144 | protected JTextArea getExcludedColumns() { 145 | return excludedColumns; 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /src/li/ktt/xml/XmlGenerator.java: -------------------------------------------------------------------------------- 1 | package li.ktt.xml; 2 | 3 | import com.intellij.database.datagrid.GridColumn; 4 | import com.intellij.database.datagrid.GridRow; 5 | import com.intellij.database.extractors.tz.TimeZonedTimestamp; 6 | import com.intellij.database.remote.jdbc.LobInfo; 7 | import li.ktt.datagrid.DataHelper; 8 | import li.ktt.settings.ExtractorProperties; 9 | 10 | import java.sql.Timestamp; 11 | import java.time.LocalDateTime; 12 | import java.time.ZoneOffset; 13 | import java.util.List; 14 | 15 | import static com.intellij.openapi.util.text.StringUtil.escapeXmlEntities; 16 | 17 | public class XmlGenerator { 18 | 19 | private ExtractorProperties extractorProperties; 20 | 21 | private DataHelper data; 22 | 23 | private final StringBuilder builder; 24 | 25 | private int rowSize; 26 | 27 | private final int columnsSize; 28 | 29 | private final String tableName; 30 | 31 | public XmlGenerator(ExtractorProperties extractorProperties, DataHelper data) { 32 | this(extractorProperties, data, new StringBuilder()); 33 | } 34 | 35 | public XmlGenerator(ExtractorProperties extractorProperties, DataHelper data, StringBuilder builder) { 36 | this.extractorProperties = extractorProperties; 37 | this.data = data; 38 | this.builder = builder; 39 | this.columnsSize = data.getFilteredColumns().size(); 40 | this.tableName = data.getTableName(); 41 | } 42 | 43 | public XmlOutput getOutput() { 44 | return new XmlOutput(this.builder.toString(), this.rowSize, this.columnsSize, this.tableName); 45 | } 46 | 47 | public XmlGenerator appendRows() { 48 | for (GridRow row : data.getRows()) { 49 | appendRow(row); 50 | } 51 | return this; 52 | } 53 | 54 | public XmlGenerator appendRows(List rows) { 55 | for (GridRow row : rows) { 56 | appendRow(row); 57 | } 58 | return this; 59 | } 60 | 61 | public void appendRow(final GridRow row) { 62 | this.rowSize++; 63 | builder.append("<"); 64 | if (extractorProperties.isIncludeSchema()) { 65 | builder.append(data.getSchemaName()).append("."); 66 | } 67 | builder.append(data.getTableName()).append(" "); 68 | 69 | for (final GridColumn column : data.getFilteredColumns()) { 70 | appendField(builder, row, column); 71 | } 72 | builder.append("/>\n"); 73 | } 74 | 75 | private void appendField(final StringBuilder builder, 76 | final GridRow row, 77 | final GridColumn column) { 78 | final String columnValue = extractStringValue(column.getValue(row)); 79 | if (notNullOrNullAllowed(columnValue) && notEmptyOrEmptyAllowed(columnValue)) { 80 | builder.append(column.getName()).append("=\""); 81 | if (columnValue != null) { 82 | builder.append(escapeXmlEntities(columnValue)); 83 | } 84 | builder.append("\" "); 85 | } 86 | } 87 | 88 | private String extractStringValue(final Object value) { 89 | if (value instanceof TimeZonedTimestamp) { 90 | return ((TimeZonedTimestamp) value).getValue().toString(); 91 | } 92 | if (value instanceof Timestamp) { 93 | return Timestamp.valueOf(LocalDateTime.ofInstant(((Timestamp) value).toInstant(), ZoneOffset.UTC)).toString(); 94 | } 95 | if (value instanceof LobInfo.ClobInfo) { 96 | return ((LobInfo.ClobInfo) value).data; 97 | } 98 | return value != null ? value.toString() : null; 99 | } 100 | 101 | private boolean notEmptyOrEmptyAllowed(final Object columnValue) { 102 | return (columnValue == null || !String.valueOf(columnValue).isEmpty()) || !extractorProperties.isSkipEmpty(); 103 | } 104 | 105 | private boolean notNullOrNullAllowed(final Object columnValue) { 106 | return columnValue != null || !extractorProperties.isSkipNull(); 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/li/ktt/xml/XmlOutput.java: -------------------------------------------------------------------------------- 1 | package li.ktt.xml; 2 | 3 | public class XmlOutput { 4 | 5 | private String text; 6 | private int rowSize; 7 | private int columnsSize; 8 | private String tableName; 9 | 10 | public XmlOutput(String text, int rowSize, int columnsSize, String tableName) { 11 | this.text = text; 12 | this.rowSize = rowSize; 13 | this.columnsSize = columnsSize; 14 | this.tableName = tableName; 15 | } 16 | 17 | public String getText() { 18 | return this.text; 19 | } 20 | 21 | public int getRowSize() { 22 | return this.rowSize; 23 | } 24 | 25 | public int getColumnsSize() { 26 | return columnsSize; 27 | } 28 | 29 | public String getTableName() { 30 | return tableName; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /test/li/ktt/datagrid/DataGridHelperTest.java: -------------------------------------------------------------------------------- 1 | package li.ktt.datagrid; 2 | 3 | import com.intellij.database.datagrid.*; 4 | import com.intellij.database.datagrid.DataConsumer.Column; 5 | import com.intellij.database.datagrid.DataConsumer.Row; 6 | import com.intellij.database.run.ui.DataAccessType; 7 | import li.ktt.settings.ExtractorProperties; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.mockito.Mock; 12 | import org.mockito.runners.MockitoJUnitRunner; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.mockito.Mockito.mock; 19 | import static org.mockito.Mockito.when; 20 | 21 | @RunWith(MockitoJUnitRunner.class) 22 | public class DataGridHelperTest { 23 | 24 | @Mock 25 | private DataGrid dataGridMock; 26 | 27 | @Mock 28 | private SelectionModel selectionModelMock; 29 | 30 | @Mock 31 | private ModelIndexSet selectedRowsMock; 32 | 33 | @Mock 34 | private ModelIndexSet selectedColumnsMock; 35 | 36 | @Mock 37 | private GridModel dataModelMock; 38 | 39 | private final List sampleRows = new ArrayList<>(); 40 | 41 | private final List sampleColumns = new ArrayList<>(); 42 | 43 | private final ExtractorProperties defaultProperties = new ExtractorProperties(true, true, true, "", null); 44 | 45 | @Before 46 | public void before() { 47 | // dataGrid 48 | when(dataGridMock.getDataModel(DataAccessType.DATABASE_DATA)).thenReturn(dataModelMock); 49 | when(dataGridMock.getSelectionModel()).thenReturn(selectionModelMock); 50 | 51 | // selectionModel 52 | when(selectionModelMock.getSelectedRows()).thenReturn(selectedRowsMock); 53 | when(selectionModelMock.getSelectedColumns()).thenReturn(selectedColumnsMock); 54 | 55 | // dataModel 56 | when(dataModelMock.getRows(selectedRowsMock)).thenReturn(sampleRows); 57 | when(dataModelMock.getColumns(selectedColumnsMock)).thenReturn(sampleColumns); 58 | 59 | sampleRows.add(mock(Row.class)); 60 | sampleRows.add(mock(Row.class)); 61 | 62 | 63 | sampleColumns.add(mock(Column.class)); 64 | sampleColumns.add(mock(Column.class)); 65 | sampleColumns.add(mock(Column.class)); 66 | } 67 | 68 | @Test 69 | public void shouldReturnAllRows() { 70 | // given 71 | 72 | DataGridHelper data = new DataGridHelper(defaultProperties, dataGridMock); 73 | 74 | // when 75 | List result = data.getRows(); 76 | 77 | // then 78 | assertEquals(sampleRows, result); 79 | } 80 | 81 | @Test 82 | public void shouldReturnFullListOfColumnIfExcludedPatternIsEmpty() { 83 | // when 84 | DataGridHelper data = new DataGridHelper(defaultProperties, dataGridMock); 85 | 86 | // then 87 | List result = data.getFilteredColumns(); 88 | assertEquals(sampleColumns, result); 89 | } 90 | 91 | @Test 92 | public void shouldReturnSchemaAndTableAsNullIfFirstColumnSchemaReturnNullAndDatabaseTableNull() { 93 | // when 94 | DataGridHelper data = new DataGridHelper(defaultProperties, dataGridMock); 95 | 96 | // then 97 | assertEquals(null, data.getSchemaName()); 98 | assertEquals(null, data.getTableName()); 99 | } 100 | 101 | @Test 102 | public void shouldReturnSchemaAndTableNamesFromFirstColumn() { 103 | // given 104 | List localSampleColumns = new ArrayList<>(); 105 | localSampleColumns.add(new Column(1, "superCol", 1, "type", "String", 1, 2, "catalog", "schema201", "superTable201")); 106 | localSampleColumns.add(new Column(2, "superCol", 1, "type", "String", 1, 2, "catalog", "schema203", "superTable201")); 107 | when(dataModelMock.getColumns()).thenReturn(localSampleColumns); 108 | 109 | // when 110 | DataGridHelper data = new DataGridHelper(defaultProperties, dataGridMock); 111 | 112 | // then 113 | assertEquals("schema201", data.getSchemaName()); 114 | assertEquals("superTable201", data.getTableName()); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /test/li/ktt/settings/ExcludedColumnsTest.java: -------------------------------------------------------------------------------- 1 | package li.ktt.settings; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public class ExcludedColumnsTest { 8 | 9 | private static final String VALID_EXCLUDED_LIST = "ha!\n" 10 | + "someValidValue\n" 11 | + ".*\\.dbo\n" 12 | + "tralala[A-Z]\n" + 13 | "((haa!))"; 14 | 15 | @Test 16 | public void shouldReturnOnlyValidPattern() { 17 | // given 18 | String excludedString = "\\\n" 19 | + "someValidValue\n" 20 | + ".*\\.dbo\n" 21 | + "tralla[\n" + 22 | "())"; 23 | 24 | // when 25 | ExcludedColumns excludedColumns = new ExcludedColumns(excludedString); 26 | 27 | // then 28 | assertEquals(false, excludedColumns.isValid()); 29 | assertEquals(2, excludedColumns.getPatterns().size()); 30 | assertEquals(3, excludedColumns.getInvalidLines().size()); 31 | assertEquals(true, excludedColumns.getInvalidLines().contains(1)); 32 | assertEquals(true, excludedColumns.getInvalidLines().contains(4)); 33 | assertEquals(true, excludedColumns.getInvalidLines().contains(5)); 34 | } 35 | 36 | @Test 37 | public void shouldBeValid() { 38 | // when 39 | ExcludedColumns excludedColumns = new ExcludedColumns(VALID_EXCLUDED_LIST); 40 | 41 | // then 42 | assertEquals(true, excludedColumns.isValid()); 43 | assertEquals(5, excludedColumns.getPatterns().size()); 44 | assertEquals(0, excludedColumns.getInvalidLines().size()); 45 | } 46 | 47 | @Test 48 | public void shouldBeAdded() { 49 | // when 50 | ExcludedColumns excludedColumns = new ExcludedColumns(VALID_EXCLUDED_LIST); 51 | 52 | // then 53 | String[] shouldBeAddedValues = new String[]{"hej", "dbo.Proudcts", "dbo.Kojoty", "tralala1"}; 54 | for (String value : shouldBeAddedValues) { 55 | assertEquals(true, excludedColumns.canBeAdded(value)); 56 | } 57 | } 58 | 59 | @Test 60 | public void shouldNotBeAdded() { 61 | // when 62 | ExcludedColumns excludedColumns = new ExcludedColumns(VALID_EXCLUDED_LIST); 63 | 64 | // then 65 | String[] shouldNotBeAddedValues = new String[]{ "tralalaZ", "tralalaA", "someValidValue"}; 66 | for (String value : shouldNotBeAddedValues) { 67 | assertEquals(false, excludedColumns.canBeAdded(value)); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /test/li/ktt/settings/ProjectSettingsPageTest.java: -------------------------------------------------------------------------------- 1 | package li.ktt.settings; 2 | 3 | import com.intellij.openapi.options.ConfigurationException; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.mockito.Mock; 7 | import org.mockito.runners.MockitoJUnitRunner; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.fail; 11 | import static org.mockito.Matchers.anyBoolean; 12 | import static org.mockito.Matchers.anyString; 13 | import static org.mockito.Mockito.*; 14 | 15 | @RunWith(MockitoJUnitRunner.class) 16 | public class ProjectSettingsPageTest { 17 | 18 | @Mock 19 | private ProjectSettings projectSettings; 20 | 21 | @Test 22 | public void shouldReturnCorrectPluginName() { 23 | // when 24 | ProjectSettingsPage page = new ProjectSettingsPage(projectSettings); 25 | 26 | // then 27 | assertEquals("DbUnit Extractor", page.getDisplayName()); 28 | assertEquals("DbUnit Extractor", page.getId()); 29 | } 30 | 31 | @Test 32 | public void shouldNotBeModify() { 33 | // given 34 | ProjectSettingsPage page = preparePageWithMocks(); 35 | // when // then 36 | assertEquals(false, page.isModified()); 37 | } 38 | 39 | @Test 40 | public void shouldBeModifyIfIncludeSchemaHasChanged() { 41 | // given 42 | ProjectSettingsPage page = preparePageWithMocks(); 43 | // when 44 | page.getIncludeSchema().setSelected(false); 45 | // then 46 | assertEquals(true, page.isModified()); 47 | } 48 | 49 | @Test 50 | public void shouldBeModifyIfSkipNullsHasChanged() { 51 | // given 52 | ProjectSettingsPage page = preparePageWithMocks(); 53 | // when 54 | page.getSkipNullValues().setSelected(true); 55 | // then 56 | assertEquals(true, page.isModified()); 57 | } 58 | 59 | @Test 60 | public void shouldBeModifyIfSkipEmptyHasChanged() { 61 | // given 62 | ProjectSettingsPage page = preparePageWithMocks(); 63 | // when 64 | page.getSkipEmptyValues().setSelected(false); 65 | // then 66 | assertEquals(true, page.isModified()); 67 | } 68 | 69 | @Test 70 | public void shouldBeModifyIfExcludedColumnsHasChanged() { 71 | // given 72 | ProjectSettingsPage page = preparePageWithMocks(); 73 | // when 74 | page.getExcludedColumns().setText("SomeNewVALUE"); 75 | // then 76 | assertEquals(true, page.isModified()); 77 | } 78 | 79 | @Test 80 | public void shouldReturnNullForUnusedElements() { 81 | // when 82 | ProjectSettingsPage page = new ProjectSettingsPage(projectSettings); 83 | 84 | // then 85 | assertEquals(null, page.getHelpTopic()); 86 | assertEquals(null, page.enableSearch("ble")); 87 | } 88 | 89 | @Test 90 | public void shouldApplyConfiguration() throws ConfigurationException { 91 | // given 92 | ProjectSettingsPage page = preparePageWithMocks(); 93 | 94 | // when 95 | page.getExcludedColumns().setText("newVALUE"); 96 | page.getIncludeSchema().setSelected(false); 97 | page.getSkipNullValues().setSelected(true); 98 | page.apply(); 99 | 100 | // then 101 | verify(projectSettings).setProperties(false, true, true, "newVALUE", null); 102 | } 103 | 104 | @Test 105 | public void shouldNotApplyConfiguration() { 106 | // given 107 | ProjectSettingsPage page = preparePageWithMocks(); 108 | 109 | // when 110 | page.getExcludedColumns().setText("newVALUE\\\n" 111 | + "someGoodValue\n" 112 | + "[\n" 113 | + "(\n" 114 | + ".*dbo\n"); 115 | page.getIncludeSchema().setSelected(false); 116 | page.getSkipNullValues().setSelected(true); 117 | try { 118 | page.apply(); 119 | fail("Should throw an exception"); 120 | } catch (ConfigurationException e) { 121 | assertEquals("Invalid regular expressions in lines: 1, 3, 4", e.getMessage()); 122 | } 123 | 124 | // then 125 | verify(projectSettings, times(0)).setProperties(anyBoolean(), anyBoolean(), anyBoolean(), anyString(), anyString()); 126 | } 127 | 128 | @Test 129 | public void shouldResetConfiguration() { 130 | // given 131 | ProjectSettingsPage page = preparePageWithMocks(); 132 | 133 | // when 134 | page.getExcludedColumns().setText("newVALUE\\\n" 135 | + "someGoodValue\n" 136 | + "[\n" 137 | + "(\n" 138 | + ".*dbo\n"); 139 | page.getIncludeSchema().setSelected(false); 140 | page.getSkipNullValues().setSelected(true); 141 | 142 | page.reset(); 143 | 144 | // then 145 | assertEquals(true, page.getIncludeSchema().isSelected()); 146 | assertEquals(false, page.getSkipNullValues().isSelected()); 147 | assertEquals(true, page.getSkipEmptyValues().isSelected()); 148 | assertEquals("ble\\.value\n", page.getExcludedColumns().getText()); 149 | } 150 | 151 | private ProjectSettingsPage preparePageWithMocks() { 152 | ExtractorProperties extractorProperties = new ExtractorProperties(true, false, true, "ble\\.value\n", null); 153 | when(projectSettings.getExtractorProperties()).thenReturn(extractorProperties); 154 | ProjectSettingsPage page = new ProjectSettingsPage(projectSettings); 155 | page.createComponent(); 156 | return page; 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /test/li/ktt/xml/XmlGeneratorTest.java: -------------------------------------------------------------------------------- 1 | package li.ktt.xml; 2 | 3 | import com.intellij.database.datagrid.DataConsumer.Column; 4 | import com.intellij.database.datagrid.DataConsumer.Row; 5 | import com.intellij.database.datagrid.GridColumn; 6 | import com.intellij.database.datagrid.GridRow; 7 | import li.ktt.datagrid.DataGridHelper; 8 | import li.ktt.settings.ExtractorProperties; 9 | import org.junit.BeforeClass; 10 | import org.junit.Test; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | 17 | public class XmlGeneratorTest { 18 | 19 | private static final String NO_EXCLUDED_COLUMNS = ""; 20 | 21 | private static final List columns = new ArrayList<>(); 22 | 23 | private static final List rows = new ArrayList<>(); 24 | 25 | @BeforeClass 26 | public static void before() { 27 | columns.add(createColumn(1, "Name")); 28 | columns.add(createColumn(2, "Value")); 29 | columns.add(createColumn(3, "Type")); 30 | 31 | rows.add(createRow(1, "a1", "Super1", "B12", "g1", "...")); 32 | rows.add(createRow(2, "a3", "Super3", "j55", "g12")); 33 | rows.add(createRow(3, "a4", "Super3", "with null", null)); 34 | rows.add(createRow(4, "a4", "Super3", "with empty value", "")); 35 | } 36 | 37 | @Test 38 | public void shouldReturnAllRowsWithSchema() { 39 | // given 40 | ExtractorProperties extractorProperties = new ExtractorProperties(true, true, true, NO_EXCLUDED_COLUMNS, null); 41 | DataGridHelper data = new DataGridHelper("superSchema2", "Table2", columns, rows); 42 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 43 | 44 | // when 45 | generator.appendRows(rows); 46 | 47 | // then 48 | String expectedResult = "\n" + 49 | "\n" + 50 | "\n" + 51 | "\n"; 52 | assertEquals(expectedResult, generator.getOutput().getText()); 53 | assertEquals(4, generator.getOutput().getRowSize()); 54 | assertEquals(3, generator.getOutput().getColumnsSize()); 55 | } 56 | 57 | @Test 58 | public void shouldReturnAllRowsIncludingEmptiesWithSchema() { 59 | // given 60 | ExtractorProperties extractorProperties = new ExtractorProperties(true, true, false, NO_EXCLUDED_COLUMNS, null); 61 | DataGridHelper data = new DataGridHelper("superSchema2", "Table2", columns, rows); 62 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 63 | 64 | // when 65 | generator.appendRows(rows); 66 | 67 | // then 68 | String expectedResult = "\n" + 69 | "\n" + 70 | "\n" + 71 | "\n"; 72 | assertEquals(expectedResult, generator.getOutput().getText()); 73 | assertEquals(4, generator.getOutput().getRowSize()); 74 | assertEquals(3, generator.getOutput().getColumnsSize()); 75 | } 76 | 77 | @Test 78 | public void shouldReturnAllRowsIncludingNullsWithSchema() { 79 | // given 80 | ExtractorProperties extractorProperties = new ExtractorProperties(true, false, true, NO_EXCLUDED_COLUMNS, null); 81 | DataGridHelper data = new DataGridHelper("superSchema2", "Table2", columns, rows); 82 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 83 | 84 | // when 85 | generator.appendRows(rows); 86 | 87 | // then 88 | String expectedResult = "\n" + 89 | "\n" + 90 | "\n" + 91 | "\n"; 92 | assertEquals(expectedResult, generator.getOutput().getText()); 93 | assertEquals(4, generator.getOutput().getRowSize()); 94 | assertEquals(3, generator.getOutput().getColumnsSize()); 95 | } 96 | 97 | @Test 98 | public void shouldReturnAllRowsIncludingNullsAndEmptiesWithSchema() { 99 | // given 100 | ExtractorProperties extractorProperties = new ExtractorProperties(true, false, false, NO_EXCLUDED_COLUMNS, null); 101 | DataGridHelper data = new DataGridHelper("superSchema2", "Table2", columns, rows); 102 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 103 | 104 | // when 105 | generator.appendRows(rows); 106 | 107 | // then 108 | String expectedResult = "\n" + 109 | "\n" + 110 | "\n" + 111 | "\n"; 112 | assertEquals(expectedResult, generator.getOutput().getText()); 113 | assertEquals(4, generator.getOutput().getRowSize()); 114 | assertEquals(3, generator.getOutput().getColumnsSize()); 115 | } 116 | 117 | @Test 118 | public void shouldReturnAllRowsWithoutSchema() { 119 | // given 120 | ExtractorProperties extractorProperties = new ExtractorProperties(false, true, true, NO_EXCLUDED_COLUMNS, null); 121 | DataGridHelper data = new DataGridHelper("superSchema1", "Table1", columns, rows); 122 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 123 | 124 | // when 125 | generator.appendRows(rows); 126 | 127 | // then 128 | String expectedResult = "\n" + 129 | "\n" + 130 | "\n" + 131 | "\n"; 132 | assertEquals(expectedResult, generator.getOutput().getText()); 133 | assertEquals(4, generator.getOutput().getRowSize()); 134 | assertEquals(3, generator.getOutput().getColumnsSize()); 135 | } 136 | 137 | @Test 138 | public void shouldReturnAllRowsIncludingEmptiesWithoutSchema() { 139 | // given 140 | ExtractorProperties extractorProperties = new ExtractorProperties(false, true, false, NO_EXCLUDED_COLUMNS, null); 141 | DataGridHelper data = new DataGridHelper("superSchema1", "Table1", columns, rows); 142 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 143 | 144 | // when 145 | generator.appendRows(); 146 | 147 | // then 148 | String expectedResult = "\n" + 149 | "\n" + 150 | "\n" + 151 | "\n"; 152 | assertEquals(expectedResult, generator.getOutput().getText()); 153 | assertEquals(4, generator.getOutput().getRowSize()); 154 | assertEquals(3, generator.getOutput().getColumnsSize()); 155 | } 156 | 157 | @Test 158 | public void shouldReturnAllRowsIncludingNullsWithoutSchema() { 159 | // given 160 | ExtractorProperties extractorProperties = new ExtractorProperties(false, false, true, NO_EXCLUDED_COLUMNS, null); 161 | DataGridHelper data = new DataGridHelper("superSchema1", "Table1", columns, rows); 162 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 163 | 164 | // when 165 | generator.appendRows(); 166 | 167 | // then 168 | String expectedResult = "\n" + 169 | "\n" + 170 | "\n" + 171 | "\n"; 172 | assertEquals(expectedResult, generator.getOutput().getText()); 173 | assertEquals(4, generator.getOutput().getRowSize()); 174 | assertEquals(3, generator.getOutput().getColumnsSize()); 175 | } 176 | 177 | @Test 178 | public void shouldReturnAllRowsIncludingNullsAndEmptiesWithoutSchema() { 179 | // given 180 | ExtractorProperties extractorProperties = new ExtractorProperties(false, false, false, NO_EXCLUDED_COLUMNS, null); 181 | DataGridHelper data = new DataGridHelper("superSchema1", "Table1", columns, rows); 182 | XmlGenerator generator = new XmlGenerator(extractorProperties, data); 183 | 184 | // when 185 | generator.appendRows(rows); 186 | 187 | // then 188 | String expectedResult = "\n" + 189 | "\n" + 190 | "\n" + 191 | "\n"; 192 | assertEquals(expectedResult, generator.getOutput().getText()); 193 | assertEquals(4, generator.getOutput().getRowSize()); 194 | assertEquals(3, generator.getOutput().getColumnsSize()); 195 | } 196 | 197 | private static Row createRow(int num, String... values) { 198 | return Row.create(num, values); 199 | } 200 | 201 | private static Column createColumn(int columnNum, String name) { 202 | return new Column(columnNum, name, 0, "typeName", "String"); 203 | } 204 | 205 | } 206 | --------------------------------------------------------------------------------