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

implement api to add spring-factories #21308

Merged
merged 1 commit into from
Mar 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions generators/app/__snapshots__/generator.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
"databaseTypeSql": true,
"defaultEnvironment": "prod",
"defaultPackaging": "jar",
"devDatabaseExtraOptions": "",
"devDatabasePassword": "",
"devDatabaseType": "h2Disk",
"devDatabaseTypeCouchbase": false,
Expand Down Expand Up @@ -484,6 +485,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
"prettierExtensions": "md,json,yml,html,cjs,mjs,js,ts,tsx,css,scss,java",
"prettierJava": undefined,
"prettierTabWidth": 2,
"prodDatabaseExtraOptions": "",
"prodDatabasePassword": "",
"prodDatabaseType": "postgresql",
"prodDatabaseTypeCouchbase": false,
Expand Down Expand Up @@ -631,6 +633,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"databaseTypeSql": true,
"defaultEnvironment": "prod",
"defaultPackaging": "jar",
"devDatabaseExtraOptions": "",
"devDatabasePassword": "",
"devDatabaseType": "h2Disk",
"devDatabaseTypeCouchbase": false,
Expand Down Expand Up @@ -967,6 +970,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"prettierExtensions": "md,json,yml,html,cjs,mjs,js,ts,tsx,css,scss,java",
"prettierJava": undefined,
"prettierTabWidth": 2,
"prodDatabaseExtraOptions": "",
"prodDatabasePassword": "",
"prodDatabaseType": "postgresql",
"prodDatabaseTypeCouchbase": false,
Expand Down Expand Up @@ -1114,6 +1118,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"databaseTypeSql": true,
"defaultEnvironment": "prod",
"defaultPackaging": "jar",
"devDatabaseExtraOptions": "",
"devDatabasePassword": "",
"devDatabaseType": "h2Disk",
"devDatabaseTypeCouchbase": false,
Expand Down Expand Up @@ -1394,6 +1399,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"prettierExtensions": "md,json,yml,html,java",
"prettierJava": undefined,
"prettierTabWidth": 2,
"prodDatabaseExtraOptions": "",
"prodDatabasePassword": "",
"prodDatabaseType": "postgresql",
"prodDatabaseTypeCouchbase": false,
Expand Down
15 changes: 15 additions & 0 deletions generators/server/cleanup.mts
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,20 @@ export default function cleanupOldServerFilesTask(this: BaseGenerator, taskParam
this.removeFile(`${application.javaPackageSrcDir}web/rest/ClientForwardController.java`);
this.removeFile(`${application.javaPackageTestDir}web/rest/ClientForwardControllerTest.java`);
}
if (
application.databaseTypeSql ||
(application as any).messageBrokerKafka ||
(application as any).cacheProviderRedis ||
application.databaseTypeMongodb ||
application.databaseTypeCassandra ||
(application as any).searchEngineElasticsearch ||
application.databaseTypeCouchbase ||
(application as any).searchEngineCouchbase ||
application.databaseTypeNeo4j
) {
// The condition is too complated, delete and recreate.
this.removeFile(`${application.srcTestResources}META-INF/spring.factories`);
this.removeFile(`${application.javaPackageTestDir}config/TestContainersSpringContextCustomizerFactory.java`);
}
}
}
15 changes: 0 additions & 15 deletions generators/server/files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -714,21 +714,6 @@ export const baseServerFiles = {
},
{
condition: generator =>
generator.databaseTypeSql ||
generator.messageBrokerKafka ||
generator.cacheProviderRedis ||
generator.databaseTypeMongodb ||
generator.databaseTypeCassandra ||
generator.searchEngineElasticsearch ||
generator.databaseTypeCouchbase ||
generator.searchEngineCouchbase ||
generator.databaseTypeNeo4j,
path: SERVER_TEST_RES_DIR,
templates: ['META-INF/spring.factories'],
},
{
condition: generator =>
generator.databaseTypeSql ||
generator.messageBrokerKafka ||
generator.cacheProviderRedis ||
generator.databaseTypeMongodb ||
Expand Down
49 changes: 41 additions & 8 deletions generators/server/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
getJavaValueGeneratorForType as getJavaValueForType,
getPrimaryKeyValue as getPKValue,
generateKeyStore,
addSpringFactory,
} from './support/index.mjs';
import { askForOptionalItems, askForServerSideOpts } from './prompts.mjs';

