summaryrefslogtreecommitdiff
path: root/src/connectors/JUTests/data/io/sql/SQLRelDataReaderTest.java
blob: a97a98d13dcaea7fcbba7e2036879fc95c614894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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);
	}

}