| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.osgi; |
| 17 | |
| 18 | import com.datastax.driver.osgi.api.MailboxException; |
| 19 | import com.datastax.driver.osgi.api.MailboxMessage; |
| 20 | import com.datastax.driver.osgi.api.MailboxService; |
| 21 | import org.osgi.framework.Bundle; |
| 22 | import org.osgi.framework.BundleContext; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Collection; |
| 29 | import java.util.GregorianCalendar; |
| 30 | |
| 31 | import static org.testng.Assert.assertEquals; |
| 32 | |
| 33 | public abstract class MailboxServiceTests { |
| 34 | |
| 35 | private static final Logger LOGGER = LoggerFactory.getLogger(MailboxServiceTests.class); |
| 36 | |
| 37 | @Inject |
| 38 | MailboxService service; |
| 39 | |
| 40 | @Inject |
| 41 | BundleContext bundleContext; |
| 42 | |
| 43 | /** |
| 44 | * <p> |
| 45 | * Exercises a 'mailbox' service provided by an OSGi bundle that depends on the driver. Ensures that |
| 46 | * queries can be made through the service with the current given configuration. |
| 47 | * </p> |
| 48 | * <p/> |
| 49 | * The following configurations are tried (defined via methods with the @Configuration annotation): |
| 50 | * <ol> |
| 51 | * <li>Default bundle (Driver with all of it's dependencies and Guava 16.0.1)</li> |
| 52 | * <li>Shaded bundle (Driver with netty shaded and Guava 16.0.1)</li> |
| 53 | * <li>With Guava 17</li> |
| 54 | * <li>With Guava 18</li> |
| 55 | * <li>With Guava 19</li> |
| 56 | * </ol> |
| 57 | */ |
| 58 | protected void checkService() throws MailboxException { |
| 59 | if (LOGGER.isDebugEnabled()) { |
| 60 | for (Bundle bundle : bundleContext.getBundles()) { |
| 61 | LOGGER.debug("Loaded bundle: {} {}", bundle.getSymbolicName(), bundle.getVersion()); |
| 62 | } |
| 63 | } |
| 64 | // Insert some data into mailbox for a particular user. |
| 65 | String recipient = "user@datastax.com"; |
| 66 | try { |
| 67 | Collection<MailboxMessage> inMessages = new ArrayList<MailboxMessage>(); |
| 68 | for (int i = 0; i < 30; i++) { |
| 69 | MailboxMessage message = new MailboxMessage(recipient, new GregorianCalendar(2015, 1, i).getTimeInMillis(), recipient, "" + i); |
| 70 | inMessages.add(message); |
| 71 | service.sendMessage(message); |
| 72 | } |
| 73 | |
| 74 | Iterable<MailboxMessage> messages = service.getMessages(recipient); |
| 75 | |
| 76 | assertEquals(messages, inMessages); |
| 77 | } finally { |
| 78 | service.clearMailbox(recipient); |
| 79 | } |
| 80 | } |
| 81 | } |