-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #761 from guidograzioli/master
Fix bug GEOT-5036
- Loading branch information
Showing
5 changed files
with
103 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
modules/unsupported/geojson/src/test/java/org/geotools/geojson/GEOT5036RegressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2002-2010, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.geojson; | ||
|
||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import org.apache.commons.lang.time.FastDateFormat; | ||
|
||
/** | ||
* Exploit bug GEOT-5036, concurrent use of GeoJSONUtil. | ||
* @author Guido Grazioli <guido.grazioli@gmail.com> | ||
* | ||
*/ | ||
public class GEOT5036RegressionTest extends TestCase { | ||
|
||
private final Random rand = new Random(); | ||
private final Map<Date, Future<String>> expectationMap = new HashMap<Date, Future<String>>(); | ||
|
||
public class Task implements Callable<String> { | ||
private final Date date; | ||
|
||
public Task(Date d) { | ||
this.date = d; | ||
} | ||
|
||
@Override | ||
public String call() throws Exception { | ||
final StringBuilder sb = new StringBuilder(); | ||
GeoJSONUtil.literal(date, sb); | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setUp() throws java.lang.Exception { | ||
// perform 50 conversions | ||
for (int i = 0; i < 50; i++) { | ||
final Date date = new Date(System.currentTimeMillis() - rand.nextInt(100) * 1000 * 3600 * 24); | ||
expectationMap.put(date, null); | ||
} | ||
} | ||
|
||
public void testDateFormatterResults() throws Exception { | ||
final FastDateFormat sdf = GeoJSONUtil.dateFormatter; | ||
|
||
// pool with 8 threads | ||
final ExecutorService exec = Executors.newFixedThreadPool(8); | ||
|
||
// perform date conversions | ||
for (final Date key : expectationMap.keySet()) { | ||
final Task task = new Task(key); | ||
expectationMap.put(key, exec.submit(task)); | ||
} | ||
exec.shutdown(); | ||
|
||
// look at the results | ||
for (final Map.Entry<Date, Future<String>> entry : expectationMap.entrySet()) { | ||
assertEquals("incorrect date string was returned: ", entry.getValue().get(), | ||
"\"" + sdf.format(entry.getKey()) + "\""); | ||
} | ||
} | ||
|
||
} |