Expand Down Expand Up @@ -101,7 +102,6 @@ import {
} from '../../jdl/jhipster/index.mjs';
import { stringifyApplicationData } from '../base-application/support/index.mjs';
import { createBase64Secret, createSecret, normalizePathEnd } from '../base/support/index.mjs';
import { getDBCExtraOption as getDBExtraOption } from '../sql/support/index.mjs';
import command from './command.mjs';

const dbTypes = fieldTypes;
Expand Down Expand Up @@ -381,6 +381,29 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator {
application.srcMainDir = MAIN_DIR;
application.srcTestDir = TEST_DIR;
},
registerSpringFactory({ source, application }) {
// We need a flag so we can recreate the file.
let testSpringFactoryCreated = false;
source.addTestSpringFactory = ({ key, value }) => {
const springFactoriesFile = `${application.srcTestResources}META-INF/spring.factories`;
if (!testSpringFactoryCreated) {
testSpringFactoryCreated = true;
this.writeDestination(springFactoriesFile, '');
}
this.editFile(springFactoriesFile, addSpringFactory({ key, value }));
};

// We need a flag so we can recreate the file.
let mainSpringFactoryCreated = false;
source.addMainSpringFactory = ({ key, value }) => {
const springFactoriesFile = `${application.srcMainResources}META-INF/spring.factories`;
if (!mainSpringFactoryCreated) {
mainSpringFactoryCreated = true;
this.writeDestination(springFactoriesFile, '');
}
this.editFile(springFactoriesFile, addSpringFactory({ key, value }));
};
},
});
}

