|
| 1 | +/* |
| 2 | + * Copyright 2019 Philipp Salvisberg <[email protected]> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.utplsql.sqldev.test |
| 17 | + |
| 18 | +import java.io.File |
| 19 | +import java.nio.file.Files |
| 20 | +import java.nio.file.Paths |
| 21 | +import org.junit.Assert |
| 22 | +import org.junit.Test |
| 23 | +import org.utplsql.sqldev.snippet.SnippetMerger |
| 24 | + |
| 25 | +class SnippetTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + def void mergeAsCopy() { |
| 29 | + val file = new File(System.getProperty("user.home") + File.separator + "UserSnippets.xml") |
| 30 | + file.delete |
| 31 | + val merger = new SnippetMerger(file) |
| 32 | + merger.merge |
| 33 | + Assert.assertTrue(file.exists) |
| 34 | + val userSnippetsXml = new String(Files.readAllBytes(Paths.get(file.absolutePath))) |
| 35 | + Assert.assertEquals(merger.template, userSnippetsXml ) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + def void mergeKeepExisting() { |
| 40 | + val file = new File(System.getProperty("user.home") + File.separator + "UserSnippets.xml") |
| 41 | + file.delete |
| 42 | + val userSnippetsXml = ''' |
| 43 | + <?xml version = '1.0' encoding = 'UTF-8'?> |
| 44 | + <snippets> |
| 45 | + <group category="utPLSQL" language="PLSQL"> |
| 46 | + <snippet name="test s" description="test s"> |
| 47 | + <code> |
| 48 | + <![CDATA[bla bla bla]]> |
| 49 | + </code> |
| 50 | + </snippet> |
| 51 | + </group> |
| 52 | + </snippets> |
| 53 | + '''.toString |
| 54 | + Files.write(Paths.get(file.absolutePath), userSnippetsXml.bytes) |
| 55 | + val merger = new SnippetMerger(file) |
| 56 | + merger.merge |
| 57 | + Assert.assertTrue(file.exists) |
| 58 | + val userSnippetsXml2 = new String(Files.readAllBytes(Paths.get(file.absolutePath))) |
| 59 | + Assert.assertTrue(userSnippetsXml2.length > userSnippetsXml.length) |
| 60 | + Assert.assertTrue(userSnippetsXml2.contains('''<group category="utPLSQL" language="PLSQL">''')) |
| 61 | + Assert.assertTrue(userSnippetsXml2.contains('''<group category="utPLSQL Annotations" language="PLSQL">''')) |
| 62 | + Assert.assertTrue(userSnippetsXml2.contains('''<group category="utPLSQL Expectations" language="PLSQL">''')) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + def void mergeRemoveExisting() { |
| 67 | + val file = new File(System.getProperty("user.home") + File.separator + "UserSnippets.xml") |
| 68 | + file.delete |
| 69 | + val userSnippetsXml = ''' |
| 70 | + <?xml version = '1.0' encoding = 'UTF-8'?> |
| 71 | + <snippets> |
| 72 | + <group category="utPLSQL Annotations" language="XYZ"/> |
| 73 | + <group category="utPLSQL Expectations" language="XYZ"/> |
| 74 | + </snippets> |
| 75 | + '''.toString |
| 76 | + Files.write(Paths.get(file.absolutePath), userSnippetsXml.bytes) |
| 77 | + val merger = new SnippetMerger(file) |
| 78 | + merger.merge |
| 79 | + Assert.assertTrue(file.exists) |
| 80 | + val userSnippetsXml2 = new String(Files.readAllBytes(Paths.get(file.absolutePath))) |
| 81 | + Assert.assertTrue(userSnippetsXml2.length > userSnippetsXml.length) |
| 82 | + Assert.assertFalse(userSnippetsXml2.contains('''<group category="utPLSQL Annotations" language="XYZ">''')) |
| 83 | + Assert.assertFalse(userSnippetsXml2.contains('''<group category="utPLSQL Expectations" language="XYZ">''')) |
| 84 | + Assert.assertTrue(userSnippetsXml2.contains('''<group category="utPLSQL Annotations" language="PLSQL">''')) |
| 85 | + Assert.assertTrue(userSnippetsXml2.contains('''<group category="utPLSQL Expectations" language="PLSQL">''')) |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +} |
0 commit comments