This is an automated email from the ASF dual-hosted git repository. davidb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-apiregions.git
commit dc20a7c0c476c689c51be10b2bcc76042b99cca9 Author: David Bosschaert <[email protected]> AuthorDate: Sat Nov 3 10:49:57 2018 +0000 Unit tests for the apiregions runtime component. --- .../feature/apiregions/impl/RegionEnforcer.java | 28 +++++---- .../apiregions/impl/RegionEnforcerTest.java | 73 ++++++++++++++++++++++ src/test/resources/idbsnver1.properties | 6 ++ 3 files changed, 94 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java b/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java index 26807ef..7a5c673 100644 --- a/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java +++ b/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java @@ -40,15 +40,16 @@ import java.util.Properties; import java.util.Set; class RegionEnforcer implements ResolverHookFactory { - private static final String idbsnverFileName = "idbsnver.properties"; - private static final String bundleFeatureFileName = "bundles.properties"; - private static final String regionPackageFileName = "regions.properties"; - private static final String featureRegionFileName = "features.properties"; + static final String PROPERTIES_FILE_PREFIX = "whitelisting."; + static final String IDBSNVER_FILENAME = "idbsnver.properties"; + static final String BUNDLE_FEATURE_FILENAME = "bundles.properties"; + static final String REGION_PACKAGE_FILENAME = "regions.properties"; + static final String FEATURE_REGION_FILENAME = "features.properties"; - private final Map<Map.Entry<String, Version>, List<String>> bsnVerMap; - private final Map<String, Set<String>> bundleFeatureMap; - private final Map<String, Set<String>> featureRegionMap; - private final Map<String, Set<String>> regionPackageMap; + final Map<Map.Entry<String, Version>, List<String>> bsnVerMap; + final Map<String, Set<String>> bundleFeatureMap; + final Map<String, Set<String>> featureRegionMap; + final Map<String, Set<String>> regionPackageMap; public RegionEnforcer() throws IOException { bsnVerMap = populateBSNVerMap(); @@ -58,7 +59,7 @@ class RegionEnforcer implements ResolverHookFactory { } private Map<Map.Entry<String, Version>, List<String>> populateBSNVerMap() throws IOException { - File idbsnverFile = getDataFile(idbsnverFileName); + File idbsnverFile = getDataFile(IDBSNVER_FILENAME); if (idbsnverFile != null && idbsnverFile.exists()) { Map<Map.Entry<String, Version>, List<String>> m = new HashMap<>(); @@ -75,6 +76,7 @@ class RegionEnforcer implements ResolverHookFactory { l = new ArrayList<>(); m.put(key, l); } + l.add(n); } Map<Map.Entry<String, Version>, List<String>> m2 = new HashMap<>(); @@ -90,15 +92,15 @@ class RegionEnforcer implements ResolverHookFactory { } private Map<String, Set<String>> populateBundleFeatureMap() throws IOException { - return loadMap(bundleFeatureFileName); + return loadMap(BUNDLE_FEATURE_FILENAME); } private Map<String, Set<String>> populateFeatureRegionMap() throws IOException { - return loadMap(featureRegionFileName); + return loadMap(FEATURE_REGION_FILENAME); } private Map<String, Set<String>> populateRegionPackageMap() throws IOException { - return loadMap(regionPackageFileName); + return loadMap(REGION_PACKAGE_FILENAME); } private Map<String, Set<String>> loadMap(String fileName) throws IOException { @@ -121,7 +123,7 @@ class RegionEnforcer implements ResolverHookFactory { } private File getDataFile(String name) throws IOException { - String fn = System.getProperty("whitelisting." + name); + String fn = System.getProperty(PROPERTIES_FILE_PREFIX + name); if (fn == null) return null; return new File(fn); diff --git a/src/test/java/org/apache/sling/feature/apiregions/impl/RegionEnforcerTest.java b/src/test/java/org/apache/sling/feature/apiregions/impl/RegionEnforcerTest.java new file mode 100644 index 0000000..419c0ea --- /dev/null +++ b/src/test/java/org/apache/sling/feature/apiregions/impl/RegionEnforcerTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.feature.apiregions.impl; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.osgi.framework.Version; + +import java.util.AbstractMap; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Properties; + +import static org.apache.sling.feature.apiregions.impl.RegionEnforcer.IDBSNVER_FILENAME; +import static org.apache.sling.feature.apiregions.impl.RegionEnforcer.PROPERTIES_FILE_PREFIX; +import static org.junit.Assert.assertEquals; + +public class RegionEnforcerTest { + private Properties savedProps; + + @Before + public void setup() { + savedProps = new Properties(); // note that new Properties(props) doesn't copy + savedProps.putAll(System.getProperties()); + } + + @After + public void teardown() { + System.setProperties(savedProps); + savedProps = null; + } + + @Test + public void testRegionEnforcerNoConfiguration() throws Exception { + RegionEnforcer re = new RegionEnforcer(); + assertEquals(0, re.bsnVerMap.size()); + assertEquals(0, re.bundleFeatureMap.size()); + assertEquals(0, re.featureRegionMap.size()); + assertEquals(0, re.regionPackageMap.size()); + } + + @Test + public void testLoadBSNVerMap() throws Exception { + String f = getClass().getResource("/idbsnver1.properties").getFile(); + System.setProperty(PROPERTIES_FILE_PREFIX + IDBSNVER_FILENAME, f); + + RegionEnforcer re = new RegionEnforcer(); + assertEquals(2, re.bsnVerMap.size()); + assertEquals(Collections.singletonList("g:b1:1"), + re.bsnVerMap.get(new AbstractMap.SimpleEntry<String,Version>("b1", new Version(1,0,0)))); + assertEquals(new HashSet<>(Arrays.asList("g:b2:1.2.3", "g2:b2:1.2.4")), + new HashSet<>(re.bsnVerMap.get(new AbstractMap.SimpleEntry<String,Version>("b2", new Version(1,2,3))))); + + } +} diff --git a/src/test/resources/idbsnver1.properties b/src/test/resources/idbsnver1.properties new file mode 100644 index 0000000..00a740d --- /dev/null +++ b/src/test/resources/idbsnver1.properties @@ -0,0 +1,6 @@ +#Generated at Sat Nov 03 10:26:37 GMT 2018 +#Sat Nov 03 10:26:37 GMT 2018 +g\:b2\:1.2.3=b2~1.2.3 +g\:b1\:1=b1~1.0.0 +g2\:b2\:1.2.4=b2~1.2.3 +
