Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 39 additions & 70 deletions ant/src/main/java/org/hibernate/tool/ant/JavaFormatterTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.tools.ant.BuildException;
Expand All @@ -37,77 +35,49 @@

public class JavaFormatterTask extends Task {

private List<FileSet> fileSets = new ArrayList<FileSet>();
private final List<FileSet> fileSets = new ArrayList<FileSet>();
private boolean failOnError;
private File configurationFile;


public void addConfiguredFileSet(FileSet fileSet) {
fileSets.add(fileSet);
}

public void setConfigurationFile(File configurationFile) {
this.configurationFile = configurationFile;
}

private Properties readConfig(File cfgfile) throws IOException {
BufferedInputStream stream = null;
try {
stream = new BufferedInputStream(new FileInputStream(cfgfile));
final Properties settings = new Properties();
settings.load(stream);
return settings;
} catch (IOException e) {
throw e;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {

}
}
}
}
try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(cfgfile))) {
final Properties settings = new Properties();
settings.load(stream);
return settings;
}
}


public void execute() throws BuildException {

Map<Object, Object> settings = null;
if(configurationFile!=null) {
try {
settings = readConfig( configurationFile );
}
catch (IOException e) {
throw new BuildException("Could not read configurationfile " + configurationFile, e);
}
}

File[] files = getFiles();

int failed = 0;

if(files.length>0) {

DefaultJavaPrettyPrinterStrategy formatter = new DefaultJavaPrettyPrinterStrategy(settings);
for (int i = 0; i < files.length; i++) {
File file = files[i];
try {
boolean ok = formatter.formatFile( file );
if(!ok) {
failed++;
getProject().log(this, "Formatting failed - skipping " + file, Project.MSG_WARN);
} else {
getProject().log(this, "Formatted " + file, Project.MSG_VERBOSE);
}
} catch(RuntimeException ee) {
failed++;
if(failOnError) {
throw new BuildException("Java formatting failed on " + file, ee);
} else {
getProject().log(this, "Java formatting failed on " + file + ", " + ee.getLocalizedMessage(), Project.MSG_ERR);
}
}
}
DefaultJavaPrettyPrinterStrategy formatter = new DefaultJavaPrettyPrinterStrategy();
for (File file : files) {
try {
boolean ok = formatter.formatFile(file);
if (!ok) {
failed++;
getProject().log(this, "Formatting failed - skipping " + file, Project.MSG_WARN);
} else {
getProject().log(this, "Formatted " + file, Project.MSG_VERBOSE);
}
} catch (RuntimeException ee) {
failed++;
if (failOnError) {
throw new BuildException("Java formatting failed on " + file, ee);
} else {
getProject().log(this, "Java formatting failed on " + file + ", " + ee.getLocalizedMessage(), Project.MSG_ERR);
}
}
}
}

getProject().log( this, "Java formatting of " + files.length + " files completed. Skipped " + failed + " file(s).", Project.MSG_INFO );
Expand All @@ -117,23 +87,22 @@ public void execute() throws BuildException {
private File[] getFiles() {

List<File> files = new LinkedList<File>();
for ( Iterator<FileSet> i = fileSets.iterator(); i.hasNext(); ) {
for (FileSet fs : fileSets) {

FileSet fs = i.next();
DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
DirectoryScanner ds = fs.getDirectoryScanner(getProject());

String[] dsFiles = ds.getIncludedFiles();
for (int j = 0; j < dsFiles.length; j++) {
File f = new File(dsFiles[j]);
if ( !f.isFile() ) {
f = new File( ds.getBasedir(), dsFiles[j] );
}
String[] dsFiles = ds.getIncludedFiles();
for (String dsFile : dsFiles) {
File f = new File(dsFile);
if (!f.isFile()) {
f = new File(ds.getBasedir(), dsFile);
}

files.add( f );
}
}
files.add(f);
}
}

return (File[]) files.toArray(new File[files.size()]);
return (File[]) files.toArray(new File[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@
*/
package org.hibernate.tool.orm.jbt.api.wrp;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import org.hibernate.mapping.Table;
import org.hibernate.tool.api.reveng.RevengStrategy;
import org.hibernate.tool.api.reveng.TableIdentifier;
import org.hibernate.tool.internal.export.common.GenericExporter;
import org.hibernate.tool.internal.reveng.strategy.DelegatingStrategy;
import org.hibernate.tool.internal.reveng.strategy.OverrideRepository;
import org.hibernate.tool.internal.reveng.strategy.TableFilter;
Expand All @@ -39,6 +36,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class OverrideRepositoryWrapperTest {

private static final String HIBERNATE_REVERSE_ENGINEERING_XML =
Expand All @@ -47,7 +46,7 @@ public class OverrideRepositoryWrapperTest {
" '-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN' "+
" 'http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd'>"+
"<hibernate-reverse-engineering> "+
" <table name='FOO'/> "+
" <table name='FOO' class='TheFoo'/> "+
"</hibernate-reverse-engineering> ";

private OverrideRepositoryWrapper overrideRepositoryWrapper = null;
Expand All @@ -72,20 +71,18 @@ public void testAddFile() throws Exception {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(HIBERNATE_REVERSE_ENGINEERING_XML);
fileWriter.close();
Field tablesField = wrappedOverrideRepository.getClass().getDeclaredField("tables");
tablesField.setAccessible(true);
Object object = tablesField.get(wrappedOverrideRepository);
List<?> tables = (List<?>)object;
assertNotNull(tables);
assertTrue(tables.isEmpty());
Field tableToClassNameField = wrappedOverrideRepository.getClass().getDeclaredField("tableToClassName");
tableToClassNameField.setAccessible(true);
Object object = tableToClassNameField.get(wrappedOverrideRepository);
Method getMethod = object.getClass().getDeclaredMethod("get", TableIdentifier.class);
assertNotNull(getMethod);
getMethod.setAccessible(true);
TableIdentifier ti = TableIdentifier.create(null, null, "FOO");
Object className = getMethod.invoke(object,ti);
assertNull(className);
overrideRepositoryWrapper.addFile(file);
object = tablesField.get(wrappedOverrideRepository);
tables = (List<?>)object;
assertNotNull(tables);
assertFalse(tables.isEmpty());
Table table = (Table)tables.get(0);
assertNotNull(table);
assertEquals("FOO", table.getName());
className = getMethod.invoke(object,ti);
assertEquals("TheFoo", className);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.google.googlejavaformat.java.FormatterException;

public class DefaultJavaPrettyPrinterStrategy {

public DefaultJavaPrettyPrinterStrategy(Map<Object, Object> settings) {}

public boolean formatFile(File file) {
try {
Expand All @@ -40,5 +38,5 @@ public boolean formatFile(File file) {
throw new RuntimeException(e);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,33 @@

import org.hibernate.mapping.Table;

import java.util.Objects;

/**
* Identifier used for the full name of tables.
* @author max
*
*/
public class TableIdentifier {

public static TableIdentifier create(Table table) {
return new TableIdentifier(table.getCatalog(), table.getSchema(), table.getName() );
}

public static TableIdentifier create(String catalog, String schema, String name) {
return new TableIdentifier(catalog, schema, name);
}

private final String catalog;
private final String schema;
private final String name;

private TableIdentifier(String catalog, String schema, String name) {
this.catalog = (catalog==null?null:catalog.intern() );
this.schema = (schema==null?null:schema.intern() );
this.name = (name==null?null:name.intern() );
this.name = (name==null?null:name.intern() );
}

public String getCatalog() {
return catalog;
}
Expand All @@ -53,47 +55,41 @@ public String getName() {
public String getSchema() {
return schema;
}

public boolean equals(Object obj) {
return obj instanceof TableIdentifier && equals( (TableIdentifier)obj);
return obj instanceof TableIdentifier
&& isEqualIdentifier( (TableIdentifier)obj);
}
public boolean equals(TableIdentifier otherIdentifier) {

private boolean isEqualIdentifier(TableIdentifier otherIdentifier) {
if (otherIdentifier==null) return false;
if (this==otherIdentifier) return true;
if (equals(this.name, otherIdentifier.name) ) {
if(equals(this.schema, otherIdentifier.schema) ) {
return equals(this.catalog, otherIdentifier.catalog);

if (Objects.equals(this.name, otherIdentifier.name) ) {
if(Objects.equals(this.schema, otherIdentifier.schema) ) {
return Objects.equals(this.catalog, otherIdentifier.catalog);
}
}

return false;
}

public int hashCode() {
int result = 13;
result = 37 * result + ( name==null ? 0 : name.hashCode() );
result = 37 * result + ( schema==null ? 0 : schema.hashCode() );
result = 37 * result + ( catalog==null ? 0 : catalog.hashCode() );

return result;
}

private boolean equals(String left, String right) {
if (left==right) return true;
if (left==null || right==null) return false;
return left.equals(right);
}


public String toString() {
StringBuffer buf = new StringBuffer()
.append( "TableIdentifier" )
.append('(');
if ( getCatalog()!=null ) buf.append( getCatalog() + "." );
if ( getSchema()!=null ) buf.append( getSchema()+ ".");
StringBuilder buf = new StringBuilder()
.append( "TableIdentifier" )
.append('(');
if ( getCatalog()!=null ) buf.append( getCatalog() ).append( "." );
if ( getSchema()!=null ) buf.append( getSchema() ).append( "." );
buf.append( getName() ).append(')');
return buf.toString();
}

}
Loading