You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by ff...@apache.org on 2012/12/25 03:17:54 UTC

svn commit: r1425706 - /karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java

Author: ffang
Date: Tue Dec 25 02:17:54 2012
New Revision: 1425706

URL: http://svn.apache.org/viewvc?rev=1425706&view=rev
Log:
[KARAF-2087]add a testcase to cover the scenario that the AdminService can always load latest instance status which changed by external process

Modified:
    karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java

Modified: karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java?rev=1425706&r1=1425705&r2=1425706&view=diff
==============================================================================
--- karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java (original)
+++ karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java Tue Dec 25 02:17:54 2012
@@ -110,6 +110,61 @@ public class InstanceServiceImplTest ext
         assertNotNull(service.getInstance(getName() + "b"));
     }
 
+
+    /**
+     * <p>
+     * Test the renaming of an existing instance.
+     * </p>
+     */
+    public void testToSimulateRenameInstanceByExternalProcess() throws Exception {
+        InstanceServiceImpl service = new InstanceServiceImpl();
+        File storageLocation = new File("target/instances/" + System.currentTimeMillis());
+        service.setStorageLocation(storageLocation);
+
+        InstanceSettings settings = new InstanceSettings(8122, 1122, 44444, getName(), null, null, null);
+        service.createInstance(getName(), settings, true);
+        
+        //to simulate the scenario that the instance name get changed by 
+        //external process, likely the admin command CLI tool, which cause
+        //the instance storage file get updated, the AdminService should be 
+        //able to reload the storage file before check any status for the 
+        //instance
+        
+        File storageFile = new File(storageLocation, InstanceServiceImpl.STORAGE_FILE);
+        assertTrue(storageFile.isFile());
+        Properties storage = loadStorage(storageFile);
+        storage.setProperty("item.0.name", getName() + "b");
+        saveStorage(storage, storageFile, "testToSimulateRenameInstanceByExternalProcess");
+        
+        assertNotNull(service.getInstance(getName() + "b"));
+    }
+
+    private void saveStorage(Properties props, File location, String comment) throws IOException {
+        OutputStream os = null;
+        try {
+            os = new FileOutputStream(location);
+            props.store(os, comment);
+        } finally {
+            if (os != null) {
+                os.close();
+            }
+        }
+    }
+    
+    private Properties loadStorage(File location) throws IOException {
+        InputStream is = null;
+        try {
+            is = new FileInputStream(location);
+            Properties props = new Properties();
+            props.load(is);
+            return props;
+        } finally {
+            if (is != null) {
+                is.close();
+            }
+        }
+    }
+
     private void assertFileExists(String path, String name) throws IOException {
         File file = new File(path, name);
         assertTrue("Expected " + file.getCanonicalPath() + " to exist",