added in testing
This commit is contained in:
@@ -3,6 +3,8 @@ package com.github.javafaker.service;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -12,7 +14,30 @@ public class FakeValues implements FakeValuesInterface {
|
||||
private final String path;
|
||||
private Map values;
|
||||
|
||||
public FakeValues(Locale locale, String filename, String path) {
|
||||
FakeValues(Locale locale) {
|
||||
this(locale, getFilename(locale), getFilename(locale));
|
||||
}
|
||||
|
||||
private static String getFilename(Locale locale) {
|
||||
final StringBuilder filename = new StringBuilder(language(locale));
|
||||
if (!"".equals(locale.getCountry())) {
|
||||
filename.append("-").append(locale.getCountry());
|
||||
}
|
||||
return filename.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* If you new up a locale with "he", it gets converted to "iw" which is old.
|
||||
* This addresses that unfortunate condition.
|
||||
*/
|
||||
private static String language(Locale l) {
|
||||
if (l.getLanguage().equals("iw")) {
|
||||
return "he";
|
||||
}
|
||||
return l.getLanguage();
|
||||
}
|
||||
|
||||
FakeValues(Locale locale, String filename, String path) {
|
||||
this.locale = locale;
|
||||
this.filename = filename;
|
||||
this.path = path;
|
||||
@@ -28,21 +53,23 @@ public class FakeValues implements FakeValuesInterface {
|
||||
}
|
||||
|
||||
private Map loadValues() {
|
||||
String path = "/" + locale.getLanguage() + "/" + this.filename;
|
||||
InputStream stream = findStream(path);
|
||||
if (stream == null) {
|
||||
String pathWithFilename = "/" + filename + ".yml";
|
||||
stream = findStream(pathWithFilename);
|
||||
if (stream == null) {
|
||||
String pathWithLocale = "/" + locale.getLanguage() + ".yml";
|
||||
stream = findStream(pathWithLocale);
|
||||
if (stream == null) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
String pathWithLocaleAndFilename = "/" + locale.getLanguage() + "/" + this.filename;
|
||||
String pathWithFilename = "/" + filename + ".yml";
|
||||
String pathWithLocale = "/" + locale.getLanguage() + ".yml";
|
||||
|
||||
List<String> paths = Arrays.asList(pathWithLocaleAndFilename, pathWithFilename, pathWithLocale);
|
||||
InputStream stream = null;
|
||||
for (String path : paths) {
|
||||
stream = findStream(path);
|
||||
if (stream != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (stream == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Map valuesMap = new Yaml().loadAs(stream, Map.class);
|
||||
Map localeBased = (Map) valuesMap.get(locale.getLanguage());
|
||||
if (localeBased == null) {
|
||||
@@ -59,7 +86,7 @@ public class FakeValues implements FakeValuesInterface {
|
||||
return getClass().getClassLoader().getResourceAsStream(filename);
|
||||
}
|
||||
|
||||
public boolean supportsPath(String path) {
|
||||
boolean supportsPath(String path) {
|
||||
return this.path.equals(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ public class FakeValuesGrouping implements FakeValuesInterface {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public Map get(String key) {
|
||||
Map result = null;
|
||||
for (FakeValues fakeValues : fakeValuesList) {
|
||||
|
||||
@@ -64,11 +64,6 @@ public class FakeValuesService {
|
||||
final List<FakeValuesInterface> all = new ArrayList(locales.size());
|
||||
|
||||
for (final Locale l : locales) {
|
||||
final StringBuilder filename = new StringBuilder(language(l));
|
||||
if (!"".equals(l.getCountry())) {
|
||||
filename.append("-").append(l.getCountry());
|
||||
}
|
||||
|
||||
boolean isEnglish = l.equals(Locale.ENGLISH);
|
||||
if (isEnglish) {
|
||||
FakeValuesGrouping fakeValuesGrouping = new FakeValuesGrouping();
|
||||
@@ -77,24 +72,13 @@ public class FakeValuesService {
|
||||
}
|
||||
all.add(fakeValuesGrouping);
|
||||
} else {
|
||||
all.add(new FakeValues(locale, filename.toString(), filename.toString()));
|
||||
all.add(new FakeValues(locale));
|
||||
}
|
||||
}
|
||||
|
||||
this.fakeValuesList = Collections.unmodifiableList(all);
|
||||
}
|
||||
|
||||
/**
|
||||
* If you new up a locale with "he", it gets converted to "iw" which is old.
|
||||
* This addresses that unfortunate condition.
|
||||
*/
|
||||
private String language(Locale l) {
|
||||
if (l.getLanguage().equals("iw")) {
|
||||
return "he";
|
||||
}
|
||||
return l.getLanguage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the specified locale into a chain of locales used for message resolution. For example:
|
||||
* <p>
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.github.javafaker.service;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class FakeValuesGroupingTest {
|
||||
|
||||
private FakeValuesGrouping fakeValuesGrouping;
|
||||
private FakeValues addressValues;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
fakeValuesGrouping = new FakeValuesGrouping();
|
||||
addressValues = new FakeValues(Locale.ENGLISH, "address.yml", "address");
|
||||
fakeValuesGrouping.add(addressValues);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesOneFakeValue() {
|
||||
assertThat(fakeValuesGrouping.get("address"), is(addressValues.get("address")));
|
||||
assertThat(fakeValuesGrouping.get("address"), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesMultipleFakeValues() {
|
||||
FakeValues catValues = new FakeValues(Locale.ENGLISH, "cat.yml", "creature");
|
||||
fakeValuesGrouping.add(catValues);
|
||||
|
||||
assertThat(fakeValuesGrouping.get("address"), is(addressValues.get("address")));
|
||||
assertThat(fakeValuesGrouping.get("address"), is(notNullValue()));
|
||||
|
||||
assertThat(fakeValuesGrouping.get("creature"), is(catValues.get("creature")));
|
||||
assertThat(fakeValuesGrouping.get("creature"), is(notNullValue()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.github.javafaker.service;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
||||
public class FakeValuesTest {
|
||||
|
||||
private static final String PATH = "address";
|
||||
private FakeValues fakeValues;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
fakeValues = new FakeValues(Locale.ENGLISH, "address.yml", PATH);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsPathIsTrueWithTheSameValueAsThePath() {
|
||||
assertThat(fakeValues.supportsPath(PATH), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsPathIsFalseWhenValueIsNotTheSame() {
|
||||
assertThat(fakeValues.supportsPath("dog"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAValueReturnsAValue() {
|
||||
assertThat(fakeValues.get(PATH), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAValueDoesNotReturnAValue() {
|
||||
assertThat(fakeValues.get("dog"), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAValueWithANonEnglishFile() {
|
||||
FakeValues frenchFakeValues = new FakeValues(Locale.FRENCH);
|
||||
assertThat(frenchFakeValues.get(PATH), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAValueForHebrewLocale() {
|
||||
FakeValues hebrew = new FakeValues(new Locale("iw"));
|
||||
assertThat(hebrew.get(PATH), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAValueFromALocaleThatCantBeLoaded() {
|
||||
FakeValues fakeValues = new FakeValues(new Locale("nothing"));
|
||||
assertThat(fakeValues.get(PATH), is(nullValue()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user