From 3fa2beb0c483b956c69602bbf2c6b386b9b3fe31 Mon Sep 17 00:00:00 2001 From: mcombellack Date: Sat, 20 Jun 2009 22:13:43 +0000 Subject: TUSCANY-3109 - Added unit test for using Atom binding on a Java component implementation class that does not have a get() method git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@786914 13f79535-47bb-0310-9956-ffa450edef68 --- .../binding/atom/AtomFeedNonCollectionTest.java | 156 +++++++++++++++++++++ .../binding/atom/CustomerNonCollectionImpl.java | 53 +++++++ .../binding/atom/ProviderNonCollection.composite | 33 +++++ 3 files changed, 242 insertions(+) create mode 100644 branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/AtomFeedNonCollectionTest.java create mode 100644 branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/CustomerNonCollectionImpl.java create mode 100644 branches/sca-java-1.x/modules/binding-atom-abdera/src/test/resources/org/apache/tuscany/sca/binding/atom/ProviderNonCollection.composite (limited to 'branches/sca-java-1.x/modules/binding-atom-abdera') diff --git a/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/AtomFeedNonCollectionTest.java b/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/AtomFeedNonCollectionTest.java new file mode 100644 index 0000000000..9623681a98 --- /dev/null +++ b/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/AtomFeedNonCollectionTest.java @@ -0,0 +1,156 @@ +/* + * 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.tuscany.sca.binding.atom; + +import java.util.Date; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import junit.framework.Assert; + +import org.apache.abdera.model.Link; +import org.apache.tuscany.sca.binding.atom.collection.Collection; +import org.apache.tuscany.sca.data.collection.Entry; +import org.apache.tuscany.sca.data.collection.Item; +import org.apache.tuscany.sca.host.embedded.SCADomain; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test cases for using an Atom feed that does not implement + * the Collections interface but does have a getAll() method. + */ +public class AtomFeedNonCollectionTest { + /** + * Used to generate unique IDs for the feed entries. + */ + private static final AtomicInteger ID_GEN = new AtomicInteger(); + + /** + * Data used for creating test feed entries. + */ + private static final String[] FEED_ENTRY_TITLES = {"Apache Tuscany", "Apache"}; + + /** + * Data used for creating test feed entries. + */ + private static final String[] FEED_ENTRY_CONTENT = {"This is Apache Tuscany", "This is Apache"}; + + /** + * Data used for creating test feed entries. + */ + private static final String[] FEED_ENTRY_LINK = {"http://tuscany.apache.org", "http://www.apache.org"}; + + /** + * The number of test feed entries. + */ + private static final int FEED_ENTRY_COUNT = FEED_ENTRY_TITLES.length; + + private static SCADomain scaConsumerDomain; + private static SCADomain scaProviderDomain; + private static CustomerClient testService; + + @BeforeClass + public static void init() throws Exception { + System.out.println(">>>AtomFeedNonCollectionTest.init entry"); + scaProviderDomain = SCADomain.newInstance("org/apache/tuscany/sca/binding/atom/ProviderNonCollection.composite"); + scaConsumerDomain = SCADomain.newInstance("org/apache/tuscany/sca/binding/atom/Consumer.composite"); + testService = scaConsumerDomain.getService(CustomerClient.class, "CustomerClient"); + } + + @AfterClass + public static void destroy() throws Exception { + System.out.println(">>>AtomFeedNonCollectionTest.destroy entry"); + if (scaConsumerDomain != null) { + scaConsumerDomain.close(); + } + if (scaProviderDomain != null) { + scaProviderDomain.close(); + } + } + + /** + * Make sure everything has been initialised correctly. + */ + @Test + public void testPrelim() { + Assert.assertNotNull(scaProviderDomain); + Assert.assertNotNull(scaConsumerDomain); + Assert.assertNotNull(testService); + } + + /** + * Test that we can retrieve entries from a feed that does not implement + * the Collection interface. + */ + @Test + public void testThatCanGetFeedEntriesFromNonCollectionImplementation() { + // Add some entries to the Atom feed + final Entry[] testEntries = new Entry[FEED_ENTRY_COUNT]; + for (int i = 0; i < FEED_ENTRY_COUNT; i++) { + testEntries[i] = createFeedEntry(FEED_ENTRY_TITLES[i], FEED_ENTRY_CONTENT[i], FEED_ENTRY_LINK[i]); + } + CustomerNonCollectionImpl.entries = testEntries; + + // Get the entries from the feed + final Collection resourceCollection = testService.getCustomerCollection(); + Assert.assertNotNull(resourceCollection); + final List entries = resourceCollection.getFeed().getEntries(); + + // Validate the feed entries + Assert.assertNotNull(entries); + Assert.assertEquals(FEED_ENTRY_COUNT, entries.size()); + for (int i = 0; i < FEED_ENTRY_COUNT; i++) { + final org.apache.abdera.model.Entry entry = entries.get(i); + Assert.assertEquals(FEED_ENTRY_TITLES[i], entry.getTitle()); + Assert.assertEquals(FEED_ENTRY_CONTENT[i], entry.getContent()); + final List links = entry.getLinks(); + Assert.assertNotNull(links); + Assert.assertEquals(1, links.size()); + final Link link = links.get(0); + final String linkStr = link.getHref().toString(); + Assert.assertEquals(FEED_ENTRY_LINK[i], linkStr); + } + } + + /** + * Creates a feed entry. + * + * @param title Title for the feed entry + * @param content Contents of the feed entry + * @param link Link for the feed entry + * @return A new feed entry. + */ + private Entry createFeedEntry(String title, String content, String link) { + final Item item = new Item(title, content, link, null, new Date()); + final Entry entry = new Entry(nextFeedID(), item); + return entry; + } + + /** + * Generates the feed entry ID. + * + * @return Next feed entry ID + */ + private String nextFeedID() { + return Integer.toString(ID_GEN.incrementAndGet()); + } +} diff --git a/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/CustomerNonCollectionImpl.java b/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/CustomerNonCollectionImpl.java new file mode 100644 index 0000000000..a25e19838f --- /dev/null +++ b/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/java/org/apache/tuscany/sca/binding/atom/CustomerNonCollectionImpl.java @@ -0,0 +1,53 @@ +/* + * 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.tuscany.sca.binding.atom; + +import org.apache.tuscany.sca.data.collection.Entry; +import org.osoa.sca.annotations.Scope; + +/** + * Implementation of an Atom feed that does not implement the Collections + * interface but does have a getAll() method that will be used by the Atom + * binding to get the feed entries. + */ +@Scope("COMPOSITE") +public class CustomerNonCollectionImpl { + /** + * All feed entries. + * This is set directly by the unit tests. + * @see AtomFeedNonCollectionTest + */ + public static Entry[] entries; + + /** + * Default constructor + */ + public CustomerNonCollectionImpl() { + } + + /** + * Get all entries for this feed. + * + * @return All entries for this feed + */ + public Entry[] getAll() { + return entries; + } +} diff --git a/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/resources/org/apache/tuscany/sca/binding/atom/ProviderNonCollection.composite b/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/resources/org/apache/tuscany/sca/binding/atom/ProviderNonCollection.composite new file mode 100644 index 0000000000..85160fc825 --- /dev/null +++ b/branches/sca-java-1.x/modules/binding-atom-abdera/src/test/resources/org/apache/tuscany/sca/binding/atom/ProviderNonCollection.composite @@ -0,0 +1,33 @@ + + + + + + + + + + + + + -- cgit v1.2.3