Skip to content

Commit

Permalink
Fix remaining test bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Jan 11, 2016
1 parent 86e4730 commit dba39b4
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 73 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Expand Up @@ -84,6 +84,10 @@
<name>Maria Sparenberg (GitHub:marylla)</name> <name>Maria Sparenberg (GitHub:marylla)</name>
<timezone>Europe/Berlin</timezone> <timezone>Europe/Berlin</timezone>
</contributor> </contributor>
<contributor>
<name>Anton Johansson (GitHub:anton-johansson)</name>
<timezone>Europe/Stockholm</timezone>
</contributor>
</contributors> </contributors>


<issueManagement> <issueManagement>
Expand Down
Expand Up @@ -37,7 +37,6 @@
import com.speedment.config.db.Dbms; import com.speedment.config.db.Dbms;
import com.speedment.config.Document; import com.speedment.config.Document;
import com.speedment.config.db.ForeignKey; import com.speedment.config.db.ForeignKey;
import com.speedment.config.db.ForeignKeyColumn;
import com.speedment.config.db.Index; import com.speedment.config.db.Index;
import com.speedment.config.db.Project; import com.speedment.config.db.Project;
import com.speedment.config.db.Schema; import com.speedment.config.db.Schema;
Expand Down
Expand Up @@ -33,7 +33,6 @@
import com.speedment.event.BeforeGenerate; import com.speedment.event.BeforeGenerate;
import com.speedment.internal.logging.Logger; import com.speedment.internal.logging.Logger;
import com.speedment.internal.logging.LoggerManager; import com.speedment.internal.logging.LoggerManager;
import static com.speedment.internal.util.document.DocumentDbUtil.traverseOver;


import com.speedment.internal.util.Statistics; import com.speedment.internal.util.Statistics;
import java.io.IOException; import java.io.IOException;
Expand All @@ -49,15 +48,14 @@
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static java.util.Objects.requireNonNull;
import static com.speedment.internal.util.document.DocumentDbUtil.traverseOver; import static com.speedment.internal.util.document.DocumentDbUtil.traverseOver;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;