Expand Down Expand Up @@ -620,6 +643,23 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator {

get postWriting() {
return this.asPostWritingTaskGroup({
addTestSpringFactory({ source, application }) {
if (
application.messageBrokerKafka ||
application.cacheProviderRedis ||
application.databaseTypeMongodb ||
application.databaseTypeCassandra ||
application.searchEngineElasticsearch ||
application.databaseTypeCouchbase ||
application.searchEngineCouchbase ||
application.databaseTypeNeo4j
) {
source.addTestSpringFactory({
key: 'org.springframework.test.context.ContextCustomizerFactory',
value: `${application.packageName}.config.TestContainersSpringContextCustomizerFactory`,
});
}
},
packageJsonScripts({ application }) {
const packageJsonConfigStorage = this.packageJson.createStorage('config').createProxy();
packageJsonConfigStorage.backend_port = application.gatewayServerPort || application.serverPort;
Expand Down Expand Up @@ -996,13 +1036,6 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator {
return 'buildSpecification';
}

/**
* @private
*/
getDBCExtraOption(databaseType) {
return getDBExtraOption(databaseType);
}

getJavaValueGeneratorForType(type) {
return getJavaValueForType(type);
}
Expand Down
4 changes: 2 additions & 2 deletions generators/server/support/__snapshots__/needles.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ exports[`generator - server - support - needles generated project should match s
"src/test/java/com/mycompany/myapp/config/SqlTestContainer.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/StaticResourcesWebConfigurerTest.java": {
"src/test/java/com/mycompany/myapp/config/SqlTestContainersSpringContextCustomizerFactory.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/TestContainersSpringContextCustomizerFactory.java": {
"src/test/java/com/mycompany/myapp/config/StaticResourcesWebConfigurerTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/WebConfigurerTest.java": {
Expand Down
2 changes: 1 addition & 1 deletion generators/server/support/database.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getDBTypeFromDBValue } from './database.mjs';

const { CASSANDRA, MONGODB, MYSQL, SQL } = databaseTypes;

describe('generator - base-private', () => {
describe('generator - server - support - database', () => {
describe('getDBTypeFromDBValue', () => {
describe('when called with sql DB name', () => {
it('return SQL', () => {
Expand Down
1 change: 1 addition & 0 deletions generators/server/support/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from './needles.mjs';
export { default as prepareEntity } from './prepare-entity.mjs';
export * from './prepare-entity.mjs';
export { default as prepareField } from './prepare-field.mjs';
export * from './spring-factories.mjs';
export * from './string.mjs';
export * from './templates/field-values.mjs';
export { default as updateLanguagesTask } from './update-languages.mjs';
Expand Down
20 changes: 20 additions & 0 deletions generators/server/support/spring-factories.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import _ from 'lodash';

const { escapeRegExp } = _;

// eslint-disable-next-line import/prefer-default-export
export const addSpringFactory =
({ key, value }) =>
content => {
const match = content?.match(`${escapeRegExp(key)}(\\w*)=`);
if (match) {
const matchEnd = match.index + match[0].length;
content = `${content.slice(0, matchEnd)}\\
${value},${content.slice(matchEnd)}`;
} else {
content = `${content ? `${content.trimEnd()}\n\n` : ''}${key}=\\
${value}
`;
}
return content;
};
35 changes: 35 additions & 0 deletions generators/server/support/spring-factories.spec.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { jestExpect as expect } from 'mocha-expect-snapshot';

import { addSpringFactory } from './spring-factories.mjs';

describe('generator - server - support - spring-factories', () => {
describe('addSpringFactory', () => {
it('should add the first property', () => {
expect(addSpringFactory({ key: 'key.prop', value: 'value' })(null)).toBe(`key.prop=\\
value
`);
});
it('should add a new value to a property', () => {
expect(
addSpringFactory({ key: 'key.prop', value: 'new.value' })(`key.prop=\\
value
`)
).toBe(`key.prop=\\
new.value,\\
value
`);
});
it('should add a new property', () => {
expect(
addSpringFactory({ key: 'key.prop2', value: 'new.value' })(`key.prop=\\
value
`)
).toBe(`key.prop=\\
value

key.prop2=\\
new.value
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ public class TestContainersSpringContextCustomizerFactory implements ContextCust
<%_ if (databaseTypeNeo4j) { _%>
private static Neo4jTestContainer neo4jBean;
<%_ } _%>
<%_ if (devDatabaseTypeMysql || devDatabaseTypeMariadb || devDatabaseTypeMssql || devDatabaseTypePostgres) { _%>
private static SqlTestContainer devTestContainer;
<%_ } _%>
<%_ if (prodDatabaseTypeMysql || prodDatabaseTypeMariadb || prodDatabaseTypeMssql || prodDatabaseTypePostgres) { _%>
private static SqlTestContainer prodTestContainer;
<%_ } _%>

@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes) {
Expand Down Expand Up @@ -127,61 +121,6 @@ public class TestContainersSpringContextCustomizerFactory implements ContextCust
testValues = testValues.and("jhipster.cache.redis.server=redis://" + redisBean.getRedisContainer().getContainerIpAddress() + ":" + redisBean.getRedisContainer().getMappedPort(6379));
}
<%_ } _%>
<%_ if (databaseTypeSql) { _%>
EmbeddedSQL sqlAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedSQL.class);
if (null != sqlAnnotation) {
log.debug("detected the EmbeddedSQL annotation on class {}", testClass.getName());
log.info("Warming up the sql database");
<%_ if (devDatabaseTypeMysql || devDatabaseTypeMariadb || devDatabaseTypeMssql || devDatabaseTypePostgres) { _%>
if (Arrays.asList(context.getEnvironment().getActiveProfiles()).contains("test" + JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) {
if (null == devTestContainer) {
try {
Class<? extends SqlTestContainer> containerClass = (Class<? extends SqlTestContainer>) Class.forName(this.getClass().getPackageName() + ".<% if (devDatabaseTypeMysql) { %>Mysql<% } else if (devDatabaseTypeMariadb) { %>Mariadb<% } else if (devDatabaseTypeMssql) { %>MsSql<% } else if (devDatabaseTypePostgres) { %>PostgreSql<% } %>TestContainer");
devTestContainer = beanFactory.createBean(containerClass);
beanFactory.registerSingleton(containerClass.getName(), devTestContainer);
// ((DefaultListableBeanFactory)beanFactory).registerDisposableBean(containerClass.getName(), devTestContainer);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
<%_ if (reactive) { _%>
testValues = testValues.and("spring.r2dbc.url=" + devTestContainer.getTestContainer().getJdbcUrl().replace("jdbc", "r2dbc")<% if (devDatabaseTypeMysql) { %>.replace("mysql", "mariadb")<% } else if (devDatabaseTypeMssql) { %>.replace(";encrypt=false", "")<% } %> + "<%- this.getDBCExtraOption(devDatabaseType) %>");
testValues = testValues.and("spring.r2dbc.username=" + devTestContainer.getTestContainer().getUsername());
testValues = testValues.and("spring.r2dbc.password=" + devTestContainer.getTestContainer().getPassword());
testValues = testValues.and("spring.liquibase.url=" + devTestContainer.getTestContainer().getJdbcUrl() + "<%- this.getDBCExtraOption(devDatabaseType) %>" );
<%_ } else { _%>
testValues = testValues.and("spring.datasource.url=" + devTestContainer.getTestContainer().getJdbcUrl() + "<%- this.getDBCExtraOption(devDatabaseType) %>");
testValues = testValues.and("spring.datasource.username=" + devTestContainer.getTestContainer().getUsername());
testValues = testValues.and("spring.datasource.password=" + devTestContainer.getTestContainer().getPassword());
<%_ } _%>
}
<%_ } _%>
<%_ if (prodDatabaseTypeMysql || prodDatabaseTypeMariadb || prodDatabaseTypeMssql || prodDatabaseTypePostgres) { _%>
if (Arrays.asList(context.getEnvironment().getActiveProfiles()).contains("test" + JHipsterConstants.SPRING_PROFILE_PRODUCTION)) {
if (null == prodTestContainer) {
try {
Class<? extends SqlTestContainer> containerClass = (Class<? extends SqlTestContainer>) Class.forName(this.getClass().getPackageName() + ".<% if (prodDatabaseTypeMysql) { %>Mysql<% } else if (prodDatabaseTypeMariadb) { %>Mariadb<% } else if (prodDatabaseTypeMssql) { %>MsSql<% } else if (prodDatabaseTypePostgres) { %>PostgreSql<% } %>TestContainer");
prodTestContainer = beanFactory.createBean(containerClass);
beanFactory.registerSingleton(containerClass.getName(), prodTestContainer);
// ((DefaultListableBeanFactory)beanFactory).registerDisposableBean(containerClass.getName(), prodTestContainer);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
<%_ if (reactive) { _%>
testValues = testValues.and("spring.r2dbc.url=" + prodTestContainer.getTestContainer().getJdbcUrl().replace("jdbc", "r2dbc")<% if (prodDatabaseTypeMysql) { %>.replace("mysql", "mariadb")<% } else if (prodDatabaseTypeMssql) { %>.replace(";encrypt=false", "")<% } %> + "<%- this.getDBCExtraOption(prodDatabaseType) %>");
testValues = testValues.and("spring.r2dbc.username=" + prodTestContainer.getTestContainer().getUsername());
testValues = testValues.and("spring.r2dbc.password=" + prodTestContainer.getTestContainer().getPassword());
testValues = testValues.and("spring.liquibase.url=" + prodTestContainer.getTestContainer().getJdbcUrl() + "<%- this.getDBCExtraOption(prodDatabaseType) %>");
<%_ } else { _%>
testValues = testValues.and("spring.datasource.url=" + prodTestContainer.getTestContainer().getJdbcUrl() + "<%- this.getDBCExtraOption(prodDatabaseType) %>");
testValues = testValues.and("spring.datasource.username=" + prodTestContainer.getTestContainer().getUsername());
testValues = testValues.and("spring.datasource.password=" + prodTestContainer.getTestContainer().getPassword());
<%_ } _%>
}
<%_ } _%>
}
<%_ } _%>
<%_ if (databaseTypeCassandra) { _%>
EmbeddedCassandra cassandraAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedCassandra.class);
if (null != cassandraAnnotation) {
Expand Down

This file was deleted.