PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/ui/listeners/ExportWikiListenerTest.java

https://bitbucket.org/atlassianlabs/universal-wiki-connector
Java | 192 lines | 122 code | 37 blank | 33 comment | 3 complexity | 9a4031e2d39ad928f5706a0cb7754170 MD5 | raw file
  1. package com.atlassian.uwc.ui.listeners;
  2. import java.io.File;
  3. import java.util.Iterator;
  4. import javax.swing.ComboBoxModel;
  5. import javax.swing.DefaultComboBoxModel;
  6. import javax.swing.JComboBox;
  7. import junit.framework.TestCase;
  8. import org.apache.log4j.Logger;
  9. import org.apache.log4j.PropertyConfigurator;
  10. import com.atlassian.uwc.ui.FeedbackWindow;
  11. import com.atlassian.uwc.ui.UWCGuiModel;
  12. public class ExportWikiListenerTest extends TestCase {
  13. private static final String CONF_DEFAULT = "conf";
  14. private static final String CONF_LOCAL = "conf-local";
  15. private static final String TEST_EXPORTSETTINGS_DIR = "/Users/laura/Code/Subversion/universal-wiki-converter/devel/sampleData/mediawiki/testexporter";
  16. ExportWikiListener tester = null;
  17. private String dir;
  18. private JComboBox wikitypes;
  19. private UWCGuiModel model;
  20. private Logger log = Logger.getLogger(this.getClass());
  21. protected void setUp() throws Exception {
  22. PropertyConfigurator.configure("log4j.properties");
  23. wikitypes = new JComboBox();
  24. model = new UWCGuiModel();
  25. this.dir = CONF_DEFAULT;
  26. //note: if we use a different dir, we need to call these methods again
  27. setupModel(this.dir);
  28. }
  29. public void testComboBoxSetting() {
  30. //just a reminder that we're using the default directory by ... default.
  31. String input = "mediawiki";
  32. wikitypes.setSelectedItem(input);
  33. assertEquals(input, wikitypes.getSelectedItem());
  34. }
  35. public void testExportWiki() {
  36. this.dir = CONF_LOCAL;
  37. setupModel(this.dir);
  38. wikitypes.setSelectedItem("mediawiki"); //I'm pretty clear on what the mediawiki exporter should do.
  39. //check that existing exported dir doesn't exist
  40. String EXPORT_DIR = "/Users/laura/Desktop/exported_mediawiki_pages";
  41. File exportFile = new File(EXPORT_DIR);
  42. if (exportFile.exists()) {
  43. String message = "You need to delete '" + EXPORT_DIR + "' directory, first.";
  44. log.error(message);
  45. fail(message);
  46. }
  47. //test pre-export feedback
  48. ExportWikiListener.Feedback expected = ExportWikiListener.Feedback.NONE;
  49. assertEquals(expected, tester.getFeedback());
  50. //XXX Remember we need the database to be on in order for the export to work.
  51. //test exporting
  52. tester.exportWiki();
  53. //test directory was created.
  54. assertTrue (exportFile.exists()); //exists
  55. assertTrue (exportFile.isDirectory()); //is a dir
  56. //test that it has contents
  57. String[] exportChildren = exportFile.list();
  58. int numExportChildren = exportChildren.length;
  59. assertTrue (numExportChildren > 0);
  60. //look for at least one specific file that should be there
  61. boolean found = false;
  62. for (String child : exportChildren) {
  63. File childFile = new File(EXPORT_DIR + File.separator + child);
  64. assertTrue(childFile.exists());
  65. //look for the Pages directory
  66. if (child.equals("Pages")) {
  67. assertTrue(childFile.exists());
  68. assertTrue(childFile.isDirectory());
  69. found = true;
  70. break;
  71. }
  72. }
  73. assertTrue("No Pages directory.", found);
  74. //what's the result for a succesful export
  75. expected = ExportWikiListener.Feedback.OK;
  76. assertEquals(expected, tester.getFeedback());
  77. }
  78. public void testNoExporterForThatWikitype() {
  79. //what's the result if the wikitype has no exporter
  80. //still using the default directory, but we have to explicitly set tikiwiki
  81. //in the model, 'cause we can't select a non-existant ite,
  82. wikitypes.setModel(new DefaultComboBoxModel(new String []{"tikiwiki", "mediawiki"}));
  83. wikitypes.setSelectedItem("tikiwiki"); //not exportable at this time
  84. tester.exportWiki();
  85. ExportWikiListener.Feedback expected = ExportWikiListener.Feedback.NO_EXPORTER_FILE;
  86. assertEquals(expected, tester.getFeedback());
  87. }
  88. public void testDbConnectionRefused() {
  89. //what's the result if the db connection is refused
  90. String test = "dbtest";
  91. setupSettingsTest(test);
  92. ExportWikiListener.Feedback expected = ExportWikiListener.Feedback.DB_FAILURE;
  93. assertEquals(expected, tester.getFeedback());
  94. }
  95. public void testBadSettings() {
  96. //what's the result if any of the non-db settings are bad
  97. String test;
  98. ExportWikiListener.Feedback expected;
  99. //bad exporter class
  100. test = "badclass";
  101. expected = ExportWikiListener.Feedback.BAD_EXPORTER_CLASS;
  102. setupSettingsTest(test);
  103. assertEquals(expected, tester.getFeedback());
  104. //bad db name
  105. test = "badDbName";
  106. expected = ExportWikiListener.Feedback.DB_FAILURE;
  107. setupSettingsTest(test);
  108. assertEquals(expected, tester.getFeedback());
  109. //bad dbUrl
  110. test = "badDbUrl";
  111. expected = ExportWikiListener.Feedback.DB_FAILURE;
  112. setupSettingsTest(test);
  113. assertEquals(expected, tester.getFeedback());
  114. //bad driver
  115. test = "badDriver";
  116. expected = ExportWikiListener.Feedback.DB_DRIVER_FAILURE;
  117. setupSettingsTest(test);
  118. assertEquals(expected, tester.getFeedback());
  119. //bad login
  120. test = "badLogin";
  121. expected = ExportWikiListener.Feedback.DB_FAILURE;
  122. setupSettingsTest(test);
  123. assertEquals(expected, tester.getFeedback());
  124. //bad password
  125. test = "badPass";
  126. expected = ExportWikiListener.Feedback.DB_FAILURE;
  127. setupSettingsTest(test);
  128. assertEquals(expected, tester.getFeedback());
  129. //bad prefix
  130. test = "badPrefix";
  131. expected = ExportWikiListener.Feedback.DB_FAILURE;
  132. setupSettingsTest(test);
  133. assertEquals(expected, tester.getFeedback());
  134. }
  135. /* Helper Methods */
  136. /**
  137. * prepares combobox model that will be used by the tester object
  138. * @param dir
  139. */
  140. private void setupModel(String dir) {
  141. wikitypes.setModel(new DefaultComboBoxModel(model.getExportTypes(dir)));
  142. tester = new ExportWikiListener(wikitypes, model, dir, new FeedbackWindow() );
  143. }
  144. /**
  145. * helper method to test a particular file's bad setting
  146. * @param test identifying string for the particular file
  147. */
  148. private void setupSettingsTest(String test) {
  149. this.dir = TEST_EXPORTSETTINGS_DIR; //this is where our test prop files will live
  150. //we've changed the dir. the model has to be reset
  151. setupModel(this.dir);
  152. wikitypes.setSelectedItem(test); //looks for exporter.dbtest.properties
  153. tester.exportWiki();
  154. }
  155. }