@@ -10,9 +10,13 @@ |
import java.sql.SQLException; |
import java.sql.Statement; |
import java.util.ArrayList; |
+import java.util.Queue; |
import java.util.Random; |
+import java.util.concurrent.ConcurrentLinkedQueue; |
import java.util.concurrent.ThreadLocalRandom; |
+import java.util.logging.Handler; |
import java.util.logging.Level; |
+import java.util.logging.LogManager; |
import java.util.logging.Logger; |
import org.testng.Assert; |
import static org.testng.Assert.*; |
@@ -30,19 +34,26 @@ |
public class TestDriver { |
|
private static Connection conn; |
- private static ArrayList<RandomMapunit> testMapunits = new ArrayList<>(); |
- private static int MAX_RANDOM_MAPUNITS = 5; |
- private static double MAX_AREA_DELTA = 0.1; |
+ //private static ArrayList<RandomMapunit> testMapunits = new ArrayList<>(); |
+ private static Queue<RandomMapunit> testMapunits = new ConcurrentLinkedQueue<>(); |
+ |
+ /** |
+ * Number of random mapunits to test intersections for. |
+ */ |
+ private static final int MAX_RANDOM_MAPUNITS = 10; |
+ |
+ /** |
+ * Acre measurement delta for comparisons in intersection tests. |
+ */ |
+ private static final double MAX_AREA_DELTA = 0.01; |
|
public TestDriver() throws ClassNotFoundException, SQLException { |
-//1395701 |
+ Logger log = LogManager.getLogManager().getLogger(""); |
+ for (Handler h : log.getHandlers()) { |
+ h.setLevel(Level.WARNING); |
+ } |
} |
|
- // TODO add test methods here. |
- // The methods must be annotated with annotation @Test. For example: |
- // |
- // @Test |
- // public void hello() {} |
@Test |
public void testResultSetIteration() { |
String query = "SELECT top 100 legend.areasymbol, mapunit.musym, mapunit.mukey, mapunit.muname, muacres, brockdepmin FROM mapunit " |
@@ -65,10 +76,10 @@ |
double brockdepmin = results.getDouble("brockdepmin"); |
count++; |
} |
- Assert.assertTrue(count == 100); |
+ Assert.assertTrue(count == 100); //We should have received 100 random mapunits, and been able to read their data. |
} catch (SQLException ex) { |
Logger.getLogger(TestDriver.class.getName()).log(Level.SEVERE, null, ex); |
- Assert.assertTrue(false); |
+ Assert.assertTrue(false, "No results were found for this test, which should have generated a random list of mapunit keys. Something may be wrong with the ResultSet implementation of the SDMDriver."); |
} |
} |
|
@@ -92,23 +103,30 @@ |
String muname = results.getString("muname"); |
double muacres = results.getDouble("muacres"); |
double brockdepmin = results.getDouble("brockdepmin"); |
- double area = results.getDouble("area"); |
+ double area = results.getDouble("area"); //This one should cause an exception if the ResultSet object is working correctly. |
count++; |
} |
- Assert.assertFalse(count > 0); //We should have recived an SQLException. |
+ Assert.assertFalse(count > 0, "This SQL statement should have thrown an SQL Exception, but did not."); //We should have recived an SQLException. |
} catch (SQLException ex) { |
Logger.getLogger(TestDriver.class.getName()).log(Level.SEVERE, ex.getMessage()); |
Assert.assertTrue(true); |
} |
} |
|
- @Test |
- public void testComplexIntersectionQuery() { |
+ @Test(threadPoolSize = 10, invocationCount = MAX_RANDOM_MAPUNITS, timeOut = 30000) |
+ public void testComplexIntersectionQuery() throws InterruptedException { |
String bufferWKT; |
|
- for (RandomMapunit tMapunit : testMapunits) { |
+ RandomMapunit tMapunit = testMapunits.poll(); |
+ long sleepTime = ThreadLocalRandom.current().nextInt(10, 2000); |
+ //System.out.println(Thread.currentThread().getId() + ": Sleepting for " + sleepTime + " milliseconds."); |
+ //System.out.flush(); |
+ Thread.sleep(sleepTime); |
+ if (null != tMapunit) { |
+ |
bufferWKT = tMapunit.getBufferWKT(); |
- |
+ System.out.println(Thread.currentThread().getId() + ": Runing intersection for mupolygonkey: " + tMapunit.mupolygonKey); |
+ System.out.flush(); |
String query = "SELECT areasymbol, musym, mukey, muname, muacres, brockdepmin, " |
+ " geography::STGeomFromText(intersectPoly.STAsText(), 4326).MakeValid().STArea() / 4046.86 AS area " |
+ " FROM (" |
@@ -125,10 +143,11 @@ |
|
try { |
ResultSet results; |
+ long timeMills = System.currentTimeMillis(); |
Statement statement = conn.createStatement(); |
results = statement.executeQuery(query); |
double area = 0.0; |
- |
+ System.out.println(Thread.currentThread().getId() + ": Query took " + ((System.currentTimeMillis()) - timeMills) / 1000.0 + " seconds."); |
while (results.next()) { |
String areasymbol = results.getString("areasymbol"); |
String musym = results.getString("musym"); |
@@ -138,17 +157,20 @@ |
double brockdepmin = results.getDouble("brockdepmin"); |
area += results.getDouble("area"); |
} |
- |
- Assert.assertEquals(area, tMapunit.getArea(), MAX_AREA_DELTA); |
+ |
+ Assert.assertEquals(area, tMapunit.getArea(), MAX_AREA_DELTA, "Intersection query based on random data from the remote server resulted in area calculation that did not match expected outcomes."); |
} catch (SQLException ex) { |
Logger.getLogger(TestDriver.class.getName()).log(Level.SEVERE, null, ex); |
Assert.assertTrue(false); |
} |
+ } else { |
+ System.out.println("Queue is empty. Finished."); |
} |
} |
|
@BeforeClass |
public static void setUpClass() throws Exception { |
+ System.out.println("Loading the SDMDriver, and testing a connection."); |
Class.forName("org.csu.csip.sdm.SDMDriver"); |
Reporter.log("Driver was loaded for: org.csu.csip.sdm.SDMDrive"); |
conn = DriverManager.getConnection("jdbc:sdm:rest://SDMDataAccess.sc.egov.usda.gov/Tabular/post.rest"); |
@@ -167,19 +189,22 @@ |
+ " AND legend.lkey=mapunit.lkey AND mapunit.mukey=mupolygon.mukey; "; |
|
try { |
+ System.out.println("Building a list of " + MAX_RANDOM_MAPUNITS + " random mapunits to test with."); |
ResultSet results; |
Statement statement = conn.createStatement(); |
results = statement.executeQuery(query); |
|
while (results.next()) { |
+ System.out.println("Got mupolygonkey: " + results.getString("mupolygonkey")); |
RandomMapunit tMapunit = new RandomMapunit(results.getString("mupolygonkey"), results.getString("mukey"), results.getString("areasymbol"), results.getString("centerPoint")); |
testMapunits.add(tMapunit); |
} |
} catch (SQLException ex) { |
Logger.getLogger(TestDriver.class.getName()).log(Level.SEVERE, null, ex); |
- Assert.assertTrue(false); |
+ Assert.assertTrue(false, "Could not generate a random set of mapunit buffers for use in upcoming tests: " + ex.getMessage()); |
} |
|
+ System.out.println("Creating randomly sized buffer polygons from each mapunit centroid."); |
for (RandomMapunit tMapunit : testMapunits) { |
tMapunit.getBuffers(conn); |
} |
@@ -187,6 +212,8 @@ |
|
@AfterClass |
public static void tearDownClass() throws Exception { |
+ System.out.println("Closing conneciton."); |
+ conn.close(); |
} |
|
@BeforeMethod |
@@ -232,7 +259,7 @@ |
} |
} catch (SQLException ex) { |
Logger.getLogger(TestDriver.class.getName()).log(Level.SEVERE, null, ex); |
- Assert.assertTrue(false); |
+ Assert.assertTrue(false, "Could not generate a randomly sized buffer within this mapunit: " + ex.getMessage()); |
} |
} |
|