Tuesday, December 13, 2011

svn commit: r1214032 - in /labs/magnet/core/src: main/java/org/apache/labs/magnet/bbs/ main/java/org/apache/labs/magnet/service/ main/resources/META-INF/spring/ test/java/org/apache/labs/magnet/annotation/ test/java/org/apache/labs/magnet/core/

Author: hadrian
Date: Wed Dec 14 02:08:41 2011
New Revision: 1214032

URL: http://svn.apache.org/viewvc?rev=1214032&view=rev
Log:
Fix tests requiring internet connection

Added:
labs/magnet/core/src/test/java/org/apache/labs/magnet/annotation/
labs/magnet/core/src/test/java/org/apache/labs/magnet/annotation/RequiresInternetConnection.java
Modified:
labs/magnet/core/src/main/java/org/apache/labs/magnet/bbs/BulletinProcessor.java
labs/magnet/core/src/main/java/org/apache/labs/magnet/service/BulletinProcessorService.java
labs/magnet/core/src/main/resources/META-INF/spring/beans.xml
labs/magnet/core/src/test/java/org/apache/labs/magnet/core/JiraAccessTest.java
labs/magnet/core/src/test/java/org/apache/labs/magnet/core/RestServiceTest.java

Modified: labs/magnet/core/src/main/java/org/apache/labs/magnet/bbs/BulletinProcessor.java
URL: http://svn.apache.org/viewvc/labs/magnet/core/src/main/java/org/apache/labs/magnet/bbs/BulletinProcessor.java?rev=1214032&r1=1214031&r2=1214032&view=diff
==============================================================================
--- labs/magnet/core/src/main/java/org/apache/labs/magnet/bbs/BulletinProcessor.java (original)
+++ labs/magnet/core/src/main/java/org/apache/labs/magnet/bbs/BulletinProcessor.java Wed Dec 14 02:08:41 2011
@@ -20,4 +20,6 @@ package org.apache.labs.magnet.bbs;
public interface BulletinProcessor {

void process(Bulletin bulletin);
+
+ Object retrieve(String id);
}

Modified: labs/magnet/core/src/main/java/org/apache/labs/magnet/service/BulletinProcessorService.java
URL: http://svn.apache.org/viewvc/labs/magnet/core/src/main/java/org/apache/labs/magnet/service/BulletinProcessorService.java?rev=1214032&r1=1214031&r2=1214032&view=diff
==============================================================================
--- labs/magnet/core/src/main/java/org/apache/labs/magnet/service/BulletinProcessorService.java (original)
+++ labs/magnet/core/src/main/java/org/apache/labs/magnet/service/BulletinProcessorService.java Wed Dec 14 02:08:41 2011
@@ -16,18 +16,47 @@
*/
package org.apache.labs.magnet.service;

+import org.apache.camel.CamelContext;
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.Exchange;
+import org.apache.camel.Header;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.labs.magnet.bbs.Bulletin;
import org.apache.labs.magnet.bbs.BulletinProcessor;

+import org.springframework.beans.factory.annotation.Autowired;
+

public class BulletinProcessorService implements BulletinProcessor {
+ @Autowired
+ private CamelContext context;
@Produce(uri="direct:apache-jira")
- private ProducerTemplate producer;
+ private ProducerTemplate jira;

@Override
public void process(Bulletin bulletin) {
- producer.sendBodyAndHeader(bulletin, "BulletinId", bulletin.getId());
+ jira.sendBodyAndHeader(bulletin, "BulletinId", bulletin.getId());
+ }
+
+ @Override
+ public Object retrieve(@Header(value="CamelFileName") String id) {
+ Exchange reply;
+ ConsumerTemplate consumer = context.createConsumerTemplate();
+ try {
+ consumer.start();
+ reply = consumer.receive("file:/x1/apache/magnet?noop=true&fileName=" + id, 5000);
+ return reply.getIn().getBody();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } finally {
+ try {
+ consumer.stop();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ return null;
}
}

Modified: labs/magnet/core/src/main/resources/META-INF/spring/beans.xml
URL: http://svn.apache.org/viewvc/labs/magnet/core/src/main/resources/META-INF/spring/beans.xml?rev=1214032&r1=1214031&r2=1214032&view=diff
==============================================================================
--- labs/magnet/core/src/main/resources/META-INF/spring/beans.xml (original)
+++ labs/magnet/core/src/main/resources/META-INF/spring/beans.xml Wed Dec 14 02:08:41 2011
@@ -35,6 +35,8 @@
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-camel.xml"/>

+ <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
+
<!-- Create an embedded ActiveMQ Broker
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
@@ -92,11 +94,22 @@

<route id="bbs-process">
<from uri="direct://bulletin-processor" />
+ <to uri="direct://bbs-store" />
+ </route>
+
+ <route id="bbs-store">
+ <from uri="direct://bbs-store" />
<setHeader headerName="CamelFileName"><simple>${body.id}.xml</simple></setHeader>
<marshal><jaxb contextPath="org.apache.labs.magnet.bbs" /></marshal>
<to uri="file:/x1/apache/magnet" />
</route>

+ <route>
+ <from uri="direct://bbs-retrieve" />
+ <bean ref="bbs-processor" method="retrieve" />
+ <unmarshal><jaxb contextPath="org.apache.labs.magnet.bbs" /></unmarshal>
+ </route>
+
<route id="jira">
<from uri="direct://apache-jira" />
<recipientList>

Added: labs/magnet/core/src/test/java/org/apache/labs/magnet/annotation/RequiresInternetConnection.java
URL: http://svn.apache.org/viewvc/labs/magnet/core/src/test/java/org/apache/labs/magnet/annotation/RequiresInternetConnection.java?rev=1214032&view=auto
==============================================================================
--- labs/magnet/core/src/test/java/org/apache/labs/magnet/annotation/RequiresInternetConnection.java (added)
+++ labs/magnet/core/src/test/java/org/apache/labs/magnet/annotation/RequiresInternetConnection.java Wed Dec 14 02:08:41 2011
@@ -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.labs.magnet.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.METHOD})
+public @interface RequiresInternetConnection {
+ String[] to();
+ int timeout() default 500;
+}

