Skip to content

Commit 0d1a753

Browse files
committed
[more tests] public db links
1 parent acc26ee commit 0d1a753

File tree

4 files changed

+216
-70
lines changed

4 files changed

+216
-70
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.googlecode.scheme2ddl;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.jdbc.CannotGetJdbcConnectionException;
8+
import org.springframework.jdbc.core.JdbcTemplate;
9+
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
10+
import org.springframework.test.util.ReflectionTestUtils;
11+
import org.testng.SkipException;
12+
import org.testng.annotations.AfterMethod;
13+
import org.testng.annotations.BeforeClass;
14+
import org.testng.annotations.BeforeMethod;
15+
16+
import java.io.ByteArrayOutputStream;
17+
import java.io.File;
18+
import java.io.IOException;
19+
import java.io.PrintStream;
20+
import java.util.UUID;
21+
22+
import static org.testng.Assert.assertEquals;
23+
import static org.testng.Assert.assertTrue;
24+
25+
/**
26+
* Created by Anton Reshetnikov on 12 Dec 2016.
27+
*/
28+
@SpringBootTest(classes = ConfigurationIT.class, properties = "test-default.properties")
29+
public abstract class BaseIT extends AbstractTestNGSpringContextTests {
30+
31+
@Value("${hrUrl}")
32+
protected String url;
33+
34+
@Value("${dbaUrl}")
35+
protected String dbaUrl;
36+
37+
@Value("${dbaAsSysdbaUrl}")
38+
protected String dbaAsSysdbaUrl;
39+
40+
41+
@Autowired
42+
protected JdbcTemplate dbaJdbcTemplate;
43+
44+
protected final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
45+
protected final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
46+
47+
protected final PrintStream outOriginal = System.out;
48+
protected final PrintStream errorOriginal = System.err;
49+
50+
51+
protected File tempOutput;
52+
53+
54+
@BeforeClass
55+
public void setUp() {
56+
try {
57+
dbaJdbcTemplate.execute("ALTER USER HR ACCOUNT UNLOCK IDENTIFIED BY pass");
58+
}
59+
catch (CannotGetJdbcConnectionException e){
60+
logger.warn("Ignore all test due", e);
61+
throw new SkipException("Ignore all test due " + e.getMessage());
62+
}
63+
64+
}
65+
66+
@BeforeMethod
67+
public void resetDefaultsForStaticFields() throws Exception {
68+
ReflectionTestUtils.setField(Main.class, "justPrintUsage", false);
69+
ReflectionTestUtils.setField(Main.class, "justPrintVersion", false);
70+
ReflectionTestUtils.setField(Main.class, "justTestConnection", false);
71+
ReflectionTestUtils.setField(Main.class, "dbUrl", null);
72+
ReflectionTestUtils.setField(Main.class, "objectFilter", "%");
73+
ReflectionTestUtils.setField(Main.class, "typeFilter", "");
74+
ReflectionTestUtils.setField(Main.class, "typeFilterMode", "include");
75+
ReflectionTestUtils.setField(Main.class, "isLaunchedByDBA", false);
76+
ReflectionTestUtils.setField(Main.class, "schemas", null);
77+
ReflectionTestUtils.setField(Main.class, "schemaList", null);
78+
ReflectionTestUtils.setField(Main.class, "replaceSequenceValues", false);
79+
ReflectionTestUtils.setField(Main.class, "customConfigLocation", null);
80+
ReflectionTestUtils.setField(Main.class, "parallelCount", 4);
81+
}
82+
83+
@BeforeMethod
84+
public void setUpStreams() {
85+
System.setOut(new PrintStream(outContent));
86+
System.setErr(new PrintStream(errContent));
87+
}
88+
89+
@AfterMethod
90+
public void cleanUpStreams() throws IOException {
91+
System.setOut(outOriginal);
92+
System.setErr(errorOriginal);
93+
outContent.reset();
94+
errContent.reset();
95+
}
96+
97+
@BeforeMethod
98+
public void setUpTempOutputDir(){
99+
tempOutput = FileUtils.getFile(FileUtils.getTempDirectoryPath(),
100+
"scheme2ddl-test-tmp-output",
101+
UUID.randomUUID().toString().substring(0,8));
102+
}
103+
104+
@AfterMethod
105+
public void cleanUpTempOutput() throws IOException {
106+
FileUtils.deleteDirectory(tempOutput);
107+
}
108+
109+
protected static void assertEqualsFileContent(String path, String content) throws IOException {
110+
File file = new File(path);
111+
assertTrue(file.exists(), "file doesn't exists " + file );
112+
String fileContent = FileUtils.readFileToString(file, "UTF-8");
113+
assertEquals(fileContent.trim().replace("\r", ""), content.replace("\r", ""));
114+
115+
}
116+
}

