PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/converters/tikiwiki/AttachmentConverterTest.java

https://bitbucket.org/appfusions/universal-wiki-converter
Java | 796 lines | 632 code | 122 blank | 42 comment | 12 complexity | da502a16cad6263c26b72b990eb66955 MD5 | raw file
  1. package com.atlassian.uwc.converters.tikiwiki;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.Set;
  5. import java.util.TreeMap;
  6. import java.util.Vector;
  7. import junit.framework.TestCase;
  8. import org.apache.log4j.Logger;
  9. import org.apache.log4j.PropertyConfigurator;
  10. import com.atlassian.uwc.ui.ConverterErrors;
  11. import com.atlassian.uwc.ui.Page;
  12. public class AttachmentConverterTest extends TestCase {
  13. AttachmentConverter tester;
  14. HashMap<String,String> idsAndNames;
  15. private static final String DB_PROP_FILENAME = "settings.tikiwiki.properties";
  16. String location = "conf-local/" + DB_PROP_FILENAME; //conf-local should be a sibling dir to conf
  17. String attachDir = "/Users/laura/Code/Workspace/tikiwiki-1.9.5";
  18. Logger log = Logger.getLogger(this.getClass());
  19. protected void setUp() throws Exception {
  20. super.setUp();
  21. PropertyConfigurator.configure("log4j.properties");
  22. tester = new AttachmentConverter();
  23. idsAndNames = new HashMap<String,String>();
  24. idsAndNames.put("1", "ABC");
  25. idsAndNames.put("12", "DEF");
  26. tester.setPropertyLocation(location);
  27. tester.FILE_SEP = "/";
  28. }
  29. public void testReplaceImageIds() {
  30. String input = "{img src=\"show_image.php?id=1\"}";
  31. String expected = "{img src=\"show_image.php?name=ABC\"}";
  32. String actual = tester.replaceImageIds(
  33. input, idsAndNames, AttachmentConverter.GalleryType.IMAGE);
  34. assertEquals(expected, actual);
  35. }
  36. public void testWithAmp() {
  37. String input = "{img src=\"show_image.php?id=12&width=200\"}";
  38. String expected = "{img src=\"show_image.php?name=DEF&width=200\"}";
  39. String actual = tester.replaceImageIds(
  40. input, idsAndNames, AttachmentConverter.GalleryType.IMAGE);
  41. assertEquals(expected, actual);
  42. }
  43. public void testWithQuotes() {
  44. String input = "{img src=\"show_image.php?id=12&width=200\" }";
  45. String expected = "{img src=\"show_image.php?name=DEF&width=200\" }";
  46. String actual = tester.replaceImageIds(
  47. input, idsAndNames, AttachmentConverter.GalleryType.IMAGE);
  48. assertEquals(expected, actual);
  49. }
  50. public void testIsGalleryConversion() {
  51. TreeMap<String,String> map = new TreeMap<String,String>();
  52. tester.properties = map;
  53. map.put(tester.PROPERTIES_CONVERTGALLERY, "true");
  54. boolean actual = tester.isGalleryConversion();
  55. assertTrue(actual);
  56. map.put(tester.PROPERTIES_CONVERTGALLERY, "false");
  57. actual = tester.isGalleryConversion();
  58. assertFalse(actual);
  59. }
  60. public void testCombineVectors() {
  61. Vector<String> a = new Vector<String>();
  62. a = vectorUtilAdd("a,b", a);
  63. Vector<String> b = new Vector<String>();
  64. b = vectorUtilAdd("b,c,x", b);
  65. Vector<String> expected = new Vector<String>();
  66. expected = vectorUtilAdd("a,b,c,x", expected);
  67. Vector<String> actual = tester.combineVectors(a, b);
  68. vectorUtilCompare("combining vectors 1", expected, actual);
  69. a.removeAllElements();
  70. a = vectorUtilAdd("a,b,c", a);
  71. b.removeAllElements();
  72. b = vectorUtilAdd("d,e,f", b);
  73. expected.removeAllElements();
  74. expected = vectorUtilAdd("a,b,c,d,e,f", expected);
  75. actual.removeAllElements();
  76. actual = tester.combineVectors(a, b);
  77. vectorUtilCompare("combining vectors 2", expected, actual);
  78. b.removeAllElements();
  79. b = vectorUtilAdd("a,b,c",b);
  80. expected.removeAllElements();
  81. expected = vectorUtilAdd("a,b,c",expected);
  82. actual.removeAllElements();
  83. actual = tester.combineVectors(a, b);
  84. vectorUtilCompare("combining vectors 2", expected, actual);
  85. }
  86. private void vectorUtilCompare(String msg, Vector<String> expected, Vector<String> actual) {
  87. assertNotNull(msg + " is null", actual);
  88. assertTrue(msg + " size not same", expected.size() == actual.size());
  89. for(int i = 0; i < actual.size(); i++) {
  90. String exp = expected.get(i);
  91. String act = actual.get(i);
  92. assertEquals(msg + " index " + i + " comparison", exp, act);
  93. }
  94. }
  95. private Vector<String> vectorUtilAdd(String commaDelimed, Vector<String> v) {
  96. String[] items = commaDelimed.split(",");
  97. for (String i : items) {
  98. v.add(i);
  99. }
  100. return v;
  101. }
  102. public void testStandardizeImageSyntax() {
  103. String input =
  104. "{img src=&quot;img/wiki_up/hobbespounce.gif&quot; width=&quot;200&quot;}\n" +
  105. "{img src=show_image.php?name=Wiki.png }\n" +
  106. "{img src=show_image.php?id=1 }\n" +
  107. "\n" +
  108. "Importance of quotes\n" +
  109. "{img src=img/wiki_up/hobbespounce.gif}\n" +
  110. "{img src=\"show_image.php?name=Wiki.png\" }\n" +
  111. "\n" +
  112. "Importance of space\n" +
  113. "{img src=\"img/wiki_up/hobbespounce.gif\"}\n" +
  114. "{img src=\"img/wiki_up/hobbespounce.gif\" }\n" +
  115. "{img src=show_image.php?name=Wiki.png}\n" +
  116. "{img src=http://localhost:8081/tikiwiki-1.9.5/images/book.gif}" +
  117. "";
  118. String expected =
  119. "{img src=\"img/wiki_up/hobbespounce.gif\" width=\"200\"}\n" +
  120. "{img src=\"show_image.php?name=Wiki.png\" }\n" +
  121. "{img src=\"show_image.php?id=1\" }\n" +
  122. "\n" +
  123. "Importance of quotes\n" +
  124. "{img src=\"img/wiki_up/hobbespounce.gif\"}\n" +
  125. "{img src=\"show_image.php?name=Wiki.png\" }\n" +
  126. "\n" +
  127. "Importance of space\n" +
  128. "{img src=\"img/wiki_up/hobbespounce.gif\"}\n" +
  129. "{img src=\"img/wiki_up/hobbespounce.gif\" }\n" +
  130. "{img src=\"show_image.php?name=Wiki.png\"}\n" +
  131. "{img src=\"http://localhost:8081/tikiwiki-1.9.5/images/book.gif\"}" +
  132. "";
  133. String actual = tester.standardizeImageSyntax(input);
  134. assertNotNull(actual);
  135. assertEquals(expected, actual);
  136. }
  137. public void testReplaceImageIdsForFileGallery() {
  138. String input = "{img src=\"tiki-download_file.php?id=1\"}";
  139. String expected = "{img src=\"tiki-download_file.php?name=ABC\"}";
  140. String actual = tester.replaceImageIds(
  141. input, idsAndNames, AttachmentConverter.GalleryType.FILE);
  142. assertEquals(expected, actual);
  143. }
  144. public void testStandardizeImageSyntaxForFileGallery() {
  145. String input = "{img src=tiki-download_file.php?fileId=1 }\n";
  146. String expected = "{img src=\"tiki-download_file.php?fileId=1\" }\n";
  147. String actual = tester.standardizeImageSyntax(input);
  148. assertNotNull(actual);
  149. assertEquals(expected, actual);
  150. input = "{img src=http://localhost:8081/tikiwiki-1.9.5/tiki-download_file.php?fileId=1}\n";
  151. expected = "{img src=\"tiki-download_file.php?fileId=1\"}\n";
  152. actual = tester.standardizeImageSyntax(input);
  153. assertNotNull(actual);
  154. assertEquals(expected, actual);
  155. }
  156. public void testGettingGalleryInfoFromDB() {
  157. String sql = "select imageId, name from tiki_images;";
  158. assertTrue(tester.readDBProperties(location));
  159. //test image gallery
  160. String imageId = "1";
  161. String expected = "Wiki.png";
  162. HashMap<String, String> map = tester.getIdsAndNames(sql, "imageId", "name");
  163. String actual = map.get(imageId);
  164. assertNotNull(actual);
  165. assertEquals(expected, actual);
  166. sql = "select fileId, fileName from tiki_files;";
  167. assertTrue(tester.readDBProperties(location));
  168. //test file gallery
  169. String fileId = "1";
  170. expected = "cow.jpg";
  171. map = tester.getIdsAndNames(sql, "fileId", "fileName");
  172. actual = map.get(fileId);
  173. assertNotNull(actual);
  174. assertEquals(expected, actual);
  175. }
  176. public void testAddAttachmentsToPage_ImageGal() {
  177. String input = "{img src=show_image.php?id=1 }";
  178. String expected = "{img src=\"show_image.php?name=Wiki.png\" }" ;
  179. Page page = new Page(new File(""));
  180. page.setOriginalText(input);
  181. tester.addAttachmentsToPage(page, this.attachDir);
  182. //check syntax
  183. String actual = page.getConvertedText();
  184. assertNotNull(actual);
  185. assertEquals(expected, actual);
  186. //check attachments
  187. Set<File> attachments = page.getAttachments();
  188. int expSize = 1;
  189. int actSize = attachments.size();
  190. assertEquals(expSize, actSize);
  191. File attachment = null;
  192. for (File file : attachments) {
  193. attachment = file;
  194. }
  195. String expFilename = "Wiki.png";
  196. String actFilename = attachment.getName();
  197. assertNotNull(actFilename);
  198. assertEquals(expFilename, actFilename);
  199. }
  200. public void testAddAttachmentsToPage_FileGal_DB() {
  201. String input = "{img src=tiki-download_file.php?fileId=1}\n";
  202. String expected = "{img src=\"tiki-download_file.php?name=cow.jpg\"}\n" ;
  203. Page page = new Page(new File(""));
  204. page.setOriginalText(input);
  205. tester.addAttachmentsToPage(page, this.attachDir);
  206. //check syntax
  207. String actual = page.getConvertedText();
  208. assertNotNull(actual);
  209. assertEquals(expected, actual);
  210. //check attachments
  211. Set<File> attachments = page.getAttachments();
  212. int expSize = 1;
  213. int actSize = attachments.size();
  214. assertEquals(expSize, actSize);
  215. File attachment = null;
  216. for (File file : attachments) {
  217. attachment = file;
  218. }
  219. String expFilename = "cow.jpg";
  220. String actFilename = attachment.getName();
  221. assertNotNull(actFilename);
  222. assertEquals(expFilename, actFilename);
  223. }
  224. public void testAddAttachmentsToPage_FileGal_Filesystem() {
  225. String input = "{img src=tiki-download_file.php?fileId=2}\n";
  226. String expected = "{img src=\"tiki-download_file.php?name=warmdrink_sm.jpg\"}\n" ;
  227. Page page = new Page(new File(""));
  228. page.setOriginalText(input);
  229. tester.addAttachmentsToPage(page, this.attachDir);
  230. //check syntax
  231. String actual = page.getConvertedText();
  232. assertNotNull(actual);
  233. assertEquals(expected, actual);
  234. //check attachments
  235. Set<File> attachments = page.getAttachments();
  236. int expSize = 1;
  237. int actSize = attachments.size();
  238. assertEquals(expSize, actSize);
  239. File attachment = null;
  240. for (File file : attachments) {
  241. attachment = file;
  242. }
  243. String expFilename = "warmdrink_sm.jpg";
  244. String actFilename = attachment.getName();
  245. assertNotNull(actFilename);
  246. assertEquals(expFilename, actFilename);
  247. }
  248. public void testReplaceIdsWithNames() {
  249. assertTrue(tester.readDBProperties(location));
  250. String input = "All id syntaxes\n" +
  251. "{img src=\"show_image.php?id=1\"}\n" +
  252. "{img src=\"tiki-download_file.php?fileId=1\"}\n" +
  253. "{img src=\"tiki-download_file.php?fileId=2\"}\n";
  254. String expected = "All id syntaxes\n" +
  255. "{img src=\"show_image.php?name=Wiki.png\"}\n" +
  256. "{img src=\"tiki-download_file.php?name=cow.jpg\"}\n" +
  257. "{img src=\"tiki-download_file.php?name=warmdrink_sm.jpg\"}\n";
  258. String actual = tester.replaceIdsWithNames(input);
  259. assertNotNull(actual);
  260. assertEquals(expected, actual);
  261. }
  262. public void testGetAllGalleryPaths() {
  263. assertTrue(tester.readDBProperties(location));
  264. String file1 = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/img/wiki_up/hobbespounce.gif";
  265. String syntax2= "{img src=\"show_image.php?name=Wiki.png\"}\n";
  266. String file2 = "/Users/laura/Desktop/tikiwiki-output/Wiki.png";
  267. String syntax3 = "{img src=\"tiki-download_file.php?name=cow.jpg\"}\n";
  268. String file3 = "/Users/laura/Desktop/tikiwiki-output/cow.jpg";
  269. String syntax4 = "{img src=\"tiki-download_file.php?name=warmdrink_sm.jpg\"}\n";
  270. String file4 = "/Users/laura/Desktop/tikiwiki-output/warmdrink_sm.jpg";
  271. String input = "All id syntaxes\n" +
  272. syntax2 +
  273. syntax3 +
  274. syntax4;
  275. Vector<String> paths = new Vector<String>();
  276. paths.add(file1);
  277. Vector<String> expected = new Vector<String>();
  278. expected.add(file1);
  279. expected.add(file2);
  280. expected.add(file3);
  281. expected.add(file4);
  282. // String attachDir = "/Users/laura/Code/Workspace/tikiwiki-1.9.5";
  283. Vector<String> actual = tester.getAllGalleryPaths(input, this.attachDir, paths);
  284. assertNotNull(actual);
  285. int expectedSize = 4;
  286. assertEquals(expectedSize, actual.size());
  287. for (int i = 0; i < actual.size(); i++) {
  288. String exp = expected.get(i);
  289. String act = actual.get(i);
  290. assertNotNull(act);
  291. assertEquals("index: " + i, exp, act);
  292. }
  293. }
  294. public void testGetGalleryPaths() {
  295. //stubs
  296. String syntax3 = "{img src=\"tiki-download_file.php?name=cow.jpg\"}\n";
  297. String syntax4 = "{img src=\"tiki-download_file.php?name=warmdrink_sm.jpg\"}\n";
  298. String file4 = "/Users/laura/Desktop/tikiwiki-output/warmdrink_sm.jpg";
  299. String input = syntax3;
  300. String table = "tiki_files";
  301. String column = "fileName";
  302. String baseDir = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/file_galleries/Attachments/";
  303. //prepare the db
  304. tester.readDBProperties(this.location);
  305. //syntax 3 will return no paths from getGalleryPaths
  306. Vector<String> actualVector = tester.getGalleryPaths(input, table, column, baseDir);
  307. assertNotNull(actualVector);
  308. assertTrue(actualVector.isEmpty());
  309. //syntax4 will return 1 path
  310. input = syntax4;
  311. actualVector = tester.getGalleryPaths(input, table, column, baseDir);
  312. assertNotNull(actualVector);
  313. int expectedSize = 1;
  314. assertEquals(expectedSize, actualVector.size());
  315. String expected = file4;
  316. String actual = actualVector.get(0);
  317. assertNotNull(actual);
  318. assertEquals(expected, actual);
  319. }
  320. public void testDownloadDbFilesToTmp() {
  321. //prepare the db
  322. tester.readDBProperties(this.location);
  323. //create stubs
  324. String syntax3 = "{img src=\"tiki-download_file.php?name=cow.jpg\"}\n";
  325. String file3 = "/Users/laura/Desktop/tikiwiki-output/cow.jpg";
  326. String syntax4 = "{img src=\"tiki-download_file.php?name=warmdrink_sm.jpg\"}\n";
  327. String input = syntax4;
  328. String table = "tiki_files";
  329. String column = "fileName";
  330. String tmpDir = tester.getTmpDir();
  331. //syntax 4 will return no paths
  332. Vector<String> actualVector = tester.downloadDbFilesToTmp(input, table, column, tmpDir);
  333. assertNotNull(actualVector);
  334. assertTrue(actualVector.isEmpty());
  335. //syntax3 will return one path
  336. input = syntax3;
  337. actualVector = tester.downloadDbFilesToTmp(input, table, column, tmpDir);
  338. assertNotNull(actualVector);
  339. int expectedSize = 1;
  340. assertEquals(expectedSize, actualVector.size());
  341. actualVector = tester.downloadDbFilesToTmp(input, table, column, tmpDir);
  342. String expected = file3;
  343. String actual = actualVector.get(0);
  344. assertNotNull(actual);
  345. assertEquals(expected, actual);
  346. //test that the tmp file exists
  347. File file = new File(file3);
  348. assertTrue(file.exists());
  349. }
  350. public void testGetTmpDir() {
  351. tester.readDBProperties(this.location);
  352. String expected = "/Users/laura/Desktop/tikiwiki-output";
  353. String actual = tester.getTmpDir();
  354. assertNotNull(actual);
  355. assertEquals(expected, actual);
  356. }
  357. public void testEntireURLs() {
  358. String input = "{img src=http://localhost:8081/tikiwiki-1.9.5/tiki-download_file.php?fileId=2}";
  359. String expected = "{img src=\"tiki-download_file.php?name=warmdrink_sm.jpg\"}";
  360. Page page = new Page(new File(""));
  361. page.setOriginalText(input);
  362. tester.addAttachmentsToPage(page, attachDir);
  363. String actual = page.getConvertedText();
  364. assertNotNull(actual);
  365. assertEquals(expected, actual);
  366. //check that the page got attached
  367. Set<File> attFiles = page.getAttachments();
  368. int expectedSize = 1;
  369. assertEquals(expectedSize, attFiles.size());
  370. File attachment = null;
  371. for (File file : attFiles) {
  372. attachment = file;
  373. }
  374. assertTrue(attachment.exists());
  375. assertTrue(attachment.isFile());
  376. expected = "warmdrink_sm.jpg";
  377. actual = attachment.getName();
  378. assertEquals(expected, actual);
  379. }
  380. public void testStandardizeUrl() {
  381. String input = "{img src=\"http://localhost:8081/tikiwiki-1.9.5/tiki-download_file.php?fileId=2\"}";
  382. String expected = "{img src=\"tiki-download_file.php?fileId=2\"}";
  383. String actual = tester.standardizeUrl(input);
  384. assertNotNull(actual);
  385. assertEquals(expected, actual);
  386. input = "{img src=\"http://localhost:8081/tikiwiki-1.9.5/img/wiki_up/" +
  387. "hobbespounce.gif\"}\n";
  388. expected = "{img src=\"img/wiki_up/hobbespounce.gif\"}\n";
  389. actual = tester.standardizeUrl(input);
  390. assertNotNull(actual);
  391. assertEquals(expected, actual);
  392. //ignore
  393. input = "{img src=\"http://localhost:8081/tikiwiki-1.9.5/images/book.gif\"}";
  394. expected = input;
  395. actual = tester.standardizeUrl(input);
  396. assertNotNull(actual);
  397. assertEquals(expected, actual);
  398. }
  399. public void testImgQuotesAsEntities() {
  400. String input = "{img src=&quot;img/wiki_up/hobbespounce.gif&quot; }";
  401. Page page = new Page(new File(""));
  402. page.setOriginalText(input);
  403. tester.addAttachmentsToPage(page, attachDir);
  404. //test attachment set's size
  405. Set<File> attachments = page.getAttachments();
  406. int expectedSize = 1;
  407. assertEquals(expectedSize, attachments.size());
  408. //test attachment name;
  409. File attachment = null;
  410. for (File file : attachments) {
  411. attachment = file;
  412. }
  413. String expectedName = "hobbespounce.gif";
  414. String actualName = attachment.getName();
  415. assertNotNull(actualName);
  416. assertEquals(expectedName, actualName);
  417. }
  418. public void testImgLargeContext() {
  419. String input = "{CODE}\n" +
  420. "*_Screenshot(s)_:\n" +
  421. "{img src=\"img/wiki_up/hobbespounce.gif\" }\n" +
  422. "^\n" ;
  423. Page page = new Page(new File(""));
  424. page.setOriginalText(input);
  425. tester.addAttachmentsToPage(page, attachDir);
  426. //test attachment set's size
  427. Set<File> attachments = page.getAttachments();
  428. int expectedSize = 1;
  429. assertEquals(expectedSize, attachments.size());
  430. //test attachment name;
  431. File attachment = null;
  432. for (File file : attachments) {
  433. attachment = file;
  434. }
  435. String expectedName = "hobbespounce.gif";
  436. String actualName = attachment.getName();
  437. assertNotNull(actualName);
  438. assertEquals(expectedName, actualName);
  439. }
  440. public void testAttachmentDirectoryIsWinDrive() {
  441. //XXX Set this to be a file in Windows location: like
  442. String winlocation = "img\\wiki_up\\image.gif";
  443. String input = "{img src=\""+winlocation+"\" }";
  444. //XXX Set this to be a local Windows drive
  445. String attachDir = "R:";
  446. tester.FILE_SEP = "\\";
  447. //On a Windows box, expected would be R:\\img\wiki_up\hobbespounce.gif
  448. String expected = attachDir + "\\\\" +winlocation;
  449. Vector<String> paths = tester.getUploadPaths(input, attachDir);
  450. //test size of paths
  451. assertNotNull(paths);
  452. int expectedSize = 1;
  453. assertEquals(expectedSize, paths.size());
  454. //test name of path
  455. String actual = paths.get(0);
  456. }
  457. public void testAttachingImageWithWhitespace() {
  458. String propLocation = "/Users/laura/Code/Clients/Webroot/Data/uwc/conf/" + DB_PROP_FILENAME;
  459. tester.setPropertyLocation(propLocation);
  460. String input = "{img src=http://216.17.247.75/tikiwiki/tiki-download_file.php?fileId=1902}";
  461. String expected = "{img src=\"tiki-download_file.php?name=BraveSentry FakeAlert Desktop.JPG\"}";
  462. Page page = new Page(null);
  463. page.setName("Testing Attachments");
  464. page.setOriginalText(input);
  465. String attachDir = "/Volumes/Spike/Work/Clients/Webroot/Data/attachments";
  466. tester.addAttachmentsToPage(page, attachDir);
  467. assertNotNull(page);
  468. assertEquals(expected, page.getConvertedText());
  469. //now test that it attached the file
  470. Set<File> attachments = page.getAttachments();
  471. assertNotNull(attachments);
  472. assertEquals(1, attachments.size());
  473. File file = null;
  474. for (File attachment : attachments) {
  475. file = attachment;
  476. }
  477. assertNotNull(file);
  478. assertTrue(file.exists());
  479. assertTrue(file.isFile());
  480. log.debug("file = " + file.getPath());
  481. String expectedName = "BraveSentry FakeAlert Desktop.JPG";
  482. String expectedPath = "/Volumes/Spike/Work/Clients/Webroot/Data/output-attachments/BraveSentry FakeAlert Desktop.JPG";
  483. assertEquals(expectedName, file.getName());
  484. assertEquals(expectedPath, file.getPath());
  485. }
  486. public void testGetImageGalleryDir() {
  487. // prepare the db
  488. tester.readDBProperties(this.location);
  489. String expected = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/images/Attachments/";
  490. String actual = tester.getImageGalleryDirectory(this.attachDir );
  491. assertNotNull(actual);
  492. assertEquals(expected, actual);
  493. }
  494. public void testGetFileGalleryDir() {
  495. // prepare the db
  496. tester.readDBProperties(this.location);
  497. String expected = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/file_galleries/Attachments/";
  498. String actual = tester.getFileGalleryDirectory(this.attachDir);
  499. assertNotNull(actual);
  500. assertEquals(expected, actual);
  501. }
  502. public void testGetAbsolutePath() {
  503. String input = "/blah";
  504. String expected = input;
  505. String actual = tester.getAbsolutePath(input, this.attachDir);
  506. assertNotNull(actual);
  507. assertEquals(expected,actual);
  508. input = "blah.txt";
  509. expected = input;
  510. actual = tester.getAbsolutePath(input, this.attachDir);
  511. assertNotNull(actual);
  512. assertEquals(expected,actual);
  513. input = "./blah";
  514. expected = this.attachDir + "/" + "blah";
  515. actual = tester.getAbsolutePath(input, this.attachDir);
  516. assertNotNull(actual);
  517. assertEquals(expected,actual);
  518. }
  519. public void testAttach() {
  520. String goodpath1 = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/img/wiki_up/hobbespounce.gif";
  521. String badpath1 = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/img/wiki_up/nofile.gif";
  522. String badpath2 = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/img/wiki_up/";
  523. String badpath3 = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/tiki-download_file.php?fileId=1";
  524. String badpath4 = "/Users/laura/Code/Workspace/tikiwiki-1.9.5/show_image.php?name=bahhumbug.gif";
  525. Page page = new Page(new File(""));
  526. page.setName("testing");
  527. Vector<String> paths = new Vector<String>();
  528. //test that only one of the paths attaches a file
  529. paths.add(goodpath1);
  530. paths.add(badpath1);
  531. paths.add(badpath2);
  532. paths.add(badpath3);
  533. paths.add(badpath4);
  534. tester.attach(paths, page);
  535. Set<File> attachments = page.getAttachments();
  536. assertNotNull(attachments);
  537. assertTrue(attachments.size() == 1);
  538. cleanTester(paths);
  539. //test that the first bad path (doesn't exist) creates errors
  540. paths.add(badpath1);
  541. tester.attach(paths, page);
  542. ConverterErrors errors = tester.getErrors();
  543. assertNotNull(errors);
  544. assertTrue(errors.hasErrors());
  545. cleanTester(paths);
  546. //test that the second bad path (a dir) creates errors
  547. paths.add(badpath2);
  548. tester.attach(paths, page);
  549. assertNotNull(tester.getErrors());
  550. assertTrue(tester.getErrors().hasErrors());
  551. cleanTester(paths);
  552. //test that the 3rd and 4th bad paths (not file system paths) suppresses errors
  553. paths.add(badpath3);
  554. tester.attach(paths, page);
  555. assertNotNull(tester.getErrors());
  556. assertFalse(tester.getErrors().hasErrors());
  557. cleanTester(paths);
  558. paths.add(badpath4);
  559. tester.attach(paths, page);
  560. assertNotNull(tester.getErrors());
  561. assertFalse(tester.getErrors().hasErrors());
  562. cleanTester(paths);
  563. }
  564. public void testConvert() {
  565. String input, expected, actual;
  566. Page page = null;
  567. tester.setAttachmentDirectory(this.attachDir);
  568. //good
  569. input = "{img src=\"img/wiki_up/testimage.png\" width=\"200\"}";
  570. expected = input;
  571. page = null;
  572. page = new Page(null);
  573. page.setOriginalText(input);
  574. tester.convert(page);
  575. actual = page.getConvertedText();
  576. assertNotNull(actual);
  577. assertEquals(expected, actual);
  578. assertNotNull(page.getAttachments());
  579. assertFalse(page.getAttachments().isEmpty());
  580. assertNotNull(tester.getErrors());
  581. assertNotNull(tester.getErrors().getErrors());
  582. assertTrue(tester.getErrors().getErrors().isEmpty());
  583. //not converter - don't attach this - leave it alone, and don't display errors
  584. input = "{img src=\"http://localhost:8081/tikiwiki-1.9.5/images/book.gif\"}";
  585. expected = input;
  586. page = null;
  587. page = new Page(null);
  588. page.setOriginalText(input);
  589. tester.convert(page);
  590. actual = page.getConvertedText();
  591. assertNotNull(actual);
  592. assertEquals(expected, actual);
  593. assertNotNull(page.getAttachments());
  594. assertTrue(page.getAttachments().isEmpty());
  595. assertNotNull(tester.getErrors());
  596. assertNotNull(tester.getErrors().getErrors());
  597. assertTrue(tester.getErrors().getErrors().isEmpty());
  598. //bad
  599. input = "{img src=\"img/wiki_up/nofile.gif\"}";
  600. expected = input;
  601. page = null;
  602. page = new Page(null);
  603. page.setOriginalText(input);
  604. tester.convert(page);
  605. actual = page.getConvertedText();
  606. assertNotNull(actual);
  607. assertEquals(expected, actual);
  608. assertNotNull(page.getAttachments());
  609. assertTrue(page.getAttachments().isEmpty());
  610. assertNotNull(tester.getErrors());
  611. assertNotNull(tester.getErrors().getErrors());
  612. assertFalse(tester.getErrors().getErrors().isEmpty());
  613. }
  614. public void testGetUploadPaths() {
  615. String input, expected, actual;
  616. Vector paths;
  617. //good
  618. paths = null;
  619. input = "{img src=\"img/wiki_up/testimage.png\" width=\"200\"}";
  620. paths = tester.getUploadPaths(input, attachDir);
  621. assertNotNull(paths);
  622. assertEquals(1, paths.size());
  623. expected = attachDir + "/img/wiki_up/testimage.png";
  624. actual = (String) paths.get(0);
  625. assertNotNull(actual);
  626. assertEquals(expected, actual);
  627. input = "{img src=\"tiki-download_file.php?fileId=2\"}";
  628. paths = tester.getUploadPaths(input, attachDir);
  629. assertNotNull(paths);
  630. assertEquals(1, paths.size());
  631. expected = attachDir + "/tiki-download_file.php?fileId=2";
  632. actual = (String) paths.get(0);
  633. assertNotNull(actual);
  634. assertEquals(expected, actual);
  635. //not converter - don't attach this - leave it alone, and don't display errors
  636. paths = null;
  637. input = "{img src=\"http://localhost:8081/tikiwiki-1.9.5/images/book.gif\"}";
  638. paths = tester.getUploadPaths(input, attachDir);
  639. assertNotNull(paths);
  640. assertEquals(1, paths.size());
  641. expected = "http://localhost:8081/tikiwiki-1.9.5/images/book.gif";
  642. actual = (String) paths.get(0);
  643. assertNotNull(actual);
  644. assertEquals(expected, actual);
  645. //bad
  646. paths = null;
  647. input = "{img src=\"img/wiki_up/nofile.gif\"}";
  648. paths = tester.getUploadPaths(input, attachDir);
  649. assertNotNull(paths);
  650. assertEquals(1, paths.size());
  651. expected = attachDir + "/img/wiki_up/nofile.gif";
  652. actual = (String) paths.get(0);
  653. assertNotNull(actual);
  654. assertEquals(expected, actual);
  655. //windows
  656. tester.FILE_SEP = "\\";
  657. paths = null;
  658. input = "{img src=\"img/wiki_up/testimage.png\" width=\"200\"}";
  659. String winattach = attachDir.replaceAll("\\/", "\\\\");
  660. tester.setAttachmentDirectory(winattach);
  661. paths = tester.getUploadPaths(input, winattach);
  662. assertNotNull(paths);
  663. assertEquals(1, paths.size());
  664. expected = winattach + "\\img\\wiki_up\\testimage.png";
  665. actual = (String) paths.get(0);
  666. assertNotNull(actual);
  667. assertEquals(expected, actual);
  668. }
  669. private void cleanTester(Vector<String> paths) {
  670. paths.clear();
  671. tester.getErrors().clear();
  672. assertFalse(tester.getErrors().hasErrors());
  673. }
  674. }