https://www.mediawiki.org/wiki/Special:Code/MediaWiki/109862

Revision: 109862
Author:   oren
Date:     2012-01-23 22:42:16 +0000 (Mon, 23 Jan 2012)
Log Message:
-----------
converted testing code from main to unit tests

Modified Paths:
--------------
    
trunk/lucene-search-3/src/test/java/org/wikimedia/lsearch/util/QueryStringMapTest.java

Modified: 
trunk/lucene-search-3/src/test/java/org/wikimedia/lsearch/util/QueryStringMapTest.java
===================================================================
--- 
trunk/lucene-search-3/src/test/java/org/wikimedia/lsearch/util/QueryStringMapTest.java
      2012-01-23 22:39:43 UTC (rev 109861)
+++ 
trunk/lucene-search-3/src/test/java/org/wikimedia/lsearch/util/QueryStringMapTest.java
      2012-01-23 22:42:16 UTC (rev 109862)
@@ -3,6 +3,8 @@
  */
 package org.wikimedia.lsearch.util;
 
+import static org.junit.Assert.assertEquals;
+
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Iterator;
@@ -17,38 +19,167 @@
 public class QueryStringMapTest {
 
        @Test
-       public void Test1() throws URISyntaxException {
+       public void JustAPath() throws URISyntaxException {
+               
+               String uri="/x";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals(0, map.size());
+       }
+       
+       @Test
+       public void SimpleQuery01() throws URISyntaxException {
+               
+               String uri="/x?foo=bar";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals(1, map.size());
+               assertEquals("bar", map.get("foo"));
 
-               // TODO convert main below to Junit Tests
+       }
 
+       @Test
+       public void SimpleQuery02() throws URISyntaxException {
+
+               String uri = "/x?foo=bar&biz=bax";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals(2, map.size());
+               assertEquals("bar", map.get("foo"));
+               assertEquals("bax", map.get("biz"));
+
        }
 
-       public static void main(String[] args) {
+       /**
+        * The %26 should _not_ split 'foo' from 'bogo'
+        * 
+        * @throws URISyntaxException
+        */
+       @Test
+       public void SpacedParamQuery01() throws URISyntaxException {
 
-               try {
-                       testURI("/x");
-                       testURI("/x?foo=bar");
-                       testURI("/x?foo=bar&biz=bax");
+               String uri = "/x?foo=bar+%26bogo&next=extreme";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 2, map.size());
+               assertEquals("bar &bogo", map.get("foo"));
+               assertEquals("extreme", map.get("next"));
 
-                       // The %26 should _not_ split 'foo' from 'bogo'
-                       testURI("/x?foo=bar+%26bogo&next=extreme");
+       }
 
-                       // UTF-8 good encoding
-                       testURI("/x?serveuse=%c3%a9nid");
+       /**
+        * UTF-8 good encoding
+        * 
+        * @throws URISyntaxException
+        */
+       @Test
+       public void UTF8EncodingQuery01() throws URISyntaxException {
 
-                       // bad encoding; you'll see replacement char
-                       testURI("/x?serveuse=%e9nid");
+               String uri = "/x?serveuse=%c3%a9nid";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 1, map.size());
+               assertEquals("énid", map.get("serveuse"));
+       }
 
-                       // corner cases; missing params
-                       testURI("/x?foo");
-                       testURI("/x?foo&bar=baz");
-                       testURI("/x?foo&&bar");
-                       testURI("/x?&");
-                       testURI("/x?=");
-                       testURI("/x?==&");
+       /**
+        * UTF-8 bad encoding
+        * 
+        * @throws URISyntaxException
+        */
+       @Test
+       public void BadUTF8EncodingQuery01() throws URISyntaxException {
 
+               String uri = "/x?serveuse=%e9nid";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 1, map.size());
+               assertEquals("�nid", map.get("serveuse"));
+       }
+
+       /**
+        * missing params
+        * 
+        * @throws URISyntaxException
+        */
+       @Test
+       public void CornerCase01() throws URISyntaxException {
+
+               String uri = "/x?foo";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 1, map.size());
+               assertEquals("", map.get("foo"));
+       }
+
+       @Test
+       public void CornerCase02() throws URISyntaxException {
+
+               String uri = "/x?foo&bar=baz";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 2, map.size());
+               assertEquals("", map.get("foo"));
+               assertEquals("baz", map.get("bar"));
+       }
+
+       @Test
+       public void CornerCase03() throws URISyntaxException {
+
+               String uri = "/x?foo&&bar";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 2, map.size());
+               assertEquals("", map.get("foo"));
+               assertEquals("", map.get("bar"));
+       }
+
+       @Test
+       public void CornerCase04() throws URISyntaxException {
+
+               String uri = "/x?&";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals(0, map.size());
+       }
+
+       @Test
+       public void CornerCase05() throws URISyntaxException {
+
+               String uri = "/x?=";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 0, map.size());
+       }
+
+       @Test
+       public void CornerCase06() throws URISyntaxException {
+
+               String uri = "/x?==&";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals(0, map.size());
+       }
+
+       @Test
+       public void RealQuery01() throws URISyntaxException {
+
+               String uri = 
"/updatePage?db=wikilucene&namespace=6&title=Nick+Gorton5+8+05.jpg";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 3, map.size());
+               assertEquals("wikilucene", map.get("db"));
+               assertEquals("6", map.get("namespace"));
+               assertEquals("Nick Gorton5 8 05.jpg", map.get("title"));
+       }
+       
+       @Test
+       public void RealQuery02() throws URISyntaxException {
+
+               String uri = "/getStatus";
+               QueryStringMap map = new QueryStringMap(new URI(uri));
+               assertEquals((int) 0, map.size());
+       }
+
+       
+       /**
+        * little debugger for query strings.
+        * result is sent to console
+        * 
+        * @param args
+        */
+       public static void main(String[] args) {
+
+               try {
                        
testURI("/updatePage?db=wikilucene&namespace=6&title=Nick+Gorton5+8+05.jpg");
-                       testURI("/getStatus");
+
                } catch (URISyntaxException e) {
                        e.printStackTrace();
                }


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to