r1105 - in trunk/wikitty-publication/src: main/java/org/nuiton/wikitty/publication/synchro test/java/org/nuiton/wikitty/publication test/java/org/nuiton/wikitty/publication/synchro
Author: mfortun Date: 2011-08-02 17:50:31 +0200 (Tue, 02 Aug 2011) New Revision: 1105 Url: http://nuiton.org/repositories/revision/wikitty/1105 Log: * test for wikittyPublicationFileSystem * correction for wikittyPublicationFileSystem Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/PropertiesExtendedTest.java trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyPublicationFallbackServiceTest.java trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystemTest.java Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-08-02 09:13:37 UTC (rev 1104) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-08-02 15:50:31 UTC (rev 1105) @@ -71,7 +71,6 @@ import org.nuiton.wikitty.services.WikittyEvent; import org.nuiton.wikitty.services.WikittyListener; - public class WikittyPublicationFileSystem extends AbstractWikittyFileService { /** to use log facility, just put in your code: log.info(\"...\"); */ @@ -80,7 +79,6 @@ static public String WIKITTYLABEL_SEPARATOR = "."; - /** * The working directory of the wikitty service */ @@ -125,7 +123,6 @@ */ static public String META_PREFIX_KEY_ID = "id."; - /** * The file name of the wikitty service property */ @@ -197,30 +194,34 @@ homeFile, WIKITTY_FILE_SERVICE); // the original label use to create if not exist - if (!propertyWikittyService.containsKey(WikittyPublicationSynchronize.LABEL_KEY)) { + if (!propertyWikittyService + .containsKey(WikittyPublicationSynchronize.LABEL_KEY)) { log.debug("Writing home property label" + propertyWikittyService.getOrigin()); - propertyWikittyService.setProperty(WikittyPublicationSynchronize.LABEL_KEY, - this.label); + propertyWikittyService.setProperty( + WikittyPublicationSynchronize.LABEL_KEY, this.label); } // the service use to update or commit String uriService = app .getOption(WikittyPublicationSynchronize.WIKITTY_SERVICE_INTERLOCUTEUR); - if (uriService !=null) { - log.debug("Writing home property service on:" - + propertyWikittyService.getOrigin() + " uri" + uriService); + if (uriService != null) { + log.debug("Writing home property service on:" + + propertyWikittyService.getOrigin() + " uri" + uriService); - propertyWikittyService.setProperty( - WikittyPublicationSynchronize.WIKITTY_SERVICE_INTERLOCUTEUR, uriService); + propertyWikittyService + .setProperty( + WikittyPublicationSynchronize.WIKITTY_SERVICE_INTERLOCUTEUR, + uriService); } - + propertyWikittyService.store(); recursion = true; - if (app.getOptions().containsKey(WikittyPublicationSynchronize.IS_RECURSION_OPTION)) { - - this.recursion = app - .getOptionAsBoolean(WikittyPublicationSynchronize.IS_RECURSION_OPTION); + if (app.getOptions().containsKey( + WikittyPublicationSynchronize.IS_RECURSION_OPTION)) { + + this.recursion = app + .getOptionAsBoolean(WikittyPublicationSynchronize.IS_RECURSION_OPTION); } // TODO mfotun-2011-04-28 add a support for filtered file with a // property file @@ -322,18 +323,13 @@ @Override public boolean exists(String securityToken, String wikittyId) { - // TODO mfortun-2011-04-05 - throw new UnsupportedOperationException("not yet implemented"); - // return false; + return getAllWikitties().containsKey(wikittyId); } @Override public boolean isDeleted(String securityToken, String wikittyId) { - // TODO mfortun-2011-04-05 - throw new UnsupportedOperationException("not yet implemented"); - // return false; - + return !exists(securityToken, wikittyId); } @Override @@ -377,7 +373,6 @@ // if label is empty it means that the wikitty have to be remove // from the FileSystem if ("".equals(ourDir)) { - // remove file if the wikitty already exist BidiMap location = harvestLocalWikitties(homeFile, true); @@ -401,7 +396,8 @@ } // create the directories from the label - boolean pathFilecreated = WikittyFileUtil.createFilesFromLabelPath(homeFile,ourDir); + boolean pathFilecreated = WikittyFileUtil + .createFilesFromLabelPath(homeFile, ourDir); if (pathFilecreated) { // create the path with the label @@ -498,10 +494,10 @@ List<String> result = new LinkedList<String>(); result.add(WikittyPubData.EXT_WIKITTYPUBDATA); result.add(WikittyPubText.EXT_WIKITTYPUBTEXT); - //result.add(WikittyPubTextCompiled.EXT_WIKITTYPUBTEXTCOMPILED); + // result.add(WikittyPubTextCompiled.EXT_WIKITTYPUBTEXTCOMPILED); result.add(WikittyLabel.EXT_WIKITTYLABEL); - - return result; + + return result; } @Override @@ -554,7 +550,7 @@ * restore file as wikitty */ public List<Wikitty> restore(String securityToken, List<String> id) { - + List<Wikitty> result = new ArrayList<Wikitty>(); log.info("restore wikitty, number: " + id.size()); try { @@ -570,7 +566,6 @@ // register wikitty in the result for (String wikid : id) { Object value = locations.get(wikid); - if (value != null) { FileSystemWIkittyId localisation = (FileSystemWIkittyId) value; @@ -583,7 +578,7 @@ e.printStackTrace(); // TODO mfortun-2011-01-12 really handle exception } - + return result; } @@ -652,8 +647,6 @@ return result; } - - @Override protected Map<String, Wikitty> getAllWikitties() { Map<String, Wikitty> wikitties = new HashMap<String, Wikitty>(); @@ -688,10 +681,7 @@ } return wikitties; } - - - @Override public WikittyEvent deleteTree(String securityToken, String treeNodeId) { // TODO mfortun-2011-04-05 @@ -863,7 +853,7 @@ log.debug("restore wikitty id:" + id + " file: " + fileId); Wikitty result = new WikittyImpl(id); - + result.addExtension(WikittyLabelImpl.extensionWikittyLabel); // preparation for mime research and file research @@ -873,6 +863,10 @@ // search for the file File fileToTransform = new File(path + File.separator + completeName); + if (!fileToTransform.exists()) { + return null; + } + String extension = FileUtil.extension(fileToTransform); String name = FileUtil.basename(completeName, "." + extension); @@ -882,7 +876,8 @@ // load properties File wikittyParentDir = new File(path); PropertiesExtended props = getWikittyPublicationProperties( - wikittyParentDir, WikittyFileUtil.WIKITTY_FILE_META_PROPERTIES_FILE); + wikittyParentDir, + WikittyFileUtil.WIKITTY_FILE_META_PROPERTIES_FILE); // set the current label WikittyLabelHelper.addLabels(result, props @@ -1038,7 +1033,8 @@ String localMd5 = StringUtil.asHex(byt); PropertiesExtended meta = getWikittyPublicationProperties( - child.getParentFile(),WikittyFileUtil.WIKITTY_FILE_META_PROPERTIES_FILE); + child.getParentFile(), + WikittyFileUtil.WIKITTY_FILE_META_PROPERTIES_FILE); String registeredMD5 = meta.getProperty(META_PREFIX_KEY_CHECKSUM + child.getName()); @@ -1115,10 +1111,6 @@ } - - - - /** * Use to search and return the home property that containt informations * about wikitty service use to synchronise a wikitty publication file Modified: trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/PropertiesExtendedTest.java =================================================================== --- trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/PropertiesExtendedTest.java 2011-08-02 09:13:37 UTC (rev 1104) +++ trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/PropertiesExtendedTest.java 2011-08-02 15:50:31 UTC (rev 1105) @@ -17,16 +17,23 @@ public class PropertiesExtendedTest { - public File tempHome; - public Properties props; - public File propsFile; + protected File tempHome; + protected Properties props; + protected File propsFile; @Before public void init() throws IOException{ + + if (! FileUtils.getTempDirectory().exists()){ + FileUtils.getTempDirectory().mkdir(); + } + tempHome = new File(FileUtils.getTempDirectory().getCanonicalFile() + File.separator + "wikittyFileUtilTest"); + System.out.println(tempHome); + if (tempHome.exists()){ FileUtil.deleteRecursively(tempHome); Modified: trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyPublicationFallbackServiceTest.java =================================================================== --- trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyPublicationFallbackServiceTest.java 2011-08-02 09:13:37 UTC (rev 1104) +++ trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyPublicationFallbackServiceTest.java 2011-08-02 15:50:31 UTC (rev 1105) @@ -3,6 +3,9 @@ public class WikittyPublicationFallbackServiceTest { + + + /* * Instancier un wikitty service sur FS et un sur cajo * mettre des wikitty dans les deux différent 10-10, et 5 de plus en commun Modified: trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystemTest.java =================================================================== --- trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystemTest.java 2011-08-02 09:13:37 UTC (rev 1104) +++ trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystemTest.java 2011-08-02 15:50:31 UTC (rev 1105) @@ -1,26 +1,327 @@ package org.nuiton.wikitty.publication.synchro; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import junit.framework.Assert; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.util.ApplicationConfig; +import org.nuiton.util.FileUtil; +import org.nuiton.util.StringUtil; +import org.nuiton.wikitty.WikittyConfigOption; +import org.nuiton.wikitty.WikittyProxy; +import org.nuiton.wikitty.WikittyServiceFactory; +import org.nuiton.wikitty.entities.Wikitty; +import org.nuiton.wikitty.entities.WikittyImpl; +import org.nuiton.wikitty.entities.WikittyLabel; +import org.nuiton.wikitty.entities.WikittyLabelHelper; +import org.nuiton.wikitty.entities.WikittyLabelImpl; +import org.nuiton.wikitty.publication.WikittyFileUtil; +import org.nuiton.wikitty.publication.entities.WikittyPubData; +import org.nuiton.wikitty.publication.entities.WikittyPubDataHelper; +import org.nuiton.wikitty.publication.entities.WikittyPubDataImpl; +import org.nuiton.wikitty.publication.entities.WikittyPubText; +import org.nuiton.wikitty.publication.entities.WikittyPubTextHelper; +import org.nuiton.wikitty.publication.entities.WikittyPubTextImpl; +import org.nuiton.wikitty.search.Criteria; +import org.nuiton.wikitty.search.Search; + public class WikittyPublicationFileSystemTest { + protected File tempHome; + protected List<Wikitty> listWikittyPubData; + protected List<Wikitty> listWikittyPubText; + protected List<String> labels; + protected ApplicationConfig config; + protected WikittyProxy proxy; + protected Criteria allCrit; + protected File starts; + + @Before + public void init() throws IOException { + + if (!FileUtils.getTempDirectory().exists()) { + FileUtils.getTempDirectory().mkdir(); + } + + tempHome = new File(FileUtils.getTempDirectory().getCanonicalFile() + + File.separator + "WikittyPublicationFileSystemTest"); + + if (tempHome.exists()) { + FileUtil.deleteRecursively(tempHome); + + } + tempHome.mkdir(); + + // prepare the wikitties list + listWikittyPubData = new LinkedList<Wikitty>(); + listWikittyPubText = new LinkedList<Wikitty>(); + + labels = new ArrayList<String>(6); + labels.add(0, "sub"); + labels.add(1, labels.get(0) + WikittyFileUtil.WIKITTY_LABEL_SEPARATOR + + "sub11"); + labels.add(2, labels.get(1) + WikittyFileUtil.WIKITTY_LABEL_SEPARATOR + + "sub12"); + labels.add(3, labels.get(0) + WikittyFileUtil.WIKITTY_LABEL_SEPARATOR + + "sub3"); + labels.add(4, labels.get(0) + WikittyFileUtil.WIKITTY_LABEL_SEPARATOR + + "sub21"); + labels.add(5, labels.get(4) + WikittyFileUtil.WIKITTY_LABEL_SEPARATOR + + "sub21"); + + for (int i = 0; i < 6; i++) { + + Wikitty pubText = new WikittyImpl(); + Wikitty pubData = new WikittyImpl(); + + pubData.addExtension(WikittyPubDataImpl.extensionWikittyPubData); + pubText.addExtension(WikittyPubTextImpl.extensionWikittyPubText); + + WikittyPubTextHelper.setContent(pubText, "content_" + i); + WikittyPubTextHelper.setFileExtension(pubText, "js"); + WikittyPubTextHelper.setMimeType(pubText, "application/javascript"); + WikittyPubTextHelper.setName(pubText, "pubtextnum_" + i); + + WikittyPubDataHelper.setContent(pubData, + ("content data" + i).getBytes()); + WikittyPubDataHelper.setFileExtension(pubData, "jpg"); + WikittyPubDataHelper.setMimeType(pubData, "image/jpg"); + WikittyPubDataHelper.setName(pubData, "pubdatanum_" + i); + + pubData.addExtension(WikittyLabelImpl.extensions); + pubText.addExtension(WikittyLabelImpl.extensions); + + WikittyLabelHelper.addLabels(pubData, labels.get(i)); + WikittyLabelHelper.addLabels(pubText, labels.get(i)); + + listWikittyPubData.add(pubData); + listWikittyPubText.add(pubText); + } + + // then initialise application config for wikitty proxy on service FS + config = new ApplicationConfig(); + config.setOption(WikittyPublicationSynchronize.IS_RECURSION_OPTION, + "true"); + config.setOption( + WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS.getKey(), + WikittyPublicationFileSystem.class.getName()); + + starts = new File(tempHome.getAbsolutePath() + File.separator + "sub"); + if (starts.exists()) { + FileUtil.deleteRecursively(starts); + } + starts.mkdir(); + + String url = "file:///" + tempHome.getAbsolutePath() + "#sub"; + + config.setOption(WikittyConfigOption.WIKITTY_SERVER_URL.getKey(), url); + + proxy = new WikittyProxy( + WikittyServiceFactory.buildWikittyService(config)); + + allCrit = Search.query().keyword("*").criteria(); + + } + + @After + public void delete() { + FileUtil.deleteRecursively(tempHome); + } + + /** + * check save wikitty + */ + @Test + public void saveWikittyOnFS() { + + Assert.assertEquals(0, proxy.findAllIdByCriteria(allCrit).getNumFound()); + + proxy.storeWikitty(listWikittyPubData); + proxy.storeWikitty(listWikittyPubText); + + int result = listWikittyPubData.size() + listWikittyPubText.size(); + + Assert.assertEquals(result, proxy.findAllIdByCriteria(allCrit) + .getNumFound()); + + } + + /** + * check that the wikitty save are restorable + */ + @Test + public void testRestoreFromFS() { + saveWikittyOnFS(); + + Map<String, Wikitty> mapwikitty = new HashMap<String, Wikitty>(); + + for (Wikitty w : listWikittyPubData) { + mapwikitty.put(w.getId(), w); + } + + for (Wikitty w : listWikittyPubText) { + mapwikitty.put(w.getId(), w); + } + + // Assert that wikitty are the same after save + // wikitty service FS doesn't not alterate version + for (Wikitty w : proxy.findAllByCriteria(allCrit)) { + Assert.assertEquals(mapwikitty.get(w.getId()), w); + } + } + + /** + * Test that service know if wikitty change + * @throws IOException + */ + @Test + public void testUpdateFileDirectly() throws IOException { + saveWikittyOnFS(); + + String label = WikittyFileUtil.labelToPath(labels.get(1)); + + File wikittyOnFS = new File(tempHome.getCanonicalPath() + + File.separator + label + File.separator + "pubtextnum_1.js"); + // check file exist + Assert.assertTrue(wikittyOnFS.exists()); + + WikittyPubTextImpl wik = new WikittyPubTextImpl(); + wik.setName("pubtextnum_1"); + + String newContent = "unNouveauContenu"; + + WikittyPubTextImpl result = proxy.findByExample(wik); + // check content on actual wikitty + Assert.assertNotSame(newContent, result.getContent()); + + // update the file + FileWriter fw = new FileWriter(wikittyOnFS); + + fw.write(newContent); + fw.flush(); + fw.close(); + + // Wikitty + + result = proxy.findByExample(wik); + // check that the wikitty is now updated with the new content + Assert.assertEquals(newContent, result.getContent()); + + } -/* - * Liste des tests à faire: - * - * 1) Crééer une collection de wikitty et les sauvegarder s'assurer on a les mêmes - * - * 2) s'assurer que les labels sont bien mit - * - * 3) test avec des wikittys pub data et pub text - * - * 4) faire des modifications en dehors du service et voir que le service le voit bien - * 4-1 suppression - * 4-2 modification - * - * 5) delete par le service - * - * 6) update par le service - * - * - */ + /** + * Test that the service know if file are deleted + * @throws IOException + */ + @Test + public void testDeleteFileDirectly() throws IOException { + saveWikittyOnFS(); + + String label = WikittyFileUtil.labelToPath(labels.get(1)); + + File wikittyOnFS = new File(tempHome.getCanonicalPath() + + File.separator + label + File.separator + "pubtextnum_1.js"); + // check file exist + Assert.assertTrue(wikittyOnFS.exists()); + + WikittyPubTextImpl wik = new WikittyPubTextImpl(); + wik.setName("pubtextnum_1"); + + WikittyPubTextImpl result = proxy.findByExample(wik); + String id = result.getWikittyId(); + // check that the wikitty exist + Assert.assertNotNull(result); + + //delete the file + wikittyOnFS.delete(); + + // check that the wikitty is now deleted + Assert.assertNull(proxy.restore(id)); + + } + + /** + * Test delete with the service + * @throws IOException + */ + @Test + public void deleteWithService() throws IOException{ + saveWikittyOnFS(); + + String label = WikittyFileUtil.labelToPath(labels.get(1)); + + File wikittyOnFS = new File(tempHome.getCanonicalPath() + + File.separator + label + File.separator + "pubtextnum_1.js"); + // check file exist + Assert.assertTrue(wikittyOnFS.exists()); + + WikittyPubTextImpl wik = new WikittyPubTextImpl(); + wik.setName("pubtextnum_1"); + + WikittyPubTextImpl result = proxy.findByExample(wik); + String id = result.getWikittyId(); + // delete the wikitt + proxy.delete(id); + // check that the wikitty and the file are now deleted + Assert.assertNull(proxy.restore(id)); + Assert.assertFalse(wikittyOnFS.exists()); + + } + + /** + * Test delete a directory, chech if that delete correctly wikitty + * under corresponding labels + * @throws IOException + */ + @Test + public void testDeleteDirectoryDirectly() throws IOException { + saveWikittyOnFS(); + + String label = WikittyFileUtil.labelToPath(labels.get(1)); + + File wikittyOnFS = new File(tempHome.getCanonicalPath() + + File.separator + label ); + // check file exist + Assert.assertTrue(wikittyOnFS.exists()); + + // Criteria to find wikittypubtext/data with label starts with + Search mainRequest = Search.query(); + Search subRoqu = mainRequest.or(); + + // must have the type of wikittypubtext/wikittypubdata + subRoqu.exteq(WikittyPubText.EXT_WIKITTYPUBTEXT).exteq( + WikittyPubData.EXT_WIKITTYPUBDATA); + Criteria criteriaOnLabels = mainRequest.exteq(WikittyLabel.EXT_WIKITTYLABEL) + .sw(WikittyLabel.FQ_FIELD_WIKITTYLABEL_LABELS, labels.get(1)) + .criteria(); + + int totalBeforeDelete = proxy.findAllIdByCriteria(allCrit).getNumFound(); + + int numConcerned = proxy.findAllIdByCriteria(criteriaOnLabels).getNumFound(); + + + FileUtil.deleteRecursively( wikittyOnFS); + + + int numAfterDelete=proxy.findAllIdByCriteria(allCrit).getNumFound(); + // assert all wikitty concerned are deleted + Assert.assertNotSame(numAfterDelete, totalBeforeDelete); + Assert.assertEquals(numAfterDelete, totalBeforeDelete-numConcerned); + + } + + }
participants (1)
-
mfortun@users.nuiton.org