2013年7月9日星期二

【Spring】Test in Spring MVC3

   1: package com.lywebii.goldprice;
   2:  
   3: import static org.junit.Assert.*;
   4:  
   5: import java.util.List;
   6:  
   7: import org.junit.After;
   8: import org.junit.Before;
   9: import org.junit.Test;
  10: import org.junit.runner.RunWith;
  11: import org.springframework.beans.factory.annotation.Autowired;
  12: import org.springframework.data.mongodb.core.MongoTemplate;
  13: import org.springframework.data.mongodb.core.geo.Box;
  14: import org.springframework.data.mongodb.core.geo.Point;
  15: import org.springframework.data.mongodb.core.index.GeospatialIndex;
  16: import org.springframework.data.mongodb.core.query.Criteria;
  17: import org.springframework.data.mongodb.core.query.Order;
  18: import org.springframework.data.mongodb.core.query.Query;
  19: import org.springframework.test.context.ContextConfiguration;
  20: import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  21:  
  22: import com.lywebii.goldprice.model.Floating;
  23:  
  24: @RunWith(SpringJUnit4ClassRunner.class)
  25: @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/root-context.xml"})
  26: public class TestFloatingController {
  27:     @Autowired
  28:     MongoTemplate mongoTemplate;
  29:     
  30:     @Before
  31:     public void setUp() throws Exception {
  32:         Floating floating = new Floating();
  33:         floating.setPosition(new double[]{25.0017035,121.5000121});//235台灣新北市中和區中和路37號
  34:         this.mongoTemplate.save(floating);
  35:         
  36:         floating = new Floating();
  37:         floating.setPosition(new double[]{25.0209304,121.4595818}); //220台灣新北市板橋區金華街90-140號
  38:         this.mongoTemplate.save(floating);
  39:         
  40:         floating = new Floating();
  41:         floating.setPosition(new double[]{25.0052171,121.4198273}); //238台灣新北市樹林區中正路214號
  42:         this.mongoTemplate.save(floating);
  43:         
  44:         floating = new Floating();
  45:         floating.setPosition(new double[]{22.6092776,120.316238}); //806台灣高雄市前鎮區南寧街
  46:         this.mongoTemplate.save(floating);
  47:     }
  48:  
  49:     @After
  50:     public void tearDown() throws Exception {
  51:         this.mongoTemplate.dropCollection(Floating.class);
  52:     }
  53:  
  54:     @Test
  55:     public void test() {
  56:         Query query = new Query();
  57:         query.sort().on("date", Order.DESCENDING);
  58:         
  59:  
  60:         this.mongoTemplate.indexOps(Floating.class).ensureIndex(new GeospatialIndex("position"));
  61:  
  62:  
  63:         List<Floating> list = this.mongoTemplate.find(new Query(Criteria.where("position").near(new Point(25.0052171,121.4198273)).maxDistance(5/111.12)),Floating.class);
  64:         System.out.println("Circle:"+list);
  65:  
  66:  
  67:         list = this.mongoTemplate.find(new Query(Criteria.where("position").within(new Box(new Point(0.2, 0.2), new Point(1, 1)))),Floating.class);
  68:         System.out.println("Box:"+list);
  69:  
  70:         
  71:     }
  72:  
  73: }

 


Above code was about usging monogdb template to save postion data and query near point that given.


IOC Configuration file using below code:



   1: @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/root-context.xml"})