Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestBuilder initial implementation #23

Merged
merged 1 commit into from
Feb 23, 2022
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>it.n-ess.queryable</groupId>
<artifactId>queryable-maven-plugin</artifactId>
<version>1.0.13</version>
<version>1.0.14</version>
<packaging>maven-plugin</packaging>
<name>Queryable Maven Plugin</name>
<description>Queryable Maven Plugin.</description>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/it/ness/queryable/annotations/QAttachment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.ness.queryable.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE}) //on class field
public @interface QAttachment {
String name() default "";
}
12 changes: 12 additions & 0 deletions src/main/java/it/ness/queryable/annotations/QSlug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.ness.queryable.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE}) //on class field
public @interface QSlug {
String name() default "";
}
13 changes: 13 additions & 0 deletions src/main/java/it/ness/queryable/annotations/TField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.ness.queryable.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE}) //on class field
public @interface TField {
String defaultValue() default "";
String updatedValue() default "";
}
11 changes: 11 additions & 0 deletions src/main/java/it/ness/queryable/annotations/TMandatory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.ness.queryable.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE}) //on class field
public @interface TMandatory {
}
54 changes: 54 additions & 0 deletions src/main/java/it/ness/queryable/builder/TestBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package it.ness.queryable.builder;

import it.ness.queryable.model.TField;
import it.ness.queryable.model.pojo.Parameters;
import it.ness.queryable.util.ModelFiles;
import it.ness.queryable.util.StringUtil;
import org.apache.maven.plugin.logging.Log;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.source.AnnotationSource;
import org.jboss.forge.roaster.model.source.FieldSource;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import java.io.File;
import java.util.List;

public class TestBuilder {

protected static String ANNOTATION_NAME = "TField";

public static void generateSources(ModelFiles mf, Log log, Parameters parameters) throws Exception {
String[] modelFiles = mf.getModelFileNames();
for (String modelFileName : modelFiles) {
String className = StringUtil.getClassNameFromFileName(modelFileName);
if (!mf.excludeClass(className)) {
try {
readModel(log, modelFileName, parameters);
} catch (Exception e) {
log.error(e);
}
}
}
if (log != null) log.info("Done generating sources");
}

private static void readModel(Log log, String modelFileName, Parameters parameters) throws Exception {
System.out.println("Creating model for class : " + modelFileName);
String className = StringUtil.getClassNameFromFileName(modelFileName);
String path = parameters.modelPath;
JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, new File(path, modelFileName));

List<FieldSource<JavaClassSource>> fieldsList = javaClass.getFields();
for (FieldSource<JavaClassSource> field : fieldsList) {
List<AnnotationSource<JavaClassSource>> list = field.getAnnotations();
for (AnnotationSource<JavaClassSource> anno : list) {
if (anno.getName().equals(ANNOTATION_NAME)) {
TField tField = new TField();
tField.defaultValue = anno.getStringValue("defaultValue");
tField.updatedValue = anno.getStringValue("updatedValue");
tField.fieldName = field.getName();
System.out.println(tField.toString());
}
}
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/it/ness/queryable/model/QFilterDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ private String getIntegerSearchMethod() {
"Integer numberof = _integer(\"%s\");" +
"search.filter(\"%s\", Parameters.with(\"%s\", numberof));" +
"}";
return String.format(formatBody, queryName, filterName, queryName, name);
return String.format(formatBody, queryName, queryName, filterName, name);
}

private String getLongSearchMethod() {
String formatBody = "if (nn(\"%s\")) {" +
"Long numberof = _long(\"%s\");" +
"search.filter(\"%s\", Parameters.with(\"%s\", numberof));" +
"}";
return String.format(formatBody, queryName, filterName, queryName, name);
return String.format(formatBody, queryName, queryName, filterName, name);
}

private String getBooleanSearchMethod() {
Expand All @@ -134,12 +134,12 @@ private String getBooleanSearchMethod() {
"Boolean valueof = _boolean(\"%s\");" +
"search.filter(\"%s\", Parameters.with(\"%s\", valueof));" +
"}";
return String.format(formatBody, queryName, filterName, queryName, name);
return String.format(formatBody, queryName, queryName, filterName, name);
}
String formatBody = "if (nn(\"%s\")) {" +
"search.filter(\"%s\", Parameters.with(\"%s\", %s));" +
"}";
return String.format(formatBody, queryName, filterName, queryName, condition);
return String.format(formatBody, queryName, queryName, filterName, condition);
}

private String getBigDecimalSearchMethod() {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/it/ness/queryable/model/TField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.ness.queryable.model;

public class TField {
public String defaultValue;
public String updatedValue;
public String fieldName;

@Override
public String toString() {
return "TField{" +
"defaultValue='" + defaultValue + '\'' +
", updatedValue='" + updatedValue + '\'' +
", fieldName='" + fieldName + '\'' +
'}';
}
}
19 changes: 19 additions & 0 deletions src/main/java/it/ness/queryable/plugin/TestSourceMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.ness.queryable.plugin;

import it.ness.queryable.util.MojoUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

/**
* Queryable is maven plugin for filter defs.
*/
@Mojo(name = "testsources",
defaultPhase = LifecyclePhase.PROCESS_RESOURCES,
threadSafe = true)
public class TestSourceMojo extends QuerableBaseMojo {

public void execute() {
init(getLog());
MojoUtils.testsource(parameters, log);
}
}
13 changes: 13 additions & 0 deletions src/main/java/it/ness/queryable/util/MojoUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.ness.queryable.util;

import it.ness.queryable.builder.QueryableBuilder;
import it.ness.queryable.builder.TestBuilder;
import it.ness.queryable.model.pojo.Data;
import it.ness.queryable.model.pojo.Parameters;
import org.apache.maven.model.Dependency;
Expand Down Expand Up @@ -58,6 +59,18 @@ public static void source(Parameters parameters, Log log) {
}
}

public static void testsource(Parameters parameters, Log log) {
ModelFiles mf = new ModelFiles(parameters.logging ? log : null, parameters);
if (!mf.isParsingSuccessful) {
return;
}
try {
TestBuilder.generateSources(mf, log, parameters);
} catch (Exception e) {
e.printStackTrace();
return;
}
}

public static Model parsePomXmlFileToMavenPomModel(String path) throws Exception {
Model model = null;
Expand Down