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 2015/09/23 15:19:18 UTC
svn commit: r1704843 - in /sling/trunk:
launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/teleporter/
launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/exported/
launchpad/test-servi...
Author: bdelacretaz
Date: Wed Sep 23 13:19:17 2015
New Revision: 1704843
URL: http://svn.apache.org/viewvc?rev=1704843&view=rev
Log:
SLING-5040 - implement and test LDAP filters for TeleporterRule-provided services
Added:
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/teleporter/LdapFilterTeleporterTest.java
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/exported/StringTransformer.java
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowerRankingStringTransformer.java
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowercaseStringTransformer.java
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/UppercaseStringTransformer.java
Modified:
sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java
Added: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/teleporter/LdapFilterTeleporterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/teleporter/LdapFilterTeleporterTest.java?rev=1704843&view=auto
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/teleporter/LdapFilterTeleporterTest.java (added)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/teleporter/LdapFilterTeleporterTest.java Wed Sep 23 13:19:17 2015
@@ -0,0 +1,46 @@
+/*
+ * 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.launchpad.webapp.integrationtest.teleporter;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
+import org.apache.sling.junit.rules.TeleporterRule;
+import org.apache.sling.launchpad.testservices.exported.StringTransformer;
+import org.junit.Rule;
+import org.junit.Test;
+
+/** Test Teleporter access to services using LDAP filters */
+public class LdapFilterTeleporterTest {
+
+ @Rule
+ public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "Launchpad");
+
+ @Test
+ public void testUppercase() {
+ final StringTransformer t = teleporter.getService(StringTransformer.class, "(mode=uppercase)");
+ assertNotNull("Expecting an uppercase transformer", t);
+ assertEquals("FOOBAR", t.transform("fooBAR"));
+ }
+
+ @Test
+ public void testLowercase() {
+ final StringTransformer t = teleporter.getService(StringTransformer.class, "(mode=lowercase)");
+ assertNotNull("Expecting a lowercase transformer", t);
+ assertEquals("foobar", t.transform("fooBAR"));
+ }
+
+}
\ No newline at end of file
Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/exported/StringTransformer.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/exported/StringTransformer.java?rev=1704843&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/exported/StringTransformer.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/exported/StringTransformer.java Wed Sep 23 13:19:17 2015
@@ -0,0 +1,21 @@
+/*
+ * 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.launchpad.testservices.exported;
+
+public interface StringTransformer {
+ String transform(String str);
+}
Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowerRankingStringTransformer.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowerRankingStringTransformer.java?rev=1704843&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowerRankingStringTransformer.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowerRankingStringTransformer.java Wed Sep 23 13:19:17 2015
@@ -0,0 +1,36 @@
+/*
+ * 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.launchpad.testservices.serversidetests;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.launchpad.testservices.exported.StringTransformer;
+
+@Component
+@Service
+@Properties({
+ @Property(name="mode", value="lowercase"),
+ @Property(name="service.ranking", intValue=-10)
+})
+
+public class LowerRankingStringTransformer implements StringTransformer {
+ public String transform(String str) {
+ throw new UnsupportedOperationException("This " + getClass().getSimpleName() + " shouldn't be called in tests due to lower ranking");
+ }
+}
Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowercaseStringTransformer.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowercaseStringTransformer.java?rev=1704843&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowercaseStringTransformer.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/LowercaseStringTransformer.java Wed Sep 23 13:19:17 2015
@@ -0,0 +1,31 @@
+/*
+ * 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.launchpad.testservices.serversidetests;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.launchpad.testservices.exported.StringTransformer;
+
+@Component
+@Service
+@Property(name="mode", value="lowercase")
+public class LowercaseStringTransformer implements StringTransformer {
+ public String transform(String str) {
+ return str.toLowerCase();
+ }
+}
Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/UppercaseStringTransformer.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/UppercaseStringTransformer.java?rev=1704843&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/UppercaseStringTransformer.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/UppercaseStringTransformer.java Wed Sep 23 13:19:17 2015
@@ -0,0 +1,31 @@
+/*
+ * 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.launchpad.testservices.serversidetests;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.launchpad.testservices.exported.StringTransformer;
+
+@Component
+@Service
+@Property(name="mode", value="uppercase")
+public class UppercaseStringTransformer implements StringTransformer {
+ public String transform(String str) {
+ return str.toUpperCase();
+ }
+}
Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java?rev=1704843&r1=1704842&r2=1704843&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java Wed Sep 23 13:19:17 2015
@@ -17,7 +17,12 @@
package org.apache.sling.junit.rules;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
/** Implements the logic used to get a service */
@@ -25,24 +30,36 @@ class ServiceGetter {
final ServiceReference serviceReference;
final Object service;
+ @SuppressWarnings("unchecked")
ServiceGetter(BundleContext bundleContext, Class<?> serviceClass, String ldapFilter) {
Object s;
- if(ldapFilter != null && !ldapFilter.isEmpty()) {
- throw new UnsupportedOperationException("LDAP service filter is not supported so far");
- }
-
if (serviceClass.equals(BundleContext.class)) {
// Special case to provide the BundleContext to tests
s = serviceClass.cast(bundleContext);
serviceReference = null;
} else {
- serviceReference = bundleContext.getServiceReference(serviceClass
- .getName());
-
+ if(ldapFilter != null && !ldapFilter.isEmpty()) {
+ try {
+ final ServiceReference [] services = bundleContext.getServiceReferences(serviceClass.getName(), ldapFilter);
+ if(services == null) {
+ serviceReference = null;
+ } else {
+ // Prefer services which have a higher ranking
+ final List<ServiceReference> sorted = Arrays.asList(services);
+ Collections.sort(sorted);
+ serviceReference = sorted.get(sorted.size() - 1);
+ }
+ } catch (InvalidSyntaxException e) {
+ throw new IllegalStateException("Invalid filter syntax:" + ldapFilter, e);
+ }
+ } else {
+ serviceReference = bundleContext.getServiceReference(serviceClass.getName());
+ }
+
if (serviceReference == null) {
throw new IllegalStateException(
- "unable to get a service reference");
+ "unable to get a service reference, class=" + serviceClass.getName() + ", filter='" + ldapFilter + "'");
}
final Object service = bundleContext.getService(serviceReference);
@@ -57,4 +74,4 @@ class ServiceGetter {
service = s;
}
-}
+}
\ No newline at end of file