camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1460259 - in /camel/trunk/components/camel-scala: ./ src/main/resources/META-INF/services/org/apache/camel/ src/main/scala/org/apache/camel/scala/converter/ src/test/scala/org/apache/camel/scala/ src/test/scala/org/apache/camel/scala/conve...
Date Sun, 24 Mar 2013 03:31:12 GMT
Author: ningjiang
Date: Sun Mar 24 03:31:11 2013
New Revision: 1460259

URL: http://svn.apache.org/r1460259
Log:
CAMEL-6182 Merged the converters into camel-scala from camel-scala-extra

Added:
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
    camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelSpec.scala
    camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/converter/ConverterSpec.scala
Modified:
    camel/trunk/components/camel-scala/pom.xml
    camel/trunk/components/camel-scala/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaTypeConverter.scala

Modified: camel/trunk/components/camel-scala/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/pom.xml?rev=1460259&r1=1460258&r2=1460259&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/pom.xml (original)
+++ camel/trunk/components/camel-scala/pom.xml Sun Mar 24 03:31:11 2013
@@ -50,6 +50,12 @@
 
         <!-- test dependencies -->
         <dependency>
+            <groupId>org.scalatest</groupId>
+            <artifactId>scalatest_${scala-version}</artifactId>
+            <version>1.8</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-core</artifactId>
             <type>test-jar</type>

Modified: camel/trunk/components/camel-scala/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/resources/META-INF/services/org/apache/camel/TypeConverter?rev=1460259&r1=1460258&r2=1460259&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
(original)
+++ camel/trunk/components/camel-scala/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
Sun Mar 24 03:31:11 2013
@@ -15,4 +15,7 @@
 # limitations under the License.
 #
 
-org.apache.camel.scala.converter.ScalaTypeConverter
\ No newline at end of file
+org.apache.camel.scala.converter.ScalaTypeConverter
+org.apache.camel.scala.converter.ScalaImmutableCollections
+org.apache.camel.scala.converter.ScalaMutableCollections
+org.apache.camel.scala.converter.ScalaOption
\ No newline at end of file

