Require device ID to have correct format using regex.

This commit is contained in:
Felix Ableitner 2014-11-06 17:53:43 +02:00
parent 5e26fcce75
commit c1c4239499
3 changed files with 13 additions and 7 deletions

View file

@ -7,6 +7,7 @@ import android.database.DatabaseErrorHandler
import android.database.sqlite.SQLiteDatabase
import android.test.AndroidTestCase
import android.test.mock.MockContext
import android.util.Log
import com.nutomic.ensichat.bluetooth.Device
import com.nutomic.ensichat.messages.MessageTest._
import junit.framework.Assert._
@ -38,20 +39,20 @@ class MessageStoreTest extends AndroidTestCase {
}
def testCount(): Unit = {
val msg1 = MessageStore.getMessages(new Device.ID("one"), 1)
val msg1 = MessageStore.getMessages(m1.sender, 1)
assertEquals(1, msg1.size)
val msg2 = MessageStore.getMessages(new Device.ID("one"), 3)
val msg2 = MessageStore.getMessages(m1.sender, 3)
assertEquals(2, msg2.size)
}
def testOrder(): Unit = {
val msg = MessageStore.getMessages(new Device.ID("two"), 1)
val msg = MessageStore.getMessages(m1.receiver, 1)
assertTrue(msg.contains(m3))
}
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(m3))
}

View file

@ -12,13 +12,13 @@ import scala.collection.immutable.TreeSet
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")
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")
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")

View file

@ -10,12 +10,17 @@ object Device {
* @param Id A bluetooth device address.
*/
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 equals(a: Any) = a match {
case other: Device.ID => Id == other.Id
case _ => false
}
override def toString = Id
}
}