Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.38 KB | None | 0 0
  1. package tests
  2.  
  3. import scala.collection.mutable.Map
  4. import org.junit.runner.RunWith
  5. import org.scalatest.junit.JUnitRunner
  6. import org.scalatest.FlatSpec
  7. import org.scalatest.Matchers
  8. import org.scalamock.scalatest.MockFactory
  9. import auction._
  10. import auction.event._
  11. import auction.bots.BidBot
  12. import auction.event.MessagingEvent
  13.  
  14. /**
  15.  * Tests for BidBot
  16.  */
  17. @RunWith(classOf[JUnitRunner])
  18. class BotTest extends FlatSpec with Matchers with MockFactory {
  19.  
  20.   // Tämä luokka on avuksi harjoituksessa ja sitä ei tarvitse muuttaa:
  21.   /**
  22.    * A mock version of Auction to record outgoing bids.
  23.    */
  24.   class MockAuction extends Auction with Dispatcher {
  25.     val makeBidRecorder = mockFunction[User, AuctionItem, Int, Boolean]('makeBid)
  26.  
  27.    override def makeBid(who: User, item: AuctionItem, amount: Int) = {
  28.      // We use mockedMakeBid to record calls, but eventually call
  29.      // the actual method. This kind of a method double is a _spy_.
  30.      makeBidRecorder(who, item, amount)
  31.      super.makeBid(who, item, amount)
  32.    }
  33.  }
  34.  
  35.  trait MockedDispatcher extends Dispatcher {
  36.    val mf = mockFunction[AuctionEvent, Unit]('fireEvent)
  37.  
  38.     override def fireEvent(e: AuctionEvent) = {
  39.       mf(e)
  40.       super.fireEvent(e)
  41.     }
  42.   }
  43.  
  44.   "BidBot" should "make a bid if the owner has money and someone else makes a new best bid" in {
  45.     val auction = new MockAuction()
  46.     // Vinkki: Käytä testeissä auction.makeBidRecorder funktiota asettamalla sille
  47.     // AuctionTest luokan tapaan odotuksia (expects) ennen kuin aloitat huutamisen.
  48.  
  49.     val james = new User("James", 123)
  50.     val pete = new User("Pete", 456)
  51.     val jane = new User("Jane", 789)
  52.     val bot = new BidBot(pete, auction)
  53.  
  54.     //create the item
  55.     val sofa = auction.addAuction(james, "blue sofa", 100, 5)
  56.    
  57.     //make bot follow the item
  58.     bot.bidOnItem(sofa, 1000, 100)
  59.    
  60.     //add credits
  61.     pete.buyCredits(1)
  62.    
  63.  
  64.     // Tell the mock what to expect.
  65.     inSequence {
  66.       auction.makeBidRecorder.expects(jane, sofa, 300)
  67.       auction.makeBidRecorder.expects(pete, sofa, 400)
  68.       auction.makeBidRecorder.expects(jane, sofa, 500)
  69.     }
  70.  
  71.     // Bid enough that it succeeds.
  72.     auction.makeBid(jane, sofa, 300)
  73.     auction.makeBid(jane, sofa, 500)
  74.   }
  75.  
  76.   "BidBot" should "not bid if the owner has the best bid" in {
  77.     val auction = new MockAuction()
  78.  
  79.     val james = new User("James", 123)
  80.     val pete = new User("Pete", 456)
  81.     val jane = new User("Jane", 789)
  82.     val bot = new BidBot(pete, auction)
  83.  
  84.     val sofa = auction.addAuction(james, "blue sofa", 100, 5)
  85.     bot.bidOnItem(sofa, 1000, 100)
  86.    
  87.     //add credits
  88.     pete.buyCredits(10)
  89.    
  90.  
  91.     // Tell the mock what to expect.
  92.     inSequence {
  93.       auction.makeBidRecorder.expects(jane, sofa, 300)
  94.       auction.makeBidRecorder.expects(pete, sofa, 400)
  95.       auction.makeBidRecorder.expects(pete, sofa, 500)
  96.     }
  97.  
  98.     // Bid enough that it succeeds.
  99.     auction.makeBid(jane, sofa, 300)
  100.     auction.makeBid(pete, sofa, 500)
  101.   }
  102.  
  103.   "BidBot" should "not bid over the limit" in {
  104.     val auction = new MockAuction()
  105.  
  106.     val james = new User("James", 123)
  107.     val pete = new User("Pete", 456)
  108.     val jane = new User("Jane", 789)
  109.     val bot = new BidBot(pete, auction)
  110.  
  111.     val sofa = auction.addAuction(james, "blue sofa", 100, 5)
  112.     bot.bidOnItem(sofa, 1000, 100)
  113.    
  114.     //add credits
  115.     pete.buyCredits(1)
  116.    
  117.  
  118.     // Tell the mock what to expect.
  119.     inSequence {
  120.       auction.makeBidRecorder.expects(jane, sofa, 1100)
  121.     }
  122.  
  123.     // Bid enough that it succeeds.
  124.     auction.makeBid(jane, sofa, 1100)
  125.   }
  126.  
  127.   "BidBot" should "not bid if the owner has no money" in {
  128.     val auction = new MockAuction()
  129.  
  130.     val james = new User("James", 123)
  131.     val pete = new User("Pete", 456)
  132.     val jane = new User("Jane", 789)
  133.     val bot = new BidBot(pete, auction)
  134.  
  135.     //create the item
  136.     val sofa = auction.addAuction(james, "blue sofa", 100, 5)
  137.    
  138.     //make bot follow the item
  139.     bot.bidOnItem(sofa, 1000, 100)
  140.    
  141.  
  142.     // Tell the mock what to expect.
  143.     inSequence {
  144.       auction.makeBidRecorder.expects(jane, sofa, 300)
  145.     }
  146.  
  147.     // Bid enough that it succeeds.
  148.     auction.makeBid(jane, sofa, 300)
  149.   }
  150.  
  151.   "BidBot" should "ask for more money after using the last credit" in {
  152.     val auction = new Auction with MockedDispatcher
  153.     // Vinkki: Voit joko kirjoittaa oman kuuntelijan dispatcherille tai korvata
  154.     // dispatcherin kuten luokassa AuctionTest.
  155.  
  156.     auction.mf expects (where { x: AuctionEvent => x.isInstanceOf[NewAuction] })
  157.     val james = new User("James", 123)
  158.     val pete = new User("Pete", 456)
  159.     val jane = new User("Jane", 789)
  160.     val bot = new BidBot(pete, auction)
  161.  
  162.     //create the item
  163.     val sofa = auction.addAuction(james, "blue sofa", 100, 5)
  164.    
  165.     //make bot follow the item
  166.     bot.bidOnItem(sofa, 1000, 100)
  167.    
  168.     //add credits
  169.     pete.buyCredits(1)
  170.    
  171.  
  172.     // Tell the mock what to expect.
  173.     auction.mf expects (where { x: AuctionEvent => x.isInstanceOf[BidRaised] }) repeat (1 to 10)
  174.     auction.mf expects (where { x: AuctionEvent => x.isInstanceOf[BidBeaten] })repeat (1 to 10)
  175.     auction.mf expects (where { x: AuctionEvent => x match {
  176.          case m:MessagingEvent => m.receiver == pete
  177.          case _ => false
  178.       }})
  179.  
  180.  
  181.     // Bid enough that it succeeds.
  182.     auction.makeBid(jane, sofa, 300)
  183.   }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement