package data.io.sql; import static org.junit.Assert.*; import java.io.File; import java.io.IOException; import java.net.URL; import org.junit.Before; import org.junit.Test; import data.MVDataEntry; import data.io.MVDataReader; import data.io.sql.SQLConnectionWrapper.DBMSType; /* CREATE TABLE sssync.people ( uid CHAR(16) NULL , uidNumber INT NOT NULL , gidNumber INT NULL , cn VARCHAR(45) NULL , sn VARCHAR(45) NULL , homeDirectory VARCHAR(45) NULL , PRIMARY KEY (uid) ); INSERT INTO sssync.people (uid, uidNumber, gidNumber, cn, sn, homeDirectory) VALUES ('lpouzenc', 1000, 999, 'Ludovic', 'Pouzenc', '/home/lpouzenc'); INSERT INTO sssync.people (uid, uidNumber, gidNumber, cn, sn, homeDirectory) VALUES ('dpouzenc', 1001, 999, 'Daniel', 'Pouzenc', '/home/dpouzenc'); for i in $(seq 10000 20000); do echo "INSERT INTO sssync.people (uid, uidNumber, gidNumber, cn, sn, homeDirectory) VALUES ('test$i', $i, 999, '$i', 'test', '/home/test$i');"; done | mysql -uroot -p DROP TABLE IF EXISTS structures; CREATE TABLE structures ( supannCodeEntite varchar(15) NOT NULL, ou varchar(45) NOT NULL, supannTypeEntite varchar(45) NOT NULL, supannCodeEntiteParent varchar(45) NOT NULL, PRIMARY KEY (supannCodeEntite) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO structures VALUES ('2','CUFR','Etablissement','2'),('9','Personnels','Groupe','2'); TODO : make automated tests with embded Derby base */ public class SQLRelDataReaderTest { private static final String TEST_REQUEST = "SELECT p.*, \"person;posixAccount;top\" as objectClass" + " FROM sssync.people p" + " ORDER BY 1 ASC;"; private SQLConnectionWrapper builder; private MVDataReader reader1; private MVDataReader reader2; @Before public void setup() throws IOException { // Find the folder containing this test class URL main = SQLRelDataReaderTest.class.getResource("SQLRelDataReaderTest.class"); if (!"file".equalsIgnoreCase(main.getProtocol())) throw new IllegalStateException("This class is not stored in a file"); File currentFolder = new File(main.getPath()).getParentFile(); // Build a connection and two readers on it builder = new SQLConnectionWrapper(DBMSType.MYSQL, "localhost", 3306, null, "root", "secret", "sssync"); reader1 = builder.newReader("testMysql1", TEST_REQUEST); reader2 = builder.newReader("testMysql2", new File(currentFolder, "req_test.sql")); } @Test public void testNext() { // First full read on reader1 int resultCount_r1i1 = 0; String previousKey_r1i1=null; for ( MVDataEntry entry : reader1 ) { //System.out.println(entry); if ( previousKey_r1i1 != null ) assertTrue(entry.getKey().compareTo(previousKey_r1i1) > 0); resultCount_r1i1++; previousKey_r1i1=entry.getKey(); } System.out.println(resultCount_r1i1); assertTrue(resultCount_r1i1 > 0); // First half read on reader2 int resultCount_r2i1 = 0; String previousKey_r2i1=null; for ( MVDataEntry entry : reader2 ) { //System.out.println(entry); if ( previousKey_r2i1 != null ) assertTrue(entry.getKey().compareTo(previousKey_r2i1) > 0); resultCount_r2i1++; previousKey_r2i1=entry.getKey(); if ( resultCount_r2i1 > resultCount_r1i1 / 2 ) break; } System.out.println(resultCount_r2i1); assertTrue(resultCount_r2i1 > resultCount_r1i1 / 2 ); // Second time with a second iterator on reader1 (must give the same results than r1i1) int resultCount_r1i2 = 0; String previousKey_r1i2=null; for ( MVDataEntry entry : reader1 ) { //System.out.println(entry); if ( previousKey_r1i2 != null ) assertTrue(entry.getKey().compareTo(previousKey_r1i2) > 0); resultCount_r1i2++; previousKey_r1i2=entry.getKey(); } System.out.println(resultCount_r1i2); assertTrue(resultCount_r1i2 == resultCount_r1i1); } }