Require device ID to have correct format using regex.
This commit is contained in:
parent
5e26fcce75
commit
c1c4239499
3 changed files with 13 additions and 7 deletions
|
@ -7,6 +7,7 @@ import android.database.DatabaseErrorHandler
|
||||||
import android.database.sqlite.SQLiteDatabase
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import android.test.AndroidTestCase
|
import android.test.AndroidTestCase
|
||||||
import android.test.mock.MockContext
|
import android.test.mock.MockContext
|
||||||
|
import android.util.Log
|
||||||
import com.nutomic.ensichat.bluetooth.Device
|
import com.nutomic.ensichat.bluetooth.Device
|
||||||
import com.nutomic.ensichat.messages.MessageTest._
|
import com.nutomic.ensichat.messages.MessageTest._
|
||||||
import junit.framework.Assert._
|
import junit.framework.Assert._
|
||||||
|
@ -38,20 +39,20 @@ class MessageStoreTest extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
def testCount(): Unit = {
|
def testCount(): Unit = {
|
||||||
val msg1 = MessageStore.getMessages(new Device.ID("one"), 1)
|
val msg1 = MessageStore.getMessages(m1.sender, 1)
|
||||||
assertEquals(1, msg1.size)
|
assertEquals(1, msg1.size)
|
||||||
|
|
||||||
val msg2 = MessageStore.getMessages(new Device.ID("one"), 3)
|
val msg2 = MessageStore.getMessages(m1.sender, 3)
|
||||||
assertEquals(2, msg2.size)
|
assertEquals(2, msg2.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
def testOrder(): Unit = {
|
def testOrder(): Unit = {
|
||||||
val msg = MessageStore.getMessages(new Device.ID("two"), 1)
|
val msg = MessageStore.getMessages(m1.receiver, 1)
|
||||||
assertTrue(msg.contains(m3))
|
assertTrue(msg.contains(m3))
|
||||||
}
|
}
|
||||||
|
|
||||||
def testSelect(): Unit = {
|
def testSelect(): Unit = {
|
||||||
val msg = MessageStore.getMessages(new Device.ID("two"), 2)
|
val msg = MessageStore.getMessages(m1.receiver, 2)
|
||||||
assertTrue(msg.contains(m1))
|
assertTrue(msg.contains(m1))
|
||||||
assertTrue(msg.contains(m3))
|
assertTrue(msg.contains(m3))
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,13 @@ import scala.collection.immutable.TreeSet
|
||||||
|
|
||||||
object MessageTest {
|
object MessageTest {
|
||||||
|
|
||||||
val m1 = new TextMessage(new Device.ID("one"), new Device.ID("two"),
|
val m1 = new TextMessage(new Device.ID("AA:AA:AA:AA:AA:AA"), new Device.ID("BB:BB:BB:BB:BB:BB"),
|
||||||
new GregorianCalendar(2014, 10, 29).getTime, "first")
|
new GregorianCalendar(2014, 10, 29).getTime, "first")
|
||||||
|
|
||||||
val m2 = new TextMessage(new Device.ID("one"), new Device.ID("three"),
|
val m2 = new TextMessage(new Device.ID("AA:AA:AA:AA:AA:AA"), new Device.ID("CC:CC:CC:CC:CC:CC"),
|
||||||
new GregorianCalendar(2014, 10, 30).getTime, "second")
|
new GregorianCalendar(2014, 10, 30).getTime, "second")
|
||||||
|
|
||||||
val m3 = new TextMessage(new Device.ID("four"), new Device.ID("two"),
|
val m3 = new TextMessage(new Device.ID("DD:DD:DD:DD:DD:DD"), new Device.ID("BB:BB:BB:BB:BB:BB"),
|
||||||
new GregorianCalendar(2014, 10, 31).getTime, "third")
|
new GregorianCalendar(2014, 10, 31).getTime, "third")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,17 @@ object Device {
|
||||||
* @param Id A bluetooth device address.
|
* @param Id A bluetooth device address.
|
||||||
*/
|
*/
|
||||||
class ID(private val Id: String) {
|
class ID(private val Id: String) {
|
||||||
|
require(Id.matches("([A-Z0-9][A-Z0-9]:){5}[A-Z0-9][A-Z0-9]"), "Invalid device ID format")
|
||||||
|
|
||||||
override def hashCode = Id.hashCode
|
override def hashCode = Id.hashCode
|
||||||
|
|
||||||
override def equals(a: Any) = a match {
|
override def equals(a: Any) = a match {
|
||||||
case other: Device.ID => Id == other.Id
|
case other: Device.ID => Id == other.Id
|
||||||
case _ => false
|
case _ => false
|
||||||
}
|
}
|
||||||
|
|
||||||
override def toString = Id
|
override def toString = Id
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue