Author: pauls
Date: Tue Aug  1 09:13:21 2017
New Revision: 1803629

URL: http://svn.apache.org/viewvc?rev=1803629&view=rev
Log:
Add some tests for Capability and Requirements and move some util classes 
around.

Added:
    
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/CapabilityMatcher.java
      - copied, changed from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/CapabilityMatcher.java
    
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/SimpleFilter.java
      - copied, changed from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/SimpleFilter.java
    
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/StringComparator.java
      - copied, changed from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/StringComparator.java
    
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/VersionRange.java
      - copied, changed from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/VersionRange.java
    
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/util/
    
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/util/CapabilityMatcherTest.java
Removed:
    
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/
Modified:
    
sling/whiteboard/cziegeler/feature-analyser/src/main/java/org/apache/sling/feature/analyser/impl/CheckRequirementsCapabilities.java
    
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestParser.java
    
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestUtil.java
    
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONReaderTest.java
    
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONWriterTest.java
    
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/U.java
    
sling/whiteboard/cziegeler/feature-support/src/test/resources/features/test.json

Modified: 
sling/whiteboard/cziegeler/feature-analyser/src/main/java/org/apache/sling/feature/analyser/impl/CheckRequirementsCapabilities.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-analyser/src/main/java/org/apache/sling/feature/analyser/impl/CheckRequirementsCapabilities.java?rev=1803629&r1=1803628&r2=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-analyser/src/main/java/org/apache/sling/feature/analyser/impl/CheckRequirementsCapabilities.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-analyser/src/main/java/org/apache/sling/feature/analyser/impl/CheckRequirementsCapabilities.java
 Tue Aug  1 09:13:21 2017
@@ -23,7 +23,7 @@ import org.apache.sling.feature.analyser
 import org.apache.sling.feature.analyser.BundleDescriptor;
 import org.apache.sling.feature.analyser.task.AnalyserTask;
 import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
-import org.apache.sling.feature.support.impl.CapabilityMatcher;
+import org.apache.sling.feature.support.util.CapabilityMatcher;
 
 import java.util.ArrayList;
 import java.util.List;

Copied: 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/CapabilityMatcher.java
 (from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/CapabilityMatcher.java)
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/CapabilityMatcher.java?p2=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/CapabilityMatcher.java&p1=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/CapabilityMatcher.java&r1=1803628&r2=1803629&rev=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/CapabilityMatcher.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/CapabilityMatcher.java
 Tue Aug  1 09:13:21 2017
@@ -1,4 +1,4 @@
-package org.apache.sling.feature.support.impl;
+package org.apache.sling.feature.support.util;
 
 import org.apache.sling.feature.Capability;
 import org.apache.sling.feature.Requirement;

Modified: 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestParser.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestParser.java?rev=1803629&r1=1803628&r2=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestParser.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestParser.java
 Tue Aug  1 09:13:21 2017
@@ -18,8 +18,6 @@ package org.apache.sling.feature.support
 
 import org.apache.sling.feature.Capability;
 import org.apache.sling.feature.Requirement;
-import org.apache.sling.feature.support.impl.SimpleFilter;
-import org.apache.sling.feature.support.impl.VersionRange;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Version;

Modified: 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestUtil.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestUtil.java?rev=1803629&r1=1803628&r2=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestUtil.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/ManifestUtil.java
 Tue Aug  1 09:13:21 2017
@@ -27,7 +27,9 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.function.BiConsumer;
+import java.util.function.Function;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 import java.util.stream.Collectors;
@@ -100,18 +102,18 @@ public class ManifestUtil {
     }
 
     public static void unmarshalAttribute(String key, Object value, 
BiConsumer<String, Object> sink) throws IOException {
-        unmarshal(key + "=" + value, sink);
+        unmarshal(key + "=" + value, Capability::getAttributes, sink);
     }
 
     public static void unmarshalDirective(String key, Object value, 
BiConsumer<String, Object> sink) throws IOException {
-        unmarshal(key + ":=" + value, sink);
+        unmarshal(key + ":=" + value, Capability::getDirectives, sink);
     }
 
