X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-persister-file-xml-adapter%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fpersist%2Fstorage%2Ffile%2Fxml%2FFileStorageAdapterTest.java;h=5322f6357a01b0a3e01e1d58a8d943113b942562;hb=3948bedd0129e44c0943bd77c91806425645cd72;hp=d6bbeb31da2f3b3caa9a86acbadcd94452c070e1;hpb=7e6e19f57c11d6a35558f230f05b5b0d13b4ebe8;p=controller.git diff --git a/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java b/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java index d6bbeb31da..5322f6357a 100644 --- a/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java +++ b/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java @@ -9,18 +9,19 @@ package org.opendaylight.controller.config.persist.storage.file.xml; import com.google.common.base.Charsets; -import junit.framework.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; - import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.nio.file.Files; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; - +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; +import org.opendaylight.controller.config.persist.test.PropertiesProviderTest; import static junit.framework.Assert.assertFalse; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; @@ -30,6 +31,8 @@ public class FileStorageAdapterTest { private static int i; private File file; + private static final String NON_EXISTENT_DIRECTORY = "./nonExistentDir/"; + private static final String NON_EXISTENT_FILE = "nonExistent.txt"; @Before public void setUp() throws Exception { @@ -40,11 +43,40 @@ public class FileStorageAdapterTest { i = 1; } + @Test + public void testNewFile() throws Exception { + XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); + PropertiesProviderTest pp = new PropertiesProviderTest(); + pp.addProperty("fileStorage",NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE); + pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); + storage.instantiate(pp); + + final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { + @Override + public String getConfigSnapshot() { + return createConfig(); + } + + @Override + public SortedSet getCapabilities() { + return createCaps(); + } + }; + storage.persistConfig(holder); + + storage.persistConfig(holder); + + Assert.assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]"); + delete(new File(NON_EXISTENT_DIRECTORY)); + } @Test public void testFileAdapter() throws Exception { XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); - storage.setFileStorage(file); - storage.setNumberOfBackups(Integer.MAX_VALUE); + PropertiesProviderTest pp = new PropertiesProviderTest(); + pp.addProperty("fileStorage",file.getPath()); + pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); + storage.instantiate(pp); + final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { @Override public String getConfigSnapshot() { @@ -89,8 +121,12 @@ public class FileStorageAdapterTest { @Test public void testFileAdapterOneBackup() throws Exception { XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); - storage.setFileStorage(file); - storage.setNumberOfBackups(1); + + PropertiesProviderTest pp = new PropertiesProviderTest(); + pp.addProperty("fileStorage",file.getPath()); + pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); + storage.instantiate(pp); + final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { @Override public String getConfigSnapshot() { @@ -106,7 +142,7 @@ public class FileStorageAdapterTest { storage.persistConfig(holder); - assertEquals(16, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); + assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); List lastConf = storage.loadLastConfigs(); assertEquals(1, lastConf.size()); @@ -185,4 +221,12 @@ public class FileStorageAdapterTest { return "" + i++ + ""; } + private void delete(File f) throws IOException { + if (f.isDirectory()) { + for (File c : f.listFiles()) + delete(c); + } + if (!f.delete()) + throw new FileNotFoundException("Failed to delete file: " + f); + } }