/* * Copyright (C) 2005-2011 Alfresco Software Limited. * * This file is part of Alfresco * * Alfresco is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Alfresco is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Alfresco. If not, see . */ package org.alfresco.util.schemacomp; import static org.alfresco.util.schemacomp.SchemaCompTestingUtils.columns; import static org.alfresco.util.schemacomp.SchemaCompTestingUtils.fk; import static org.alfresco.util.schemacomp.SchemaCompTestingUtils.fkeys; import static org.alfresco.util.schemacomp.SchemaCompTestingUtils.indexes; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.regex.Pattern; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import org.alfresco.util.schemacomp.model.Column; import org.alfresco.util.schemacomp.model.ForeignKey; import org.alfresco.util.schemacomp.model.Index; import org.alfresco.util.schemacomp.model.PrimaryKey; import org.alfresco.util.schemacomp.model.Schema; import org.alfresco.util.schemacomp.model.Sequence; import org.alfresco.util.schemacomp.model.Table; import org.alfresco.util.schemacomp.validator.DbValidator; import org.alfresco.util.schemacomp.validator.NameValidator; import org.junit.Before; import org.junit.Test; import com.sun.star.uno.RuntimeException; /** * Tests for the {@link DbObjectXMLTransformer} class. * * @author Matt Ward */ public class DbObjectXMLTransformerTest { private DbObjectXMLTransformer transformer; private TransformerHandler xmlOut; private Writer writer; private boolean outputDumpEnabled = true; @Before public void setUp() { final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance(); try { xmlOut = stf.newTransformerHandler(); } catch (TransformerConfigurationException error) { throw new RuntimeException("Unable to create TransformerHandler.", error); } final Transformer t = xmlOut.getTransformer(); try { t.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); } catch (final IllegalArgumentException e) { // It was worth a try } t.setOutputProperty(OutputKeys.INDENT, "yes"); writer = new StringWriter(); xmlOut.setResult(new StreamResult(writer)); transformer = new DbObjectXMLTransformer(xmlOut); } @Test public void transformColumn() throws IOException { Column column = new Column(null, "last_name", "VARCHAR2(100)", true); transformer.output(column); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" VARCHAR2(100)", reader.readLine()); assertEquals(" true", reader.readLine()); assertEquals("", reader.readLine()); } @Test public void transformForeignKey() throws IOException { ForeignKey fk = new ForeignKey(null, "fk_for_some_table", "local_column", "target_table", "target_column"); transformer.output(fk); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" local_column", reader.readLine()); assertEquals(" target_table", reader.readLine()); assertEquals(" target_column", reader.readLine()); assertEquals("", reader.readLine()); } @Test public void transformIndex() throws IOException { Index index = new Index(null, "index_name", Arrays.asList("first", "second")); transformer.output(index); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals(" first", reader.readLine()); assertEquals(" second", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals("", reader.readLine()); } @Test public void transformPrimaryKey() throws IOException { PrimaryKey pk = new PrimaryKey(null, "pk_name", Arrays.asList("first", "second")); transformer.output(pk); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals(" first", reader.readLine()); assertEquals(" second", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals("", reader.readLine()); } @Test public void transformSchema() throws IOException { Collection columns = columns("one VARCHAR2(100)", "two NUMBER(10)"); PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id")); Collection fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc")); Collection indexes = indexes("index_one col1 col2", "index_two col3 col4"); Table tableOne = new Table(null, "table_one", columns, pk, fks, indexes); Table tableTwo = new Table(null, "table_two", columns, pk, fks, indexes); Schema schema = new Schema("my_schema"); schema.add(tableOne); schema.add(tableTwo); schema.add(new Sequence(null, "sequence_one")); schema.add(new Sequence(null, "sequence_two")); schema.add(new Sequence(null, "sequence_three")); transformer.output(schema); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" ", reader.readLine()); skipUntilEnd(" {table}", reader); skipUntilEnd(" {table}", reader); skipUntilEnd(" {sequence}", reader, true); skipUntilEnd(" {sequence}", reader, true); skipUntilEnd(" {sequence}", reader, true); assertEquals(" ", reader.readLine()); assertEquals("", reader.readLine()); } @Test public void transformSequence() throws IOException { Sequence sequence = new Sequence(null, "my_sequence"); transformer.output(sequence); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); } @Test public void transformTable() throws IOException { Collection columns = columns("one VARCHAR2(100)", "two NUMBER(10)"); PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id")); Collection fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc")); Collection indexes = indexes("index_one col1 col2", "index_two col3 col4"); Table table = new Table(null, "my_table", columns, pk, fks, indexes); transformer.output(table); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" ", reader.readLine()); skipUntilEnd(" {column}", reader); skipUntilEnd(" {column}", reader); assertEquals(" ", reader.readLine()); skipUntilEnd(" {primarykey}", reader); assertEquals(" ", reader.readLine()); skipUntilEnd(" {foreignkey}", reader); skipUntilEnd(" {foreignkey}", reader); assertEquals(" ", reader.readLine()); assertEquals(" ", reader.readLine()); skipUntilEnd(" {index}", reader); skipUntilEnd(" {index}", reader); assertEquals(" ", reader.readLine()); assertEquals("
", reader.readLine()); } @Test public void transformObjectWithValidators() throws IOException { Collection columns = columns("one VARCHAR2(100)", "two NUMBER(10)"); PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id")); Collection fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc")); Collection indexes = indexes("index_one col1 col2", "index_two col3 col4"); Table table = new Table(null, "my_table", columns, pk, fks, indexes); NameValidator nameValidator = new NameValidator(); nameValidator.setPattern(Pattern.compile("match_me_if_you_can")); List validators = new ArrayList(); validators.add(nameValidator); table.setValidators(validators); transformer.output(table); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" ", reader.readLine()); skipUntilEnd(" {column}", reader); skipUntilEnd(" {column}", reader); assertEquals(" ", reader.readLine()); skipUntilEnd(" {primarykey}", reader); assertEquals(" ", reader.readLine()); skipUntilEnd(" {foreignkey}", reader); skipUntilEnd(" {foreignkey}", reader); assertEquals(" ", reader.readLine()); assertEquals(" ", reader.readLine()); skipUntilEnd(" {index}", reader); skipUntilEnd(" {index}", reader); assertEquals(" ", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals(" match_me_if_you_can", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals("
", reader.readLine()); } /** * Ignore lines that are tested elsewhere, e.g. ignore serialized Column objects * in the context of a Table since we only need to know that there was text for a * Column object in the right place - the actual Column text being tested in its own test. *

* Leading and trailing spaces are ignored in the comparison. * * @param textToFind * @param reader */ private void skipUntilEnd(String textToFind, BufferedReader reader, boolean emptyTag) { // To aid test code clarity, and distinguish between text we're actually // testing for and text that needs to be ignored... // {mytag} becomes // or if an empty tag is expected // {mytag} becomes if (emptyTag) { textToFind = textToFind.trim(). replace("{", "<"). replace("}", "\\s+.*/>"); } else { textToFind = textToFind.trim(). replace("{", ""); } try { String line; while ((line = reader.readLine()) != null) { if (line.trim().matches(textToFind)) { return; } } fail("Unable to find text: " + textToFind); } catch (IOException error) { throw new RuntimeException("Unable to skip text whilst looking for: " + textToFind, error); } } private void skipUntilEnd(String textToFind, BufferedReader reader) { skipUntilEnd(textToFind, reader, false); } private void assertHasPreamble(BufferedReader reader) throws IOException { assertEquals("", reader.readLine()); } private void dumpOutput() { if (outputDumpEnabled) { System.out.println(writer.toString()); } } }