-    private static void unmarshal(String header, BiConsumer<String, Object> 
sink) throws IOException {
+    private static void unmarshal(String header, Function<Capability, 
Map<String, Object>> lookup, BiConsumer<String, Object> sink) throws 
IOException {
         try {
             convertProvideCapabilities(
                     normalizeCapabilityClauses(parseStandardHeader("foo;" + 
header), "2"))
-                    .forEach(capability -> 
capability.getAttributes().forEach(sink));
+                    .forEach(capability -> 
lookup.apply(capability).forEach(sink));
         } catch (Exception e) {
             throw new IOException(e);
         }

Copied: 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/SimpleFilter.java
 (from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/SimpleFilter.java)
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/SimpleFilter.java?p2=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/SimpleFilter.java&p1=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/SimpleFilter.java&r1=1803628&r2=1803629&rev=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/SimpleFilter.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/SimpleFilter.java
 Tue Aug  1 09:13:21 2017
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.feature.support.impl;
+package org.apache.sling.feature.support.util;
 
 import java.util.ArrayList;
 import java.util.List;

Copied: 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/StringComparator.java
 (from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/StringComparator.java)
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/StringComparator.java?p2=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/StringComparator.java&p1=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/StringComparator.java&r1=1803628&r2=1803629&rev=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/StringComparator.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/StringComparator.java
 Tue Aug  1 09:13:21 2017
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.feature.support.impl;
+package org.apache.sling.feature.support.util;
 
 import java.util.Comparator;
 

Copied: 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/VersionRange.java
 (from r1803628, 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/VersionRange.java)
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/VersionRange.java?p2=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/VersionRange.java&p1=sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/VersionRange.java&r1=1803628&r2=1803629&rev=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/impl/VersionRange.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/main/java/org/apache/sling/feature/support/util/VersionRange.java
 Tue Aug  1 09:13:21 2017
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.feature.support.impl;
+package org.apache.sling.feature.support.util;
 
 import org.osgi.framework.Version;
 

Modified: 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONReaderTest.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONReaderTest.java?rev=1803629&r1=1803628&r2=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONReaderTest.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONReaderTest.java
 Tue Aug  1 09:13:21 2017
@@ -16,13 +16,15 @@
  */
 package org.apache.sling.feature.support.json;
 
+import org.apache.sling.feature.Capability;
 import org.apache.sling.feature.Configuration;
 import org.apache.sling.feature.Feature;
 import org.junit.Test;
 
-import java.util.List;
+import java.util.Arrays;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
@@ -40,29 +42,17 @@ public class FeatureJSONReaderTest {
         assertNull(feature.getId().getClassifier());
 
         assertEquals(2, feature.getConfigurations().size());
-        final Configuration cfg1 = 
findConfiguration(feature.getConfigurations(), "my.pid");
+        final Configuration cfg1 = 
U.findConfiguration(feature.getConfigurations(), "my.pid");
         assertEquals(7, cfg1.getProperties().get("number"));
-        final Configuration cfg2 = 
findFactoryConfiguration(feature.getConfigurations(), "my.factory.pid", "name");
+        final Configuration cfg2 = 
U.findFactoryConfiguration(feature.getConfigurations(), "my.factory.pid", 
"name");
         assertEquals("yeah", cfg2.getProperties().get("a.value"));
-    }
 
-    private Configuration findConfiguration(final List<Configuration> cfgs, 
final String pid) {
-        for(final Configuration c : cfgs) {
-            if ( !c.isFactoryConfiguration() && pid.equals(c.getPid()) ) {
-                return c;
-            }
-        }
-        fail("Configuration not found " + pid);
-        return null;
-    }
+        assertEquals(3, feature.getCapabilities().size());
+        Capability capability = 
U.findCapability(feature.getCapabilities(),"osgi.service");
+        assertNotNull(capability.getAttributes().get("objectClass"));
+
+        
assertEquals(Arrays.asList("org.osgi.service.http.runtime.HttpServiceRuntime"), 
capability.getAttributes().get("objectClass"));
 
-    private Configuration findFactoryConfiguration(final List<Configuration> 
cfgs, final String factoryid, final String name) {
-        for(final Configuration c : cfgs) {
-            if ( c.isFactoryConfiguration() && 
factoryid.equals(c.getFactoryPid()) && name.equals(c.getName())) {
-                return c;
-            }
-        }
-        fail("Factory Configuration not found " + factoryid + "~" + name);
-        return null;
     }
+
 }

Modified: 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONWriterTest.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONWriterTest.java?rev=1803629&r1=1803628&r2=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONWriterTest.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/FeatureJSONWriterTest.java
 Tue Aug  1 09:13:21 2017
@@ -21,6 +21,7 @@ import org.junit.Test;
 
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.util.Arrays;
 
 import static org.junit.Assert.assertEquals;
 
@@ -36,6 +37,8 @@ public class FeatureJSONWriterTest {
             }
         }
         assertEquals(feature.getId(), readFeature.getId());
+        
assertEquals(Arrays.asList("org.osgi.service.http.runtime.HttpServiceRuntime"),
+                U.findCapability(readFeature.getCapabilities(), 
"osgi.service").getAttributes().get("objectClass"));
     }
 
 }

