Skip to content

Commit

Permalink
# IGNITE-32: Tests for db metadata parsing and XML generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
akuznetsov-gridgain committed Jan 29, 2015
1 parent e28ddd2 commit e716d82
Show file tree
Hide file tree
Showing 8 changed files with 606 additions and 127 deletions.
Expand Up @@ -49,9 +49,9 @@ private static void addComment(Document doc) {
" contributor license agreements. See the NOTICE file distributed with\n" +
" this work for additional information regarding copyright ownership.\n" +
" The ASF licenses this file to You under the Apache License, Version 2.0\n" +
" (the \"License\" + you may not use this file except in compliance with\n" +
" (the \"License\"); you may not use this file except in compliance with\n" +
" the License. You may obtain a copy of the License at\n\n" +
" http://www.apache.org/licenses/LICENSE-2.0\n\n" +
" http://www.apache.org/licenses/LICENSE-2.0\n\n" +
" Unless required by applicable law or agreed to in writing, software\n" +
" distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
Expand Down
Expand Up @@ -375,7 +375,7 @@ public CacheQueryTypeDescriptor descriptor() {
}

/**
* @return {@code true}
* @return {@code true} if type of field is primitive type.
*/
public boolean primitive() {
return PRIMITIVES.contains(javaTypeName());
Expand Down
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.schema.load;

import junit.framework.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.schema.model.PojoDescriptor;
import org.apache.ignite.schema.parser.DatabaseMetadataParser;
import org.apache.ignite.schema.ui.*;

import java.io.*;
import java.sql.*;
import java.util.List;

import static org.apache.ignite.schema.ui.MessageBox.Result.*;

/**
* Base functional for ignite-schema-loader tests.
*/
public class BaseSchemaLoaderSelfTest extends TestCase {
/** Default connection URL (value is <tt>jdbc:h2:mem:jdbcCacheStore;DB_CLOSE_DELAY=-1</tt>). */
private static final String DFLT_CONN_URL = "jdbc:h2:mem:autoCacheStore;DB_CLOSE_DELAY=-1";

/** Path to temp folder where generated POJOs will be saved. */
protected static final String OUT_DIR_PATH =
String.format("%s/ignite-schema-loader/out", System.getProperty("java.io.tmpdir"));

/** Auto confirmation of file conflicts. */
protected ConfirmCallable askOverwrite = new ConfirmCallable(null, "") {
@Override public MessageBox.Result confirm(String msg) {
return YES_TO_ALL;
}
};

/** List of generated for test database POJO objects. */
protected List<PojoDescriptor> pojoLst;

/** {@inheritDoc} */
@Override public void setUp() throws Exception {
Class.forName("org.h2.Driver");

Connection conn = DriverManager.getConnection(DFLT_CONN_URL, "sa", "");

Statement stmt = conn.createStatement();

stmt.executeUpdate("CREATE TABLE PRIMITIVES (pk INTEGER PRIMARY KEY, " +
" boolCol BOOLEAN NOT NULL," +
" byteCol TINYINT NOT NULL," +
" shortCol SMALLINT NOT NULL," +
" intCol INTEGER NOT NULL, " +
" longCol BIGINT NOT NULL," +
" floatCol REAL NOT NULL," +
" doubleCol DOUBLE NOT NULL," +
" doubleCol2 DOUBLE NOT NULL, " +
" bigDecimalCol DECIMAL(10, 0)," +
" strCol VARCHAR(10)," +
" dateCol DATE," +
" timeCol TIME," +
" tsCol TIMESTAMP, " +
" arrCol BINARY(10))");

stmt.executeUpdate("CREATE TABLE OBJECTS (pk INTEGER PRIMARY KEY, " +
" boolCol BOOLEAN," +
" byteCol TINYINT," +
" shortCol SMALLINT," +
" intCol INTEGER," +
" longCol BIGINT," +
" floatCol REAL," +
" doubleCol DOUBLE," +
" doubleCol2 DOUBLE," +
" bigDecimalCol DECIMAL(10, 0)," +
" strCol VARCHAR(10), " +
" dateCol DATE," +
" timeCol TIME," +
" tsCol TIMESTAMP," +
" arrCol BINARY(10))");

conn.commit();

U.closeQuiet(stmt);

pojoLst = DatabaseMetadataParser.parse(conn, false);

U.closeQuiet(conn);
}

/**
* Compare files by lines.
*
* @param exp Stream to read of expected file from test resources.
* @param generated Generated file instance.
* @param excludePtrn Marker string to exclude lines from comparing.
* @return true if generated file correspond to expected.
*/
protected boolean compareFilesInt(InputStream exp, File generated, String excludePtrn) {
try (BufferedReader baseReader = new BufferedReader(new InputStreamReader(exp))) {
try (BufferedReader generatedReader = new BufferedReader(new FileReader(generated))) {
String baseLine;

while ((baseLine = baseReader.readLine()) != null) {
String generatedLine = generatedReader.readLine();

if (!baseLine.equals(generatedLine) && !baseLine.contains(excludePtrn)
&& !generatedLine.contains(excludePtrn)) {
System.out.println("Expected: " + baseLine);
System.out.println("Generated: " + generatedLine);

return false;
}
}

return true;
}
} catch (IOException e) {
e.printStackTrace();

return false;
}
}
}
Expand Up @@ -17,151 +17,54 @@

package org.apache.ignite.schema.load.generator;

import junit.framework.TestCase;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.schema.generator.PojoGenerator;
import org.apache.ignite.schema.load.BaseSchemaLoaderSelfTest;
import org.apache.ignite.schema.model.PojoDescriptor;
import org.apache.ignite.schema.parser.DatabaseMetadataParser;
import org.apache.ignite.schema.ui.ConfirmCallable;
import org.apache.ignite.schema.ui.MessageBox;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;

import static org.apache.ignite.schema.ui.MessageBox.Result.YES_TO_ALL;
import java.io.File;

/**
* Tests for POJO generator.
*/
public class PojoGeneratorSelfTest extends TestCase {
/** Default connection URL (value is <tt>jdbc:h2:mem:jdbcCacheStore;DB_CLOSE_DELAY=-1</tt>). */
protected static final String DFLT_CONN_URL = "jdbc:h2:mem:autoCacheStore;DB_CLOSE_DELAY=-1";

/** File separator. */
private static final String PS = File.separator;

/** Path to temp folder where generated POJOs will be saved. */
private static final String OUT_DIR_PATH = System.getProperty("java.io.tmpdir") + PS +
"ignite-schema-loader" + PS + "out";

public class PojoGeneratorSelfTest extends BaseSchemaLoaderSelfTest {
/** Marker string to skip date generation while comparing.*/
private static final String GEN_PTRN = "Code generated by Apache Ignite Schema Load utility";

/** Connection to in-memory H2 database. */
private Connection conn;

/** {@inheritDoc} */
@Override public void setUp() throws Exception {
Class.forName("org.h2.Driver");

conn = DriverManager.getConnection(DFLT_CONN_URL, "sa", "");

Statement stmt = conn.createStatement();

stmt.executeUpdate("CREATE TABLE PRIMITIVES (pk INTEGER PRIMARY KEY, " +
" boolCol BOOLEAN NOT NULL," +
" byteCol TINYINT NOT NULL," +
" shortCol SMALLINT NOT NULL," +
" intCol INTEGER NOT NULL, " +
" longCol BIGINT NOT NULL," +
" floatCol REAL NOT NULL," +
" doubleCol DOUBLE NOT NULL," +
" doubleCol2 DOUBLE NOT NULL, " +
" bigDecimalCol DECIMAL(10, 0)," +
" strCol VARCHAR(10)," +
" dateCol DATE," +
" timeCol TIME," +
" tsCol TIMESTAMP, " +
" arrCol BINARY(10))");

stmt.executeUpdate("CREATE TABLE OBJECTS (pk INTEGER PRIMARY KEY, " +
" boolCol BOOLEAN," +
" byteCol TINYINT," +
" shortCol SMALLINT," +
" intCol INTEGER," +
" longCol BIGINT," +
" floatCol REAL," +
" doubleCol DOUBLE," +
" doubleCol2 DOUBLE," +
" bigDecimalCol DECIMAL(10, 0)," +
" strCol VARCHAR(10), " +
" dateCol DATE," +
" timeCol TIME," +
" tsCol TIMESTAMP," +
" arrCol BINARY(10))");

conn.commit();

U.closeQuiet(stmt);
}

/** {@inheritDoc} */
@Override public void tearDown() throws Exception {
U.closeQuiet(conn);
}

/**
* Test that POJOs generated correctly.
*/
public void testPojoGeneration() throws Exception {
List<PojoDescriptor> pojos = DatabaseMetadataParser.parse(conn, true);

ConfirmCallable askOverwrite = new ConfirmCallable(null, "") {
@Override public MessageBox.Result confirm(String msg) {
return YES_TO_ALL;
}
};

String pkg = "org.apache.ignite.schema.load.model";
String intPath = "org" + PS + "apache" + PS + "ignite" + PS + "schema" + PS + "load" + PS + "model";
String intPath = "org/apache/ignite/schema/load/model";

for (PojoDescriptor pojo : pojos) {
if (!pojo.valueClassName().isEmpty()) {
Boolean containsSchema = false;

for (PojoDescriptor pojo : pojoLst) {
if (pojo.valueClassName().isEmpty())
containsSchema = true;
else {
PojoGenerator.generate(pojo, OUT_DIR_PATH, pkg, true, true, askOverwrite);

assertTrue("Generated POJO files does not accordance to predefined for type " + pojo.keyClassName(),
compareFiles(pojo.keyClassName(), intPath));
assertTrue("Generated key class POJO content is differ from expected for type " + pojo.keyClassName(),
compareFiles(pojo.keyClassName(), intPath, GEN_PTRN));

assertTrue("Generated POJO files does not accordance to predefined for type " + pojo.valueClassName(),
compareFiles(pojo.valueClassName(), intPath));
assertTrue("Generated value class POJO content is differ from expected for type " + pojo.valueClassName(),
compareFiles(pojo.valueClassName(), intPath, GEN_PTRN));
}
}

assertTrue("Generated POJOs does not contains schema.", containsSchema);
}

/**
* @param typeName Type name.
* @param intPath Int path.
* @return {@code true} if generated POJO as expected.
*/
private boolean compareFiles(String typeName, String intPath) {
String relPath = intPath + PS + typeName;

try (BufferedReader baseReader = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("/" + relPath + ".txt")))) {
try (BufferedReader generatedReader = new BufferedReader(new FileReader(OUT_DIR_PATH + PS + relPath + ".java"))) {
String baseLine;

while ((baseLine = baseReader.readLine()) != null) {
String generatedLine = generatedReader.readLine();

if (!baseLine.equals(generatedLine) && !baseLine.contains(GEN_PTRN)
&& !generatedLine.contains(GEN_PTRN)) {
System.out.println("Expected: " + baseLine);
System.out.println("Generated: " + generatedLine);

return false;
}
}

return true;
}
} catch (IOException e) {
e.printStackTrace();
}
private boolean compareFiles(String typeName, String intPath, String excludePtrn) {
String relPath = intPath + "/" + typeName;

return false;
return compareFilesInt(getClass().getResourceAsStream("/" + relPath + ".txt"),
new File(OUT_DIR_PATH + "/" + relPath + ".java"), excludePtrn);
}
}
Expand Up @@ -17,9 +17,36 @@

package org.apache.ignite.schema.load.generator;

import org.apache.ignite.cache.query.*;
import org.apache.ignite.schema.generator.*;
import org.apache.ignite.schema.load.*;
import org.apache.ignite.schema.model.*;

import java.io.*;
import java.util.*;

/**
* Tests for XML generator.
*/
public class XmlGeneratorSelfTest {
public class XmlGeneratorSelfTest extends BaseSchemaLoaderSelfTest {
/**
* Test that XML generated correctly.
*/
public void testXmlGeneration() throws Exception {
Collection<CacheQueryTypeMetadata> all = new ArrayList<>();

// Generate XML.
for (PojoDescriptor pojo : pojoLst)
if (!pojo.valueClassName().isEmpty())
all.add(pojo.metadata(true));

String fileName = "Ignite.xml";

XmlGenerator.generate("org.apache.ignite.schema.load.model", all, new File(OUT_DIR_PATH, fileName),
askOverwrite);

assertTrue("Generated XML file content is differ from expected one",
compareFilesInt(getClass().getResourceAsStream("/org/apache/ignite/schema/load/model/" + fileName),
new File(OUT_DIR_PATH + "/" + fileName), "XML generated by Apache Ignite Schema Load utility"));
}
}

0 comments on commit e716d82

Please sign in to comment.