You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2016/11/23 13:35:47 UTC

svn commit: r1770966 - /sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/OsgiInstallerImplTest.java

Author: bdelacretaz
Date: Wed Nov 23 13:35:47 2016
New Revision: 1770966

URL: http://svn.apache.org/viewvc?rev=1770966&view=rev
Log:
SLING-6319 - test that demonstrates the .ser file deletion issue, see TODO in test

Added:
    sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/OsgiInstallerImplTest.java
      - copied, changed from r1770845, sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java

Copied: sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/OsgiInstallerImplTest.java (from r1770845, sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java)
URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/OsgiInstallerImplTest.java?p2=sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/OsgiInstallerImplTest.java&p1=sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java&r1=1770845&r2=1770966&rev=1770966&view=diff
==============================================================================
--- sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java (original)
+++ sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/OsgiInstallerImplTest.java Wed Nov 23 13:35:47 2016
@@ -18,396 +18,120 @@
  */
 package org.apache.sling.installer.core.impl;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
-import java.util.Collection;
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.Map;
-
-import org.osgi.framework.Bundle;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Random;
+import org.apache.sling.installer.api.InstallableResource;
+import static org.junit.Assert.fail;
+import org.junit.Before;
+import org.junit.Test;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.BundleListener;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkListener;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceFactory;
-import org.osgi.framework.ServiceListener;
-import org.osgi.framework.ServiceObjects;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.framework.Version;
-
-public class MockBundleContext implements BundleContext {
-
-    @Override
-    public boolean ungetService(ServiceReference reference) {
-        // TODO Auto-generated method stub
-        return false;
-    }
-
-    @Override
-    public void removeServiceListener(ServiceListener listener) {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void removeFrameworkListener(FrameworkListener listener) {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void removeBundleListener(BundleListener listener) {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    @SuppressWarnings("rawtypes")
-    public ServiceRegistration registerService(String clazz, Object service,
-            Dictionary properties) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    @SuppressWarnings("rawtypes")
-    public ServiceRegistration registerService(String[] clazzes,
-            Object service, Dictionary properties) {
-        // TODO Auto-generated method stub
-        return null;
-    }
 
-    @Override
-    public Bundle installBundle(String location) throws BundleException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Bundle installBundle(String location, InputStream input)
-            throws BundleException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public ServiceReference[] getServiceReferences(String clazz, String filter)
-            throws InvalidSyntaxException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public ServiceReference getServiceReference(String clazz) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Object getService(ServiceReference reference) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public String getProperty(String key) {
-        return null;
-    }
-
-    @Override
-    public File getDataFile(String filename) {
-        try {
-            if ( "installer".equals(filename) ) {
-                final File dir = this.getDataFile("test").getParentFile();
-                dir.deleteOnExit();
-                return new File(dir, filename);
-            }
-            final File f = File.createTempFile(filename, ".data");
-            f.deleteOnExit();
-            return f;
-        } catch (final IOException ioe) {
-            return null;
+/** Partial tests of the OsgiInstallerImpl. A number of things
+ *  are tested in the "it" module.
+ */
+public class OsgiInstallerImplTest {
+    private OsgiInstallerImpl installer;
+    private File storageDir;
+    private static final String SCHEME = OsgiInstallerImplTest.class.getSimpleName();
+    
+    private static final Random random = new Random();
+    
+    private static final String A = "a-" + random.nextInt();
+    private static final String B = "b-" + random.nextInt();
+    private static final String C = "c-" + random.nextInt();
+    private static final String D = "d-" + random.nextInt();
+    
+    private InstallableResource mockBundle(String id) {
+        final InputStream data = new ByteArrayInputStream(id.getBytes());
+        final String digest = id + id.length();
+        final int priority = 10;
+        final String type = InstallableResource.TYPE_FILE;
+        return new InstallableResource(id, data, null, digest, type, priority);
+    }
+    
+    private InstallableResource [] mockBundles(String ... ids) {
+        final InstallableResource [] result = new InstallableResource[ids.length];
+        for(int i=0; i < ids.length; i++) {
+            result[i] = mockBundle(ids[i]);
         }
+        return result;
     }
-
-    @Override
-    public Bundle[] getBundles() {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Bundle getBundle(long id) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Bundle getBundle() {
-        return new Bundle() {
-
-            @Override
-            public int getState() {
-                // TODO Auto-generated method stub
-                return 0;
-            }
-
-            @Override
-            public void start(int options) throws BundleException {
-                // TODO Auto-generated method stub
-
-            }
-
-            @Override
-            public void start() throws BundleException {
-                // TODO Auto-generated method stub
-
-            }
-
-            @Override
-            public void stop(int options) throws BundleException {
-                // TODO Auto-generated method stub
-
-            }
-
-            @Override
-            public void stop() throws BundleException {
-                // TODO Auto-generated method stub
-
-            }
-
-            @Override
-            public void update(InputStream input) throws BundleException {
-                // TODO Auto-generated method stub
-
-            }
-
-            @Override
-            public void update() throws BundleException {
-                // TODO Auto-generated method stub
-
-            }
-
-            @Override
-            public void uninstall() throws BundleException {
-                // TODO Auto-generated method stub
-
-            }
-
-            @Override
-            @SuppressWarnings("rawtypes")
-            public Dictionary getHeaders() {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public long getBundleId() {
-                // TODO Auto-generated method stub
-                return 0;
-            }
-
-            @Override
-            public String getLocation() {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public ServiceReference[] getRegisteredServices() {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public ServiceReference[] getServicesInUse() {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public boolean hasPermission(Object permission) {
-                // TODO Auto-generated method stub
-                return false;
-            }
-
-            @Override
-            public URL getResource(String name) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            @SuppressWarnings("rawtypes")
-            public Dictionary getHeaders(String locale) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public String getSymbolicName() {
-                return "test-bundle";
-            }
-
-            @Override
-            @SuppressWarnings("rawtypes")
-            public Class loadClass(String name) throws ClassNotFoundException {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            @SuppressWarnings("rawtypes")
-            public Enumeration getResources(String name) throws IOException {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            @SuppressWarnings("rawtypes")
-            public Enumeration getEntryPaths(String path) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public URL getEntry(String path) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public long getLastModified() {
-                // TODO Auto-generated method stub
-                return 0;
-            }
-
-            @Override
-            @SuppressWarnings("rawtypes")
-            public Enumeration findEntries(String path, String filePattern,
-                    boolean recurse) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public BundleContext getBundleContext() {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            @SuppressWarnings("rawtypes")
-            public Map getSignerCertificates(int signersType) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public Version getVersion() {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public <A> A adapt(Class<A> type) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-
-            @Override
-            public int compareTo(Bundle o) {
-                // TODO Auto-generated method stub
-                return 0;
-            }
-
-            @Override
-            public File getDataFile(String filename) {
-                // TODO Auto-generated method stub
-                return null;
+    
+    private void assertDataFiles(String ... ids) {
+        final List<String> dataFileNames = new ArrayList<>();
+        
+        final String [] filenames = storageDir.list();
+        if(filenames != null) {
+            dataFileNames.addAll(Arrays.asList(filenames));
+        }
+        
+        final List<String> notFound = new ArrayList<>(Arrays.asList(ids));
+        for(String id : ids) {
+            final Iterator<String> it = dataFileNames.iterator();
+            while(it.hasNext()) {
+                final String filename = it.next();
+                
+                // This assumes FileDataStore creates files that
+                // contain the resource ID in their filename, and
+                // that our IDs are sufficiently unique. Works for
+                // this test but somewhat hacky.
+                if(filename.contains(id)) {
+                    notFound.remove(id);
+                    it.remove();
+                    break;
+                }
             }
-        };
-    }
-
-    @Override
-    public ServiceRegistration registerService(Class clazz, Object service, Dictionary properties) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public ServiceReference getServiceReference(Class clazz) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Collection getServiceReferences(Class clazz, String filter)
-            throws InvalidSyntaxException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Bundle getBundle(String location) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public ServiceReference[] getAllServiceReferences(String clazz,
-            String filter) throws InvalidSyntaxException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Filter createFilter(String filter) throws InvalidSyntaxException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public void addServiceListener(ServiceListener listener) {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void addServiceListener(ServiceListener listener, String filter)
-            throws InvalidSyntaxException {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void addFrameworkListener(FrameworkListener listener) {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void addBundleListener(BundleListener listener) {
-        // TODO Auto-generated method stub
-
+        }
+        
+        
+        if(!notFound.isEmpty()) {
+            fail("Some expected data files were not found:" + notFound);
+        }
+        
+        if(!dataFileNames.isEmpty()) {
+            fail("Extra data files found: " + dataFileNames);
+        }
     }
-
-    @Override
-    public <S> ServiceRegistration<S> registerService(Class<S> clazz, ServiceFactory<S> factory,
-            Dictionary<String, ?> properties) {
-        // TODO Auto-generated method stub
-        return null;
+    
+    @Before
+    public void setup() {
+        final BundleContext ctx = new MockBundleContext();
+        installer = new OsgiInstallerImpl(ctx);
+        
+        // storageDir points to the folder used by FileDataStore
+        // to store the private files that it creates, so
+        // that we can check them.
+        final FileDataStore fds = new FileDataStore(ctx);
+        final File testFile = fds.getDataFile("f00");
+        storageDir = testFile.getParentFile();
+        
+        // Cleanup storage dir so that we can check
+        // exactly which files are created there
+        for(String f : storageDir.list()) {
+            new File(storageDir, f).delete();
+        }
     }
-
-    @Override
-    public <S> ServiceObjects<S> getServiceObjects(ServiceReference<S> reference) {
-        // TODO Auto-generated method stub
-        return null;
+    
+    @Test
+    public void testDataFiles() {
+        // The installer creates data files for all installed bundles,
+        // and deletes them when they are not needed anymore.
+        // This verifies the create/delete logic
+        
+        installer.registerResources(SCHEME, mockBundles(A, B));
+        assertDataFiles(A, B);
+        
+        installer.updateResources(SCHEME, mockBundles(A, C), null);
+        assertDataFiles(A, B, C);
+        
+        installer.registerResources(SCHEME, mockBundles(C, D));
+        // TODO this is wrong, should be C, D
+        assertDataFiles(B, D);
     }
 }
\ No newline at end of file