src/test/java/com/googlecode/scheme2ddl/MainIT.java

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,9 @@
2828
* @since Date: 17.09.2016
2929
*/
3030

31-
@SpringBootTest(classes = ConfigurationIT.class, properties = "test-default.properties")
32-
public class MainIT extends AbstractTestNGSpringContextTests {
31+
//@SpringBootTest(classes = ConfigurationIT.class, properties = "test-default.properties")
32+
public class MainIT extends BaseIT {
3333

34-
@Value("${hrUrl}")
35-
private String url;
36-
37-
@Value("${dbaUrl}")
38-
private String dbaUrl;
39-
40-
@Value("${dbaAsSysdbaUrl}")
41-
private String dbaAsSysdbaUrl;
42-
43-
44-
@Autowired
45-
private JdbcTemplate dbaJdbcTemplate;
46-
47-
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
48-
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
49-
50-
private final PrintStream outOriginal = System.out;
51-
private final PrintStream errorOriginal = System.err;
52-
53-
54-
private File tempOutput;
5534

5635

5736
@BeforeClass
@@ -66,48 +45,7 @@ public void setUp() {
6645

6746
}
6847

69-
@BeforeMethod
70-
public void resetDefaultsForStaticFields() throws Exception {
71-
ReflectionTestUtils.setField(Main.class, "justPrintUsage", false);
72-
ReflectionTestUtils.setField(Main.class, "justPrintVersion", false);
73-
ReflectionTestUtils.setField(Main.class, "justTestConnection", false);
74-
ReflectionTestUtils.setField(Main.class, "dbUrl", null);
75-
ReflectionTestUtils.setField(Main.class, "objectFilter", "%");
76-
ReflectionTestUtils.setField(Main.class, "typeFilter", "");
77-
ReflectionTestUtils.setField(Main.class, "typeFilterMode", "include");
78-
ReflectionTestUtils.setField(Main.class, "isLaunchedByDBA", false);
79-
ReflectionTestUtils.setField(Main.class, "schemas", null);
80-
ReflectionTestUtils.setField(Main.class, "schemaList", null);
81-
ReflectionTestUtils.setField(Main.class, "replaceSequenceValues", false);
82-
ReflectionTestUtils.setField(Main.class, "customConfigLocation", null);
83-
ReflectionTestUtils.setField(Main.class, "parallelCount", 4);
84-
}
8548

86-
@BeforeMethod
87-
public void setUpStreams() {
88-
System.setOut(new PrintStream(outContent));
89-
System.setErr(new PrintStream(errContent));
90-
}
91-
92-
@AfterMethod
93-
public void cleanUpStreams() throws IOException {
94-
System.setOut(outOriginal);
95-
System.setErr(errorOriginal);
96-
outContent.reset();
97-
errContent.reset();
98-
}
99-
100-
@BeforeMethod
101-
public void setUpTempOutputDir(){
102-
tempOutput = FileUtils.getFile(FileUtils.getTempDirectoryPath(),
103-
"scheme2ddl-test-tmp-output",
104-
UUID.randomUUID().toString().substring(0,8));
105-
}
106-
107-
@AfterMethod
108-
public void cleanUpTempOutput() throws IOException {
109-
FileUtils.deleteDirectory(tempOutput);
110-
}
11149

11250

11351
@DataProvider
@@ -344,11 +282,5 @@ public void testStopOnWarning() throws Exception {
344282
}
345283

346284

347-
private static void assertEqualsFileContent(String path, String content) throws IOException {
348-
File file = new File(path);
349-
assertTrue(file.exists(), "file doesn't exists " + file );
350-
String fileContent = FileUtils.readFileToString(file, "UTF-8");
351-
assertEquals(fileContent.trim().replace("\r", ""), content.replace("\r", ""));
352285

353-
}
354286
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.googlecode.scheme2ddl;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.dao.InvalidDataAccessResourceUsageException;
8+
import org.springframework.jdbc.BadSqlGrammarException;
9+
import org.springframework.jdbc.CannotGetJdbcConnectionException;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
12+
import org.springframework.test.util.ReflectionTestUtils;
13+
import org.testng.Assert;
14+
import org.testng.SkipException;
15+
import org.testng.annotations.*;
16+
17+
import java.io.ByteArrayOutputStream;
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.PrintStream;
21+
import java.sql.SQLException;
22+
import java.util.UUID;
23+
24+
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.hamcrest.core.StringContains.containsString;
26+
import static org.testng.Assert.assertEquals;
27+
import static org.testng.Assert.assertTrue;
28+
29+
/**
30+
* @author A_Reshetnikov
31+
* @since Date: 17.09.2016
32+
*/
33+
34+
35+
public class PublicDbLinkIT extends BaseIT {
36+
37+
@Value("${testUserUrl}")
38+
protected String testUserUrl;
39+
40+
41+
@BeforeClass
42+
public void createSchema() {
43+
try{
44+
dropUser();
45+
}catch (InvalidDataAccessResourceUsageException e) {
46+
logger.warn("", e);
47+
}
48+
try {
49+
createUser();
50+
} catch (CannotGetJdbcConnectionException e) {
51+
logger.warn("Ignore all test due", e);
52+
throw new SkipException("Ignore all test due " + e.getMessage());
53+
}
54+
55+
}
56+
57+
private void createUser() {
58+
dbaJdbcTemplate.execute("CREATE USER scheme2ddl_test02 IDENTIFIED BY pass");
59+
dbaJdbcTemplate.execute("GRANT CONNECT, SELECT_CATALOG_ROLE to scheme2ddl_test02");
60+
dbaJdbcTemplate.execute("ALTER USER scheme2ddl_test02 ACCOUNT UNLOCK IDENTIFIED BY pass");
61+
}
62+
63+
64+
// @AfterClass(alwaysRun = true)
65+
public void dropUser(){
66+
dbaJdbcTemplate.execute("drop USER scheme2ddl_test02");
67+
}
68+
69+
@BeforeClass
70+
public void createDBLink(){
71+
dbaJdbcTemplate.execute("CREATE PUBLIC DATABASE LINK remote USING 'remote'");
72+
}
73+
74+
@AfterClass
75+
public void dropDBLink(){
76+
dbaJdbcTemplate.execute("DROP PUBLIC DATABASE LINK remote");
77+
}
78+
79+
80+
@Test
81+
public void testPublicDbLinksWithSelectCatalogRole() throws Exception {
82+
String outputPath = tempOutput.getAbsolutePath();
83+
String[] args = {"-url", testUserUrl, "-o", outputPath};
84+
Main.main(args);
85+
String out = outContent.toString();
86+
87+
assertThat(out, containsString("Written 1 ddls with user objects from total 1 in schema"));
88+
89+
assertEqualsFileContent(outputPath + "/PUBLIC/public_db_links/remote.sql",
90+
"CREATE PUBLIC DATABASE LINK \"REMOTE\"\n" +
91+
" USING 'remote';");
92+
93+
}
94+
95+
96+
}

src/test/resources/test-default.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ dbaUrl=system/[email protected]:49161/XE
66
dbaAsSysdbaUrl=sys as sysdba/[email protected]:49161/XE
77
hrUrl=hr/[email protected]:49161/XE
88

9+
testUserUrl=scheme2ddl_test02/[email protected]:49161/XE
10+
911

1012
dba.datasource.url=jdbc:oracle:thin:@127.0.0.1:49161:XE
1113
dba.datasource.username=system

0 commit comments

Comments
 (0)