Added: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala?rev=1460259&view=auto
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
(added)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaCollections.scala
Sun Mar 24 03:31:11 2013
@@ -0,0 +1,115 @@
+/**
+ * Copyright (C) 2011 Osinka <http://osinka.ru>
+ *
+ * Licensed 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.camel.scala.converter
+
+import org.apache.camel.Converter
+import collection.JavaConversions._
+
+/**
+ * @author Alexander Azarov <azarov@osinka.ru>
+ */
+@Converter
+class ScalaImmutableCollections {
+  import collection.{Iterator, Iterable, Map, Seq, Set}
+  import java.lang.{Iterable => JIterable}
+  import java.util.{Collection => JCollection, Enumeration => JEnumeration, Iterator
=> JIterator, List => JList, Map => JMap, Set => JSet}
+
+  @Converter
+  def toJavaIterator[T](iterator: Iterator[T]): JIterator[T] = iterator
+
+  @Converter
+  def toEnumeration[T](iterator: Iterator[T]): JEnumeration[T] = iterator
+
+  @Converter
+  def toScalaIterator[T](iterator: JIterator[T]): Iterator[T] = iterator
+
+  @Converter
+  def toScalaIterator[T](enumeration: JEnumeration[T]): Iterator[T] = enumeration
+
+  @Converter
+  def toJavaIterable[T](iterable: Iterable[T]): JIterable[T] = asJavaIterable(iterable)
+
+  @Converter
+  def toScalaIterable[T](iterable: JIterable[T]): Iterable[T] = iterable
+
+  @Converter
+  def toJavaCollection[T](iterable: Iterable[T]): JCollection[T] = iterable
+
+  @Converter
+  def toJavaCollection[T](list: List[T]): JCollection[T] = list
+
+  @Converter
+  def toScalaIterable[T](collection: JCollection[T]): Iterable[T] = collection
+
+  @Converter
+  def toScalaList[T](collection: JCollection[T]): List[T] = (collection:Iterable[T]).toList
+
+  @Converter
+  def toJavaSet[T](set: Set[T]): JSet[T] = set
+
+  @Converter
+  def toJavaMap[A,B](map: Map[A,B]): JMap[A,B] = map
+  
+  @Converter
+  def toJavaList[T](list: List[T]): JList[T] = list
+
+  @Converter
+  def toScalaList[T](list: JList[T]): List[T] = (list:Iterable[T]).toList
+
+  @Converter
+  def toJavaList[T](seq: Seq[T]): JList[T] = seq
+}
+
+@Converter
+class ScalaMutableCollections {
+  import collection.mutable.{Buffer, Map, Seq, Set}
+  import collection.concurrent.{Map => ConcurrentMap}
+  import java.util.{Dictionary => JDictionary, List => JList, Map => JMap, Set =>
JSet}
+  import java.util.concurrent.{ConcurrentMap => JConcurrentMap}
+
+  @Converter
+  def toJavaList[T](buffer: Buffer[T]): JList[T] = buffer
+
+  @Converter
+  def toScalaBuffer[T](list: JList[T]): Buffer[T] = list
+
+  @Converter
+  def toJavaSet[T](set: Set[T]): JSet[T] = set
+
+  @Converter
+  def toScalaSet[T](set: JSet[T]): Set[T] = set
+
+  @Converter
+  def toJavaDictionary[A,B](map: Map[A,B]): JDictionary[A,B] = map
+
+  @Converter
+  def toScalaMap[A,B](dictionary: JDictionary[A,B]): Map[A,B] = dictionary
+
+  @Converter
+  def toJavaMap[A,B](map: Map[A,B]): JMap[A,B] = map
+
+  @Converter
+  def toScalaMap[A,B](map: JMap[A,B]): Map[A,B] = map
+
+  @Converter
+  def toJavaConcurrentMap[A,B](map: ConcurrentMap[A,B]): JConcurrentMap[A,B] = map
+
+  @Converter
+  def toScalaConcurrentMap[A,B](map: JConcurrentMap[A,B]): ConcurrentMap[A,B] = map
+
+  @Converter
+  def toJavaList[T](seq: Seq[T]): JList[T] = seq
+}

