Skip to content

Commit

Permalink
1. move non-latin generateDomain() back to default CompanyProvider
Browse files Browse the repository at this point in the history
2. call super.configure() from ZhFairyModule and keep the FairyModule fields private.
  • Loading branch information
lhfcws committed Apr 10, 2017
1 parent 65243b8 commit 7a953b2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/codearte/jfairy/FairyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
*/
public abstract class FairyModule extends AbstractModule {

protected final Random random;
private final Random random;

protected final DataMaster dataMaster;
private final DataMaster dataMaster;

public FairyModule(DataMaster dataMaster, Random random) {
this.random = random;
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/io/codearte/jfairy/ZhFairyModule.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package io.codearte.jfairy;

import com.google.inject.assistedinject.FactoryModuleBuilder;
import io.codearte.jfairy.data.DataMaster;
import io.codearte.jfairy.producer.VATIdentificationNumberProvider;
import io.codearte.jfairy.producer.company.CompanyFactory;
import io.codearte.jfairy.producer.company.CompanyProvider;
import io.codearte.jfairy.producer.company.DefaultCompanyProvider;
import io.codearte.jfairy.producer.company.locale.zh.ZhCompanyProvider;
import io.codearte.jfairy.producer.company.locale.zh.ZhVATIdentificationNumberProvider;
import io.codearte.jfairy.producer.payment.DefaultIBANProvider;
import io.codearte.jfairy.producer.payment.IBANFactory;
import io.codearte.jfairy.producer.payment.IBANProvider;
import io.codearte.jfairy.producer.person.*;
import io.codearte.jfairy.producer.person.locale.NoNationalIdentificationNumberFactory;
import io.codearte.jfairy.producer.person.locale.zh.ZhAddressProvider;
Expand All @@ -32,13 +24,7 @@ public ZhFairyModule(DataMaster dataMaster, Random random) {

@Override
protected void configure() {
bind(DataMaster.class).toInstance(dataMaster);
bind(Random.class).toInstance(random);

install(new FactoryModuleBuilder().build(FairyFactory.class));
install(new FactoryModuleBuilder().implement(PersonProvider.class, DefaultPersonProvider.class).build(PersonFactory.class));
install(new FactoryModuleBuilder().implement(CompanyProvider.class, ZhCompanyProvider.class).build(CompanyFactory.class));
install(new FactoryModuleBuilder().implement(IBANProvider.class, DefaultIBANProvider.class).build(IBANFactory.class));
super.configure();

// Social Insurance Number is the same as ID number in China now
bind(NationalIdentificationNumberFactory.class).to(NoNationalIdentificationNumberFactory.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.codearte.jfairy.producer.BaseProducer;
import io.codearte.jfairy.producer.VATIdentificationNumberProvider;
import io.codearte.jfairy.producer.util.TextUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import javax.inject.Inject;
Expand Down Expand Up @@ -57,13 +58,28 @@ public void generateName() {
}
}

/**
* In case of the illegal hostname characters in company name
* and truncate it if it is too long (length > 10) after escape
*
* It is compatible with other non-latin language and will not change the original result for latin language.
*
* P.S. Actually the best way for Chinese here is to use phonetic writing (so as Japanese or Korean)
*/
@Override
public void generateDomain() {
if (domain != null) {
return;
}
domain = TextUtils.stripAccents(StringUtils.strip(StringUtils.deleteWhitespace(name.toLowerCase()), ".").replace("/", ""))
+ "." + dataMaster.getRandomValue(DOMAIN);

String host = TextUtils.stripAccents(StringUtils.strip(StringUtils.deleteWhitespace(name.toLowerCase()), ".").replace("/", ""));
int len1 = host.length();
host = StringEscapeUtils.escapeJava(host).replaceAll("\\\\u", "");
int len2 = host.length();
if (len2 > len1 && len2 > 10)
host = host.substring(0, 10);

domain = host + "." + dataMaster.getRandomValue(DOMAIN);
}

@Override
Expand Down

This file was deleted.

0 comments on commit 7a953b2

Please sign in to comment.