Modified: 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/U.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/U.java?rev=1803629&r1=1803628&r2=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/U.java
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/json/U.java
 Tue Aug  1 09:13:21 2017
@@ -16,10 +16,16 @@
  */
 package org.apache.sling.feature.support.json;
 
+import org.apache.sling.feature.Capability;
+import org.apache.sling.feature.Configuration;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.Requirement;
 
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.util.List;
+
+import static org.junit.Assert.fail;
 
 /** Test utilities */
 public class U {
@@ -31,4 +37,46 @@ public class U {
             return FeatureJSONReader.read(reader, name);
         }
     }
+
+    public static Configuration findConfiguration(final List<Configuration> 
cfgs, final String pid) {
+        for(final Configuration c : cfgs) {
+            if ( !c.isFactoryConfiguration() && pid.equals(c.getPid()) ) {
+                return c;
+            }
+        }
+        fail("Configuration not found " + pid);
+        return null;
+    }
+
+    public static Configuration findFactoryConfiguration(final 
List<Configuration> cfgs, final String factoryid, final String name) {
+        for(final Configuration c : cfgs) {
+            if ( c.isFactoryConfiguration() && 
factoryid.equals(c.getFactoryPid()) && name.equals(c.getName())) {
+                return c;
+            }
+        }
+        fail("Factory Configuration not found " + factoryid + "~" + name);
+        return null;
+    }
+
+    public static Capability findCapability(List<Capability> capabilities, 
final String namespace) {
+        for (Capability capability : capabilities) {
+            if (capability.getNamespace().equals(namespace)) {
+                return capability;
+            }
+        }
+
+        fail(String.format("No Capability with namespace '%s' found", 
namespace));
+        return null;
+    }
+
+    public static Requirement findRequirement(List<Requirement> requirements, 
final String namespace) {
+        for (Requirement requirement : requirements) {
+            if (requirement.getNamespace().equals(namespace)) {
+                return requirement;
+            }
+        }
+
+        fail(String.format("No Requirement with namespace '%s' found", 
namespace));
+        return null;
+    }
 }

Added: 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/util/CapabilityMatcherTest.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/util/CapabilityMatcherTest.java?rev=1803629&view=auto
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/util/CapabilityMatcherTest.java
 (added)
+++ 
sling/whiteboard/cziegeler/feature-support/src/test/java/org/apache/sling/feature/support/util/CapabilityMatcherTest.java
 Tue Aug  1 09:13:21 2017
@@ -0,0 +1,17 @@
+package org.apache.sling.feature.support.util;
+
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.Requirement;
+import org.apache.sling.feature.support.json.U;
+import org.junit.Test;
+
+import static junit.framework.TestCase.assertTrue;
+
+public class CapabilityMatcherTest {
+    @Test public void testCapabilityMatching() throws Exception {
+        Feature feature = U.readFeature("test");
+        Requirement requirement = U.findRequirement(feature.getRequirements(), 
"osgi.contract");
+        
assertTrue(CapabilityMatcher.matches(U.findCapability(feature.getCapabilities(),
 "osgi.contract"),
+                SimpleFilter.parse((String) 
requirement.getDirectives().get("filter"))));
+    }
+}

Modified: 
sling/whiteboard/cziegeler/feature-support/src/test/resources/features/test.json
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature-support/src/test/resources/features/test.json?rev=1803629&r1=1803628&r2=1803629&view=diff
==============================================================================
--- 
sling/whiteboard/cziegeler/feature-support/src/test/resources/features/test.json
 (original)
+++ 
sling/whiteboard/cziegeler/feature-support/src/test/resources/features/test.json
 Tue Aug  1 09:13:21 2017
@@ -18,7 +18,7 @@
           {
               "namespace" : "osgi.contract",
               "directives" : {
-                  "filter" : "(&(osgi.contract=JavaServlet)(version=3.1))"
+                  "filter" : 
"(&(osgi.contract=JavaServlet)(&(version>=3.0)(!(version>=4.0))))"
               }
           }
     ],
@@ -41,6 +41,17 @@
              "directives" : {
                   "uses" : 
"org.osgi.service.http.runtime,org.osgi.service.http.runtime.dto"
              }
+        },
+        {
+          "namespace" : "osgi.contract",
+          "attributes" : {
+            "osgi.contract" : "JavaServlet",
+            "osgi.implementation" : "osgi.http",
+            "version:Version" : "3.1"
+          },
+          "directives" : {
+            "uses" : 
"org.osgi.service.http.runtime,org.osgi.service.http.runtime.dto"
+          }
         }
     ],
     "framework-properties" : {


Reply via email to