HHH-16887 smoke test HQL validation

This commit is contained in:
Gavin King 2023-07-04 17:16:24 +02:00
parent 28b1670d18
commit a2defad7a4
9 changed files with 105 additions and 14 deletions

View File

@ -1,8 +1,7 @@
package org.hibernate.jpamodelgen.test.querymethod; package org.hibernate.jpamodelgen.test.hqlsql;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.NamedEntityGraph;
@Entity @Entity
public class Book { public class Book {

View File

@ -1,4 +1,4 @@
package org.hibernate.jpamodelgen.test.querymethod; package org.hibernate.jpamodelgen.test.hqlsql;
import jakarta.persistence.TypedQuery; import jakarta.persistence.TypedQuery;
import org.hibernate.annotations.processing.HQL; import org.hibernate.annotations.processing.HQL;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.jpamodelgen.test.querymethod; package org.hibernate.jpamodelgen.test.hqlsql;
import org.hibernate.jpamodelgen.test.util.CompilationTest; import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestUtil; import org.hibernate.jpamodelgen.test.util.TestUtil;

View File

@ -0,0 +1,11 @@
package org.hibernate.jpamodelgen.test.hqlvalidation;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Book {
@Id String isbn;
String title;
String text;
}

View File

@ -0,0 +1,21 @@
package org.hibernate.jpamodelgen.test.hqlvalidation;
import jakarta.persistence.TypedQuery;
import org.hibernate.annotations.processing.HQL;
import org.hibernate.annotations.processing.SQL;
import java.util.List;
public interface Dao1 {
@HQL("from Book where title like ?1")
TypedQuery<Book> findByTitle(String title);
@HQL("from Book where title like ?1 order by title fetch first ?2 rows only")
List<Book> findFirstNByTitle(String title, int N);
@HQL("from Boook where isbn = :isbn")
Book findByIsbn(String isbn);
@SQL("select * from Book where isbn = :isbn")
Book findByIsbnNative(String isbn);
}

View File

@ -0,0 +1,21 @@
package org.hibernate.jpamodelgen.test.hqlvalidation;
import jakarta.persistence.TypedQuery;
import org.hibernate.annotations.processing.HQL;
import org.hibernate.annotations.processing.SQL;
import java.util.List;
public interface Dao2 {
@HQL("from Book where tile like ?1")
TypedQuery<Book> findByTitle(String title);
@HQL("from Book where title like ?1 order by title fetch first ?2 rows only")
List<Book> findFirstNByTitle(String title, int N);
@HQL("from Book where isbn = :isbn")
Book findByIsbn(String isbn);
@SQL("select * from Book where isbn = :isbn")
Book findByIsbnNative(String isbn);
}

View File

@ -0,0 +1,36 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpamodelgen.test.hqlvalidation;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.IgnoreCompilationErrors;
import org.hibernate.jpamodelgen.test.util.TestUtil;
import org.hibernate.jpamodelgen.test.util.WithClasses;
import org.junit.Test;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoMetamodelClassGeneratedFor;
/**
* @author Gavin King
*/
public class ValidationTest extends CompilationTest {
@Test @IgnoreCompilationErrors
@WithClasses({ Book.class, Dao1.class })
public void testError1() {
System.out.println( TestUtil.getMetaModelSourceAsString( Dao1.class ) );
assertNoMetamodelClassGeneratedFor( Book.class );
assertNoMetamodelClassGeneratedFor( Dao1.class );
}
@Test @IgnoreCompilationErrors
@WithClasses({ Book.class, Dao2.class })
public void testError2() {
System.out.println( TestUtil.getMetaModelSourceAsString( Dao2.class ) );
assertNoMetamodelClassGeneratedFor( Book.class );
assertNoMetamodelClassGeneratedFor( Dao2.class );
}
}

View File

@ -6,7 +6,6 @@
*/ */
package org.hibernate.jpamodelgen.test.util; package org.hibernate.jpamodelgen.test.util;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -35,10 +34,10 @@ public class CompilationRunner extends BlockJUnit4ClassRunner {
public CompilationRunner(Class<?> clazz) throws InitializationError { public CompilationRunner(Class<?> clazz) throws InitializationError {
super( clazz ); super( clazz );
this.testEntities = new ArrayList<Class<?>>(); this.testEntities = new ArrayList<>();
this.preCompileEntities = new ArrayList<Class<?>>(); this.preCompileEntities = new ArrayList<>();
this.mappingFiles = new ArrayList<String>(); this.mappingFiles = new ArrayList<>();
this.processorOptions = new HashMap<String, String>(); this.processorOptions = new HashMap<>();
Package pkg = clazz.getPackage(); Package pkg = clazz.getPackage();
this.packageName = pkg != null ? pkg.getName() : null; this.packageName = pkg != null ? pkg.getName() : null;

View File

@ -24,11 +24,7 @@ import javax.tools.Diagnostic;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
@ -165,6 +161,14 @@ public class TestUtil {
assertNotNull( getMetamodelClassFor( clazz ) ); assertNotNull( getMetamodelClassFor( clazz ) );
} }
public static void assertNoMetamodelClassGeneratedFor(Class<?> clazz) {
try {
getMetamodelClassFor( clazz );
fail();
}
catch (AssertionError ae) {}
}
/** /**
* Deletes recursively all files found in the output directory for the annotation processor. * Deletes recursively all files found in the output directory for the annotation processor.
*/ */