Changeset 14039 in josm for trunk/test/unit/org
- Timestamp:
- 2018-07-20T21:54:42+02:00 (6 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/io/ChangesetQueryUrlParserTest.java
r14035 r14039 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.io 3 4 import static groovy.test.GroovyAssert.shouldFail 5 6 import java.time.OffsetDateTime 7 import java.time.ZoneOffset 8 9 import org.junit.Test 10 import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlException 11 import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlParser 12 13 class ChangesetQueryUrlParserTest { 14 15 @Test 16 public void testConstructor() { 17 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 18 } 19 20 @Test 21 public void testParseBasic() { 22 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 23 24 // OK 25 parser.parse "" 2 package org.openstreetmap.josm.io; 3 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertNotNull; 6 import static org.junit.Assert.assertTrue; 7 import static org.junit.Assert.fail; 8 9 import java.time.OffsetDateTime; 10 import java.time.ZoneOffset; 11 import java.util.Arrays; 12 13 import org.junit.Test; 14 import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlException; 15 import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlParser; 16 17 /** 18 * Unit tests of {@link ChangesetQueryUrlParser} class 19 */ 20 public class ChangesetQueryUrlParserTest { 21 22 /** 23 * Basic unit test of {@link ChangesetQueryUrlParser#parse} 24 * @throws ChangesetQueryUrlException never 25 */ 26 @Test 27 public void testParseBasic() throws ChangesetQueryUrlException { 28 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 29 30 // OK 31 parser.parse(""); 26 32 27 33 // should be OK 28 ChangesetQuery q = parser.parse(null) 29 assert q != null34 ChangesetQuery q = parser.parse(null); 35 assertNotNull(q); 30 36 31 37 // should be OK 32 q = parser.parse("") 33 assert q != null 34 } 35 36 @Test 37 public void testUid() { 38 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 39 def ChangesetQuery q 40 41 // OK 42 q = parser.parse("uid=1234") 43 assert q != null 44 45 shouldFail(ChangesetQueryUrlException) { 46 q = parser.parse("uid=0") 38 q = parser.parse(""); 39 assertNotNull(q); 40 } 41 42 private static void shouldFail(String s) { 43 try { 44 new ChangesetQueryUrlParser().parse(s); 45 fail("should throw exception"); 46 } catch (ChangesetQueryUrlException e) { 47 // OK 47 48 } 48 49 shouldFail(ChangesetQueryUrlException) { 50 q = parser.parse("uid=-1") 51 } 52 53 shouldFail(ChangesetQueryUrlException) { 54 q = parser.parse("uid=abc") 55 } 56 } 57 58 @Test 59 public void testDisplayName() { 60 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 61 def ChangesetQuery q 62 63 // OK 64 q = parser.parse("display_name=abcd") 65 assert q != null 66 assert q.@userName == "abcd" 67 } 68 69 @Test 70 public void testOpen() { 71 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 72 def ChangesetQuery q 73 74 // OK 75 q = parser.parse("open=true") 76 assert q != null 77 assert q.@open == true 78 79 // OK 80 q = parser.parse("open=false") 81 assert q != null 82 assert q.@open == false 49 } 50 51 /** 52 * Parse "uid=" 53 * @throws ChangesetQueryUrlException never 54 */ 55 @Test 56 public void testUid() throws ChangesetQueryUrlException { 57 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 58 ChangesetQuery q; 59 60 // OK 61 q = parser.parse("uid=1234"); 62 assertNotNull(q); 63 64 shouldFail("uid=0"); 65 shouldFail("uid=-1"); 66 shouldFail("uid=abc"); 67 } 68 69 /** 70 * Parse "display_name=" 71 * @throws ChangesetQueryUrlException never 72 */ 73 @Test 74 public void testDisplayName() throws ChangesetQueryUrlException { 75 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 76 ChangesetQuery q; 77 78 // OK 79 q = parser.parse("display_name=abcd"); 80 assertNotNull(q); 81 assertEquals("abcd", q.getUserName()); 82 } 83 84 /** 85 * Parse "open=" 86 * @throws ChangesetQueryUrlException never 87 */ 88 @Test 89 public void testOpen() throws ChangesetQueryUrlException { 90 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 91 ChangesetQuery q; 92 93 // OK 94 q = parser.parse("open=true"); 95 assertNotNull(q); 96 assertEquals(Boolean.TRUE, q.getRestrictionToOpen()); 97 98 // OK 99 q = parser.parse("open=false"); 100 assertNotNull(q); 101 assertEquals(Boolean.FALSE, q.getRestrictionToOpen()); 83 102 84 103 // illegal value for open 85 shouldFail(ChangesetQueryUrlException) { 86 q = parser.parse("open=abcd") 87 } 88 } 89 90 @Test 91 public void testClosed() { 92 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 93 def ChangesetQuery q 94 95 // OK 96 q = parser.parse("closed=true") 97 assert q != null 98 assert q.@closed == true 99 100 // OK 101 q = parser.parse("closed=false") 102 assert q != null 103 assert q.@closed == false 104 shouldFail("open=abcd"); 105 } 106 107 /** 108 * Parse "closed=" 109 * @throws ChangesetQueryUrlException never 110 */ 111 @Test 112 public void testClosed() throws ChangesetQueryUrlException { 113 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 114 ChangesetQuery q; 115 116 // OK 117 q = parser.parse("closed=true"); 118 assertNotNull(q); 119 assertEquals(Boolean.TRUE, q.getRestrictionToClosed()); 120 121 // OK 122 q = parser.parse("closed=false"); 123 assertNotNull(q); 124 assertEquals(Boolean.FALSE, q.getRestrictionToClosed()); 104 125 105 126 // illegal value for open 106 shouldFail(ChangesetQueryUrlException) { 107 q = parser.parse("closed=abcd") 108 } 109 } 110 127 shouldFail("closed=abcd"); 128 } 129 130 /** 131 * Check we can't have both an uid and a display name 132 */ 111 133 @Test 112 134 public void testUidAndDisplayName() { 113 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 114 def ChangesetQuery q 115 116 // we can't have both an uid and a display name 117 shouldFail(ChangesetQueryUrlException) { 118 q = parser.parse("uid=1&display_name=abcd") 119 } 120 } 121 122 @Test 123 public void testTime() { 124 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 125 def ChangesetQuery q 126 127 // OK 128 q = parser.parse("time=2009-12-25T10:00:00Z") 129 assert q != null 130 assert q.@closedAfter != null 131 def cal = q.@closedAfter.toInstant().atOffset(ZoneOffset.UTC) 132 assert cal == OffsetDateTime.of(2009, 12, 25, 10, 0, 0, 0, ZoneOffset.UTC) 133 134 // OK 135 q = parser.parse("time=2009-12-25T10:00:00Z,2009-11-25T10:00:00Z") 136 assert q!= null 137 assert q.@closedAfter != null 138 assert q.@createdBefore != null 135 shouldFail("uid=1&display_name=abcd"); 136 } 137 138 /** 139 * Parse "time=" 140 * @throws ChangesetQueryUrlException never 141 */ 142 @Test 143 public void testTime() throws ChangesetQueryUrlException { 144 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 145 ChangesetQuery q; 146 147 // OK 148 q = parser.parse("time=2009-12-25T10:00:00Z"); 149 assertNotNull(q); 150 assertNotNull(q.getClosedAfter()); 151 OffsetDateTime cal = q.getClosedAfter().toInstant().atOffset(ZoneOffset.UTC); 152 assertEquals(OffsetDateTime.of(2009, 12, 25, 10, 0, 0, 0, ZoneOffset.UTC), cal); 153 154 // OK 155 q = parser.parse("time=2009-12-25T10:00:00Z,2009-11-25T10:00:00Z"); 156 assertNotNull(q); 157 assertNotNull(q.getClosedAfter()); 158 assertNotNull(q.getCreatedBefore()); 139 159 140 160 // should fail 141 shouldFail(ChangesetQueryUrlException) { 142 q = parser.parse("time=asdf") 143 } 144 } 145 146 @Test 147 public void testBbox() { 148 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 149 def ChangesetQuery q 150 151 // OK 152 q = parser.parse("bbox=-1,-1,1,1") 153 assert q != null 154 assert q.@bounds != null 161 shouldFail("time=asdf"); 162 } 163 164 /** 165 * Parse "bbox=" 166 * @throws ChangesetQueryUrlException never 167 */ 168 @Test 169 public void testBbox() throws ChangesetQueryUrlException { 170 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 171 ChangesetQuery q; 172 173 // OK 174 q = parser.parse("bbox=-1,-1,1,1"); 175 assertNotNull(q); 176 assertNotNull(q.getBounds()); 155 177 156 178 // should fail 157 shouldFail(ChangesetQueryUrlException) { 158 q = parser.parse("bbox=-91,-1,1,1") 159 } 179 shouldFail("bbox=-91,-1,1,1"); 180 shouldFail("bbox=-1,-181,1,1"); 181 shouldFail("bbox=-1,-1,91,1"); 182 shouldFail("bbox=-1,-1,1,181"); 183 shouldFail("bbox=-1,-1,1"); 184 } 185 186 /** 187 * Parse "changesets=" 188 * @throws ChangesetQueryUrlException never 189 */ 190 @Test 191 public void testChangesetIds() throws ChangesetQueryUrlException { 192 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 193 ChangesetQuery q; 194 195 // OK 196 q = parser.parse("changesets=1,2,3"); 197 assertNotNull(q); 198 assertTrue(q.getAdditionalChangesetIds().containsAll(Arrays.asList(1L, 2L, 3L))); 199 assertEquals(3, q.getAdditionalChangesetIds().size()); 200 201 // OK 202 q = parser.parse("changesets=1,2,3,4,1"); 203 assertNotNull(q); 204 assertTrue(q.getAdditionalChangesetIds().containsAll(Arrays.asList(1L, 2L, 3L, 4L))); 205 assertEquals(4, q.getAdditionalChangesetIds().size()); 206 207 // OK 208 q = parser.parse("changesets="); 209 assertNotNull(q); 210 assertEquals(0, q.getAdditionalChangesetIds().size()); 160 211 161 212 // should fail 162 shouldFail(ChangesetQueryUrlException) { 163 q = parser.parse("bbox=-1,-181,1,1") 164 } 165 166 // should fail 167 shouldFail(ChangesetQueryUrlException) { 168 q = parser.parse("bbox=-1,-1,91,1") 169 } 170 // should fail 171 shouldFail(ChangesetQueryUrlException) { 172 q = parser.parse("bbox=-1,-1,1,181") 173 } 174 // should fail 175 shouldFail(ChangesetQueryUrlException) { 176 q = parser.parse("bbox=-1,-1,1") 177 } 178 } 179 180 @Test 181 public void testChangesetIds() { 182 ChangesetQueryUrlParser parser = new ChangesetQueryUrlParser(); 183 def ChangesetQuery q 184 185 // OK 186 q = parser.parse("changesets=1,2,3") 187 assert q != null 188 assert q.@changesetIds.containsAll(Arrays.asList(1L, 2L, 3L)) 189 assert q.@changesetIds.size() == 3 190 191 // OK 192 q = parser.parse("changesets=1,2,3,4,1") 193 assert q != null 194 assert q.@changesetIds.containsAll(Arrays.asList(1L, 2L, 3L, 4L)) 195 assert q.@changesetIds.size() == 4 196 197 // OK 198 q = parser.parse("changesets=") 199 assert q != null 200 assert q.@changesetIds.size() == 0 201 202 // should fail 203 shouldFail(ChangesetQueryUrlException) { 204 q = parser.parse("changesets=foo") 205 } 213 shouldFail("changesets=foo"); 206 214 } 207 215 }
Note:
See TracChangeset
for help on using the changeset viewer.