/** /**
* *
* @author pemi * @author pemi
*/ */
public final class MainGenerator implements Consumer<Project> { public class MainGenerator implements Consumer<Project> {


private final static Logger LOGGER = LoggerManager.getLogger(MainGenerator.class); private final static Logger LOGGER = LoggerManager.getLogger(MainGenerator.class);
private final AtomicInteger fileCounter = new AtomicInteger(0); private final AtomicInteger fileCounter = new AtomicInteger(0);
Expand Down Expand Up @@ -113,7 +111,7 @@ public int getFilesCreated() {
return fileCounter.get(); return fileCounter.get();
} }


public static void writeToFile(Project project, Meta<File, String> meta, AtomicInteger fileCounter) { protected void writeToFile(Project project, Meta<File, String> meta, AtomicInteger fileCounter) {
requireNonNull(meta); requireNonNull(meta);


final String fname = project.getPackageLocation() final String fname = project.getPackageLocation()
Expand Down
Expand Up @@ -45,8 +45,8 @@ public BaseDocument(Map<String, Object> data) {
} }


public BaseDocument(Document parent, Map<String, Object> data) { public BaseDocument(Document parent, Map<String, Object> data) {
this.parent = parent; this.parent = requireNonNull(parent);
this.config = data; this.config = requireNonNull(data);
} }


@Override @Override
Expand Down
Expand Up @@ -30,6 +30,7 @@
import com.speedment.internal.util.document.DocumentTranscoder; import com.speedment.internal.util.document.DocumentTranscoder;
import com.speedment.config.db.Schema; import com.speedment.config.db.Schema;
import com.speedment.config.db.trait.HasEnabled; import com.speedment.config.db.trait.HasEnabled;
import com.speedment.config.db.trait.HasName;
import com.speedment.internal.core.config.db.immutable.ImmutableProject; import com.speedment.internal.core.config.db.immutable.ImmutableProject;
import com.speedment.internal.logging.Logger; import com.speedment.internal.logging.Logger;
import com.speedment.internal.logging.LoggerManager; import com.speedment.internal.logging.LoggerManager;
Expand Down Expand Up @@ -116,7 +117,7 @@ public <C extends Document & HasEnabled> T with(final Class<C> type, final Strin
* @return this instance * @return this instance
*/ */
public <C extends Document & HasEnabled> T with(final Class<C> type, final Consumer<C> consumer) { public <C extends Document & HasEnabled> T with(final Class<C> type, final Consumer<C> consumer) {
requireNonNulls(type, consumer); requireNonNulls(type, consumer);
// @SuppressWarnings("unchecked") // @SuppressWarnings("unchecked")
// final Consumer<? extends Document> consumerCasted = (Consumer<? extends Document>) requireNonNull(consumer); // final Consumer<? extends Document> consumerCasted = (Consumer<? extends Document>) requireNonNull(consumer);
withsAll.add(Tuples.of(type, consumer)); withsAll.add(Tuples.of(type, consumer));
Expand All @@ -133,7 +134,7 @@ public <C extends Document & HasEnabled> T with(final Class<C> type, final Consu
*/ */
public T withPassword(final String password) { public T withPassword(final String password) {
// password nullable // password nullable
with(Dbms.class, d -> d.setPassword(password)); with(Dbms.class, dbms -> speedment.getPasswordComponent().put(dbms, password));
return self(); return self();
} }


Expand All @@ -148,7 +149,7 @@ public T withPassword(final String password) {
*/ */
public T withPassword(final String dbmsName, final String password) { public T withPassword(final String dbmsName, final String password) {
// password nullable // password nullable
with(Dbms.class, dbmsName, d -> d.setPassword(password)); with(Dbms.class, dbmsName, dbms -> speedment.getPasswordComponent().put(dbms, password));
return self(); return self();
} }


Expand Down Expand Up @@ -242,9 +243,9 @@ public T withPort(final String dbmsName, final int port) {
* schema name will then be applied after the configuration has been read * schema name will then be applied after the configuration has been read
* and after the System properties have been applied. * and after the System properties have been applied.
* <p> * <p>
* This method is useful for multi-tenant projects where there are several * This method is useful for multi-tenant projects where there are several
* identical schemas separated only by their names. * identical schemas separated only by their names.
* *
* @param schemaName to use for all schemas this project * @param schemaName to use for all schemas this project
* @return this instance * @return this instance
*/ */
Expand All @@ -259,9 +260,9 @@ public T withSchema(final String schemaName) {
* schema name will then be applied after the configuration has been read * schema name will then be applied after the configuration has been read
* and after the System properties have been applied. * and after the System properties have been applied.
* <p> * <p>
* This method is useful for multi-tenant projects where there are several * This method is useful for multi-tenant projects where there are several
* identical schemas separated only by their names. * identical schemas separated only by their names.
* *
* @param oldSchemaName the current name of a schema * @param oldSchemaName the current name of a schema
* @param schemaName to use for the named schema * @param schemaName to use for the named schema
* @return this instance * @return this instance
Expand Down Expand Up @@ -325,17 +326,19 @@ protected void onStop() {
* Builds up the complete Project meta data tree. * Builds up the complete Project meta data tree.
*/ */
protected void loadAndSetProject() { protected void loadAndSetProject() {
try { final Optional<Path> oPath = getConfigPath();
final Optional<Path> oPath = getConfigPath(); final Project project;
final Project project; if (oPath.isPresent()) {
if (oPath.isPresent()) { project = DocumentTranscoder.load(oPath.get());
project = DocumentTranscoder.load(oPath.get()); } else {
} else { project = DocumentTranscoder.load(getSpeedmentApplicationMetadata().getMetadata());
project = DocumentTranscoder.load(getSpeedmentApplicationMetadata().getMetadata()); }
}

// Apply overridden values from the system properties (if any)
// Apply overridden values from the system properties (if any) //final Function<Document, Stream<Node>> traverser = n -> n.asParent().map(p -> p.stream()).orElse(Stream.empty()).map(c -> (Node) c);
//final Function<Document, Stream<Node>> traverser = n -> n.asParent().map(p -> p.stream()).orElse(Stream.empty()).map(c -> (Node) c);
/*
DocumentUtil.traverseOver(project) DocumentUtil.traverseOver(project)
// Trees.traverse(project, traverser, Trees.TraversalOrder.DEPTH_FIRST_PRE) // Trees.traverse(project, traverser, Trees.TraversalOrder.DEPTH_FIRST_PRE)
.forEach((Document node) -> { .forEach((Document node) -> {
Expand Down Expand Up @@ -364,36 +367,36 @@ protected void loadAndSetProject() {
); );
}); });
// Apply overidden item (if any) for all ConfigEntities of a given class */
withsAll.forEach(t2 -> { // Apply overidden item (if any) for all ConfigEntities of a given class
final Class<? extends Document> clazz = t2.get0(); withsAll.forEach(t2 -> {
final Consumer<? extends Document> consumer = t2.get1(); final Class<? extends Document> clazz = t2.get0();
traverseOver(project) @SuppressWarnings("unchecked")
.filter(c -> clazz.isAssignableFrom(c.getClass())) final Consumer<Document> consumer = (Consumer<Document>) t2.get1();
.map(Document.class::cast) traverseOver(project)
.forEachOrdered(consumer::accept); .filter(c -> clazz.isAssignableFrom(c.getClass()))
}); .map(Document.class::cast)

.forEachOrdered(consumer::accept);
// Apply a named overidden item (if any) for all ConfigEntities of a given class });
withsNamed.forEach(t3 -> {
final Class<? extends Node> clazz = t3.get0(); // Apply a named overidden item (if any) for all ConfigEntities of a given class
final String name = t3.get1(); withsNamed.forEach(t3 -> {

final Class<? extends Document> clazz = t3.get0();
@SuppressWarnings("unchecked") final String name = t3.get1();
final Consumer<Node> consumer = (Consumer<Node>) t3.get2();

@SuppressWarnings("unchecked")
project.traverse() final Consumer<Document> consumer = (Consumer<Document>) t3.get2();
.filter(c -> clazz.isAssignableFrom(c.getClass()))
.map(Node.class::cast) traverseOver(project)
.filter(c -> name.equals(c.getRelativeName(Project.class))) .filter(c -> clazz.isAssignableFrom(c.getClass()))
.forEachOrdered(consumer::accept); .filter(HasName.class::isInstance)
}); .map(d -> (Document & HasName) d)

.filter(c -> name.equals(relativeName(c, Project.class)))
speedment.getProjectComponent().setProject(project); .forEachOrdered(consumer::accept);

});
} catch (IOException ioe) {
throw new SpeedmentException("Failed to read config file", ioe); speedment.getProjectComponent().setProject(project);
}
} }


protected <ENTITY> void put(Manager<ENTITY> manager) { protected <ENTITY> void put(Manager<ENTITY> manager) {
Expand Down Expand Up @@ -457,7 +460,7 @@ public Speedment build() {
} }
// Replace the metadata model with an immutable version (potentially much faster) // Replace the metadata model with an immutable version (potentially much faster)
final Project project = speedment.getProjectComponent().getProject(); final Project project = speedment.getProjectComponent().getProject();
final Project immutableProject = new ImmutableProject(project); final Project immutableProject = ImmutableProject.wrap(project);
speedment.getProjectComponent().setProject(immutableProject); speedment.getProjectComponent().setProject(immutableProject);
// //
return speedment; return speedment;
Expand Down
Expand Up @@ -34,7 +34,7 @@
@Api(version = "2.3") @Api(version = "2.3")
public final class DocumentTranscoder { public final class DocumentTranscoder {


public static final String ROOT = "project"; public static final String ROOT = "config";


public static String save(Project project) throws SpeedmentException { public static String save(Project project) throws SpeedmentException {
final Gson gson = new Gson(); final Gson gson = new Gson();
Expand Down
Expand Up @@ -83,7 +83,7 @@ public static Map<String, Object> newDocument(Document document, String key) {


public static <T extends Document & HasName, D extends Document & HasName> String relativeName(D document, final Class<T> from, Function<String, String> nameMapper) { public static <T extends Document & HasName, D extends Document & HasName> String relativeName(D document, final Class<T> from, Function<String, String> nameMapper) {
requireNonNulls(document, from, nameMapper); requireNonNulls(document, from, nameMapper);
final StringJoiner sj = new StringJoiner(".", "", ".").setEmptyValue(""); final StringJoiner sj = new StringJoiner(".").setEmptyValue("");
final List<Document> ancestors = document.ancestors()/*.map(p -> (Parent<?>) p)*/.collect(toList()); final List<Document> ancestors = document.ancestors()/*.map(p -> (Parent<?>) p)*/.collect(toList());
boolean add = false; boolean add = false;
for (final Document parent : ancestors) { for (final Document parent : ancestors) {
Expand Down
Expand Up @@ -16,8 +16,11 @@
*/ */
package com.speedment.internal.core.code.model.java; package com.speedment.internal.core.code.model.java;


import com.speedment.config.db.Project;
import com.speedment.internal.codegen.base.Meta;
import com.speedment.internal.codegen.lang.models.File;
import com.speedment.internal.core.code.MainGenerator; import com.speedment.internal.core.code.MainGenerator;
import org.junit.Ignore; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test; import org.junit.Test;


/** /**
Expand All @@ -31,7 +34,15 @@ public class MainGeneratorTest extends SimpleModelTest {
public void testAccept() { public void testAccept() {
System.out.println("accept"); System.out.println("accept");


final MainGenerator instance = new MainGenerator(speedment); final MainGenerator instance = new MainGenerator(speedment) {

@Override
protected void writeToFile(Project project, Meta<File, String> meta, AtomicInteger fileCounter) {
System.out.println("Processing "+meta.getModel().getName());
// Do nothing on file...
}

};
instance.accept(project); instance.accept(project);
} }


Expand Down
Expand Up @@ -29,8 +29,14 @@
import com.speedment.config.db.Project; import com.speedment.config.db.Project;
import com.speedment.config.db.Schema; import com.speedment.config.db.Schema;
import com.speedment.config.db.Table; import com.speedment.config.db.Table;
import com.speedment.config.db.mapper.TypeMapper;
import com.speedment.config.db.parameters.DbmsType;
import com.speedment.config.db.trait.HasName;
import static com.speedment.internal.codegen.util.Formatting.indent;
import com.speedment.internal.core.config.dbms.MySqlDbmsType;
import com.speedment.internal.core.config.dbms.StandardDbmsType;
import com.speedment.internal.core.config.mapper.identity.StringIdentityMapper;
import com.speedment.internal.core.platform.SpeedmentFactory; import com.speedment.internal.core.platform.SpeedmentFactory;
import static java.util.stream.Collectors.joining;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.Before; import org.junit.Before;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
Expand All @@ -57,30 +63,39 @@ private String quote(String s) {
} }


private String name(String s) { private String name(String s) {
return quote("name") + " : " + quote(s); return quote(HasName.NAME) + " : " + quote(s);
}

private String typeMapper(Class<? extends TypeMapper> tmc) {
return quote(Column.TYPE_MAPPER) + " : " + quote(tmc.getName());
}

private String dbTypeName(String dbmsTypeName) {
return quote(Dbms.TYPE_NAME) + " : " + quote(dbmsTypeName);
} }


private String array(String name, String... s) { private String array(String name, String... s) {
return quote(name) + " : [\n" + Stream.of(s).collect(joining(",\n")) + "\n]"; return quote(name) + " : [\n" + indent(Stream.of(s).collect(joining(",\n"))) + "\n]";
} }


private String objectWithKey(String name, String... s) { private String objectWithKey(String name, String... s) {
return quote(name) + " : " + object(s); return quote(name) + " : " + object(s);
} }


private String object(String... s) { private String object(String... s) {
return "{\n" + Stream.of(s).collect(joining(",\n")) + "\n}"; return "{\n" + indent(Stream.of(s).collect(joining(",\n"))) + "\n}";
} }


@Before @Before
public void setUp() { public void simpleModelTestSetUp() {


final String json = "{" final String json = "{"
+ objectWithKey(DocumentTranscoder.ROOT, + objectWithKey(DocumentTranscoder.ROOT,
name("myProject"), name("myProject"),
array(Project.DBMSES, array(Project.DBMSES,
object( object(
name("myDbms"), name("myDbms"),
dbTypeName(StandardDbmsType.defaultType().getName()),
array(Dbms.SCHEMAS, array(Dbms.SCHEMAS,
object( object(
name("mySchema"), name("mySchema"),
Expand All @@ -89,7 +104,8 @@ public void setUp() {
name(TABLE_NAME), name(TABLE_NAME),
array(Table.COLUMNS, array(Table.COLUMNS,
object( object(
name(COLUMN_NAME) name(COLUMN_NAME),
typeMapper(StringIdentityMapper.class)
) )
), ),
array(Table.PRIMARY_KEY_COLUMNS, array(Table.PRIMARY_KEY_COLUMNS,
Expand All @@ -104,15 +120,14 @@ public void setUp() {
) )
) )
) )
+"}"; + "}";


System.out.println(json); System.out.println(json);

project = DocumentTranscoder.load(json); project = DocumentTranscoder.load(json);

System.out.println(project); System.out.println(project);



speedment = SpeedmentFactory.newSpeedmentInstance(); speedment = SpeedmentFactory.newSpeedmentInstance();
// project = new ProjectImpl(speedment); // project = new ProjectImpl(speedment);
// dbms = project.addNewDbms(); // dbms = project.addNewDbms();
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/ */
package com.speedment.internal.core.code.model.java.entity; package com.speedment.internal.core.code.model.java.entity;


import com.speedment.config.Document;
import com.speedment.internal.core.code.entity.EntityTranslator; import com.speedment.internal.core.code.entity.EntityTranslator;
import com.speedment.internal.codegen.base.Generator; import com.speedment.internal.codegen.base.Generator;
import com.speedment.internal.codegen.java.JavaGenerator; import com.speedment.internal.codegen.java.JavaGenerator;
Expand All @@ -40,8 +41,9 @@ public void testApply() {
final Generator cg = new JavaGenerator(); final Generator cg = new JavaGenerator();


final Table table2 = traverseOver(project, Table.class) final Table table2 = traverseOver(project, Table.class)
.filter(e -> TABLE_NAME.equals(e.getName())) .peek(this::print)
.findAny().get(); .filter(e -> TABLE_NAME.equals(e.getName()))
.findAny().get();


final EntityTranslator instance = new EntityTranslator(speedment, cg, table2); final EntityTranslator instance = new EntityTranslator(speedment, cg, table2);
final File file = instance.get(); final File file = instance.get();
Expand All @@ -52,4 +54,10 @@ public void testApply() {


System.out.println(code.get()); System.out.println(code.get());
} }

private void print(Document d) {
System.out.println(d);
}


} }

0 comments on commit dba39b4

Please sign in to comment.