Skip to content

Commit

Permalink
Fix bug in ValueMapper ChannelBuffer conversion, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franklin-stripe committed Feb 27, 2015
1 parent 0f6c684 commit 42ee798
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ object MySqlValue {
case v: Int => new MySqlValue(IntValue(v))
case v: Long => new MySqlValue(LongValue(v))
case v: Short => new MySqlValue(ShortValue(v))
case v: ChannelBuffer => new MySqlValue(RawValue(Type.String, Charset.Utf8_general_ci, false, v.toString(UTF_8).getBytes))
case v: ChannelBuffer =>
val bytes = Array.ofDim[Byte](v.readableBytes)
v.markReaderIndex()
v.readBytes(bytes)
v.resetReaderIndex()
new MySqlValue(RawValue(Type.Blob, Charset.Binary, isBinary = true, bytes))
case _ => throw new UnsupportedOperationException(v.getClass.getName + " is currently not supported.")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2013 Twitter Inc.
*
* 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 com.twitter.storehaus.mysql

import java.util.Arrays

import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer}
import org.scalacheck.{Gen, Arbitrary, Properties}
import org.scalacheck.Prop.forAll

object MySqlValueInjectionProperties extends Properties("MySqlValue Injections") {

val channelBufferGenerator = for {
b <- Gen.containerOf[Array, Byte](Arbitrary.arbitrary[Byte])
} yield ChannelBuffers.wrappedBuffer(b)

implicit val gen = Arbitrary(channelBufferGenerator)

property("String2MySqlValue apply and invert") = {
val injection = String2MySqlValueInjection
forAll { (s: String) =>
injection.invert(injection(s)).toOption == Some(s)
}
}

property("ChannelBuffer2MySqlValue apply and invert") = {
val injection = ChannelBuffer2MySqlValueInjection

forAll { (c: ChannelBuffer) =>
val bytes = bytesFromChannelBuffer(c)
val inverted = injection.invert(injection(c)).toOption
inverted.exists { invertedBuf =>
val invertedBytes = bytesFromChannelBuffer(invertedBuf)
Arrays.equals(invertedBytes, bytes)
}
}
}

def bytesFromChannelBuffer(c: ChannelBuffer): Array[Byte] = {
val arr = Array.ofDim[Byte](c.readableBytes)
c.markReaderIndex()
c.readBytes(arr)
c.resetReaderIndex()
arr
}
}

0 comments on commit 42ee798

Please sign in to comment.