|
| 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 | + ReflectionTestUtils.setField(Main.class, "outputPath", null); |
| 82 | + } |
| 83 | + |
| 84 | + @BeforeMethod |
| 85 | + public void setUpStreams() { |
| 86 | + System.setOut(new PrintStream(outContent)); |
| 87 | + System.setErr(new PrintStream(errContent)); |
| 88 | + } |
| 89 | + |
| 90 | + @AfterMethod |
| 91 | + public void cleanUpStreams() throws IOException { |
| 92 | + System.setOut(outOriginal); |
| 93 | + System.setErr(errorOriginal); |
| 94 | + outContent.reset(); |
| 95 | + errContent.reset(); |
| 96 | + } |
| 97 | + |
| 98 | + @BeforeMethod |
| 99 | + public void setUpTempOutputDir(){ |
| 100 | + tempOutput = FileUtils.getFile(FileUtils.getTempDirectoryPath(), |
| 101 | + "scheme2ddl-test-tmp-output", |
| 102 | + UUID.randomUUID().toString().substring(0,8)); |
| 103 | + } |
| 104 | + |
| 105 | + @AfterMethod |
| 106 | + public void cleanUpTempOutput() throws IOException { |
| 107 | + FileUtils.deleteDirectory(tempOutput); |
| 108 | + } |
| 109 | + |
| 110 | + protected static void assertEqualsFileContent(String path, String content) throws IOException { |
| 111 | + File file = new File(path); |
| 112 | + assertTrue(file.exists(), "file doesn't exists " + file ); |
| 113 | + String fileContent = FileUtils.readFileToString(file, "UTF-8"); |
| 114 | + assertEquals(fileContent.trim().replace("\r", ""), content.replace("\r", "")); |
| 115 | + |
| 116 | + } |
| 117 | +} |
0 commit comments