Skip to content

Commit

Permalink
Merge branch 'release/0.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Nievelt committed May 8, 2015
2 parents d8f7f60 + cf205bd commit 46e6fc1
Show file tree
Hide file tree
Showing 31 changed files with 411 additions and 152 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Bijection #

### 0.8.0
* add twitter util Buf <-> Array[Byte] bijection https://github.com/twitter/bijection/pull/208
* upgrade testing libraries https://github.com/twitter/bijection/pull/207
* Add implicit conversion for GZippedBase64String to String https://github.com/twitter/bijection/pull/203
* Make property tests work again https://github.com/twitter/bijection/pull/198
* Add a module for finagle-mysql bijections. https://github.com/twitter/bijection/pull/197
* Build bijection-scrooge for scala 2.11 https://github.com/twitter/bijection/pull/196

### 0.7.2
* FIX: gzip Bijection resource leak. https://github.com/twitter/bijection/pull/193
* Use new Travis CI infrastructure https://github.com/twitter/bijection/pull/191
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ string2Long2Bytes2B64: com.twitter.bijection.Bijection[String,com.twitter.biject
scala> 243L.as[Base64String]
res0: com.twitter.bijection.Base64String = Base64String(MjQz)

scala> long2String2Bytes2B64.invert(res5)
scala> long2String2Bytes2B64.invert(res0)
res1: Try[Long] = Success(243)
```

Expand Down Expand Up @@ -134,7 +134,7 @@ Discussion occurs primarily on the [Bijection mailing list](https://groups.googl

## Maven

Bijection modules are available on maven central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.7.2`.
Bijection modules are available on maven central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.8.0`.

Current published artifacts are

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ limitations under the License.
*/
package com.twitter.bijection.avro

import com.twitter.bijection.{ BaseProperties, Injection }

import org.scalatest.PropSpec
import org.scalatest.prop.PropertyChecks

import org.apache.avro.generic.{ GenericRecord, GenericData }
import com.twitter.bijection.{ BaseProperties, CheckProperties, Injection }
import org.apache.avro.Schema
import org.apache.avro.generic.{ GenericData, GenericRecord }

/**
* @author Muhammad Ashraf
* @since 7/5/13
*/
class GenericAvroCodecLaws extends PropSpec with PropertyChecks with BaseProperties {
class GenericAvroCodecLaws extends CheckProperties with BaseProperties {
val testSchema = new Schema.Parser().parse("""{
"type":"record",
"name":"FiscalRecord",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.apache.avro.generic.{ GenericData, GenericRecord }
* @author Muhammad Ashraf
* @since 7/6/13
*/
object GenericAvroCodecsSpecification extends WordSpec with Matchers with BaseProperties {
class GenericAvroCodecsSpecification extends WordSpec with Matchers with BaseProperties {
val testSchema = new Schema.Parser().parse("""{
"type":"record",
"name":"FiscalRecord",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.twitter.bijection.avro

import org.scalatest.{ PropSpec, MustMatchers }
import org.scalatest.prop.PropertyChecks
import com.twitter.bijection.{ Injection, BaseProperties }
import org.apache.avro.Schema
import avro.FiscalRecord
import com.twitter.bijection.{ BaseProperties, CheckProperties, Injection }
import org.apache.avro.Schema

/**
* @author Muhammad Ashraf
* @since 10/5/13
*/
class SpecificAvroCodecLaws extends PropSpec with PropertyChecks with MustMatchers with BaseProperties {
class SpecificAvroCodecLaws extends CheckProperties with BaseProperties {
val testSchema = new Schema.Parser().parse("""{
"type":"record",
"name":"FiscalRecord",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import avro.FiscalRecord
* @author Muhammad Ashraf
* @since 10/5/13
*/
object SpecificAvroCodecsSpecification extends WordSpec with Matchers with BaseProperties {
class SpecificAvroCodecsSpecification extends WordSpec with Matchers with BaseProperties {
val testSchema = new Schema.Parser().parse("""{
"type":"record",
"name":"FiscalRecord",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ case class GZippedBytes(bytes: Array[Byte]) extends AnyVal {

case class GZippedBase64String(str: String) extends AnyVal

object GZippedBase64String {
implicit val unwrap: Injection[GZippedBase64String, String] =
new AbstractInjection[GZippedBase64String, String] {
override def apply(gzbs: GZippedBase64String) = gzbs.str
override def invert(str: String) = attemptWhen(str)(Base64.isBase64)(GZippedBase64String(_))
}
}

case class Base64String(str: String) extends AnyVal

object Base64String {
implicit val unwrap: Injection[Base64String, String] =
new AbstractInjection[Base64String, String] {
override def apply(bs: Base64String) = bs.str
override def invert(str: String) = attemptWhen(str)(Base64.isBase64(_))(Base64String(_))
override def invert(str: String) = attemptWhen(str)(Base64.isBase64)(Base64String(_))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ limitations under the License.
package com.twitter.bijection

import java.nio.ByteBuffer
import org.scalatest.{ PropSpec, MustMatchers }
import org.scalatest.prop.PropertyChecks

import org.scalacheck.Prop._
import com.twitter.bijection.codec.Base64
import org.scalacheck.Arbitrary
import org.scalatest.MustMatchers

class BinaryBijectionLaws extends PropSpec with PropertyChecks with MustMatchers
class BinaryBijectionLaws extends CheckProperties with MustMatchers
with BaseProperties {
implicit val arbBB = arbitraryViaFn[Array[Byte], ByteBuffer] { ByteBuffer.wrap(_) }
implicit val arbBB = arbitraryViaFn[Array[Byte], ByteBuffer] { ByteBuffer.wrap }

// TODO: These are all bijections,
property("Array[Byte] <=> ByteBuffer") {
Expand All @@ -40,10 +40,24 @@ class BinaryBijectionLaws extends PropSpec with PropertyChecks with MustMatchers
isInjective[Array[Byte], Base64String]
}

property("Base64String -> String") {
implicit val arbB64: Arbitrary[Base64String] = arbitraryViaFn { s: String =>
Base64String(Base64.encodeBase64String(s.getBytes("UTF-8")))
}
isInjection[Base64String, String]
}

property("rts Array[Byte] -> GZippedBase64String") {
isInjective[Array[Byte], GZippedBase64String]
}

property("GZippedBase64String -> String") {
implicit val arbGzB64: Arbitrary[GZippedBase64String] = arbitraryViaFn { s: String =>
GZippedBase64String(Base64.encodeBase64String(s.getBytes("UTF-8")))
}
isInjection[GZippedBase64String, String]
}

implicit val optSer = JavaSerializationInjection[Option[Int]]

property("java serialize Option[Int]") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,9 @@ limitations under the License.

package com.twitter.bijection

import java.lang.{
Short => JShort,
Integer => JInt,
Long => JLong,
Float => JFloat,
Double => JDouble,
Byte => JByte
}

import java.lang.{ Byte => JByte, Double => JDouble, Float => JFloat, Integer => JInt, Long => JLong, Short => JShort }
import java.nio.ByteBuffer

import org.scalatest.{ PropSpec, MustMatchers }
import org.scalatest.prop.PropertyChecks

import org.scalacheck.Arbitrary
import org.scalacheck.Prop.forAll

Expand All @@ -51,23 +40,24 @@ trait BaseBufferable {
}
}

class BufferableLaws extends PropSpec with PropertyChecks with MustMatchers with BaseBufferable {
class BufferableLaws extends CheckProperties with BaseBufferable {

property("Reallocate works properly") {
forAll { (bytes: Array[Byte]) =>
val bb = ByteBuffer.wrap(bytes)
bb.position(bytes.size)
val newBb = Bufferable.reallocate(bb)
assert(Bufferable.getBytes(bb).toList == Bufferable.getBytes(newBb).toList)
assert(newBb.capacity > bb.capacity)
assert(newBb.position == bb.position)
Bufferable.getBytes(bb).toList == Bufferable.getBytes(newBb).toList &&
newBb.capacity > bb.capacity &&
newBb.position == bb.position
}
}

property("getBytes works") {
forAll { (bytes: Array[Byte]) =>
val bb = ByteBuffer.wrap(bytes)
bb.position(bytes.size)
assert(Bufferable.getBytes(bb).toList == bytes.toList)
Bufferable.getBytes(bb).toList == bytes.toList
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.twitter.bijection

import org.scalatest.PropSpec
import org.scalatest.prop.Checkers

/**
* @author Mansur Ashraf.
*/
trait CheckProperties extends PropSpec with Checkers {

def property(testName: scala.Predef.String, testTags: org.scalatest.Tag*)(testFun: org.scalacheck.Prop): scala.Unit =
super.property(testName, testTags: _*) { check { testFun } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ limitations under the License.

package com.twitter.bijection

import org.scalatest.{ PropSpec, MustMatchers }
import org.scalatest.prop.PropertyChecks

import org.scalacheck.Gen._
import org.scalacheck.Arbitrary
import org.scalacheck.Prop._
import org.scalatest.MustMatchers

class CollectionLaws extends PropSpec with PropertyChecks with MustMatchers
with BaseProperties {
import StringArbs._
class CollectionLaws extends CheckProperties with BaseProperties {
import com.twitter.bijection.StringArbs._

implicit def vectorArb[A](implicit la: Arbitrary[List[A]]) =
arbitraryViaFn { (l: List[A]) => Vector(l: _*) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.scalatest.prop.PropertyChecks

import Conversion.asMethod // get the .as syntax

class EnglishIntLaws extends PropSpec with PropertyChecks with MustMatchers
class EnglishIntLaws extends CheckProperties with MustMatchers
with BaseProperties {
var ct = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ limitations under the License.

package com.twitter.bijection

import org.scalatest.{ PropSpec, MustMatchers }
import org.scalatest.prop.PropertyChecks
import java.net.URL
import java.util.UUID

import org.scalacheck.Gen._
import org.scalacheck.Arbitrary
import org.scalacheck.Gen._
import org.scalacheck.Prop._

import java.util.UUID
import java.net.URL
import scala.util.Try

object StringArbs extends BaseProperties {
import Rep._

implicit val strByte = arbitraryViaBijection[Byte, String @@ Rep[Byte]]
implicit val strShort = arbitraryViaBijection[Short, String @@ Rep[Short]]
Expand All @@ -37,9 +35,7 @@ object StringArbs extends BaseProperties {
implicit val strDouble = arbitraryViaBijection[Double, String @@ Rep[Double]]
}

class StringBijectionLaws extends PropSpec with PropertyChecks with MustMatchers
with BaseProperties {
import StringArbs._
class StringBijectionLaws extends CheckProperties with BaseProperties {

property("round trips string -> Array[String]") {
isLooseInjection[String, Array[Byte]]
Expand All @@ -65,17 +61,10 @@ class StringBijectionLaws extends PropSpec with PropertyChecks with MustMatchers
// isBijection[UUID, String @@ Rep[UUID]]()
// }

def toUrl(s: String): Option[URL] =
try { Some(new URL("http://" + s + ".com")) }
catch { case _: Throwable => None }
def toUrl(s: String): Try[URL] = Try(new URL("http://" + s + ".com"))

implicit val urlArb = Arbitrary {
implicitly[Arbitrary[String]]
.arbitrary
.map { toUrl(_) }
.filter { _.isDefined }
.map { _.get }
}
implicit val urlArb: Arbitrary[URL] =
Arbitrary { Arbitrary.arbitrary[String] map (toUrl(_)) suchThat (_.isSuccess) map (_.get) }

// This is trivially a bijection if it injective
property("URL -> String") {
Expand All @@ -86,9 +75,7 @@ class StringBijectionLaws extends PropSpec with PropertyChecks with MustMatchers
forAll { (sep: String, xs: List[String]) =>
val sjBij = StringJoinBijection(sep)
val iter = xs.toIterable
whenever(!iter.exists(_.contains(sep))) {
assert(iter == rt(iter)(sjBij))
}
(!iter.exists(_.contains(sep))) ==> (iter == rt(iter)(sjBij))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import java.lang.{
import org.scalatest.{ PropSpec, MustMatchers }
import org.scalatest.prop.PropertyChecks

class TupleBijectionLaws extends PropSpec with PropertyChecks with MustMatchers
with BaseProperties {
class TupleBijectionLaws extends CheckProperties with BaseProperties {
import StringArbs._

property("round trips (Int,Long) -> (String,String)") {
Expand Down
Loading

0 comments on commit 46e6fc1

Please sign in to comment.