Modified: labs/magnet/core/src/test/java/org/apache/labs/magnet/core/JiraAccessTest.java
URL: http://svn.apache.org/viewvc/labs/magnet/core/src/test/java/org/apache/labs/magnet/core/JiraAccessTest.java?rev=1214032&r1=1214031&r2=1214032&view=diff
==============================================================================
--- labs/magnet/core/src/test/java/org/apache/labs/magnet/core/JiraAccessTest.java (original)
+++ labs/magnet/core/src/test/java/org/apache/labs/magnet/core/JiraAccessTest.java Wed Dec 14 02:08:41 2011
@@ -16,8 +16,14 @@
*/
package org.apache.labs.magnet.core;

+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.InetAddress;
+
import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.apache.labs.magnet.annotation.RequiresInternetConnection;
import org.apache.labs.magnet.bbs.Bulletin;
+import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@@ -28,7 +34,25 @@ public class JiraAccessTest extends Came
return new ClassPathXmlApplicationContext("META-INF/spring/beans.xml");
}

+ @Before
+ public void inetConnection() throws SecurityException, NoSuchMethodException {
+ Method testMethod = this.getClass().getMethod(testName.getMethodName());
+ if (testMethod.isAnnotationPresent(RequiresInternetConnection.class)) {
+ RequiresInternetConnection cnx = testMethod.getAnnotation(RequiresInternetConnection.class);
+ for (String address : cnx.to()) {
+ boolean inetReachable = false;
+ try {
+ inetReachable = InetAddress.getByName(address).isReachable(cnx.timeout());
+ } catch (IOException e) {
+ log.warn("Skip test: Internet connection to '{}' is unavaible (timeout='{}')", address, cnx.timeout());
+ }
+ org.junit.Assume.assumeTrue(inetReachable);
+ }
+ }
+ }
+
@Test
+ @RequiresInternetConnection(to = {"issues.apache.org"})
public void testFetchIssue() throws Exception {
Object reply = template.requestBodyAndHeader("direct://apache-jira", "", "BulletinId", "CAMEL-1000");
assert(reply instanceof Bulletin);

Modified: labs/magnet/core/src/test/java/org/apache/labs/magnet/core/RestServiceTest.java
URL: http://svn.apache.org/viewvc/labs/magnet/core/src/test/java/org/apache/labs/magnet/core/RestServiceTest.java?rev=1214032&r1=1214031&r2=1214032&view=diff
==============================================================================
--- labs/magnet/core/src/test/java/org/apache/labs/magnet/core/RestServiceTest.java (original)
+++ labs/magnet/core/src/test/java/org/apache/labs/magnet/core/RestServiceTest.java Wed Dec 14 02:08:41 2011
@@ -16,6 +16,7 @@
*/
package org.apache.labs.magnet.core;

+import org.apache.camel.Exchange;
import org.apache.camel.test.junit4.CamelSpringTestSupport;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
@@ -53,4 +54,10 @@ public class RestServiceTest extends Cam
// TODO: could improve this by using a mock endpoint in a test environment only
Thread.sleep(500);
}
+
+ @Test
+ public void testGetBulletin() throws Exception {
+ Object reply = template.requestBodyAndHeader("direct://bbs-retrieve", "", Exchange.FILE_NAME, "CAMEL-1234.xml");
+ System.out.println(reply);
+ }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org

No comments:

Post a Comment