Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/SynchronousQueueServiceTestCase.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/SynchronousQueueServiceTestCase.java?rev=1453391&view=auto ============================================================================== --- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/SynchronousQueueServiceTestCase.java (added) +++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/SynchronousQueueServiceTestCase.java Wed Mar 6 15:44:12 2013 @@ -0,0 +1,96 @@ +/* + * 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.felix.ipojo.extender.internal.queue; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.verify; + +import java.util.Dictionary; + +import org.apache.felix.ipojo.extender.internal.queue.callable.StringCallable; +import org.apache.felix.ipojo.extender.queue.Callback; +import org.apache.felix.ipojo.extender.queue.JobInfo; +import org.apache.felix.ipojo.extender.queue.QueueService; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import junit.framework.TestCase; + +/** + * Checks the synchronous queue behavior + */ +public class SynchronousQueueServiceTestCase extends TestCase { + + @Mock + private BundleContext m_bundleContext; + + @Mock + private ServiceRegistration<?> m_registration; + + @Mock + private Callback<String> m_callback; + + @Captor + private ArgumentCaptor<JobInfo> infos; + + @Override + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + public void testRegistration() throws Exception { + SynchronousQueueService queueService = new SynchronousQueueService(m_bundleContext); + + Mockito.<ServiceRegistration<?>>when(m_bundleContext.registerService(eq(QueueService.class.getName()), eq(queueService), any(Dictionary.class))).thenReturn(m_registration); + + queueService.start(); + + verify(m_bundleContext).registerService(eq(QueueService.class.getName()), eq(queueService), any(Dictionary.class)); + + queueService.stop(); + + verify(m_registration).unregister(); + } + + public void testSubmitsAreSequential() throws Exception { + + SynchronousQueueService queueService = new SynchronousQueueService(m_bundleContext); + + queueService.submit(new StringCallable("one"), m_callback, null); + queueService.submit(new StringCallable("two"), m_callback, null); + + InOrder order = inOrder(m_callback); + order.verify(m_callback).success(infos.capture(), eq("one")); + order.verify(m_callback).success(infos.capture(), eq("two")); + + JobInfo one = infos.getAllValues().get(0); + JobInfo two = infos.getAllValues().get(1); + + assertTrue(two.getEnlistmentTime() >= one.getEndTime()); + } +}
Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/SleepingCallable.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/SleepingCallable.java?rev=1453391&view=auto ============================================================================== --- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/SleepingCallable.java (added) +++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/SleepingCallable.java Wed Mar 6 15:44:12 2013 @@ -0,0 +1,40 @@ +/* + * 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.felix.ipojo.extender.internal.queue.callable; + +import java.util.concurrent.Callable; + +/** +* A dummy job taking some time to complete.... +*/ +public class SleepingCallable implements Callable<String> { + private int m_time; + private String m_value; + + public SleepingCallable(int time, String value) { + m_time = time; + m_value = value; + } + + public String call() throws Exception { + Thread.sleep(m_time); + return m_value; + } +} Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/StringCallable.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/StringCallable.java?rev=1453391&view=auto ============================================================================== --- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/StringCallable.java (added) +++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/callable/StringCallable.java Wed Mar 6 15:44:12 2013 @@ -0,0 +1,42 @@ +/* + * 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.felix.ipojo.extender.internal.queue.callable; + +import java.util.concurrent.Callable; + +/** +* A dummy job. +*/ +public class StringCallable implements Callable<String> { + + private final String m_hello; + + public StringCallable() { + this("hello"); + } + + public StringCallable(String hello) { + m_hello = hello; + } + + public String call() throws Exception { + return m_hello; + } +} Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/HeaderPreferenceSelectionTestCase.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/HeaderPreferenceSelectionTestCase.java?rev=1453391&view=auto ============================================================================== --- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/HeaderPreferenceSelectionTestCase.java (added) +++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/HeaderPreferenceSelectionTestCase.java Wed Mar 6 15:44:12 2013 @@ -0,0 +1,79 @@ +/* + * 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.felix.ipojo.extender.internal.queue.pref; + +import static org.mockito.Mockito.when; + +import java.util.Hashtable; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.osgi.framework.Bundle; + +import junit.framework.TestCase; + +/** + * Checks the selection of the job processing preference from the bundle manifest. + */ +public class HeaderPreferenceSelectionTestCase extends TestCase { + + public static final String HEADER = "Header"; + + @Mock + private Bundle m_bundle; + + @Override + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + public void testMissingHeader() throws Exception { + Hashtable<String, String> headers = new Hashtable<String, String>(); + when(m_bundle.getHeaders()).thenReturn(headers); + HeaderPreferenceSelection selection = new HeaderPreferenceSelection(HEADER); + assertEquals(Preference.DEFAULT, selection.select(m_bundle)); + } + + public void testUnrecognizedHeader() throws Exception { + Hashtable<String, String> headers = new Hashtable<String, String>(); + headers.put(HEADER, "invalid"); + when(m_bundle.getHeaders()).thenReturn(headers); + HeaderPreferenceSelection selection = new HeaderPreferenceSelection(HEADER); + assertEquals(Preference.DEFAULT, selection.select(m_bundle)); + } + + public void testSyncHeader() throws Exception { + Hashtable<String, String> headers = new Hashtable<String, String>(); + // We should ignore case + headers.put(HEADER, "SyNc"); + when(m_bundle.getHeaders()).thenReturn(headers); + HeaderPreferenceSelection selection = new HeaderPreferenceSelection(HEADER); + assertEquals(Preference.SYNC, selection.select(m_bundle)); + } + + public void testAsyncHeader() throws Exception { + Hashtable<String, String> headers = new Hashtable<String, String>(); + // We should ignore case + headers.put(HEADER, "aSyNc"); + when(m_bundle.getHeaders()).thenReturn(headers); + HeaderPreferenceSelection selection = new HeaderPreferenceSelection(HEADER); + assertEquals(Preference.ASYNC, selection.select(m_bundle)); + } +} Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/enforce/EnforcedQueueServiceTestCase.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/enforce/EnforcedQueueServiceTestCase.java?rev=1453391&view=auto ============================================================================== --- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/enforce/EnforcedQueueServiceTestCase.java (added) +++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/pref/enforce/EnforcedQueueServiceTestCase.java Wed Mar 6 15:44:12 2013 @@ -0,0 +1,102 @@ +/* + * 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.felix.ipojo.extender.internal.queue.pref.enforce; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import java.util.concurrent.Callable; + +import org.apache.felix.ipojo.extender.internal.LifecycleQueueService; +import org.apache.felix.ipojo.extender.internal.queue.callable.StringCallable; +import org.apache.felix.ipojo.extender.internal.queue.pref.Preference; +import org.apache.felix.ipojo.extender.internal.queue.pref.PreferenceSelection; +import org.apache.felix.ipojo.extender.queue.QueueService; +import org.apache.felix.ipojo.util.Log; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleReference; + +import junit.framework.TestCase; + +/** + * Checks the enforced queue service. + */ +public class EnforcedQueueServiceTestCase extends TestCase { + + @Mock + private LifecycleQueueService delegate; + + @Mock + private Bundle m_bundle; + + @Mock + private Log m_log; + + @Mock + private PreferenceSelection m_selection; + + @Override + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + public void testNoEnforcement() throws Exception { + when(m_selection.select(m_bundle)).thenReturn(Preference.DEFAULT); + EnforcedQueueService queueService = new EnforcedQueueService(m_selection, delegate, Preference.ASYNC, m_log); + queueService.submit(new ReferenceCallable()); + verifyZeroInteractions(m_log); + } + + public void testNoEnforcementBecauseNoBundleReference() throws Exception { + EnforcedQueueService queueService = new EnforcedQueueService(m_selection, delegate, Preference.ASYNC, m_log); + queueService.submit(new StringCallable()); + verifyZeroInteractions(m_log); + } + + public void testIncompatibleEnforcement() throws Exception { + when(m_selection.select(m_bundle)).thenReturn(Preference.SYNC); + EnforcedQueueService queueService = new EnforcedQueueService(m_selection, delegate, Preference.ASYNC, m_log); + queueService.submit(new ReferenceCallable()); + + verify(m_log).log(eq(Log.WARNING), anyString()); + } + + public void testCompatibleEnforcement() throws Exception { + when(m_selection.select(m_bundle)).thenReturn(Preference.ASYNC); + EnforcedQueueService queueService = new EnforcedQueueService(m_selection, delegate, Preference.ASYNC, m_log); + queueService.submit(new ReferenceCallable()); + verifyZeroInteractions(m_log); + } + + private class ReferenceCallable implements Callable<String>, BundleReference { + public String call() throws Exception { + return "hello"; + } + + public Bundle getBundle() { + return m_bundle; + } + } +}