Added: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala?rev=1460259&view=auto
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
(added)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaOption.scala
Sun Mar 24 03:31:11 2013
@@ -0,0 +1,51 @@
+/**
+ * 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.camel.scala.converter
+
+import org.apache.camel.Converter
+import collection.JavaConversions._
+
+@Converter
+class ScalaOption {
+  import collection.{Iterator, Iterable, Seq}
+  import java.lang.{Iterable => JIterable}
+  import java.util.{Collection => JCollection, Iterator => JIterator, List => JList}
+
+  @Converter
+  def toList[T](opt: Option[T]): List[T] = opt.toList
+
+  @Converter
+  def toIterator[T](opt: Option[T]): Iterator[T] = opt.iterator
+
+  @Converter
+  def toIterable[T](opt: Option[T]): Iterable[T] = opt.toList
+
+  @Converter
+  def toSeq[T](opt: Option[T]): Seq[T] = opt.toList
+
+  @Converter
+  def toJavaIterator[T](opt: Option[T]): JIterator[T] = opt.iterator
+
+  @Converter
+  def toJavaIterable[T](opt: Option[T]): JIterable[T] = opt.toList
+
+  @Converter
+  def toJavaList[T](opt: Option[T]): JList[T] = opt.toList
+
+  @Converter
+  def toJavaCollection[T](opt: Option[T]): JCollection[T] = opt.toList
+}

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaTypeConverter.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaTypeConverter.scala?rev=1460259&r1=1460258&r2=1460259&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaTypeConverter.scala
(original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/converter/ScalaTypeConverter.scala
Sun Mar 24 03:31:11 2013
@@ -32,6 +32,12 @@ import org.apache.camel.{Exchange, Conve
 object ScalaTypeConverter {
 
    @Converter
+   def toString(symbol: Symbol): String = symbol.name
+
+   @Converter
+   def toSymbol(string: String): Symbol = Symbol(string)
+
+   @Converter
    def convertToDocument(xml: Elem, exchange : Exchange) : Document = {
      exchange.getContext.getTypeConverter.convertTo(classOf[Document], exchange, xml.toString())
    }

Added: camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelSpec.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelSpec.scala?rev=1460259&view=auto
==============================================================================
--- camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelSpec.scala
(added)
+++ camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelSpec.scala
Sun Mar 24 03:31:11 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.camel.scala
+
+import org.scalatest.Suite
+import org.scalatest.BeforeAndAfterAll
+
+trait CamelSpec extends BeforeAndAfterAll { self: Suite =>
+  import org.apache.camel.CamelContext
+  import org.apache.camel.impl.{DefaultCamelContext, DefaultExchange}
+  import org.apache.camel.util.ServiceHelper
+  
+  protected var context: CamelContext = _
+
+  override def beforeAll {
+    super.beforeAll
+
+    context = new DefaultCamelContext
+    ServiceHelper.startService(context)
+  }
+
+  override def afterAll {
+    ServiceHelper.stopService(context)
+    super.afterAll
+  }
+
+  protected def createExchange = new DefaultExchange(context)
+}

Added: camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/converter/ConverterSpec.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/converter/ConverterSpec.scala?rev=1460259&view=auto
==============================================================================
--- camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/converter/ConverterSpec.scala
(added)
+++ camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/converter/ConverterSpec.scala
Sun Mar 24 03:31:11 2013
@@ -0,0 +1,164 @@
+/**
+ * Copyright (C) 2011 Osinka <http://osinka.ru>
+ *
+ * Licensed 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.camel.scala.converter
+
+import org.apache.camel.scala.CamelSpec
+import org.scalatest.FunSpec
+import org.scalatest.matchers.MustMatchers
+import org.scalatest.junit.JUnitRunner
+import org.junit.runner.RunWith
+
+import reflect.Manifest
+
+@RunWith(classOf[JUnitRunner])
+class ConverterSpec extends FunSpec with CamelSpec with MustMatchers {
+  describe("types converter") {
+    it("must convert symbol to string") {
+      to[String]('Test) must equal(Some("Test"))
+    }
+    it("must convert string to symbol") {
+      to[Symbol]("Test") must equal(Some('Test))
+    }
+  }
+
+  describe("immutable collections converter") {
+    import collection.{Iterator, Iterable, Map, Seq, Set}
+    import java.lang.{Iterable => JIterable}
+    import java.util.{Collection => JCollection, Collections, Iterator => JIterator,
Enumeration, List => JList, Map => JMap, Set => JSet}
+
+    it("must convert scala iterators") {
+      val it = Iterator.single(1)
+      to[JIterator[Int]](it) must be('defined)
+      to[Enumeration[Int]](it) must be('defined)
+    }
+    it("must convert java iterators") {
+      val l = Collections.singletonList(1)
+      to[Iterator[Int]](l.iterator) must be('defined)
+      to[Iterator[Int]](Collections.enumeration(l)) must be('defined)
+    }
+    it("must convert scala iterable") {
+      val it = Iterable(1)
+      to[JIterable[Int]](it) must be('defined)
+      to[JCollection[Int]](it) must be('defined)
+    }
+    it("must convert java list -> scala list") {
+      to[List[Int]]( Collections.singletonList(2) ) must be('defined)
+      to[List[Int]]( Collections.emptyList ) must equal(Some(Nil))
+    }
+    it("must convert scala list -> java list") {
+      to[JList[Int]]( 2 :: Nil) must be('defined)
+      to[JList[Int]]( Nil ) must be('defined)
+    }
+    it("must convert java collection") {
+      val it = Collections.singletonList(1)
+      to[Iterable[Int]](it) must be('defined)
+    }
+    it("must convert scala set") {
+      val s = Set(1)
+      to[JSet[Int]](s) must be('defined)
+    }
+    it("must convert java set") {
+      val s = Collections.singleton(1)
+      to[Set[Int]](s) must (be('defined) and equal(Some(Set(1))))
+    }
+    it("must convert scala seq") {
+      val s = Seq(1)
+      to[JList[Int]](s) must be('defined)
+    }
+    it("must convert scala map") {
+      val m = Map("a" -> 1)
+      to[JMap[String, Int]](m) must be('defined)
+    }
+    it("must convert java map") {
+      val m = Collections.singletonMap("a", 1)
+      to[Map[String, Int]](m) must (be('defined) and equal(Some(Map("a" -> 1))))
+    }
+  }
+
+  describe("mustable collections converter") {
+    import collection.mutable.{ConcurrentMap, Buffer, Map, Seq, Set}
+    import java.util.{Dictionary => JDictionary, Hashtable => JHashtable, Collections,
List => JList, Map => JMap, Set => JSet}
+    import java.util.concurrent.{ConcurrentHashMap => JConcurrentHashMap}
+
+    it("must convert scala buffer") {
+      val b = Buffer(1)
+      to[JList[Int]](b) must be('defined)
+    }
+    it("must convert java buffer") {
+      val b = Collections.singletonList(1)
+      to[Buffer[Int]](b) must be('defined)
+    }
+    it("must convert scala set") {
+      val s = Set(1)
+      to[JSet[Int]](s) must be('defined)
+    }
+    it("must convert java set") {
+      val s = Collections.singleton(1)
+      to[Set[Int]](s) must be('defined)
+    }
+    it("must convert scala map") {
+      val m = Map("a" -> 1)
+      to[JDictionary[String,Int]](m) must be('defined)
+      to[JMap[String,Int]](m) must be('defined)
+    }
+    it("must convert java map") {
+      val m = Collections.singletonMap("a", 1)
+      to[Map[String,Int]](m) must be('defined)
+    }
+    it("must convert java dictionary") {
+      val d = new JHashtable[String,Int](Collections.singletonMap("a", 1))
+      to[Map[String,Int]](d) must be('defined)
+    }
+    it("must convert java concurrent map") {
+      val m = new JConcurrentHashMap[String,Int](Collections.singletonMap("a", 1))
+      to[ConcurrentMap[String,Int]](m) must be('defined)
+    }
+    it("must convert scala seq") {
+      val s = Seq(1)
+      to[JList[Int]](s) must be('defined)
+    }
+  }
+
+  describe("option converter") {
+    import collection.{Iterator, Iterable}
+    import java.lang.{Iterable => JIterable}
+    import java.util.{Collection => JCollection, Iterator => JIterator, List =>
JList}
+
+    it("must convert option -> list") {
+      to[List[Int]]( Some(2) ) must be('defined)
+    }
+    it("must convert option -> iterable") {
+      to[Iterable[Int]]( Some(2) ) must be('defined)
+    }
+    it("must convert option -> iterator") {
+      to[Iterator[Int]]( Some(2) ) must be('defined)
+    }
+    it("must convert option -> jlist") {
+      to[JList[Int]]( Some(2) ) must be('defined)
+    }
+    it("must convert option -> jcollection") {
+      to[JCollection[Int]]( Some(2) ) must be('defined)
+    }
+    it("must convert option -> jiterator") {
+      to[JIterator[Int]]( Some(2) ) must be('defined)
+    }
+    it("must convert option -> jiterable") {
+      to[JIterable[Int]]( Some(2) ) must be('defined)
+    }
+  }
+
+  private def to[T](x: AnyRef)(implicit m: Manifest[T]): Option[T] = Option( context.getTypeConverter.mandatoryConvertTo(m.runtimeClass,
x).asInstanceOf[T] )
+}



Mime
View raw message