Author: mfortun Date: 2011-08-02 11:13:37 +0200 (Tue, 02 Aug 2011) New Revision: 1104 Url: http://nuiton.org/repositories/revision/wikitty/1104 Log: * add some tests Modified: trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/PropertiesExtendedTest.java trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyFileUtilTest.java 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-01 16:10:14 UTC (rev 1103) +++ trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/PropertiesExtendedTest.java 2011-08-02 09:13:37 UTC (rev 1104) @@ -1,21 +1,104 @@ package org.nuiton.wikitty.publication; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; + +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.FileUtil; + public class PropertiesExtendedTest { - - /* - * 1) création d'un properties extended écriture de chose dedans - * vérification ya tout - * - * 2) création fichier de propriété classiquement et vérifier bien loadé - * - * 3) test sauvegarde - * - * 4) changement de file en passant - * - * - * - * + + public File tempHome; + public Properties props; + public File propsFile; + + + @Before + public void init() throws IOException{ + tempHome = new File(FileUtils.getTempDirectory().getCanonicalFile() + + File.separator + "wikittyFileUtilTest"); + + if (tempHome.exists()){ + FileUtil.deleteRecursively(tempHome); + + } + tempHome.mkdir(); + + propsFile = new File(tempHome.getCanonicalPath()+File.separator+"props.properties"); + + if(propsFile.exists()){ + propsFile.delete(); + } + propsFile.createNewFile(); + + + props = new Properties(); + props.put("propertie1", "value1"); + props.put("propertie2", "value2"); + props.put("propertie3", "value3"); + props.put("propertie4", "value4"); + props.put("propertie5", "value5"); + props.put("propertie6", "value6"); + + } + + @After + public void delete(){ + FileUtil.deleteRecursively(tempHome); + propsFile.delete(); + } + + /** + * test how to write and store with properties extended + * @throws FileNotFoundException + * @throws IOException */ + @Test + public void testWrite() throws FileNotFoundException, IOException{ + + PropertiesExtended propsExtended = new PropertiesExtended(propsFile); + // assert empty + Assert.assertEquals(0, propsExtended.keySet().size()); + + // put in + propsExtended.put("propertie1", "value1"); + + Properties proper = new Properties(); + + proper.load(new FileReader(propsFile)); + + Assert.assertNull(proper.get("propertie1")); + + propsExtended.store(); + + proper.load(new FileReader(propsFile)); + // assert after save that propertie have been set + Assert.assertEquals("value1", proper.get("propertie1")); + } - + @Test + public void testLoad() throws FileNotFoundException, IOException { + PropertiesExtended propsExtended = new PropertiesExtended(propsFile); + // assert empty + Assert.assertEquals(0, propsExtended.keySet().size()); + + FileWriter fw = new FileWriter(propsFile); + + props.store(fw, ""); + + // load properties from file + propsExtended.load(propsFile); + + // assert properties are write + Assert.assertEquals(props.size(), propsExtended.keySet().size()); + } } Modified: trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyFileUtilTest.java =================================================================== --- trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyFileUtilTest.java 2011-08-01 16:10:14 UTC (rev 1103) +++ trunk/wikitty-publication/src/test/java/org/nuiton/wikitty/publication/WikittyFileUtilTest.java 2011-08-02 09:13:37 UTC (rev 1104) @@ -1,12 +1,86 @@ package org.nuiton.wikitty.publication; +import java.io.File; +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.commons.io.FileUtils; +import org.junit.Test; +import org.nuiton.util.FileUtil; +import org.nuiton.util.StringUtil; + public class WikittyFileUtilTest { - - /* * 1) test création du path à partir de wikitty * * 2) test de la cohérence dans la création des paths à partir de label */ + + /** + * Test the label conversion to path + */ + @Test + public void testStringLabelToPath() { + + List<String> listLabel = new LinkedList<String>(); + listLabel.add("sub1"); + listLabel.add("sub2"); + listLabel.add("sub3"); + // construct label + String label = StringUtil.join(listLabel, + WikittyFileUtil.WIKITTY_LABEL_SEPARATOR, true); + // contruct path + String path = StringUtil.join(listLabel, File.separator, true); + + // convert label to path + String resultPath = WikittyFileUtil.labelToPath(label); + + Assert.assertEquals(resultPath, path); + } + + + /** + * Test the path creation from a label + * @throws IOException + */ + @Test + public void testDirectoryCreationFromPath() throws IOException { + + List<String> listLabel = new LinkedList<String>(); + listLabel.add("sub1"); + listLabel.add("sub2"); + listLabel.add("sub3"); + // construct label + String label = StringUtil.join(listLabel, + WikittyFileUtil.WIKITTY_LABEL_SEPARATOR, true); + + // contruct path + String path = StringUtil.join(listLabel, File.separator, true); + + File home = new File(FileUtils.getTempDirectory().getCanonicalFile() + + File.separator + "wikittyFileUtilTest"); + + // assert the temporary file does not exist + if (home.exists()){ + FileUtil.deleteRecursively(home); + + } + home.mkdir(); + + + File resultCreated = new File(home.getCanonicalPath()+File.separator+path); + // assert the complete path not exist + Assert.assertFalse(resultCreated.exists()); + // construct the directory + WikittyFileUtil.createFilesFromLabelPath(home, label); + // assert the diretories exists + Assert.assertTrue(resultCreated.exists()); + + FileUtil.deleteRecursively(home); + } + }