Skip to content

Commit

Permalink
release 0.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
keepcosmos committed Sep 20, 2017
1 parent cc1267a commit 2410336
Show file tree
Hide file tree
Showing 41 changed files with 820 additions and 119 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ Apache Maven
<dependency>
<groupId>io.beanmother</groupId>
<artifactId>beanmother-core</artifactId>
<version>0.7.1</version>
<version>0.7.2</version>
<scope>test</scope>
</dependency>

<!-- For java 8 converter add additional dependency -->
<dependency>
<groupId>io.beanmother</groupId>
<artifactId>beanmother-java8-converter</artifactId>
<version>0.7.1</version>
<version>0.7.2</version>
<scope>test</scope>
</dependency>
```

Gradle
```groovy
testCompile 'io.beanmother:beanmother-core:0.7.1'
testCompile 'io.beanmother:beanmother-core:0.7.2'
# For java 8
testCompile 'io.beanmother:beanmother-java8-converter:0.7.1'
testCompile 'io.beanmother:beanmother-java8-converter:0.7.2'
```

## Example
Expand Down
2 changes: 1 addition & 1 deletion beanmother-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.beanmother</groupId>
<artifactId>beanmother</artifactId>
<version>0.7.1</version>
<version>0.7.2</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.beanmother.core.script.ScriptHandler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -48,26 +49,39 @@ protected AbstractBeanMother() {

@Override
public <T> T bear(String fixtureName, T target) {
assertFixtureMapExists(fixtureName);
return bear(fixtureName, target, null);
}

@Override
public <T> T bear(String fixtureName, Class<T> targetClass) {
FixtureMap fixtureMap = fixturesStore.reproduce(fixtureName);
return _bear(target, fixtureMap);
T inst = (T) ConstructHelper.construct(targetClass, fixtureMap, fixtureConverter);
return _bear(inst, fixtureMap,null);
}

@Override
public <T> T bear(String fixtureName, Class<T> targetClass) {
assertFixtureMapExists(fixtureName);
public <T> T bear(String fixtureName, T target, PostProcessor<T> postProcessor) {
FixtureMap fixtureMap = fixturesStore.reproduce(fixtureName);
return _bear(target, fixtureMap, postProcessor);
}

@Override
public <T> T bear(String fixtureName, Class<T> targetClass, PostProcessor<T> postProcessor) {
FixtureMap fixtureMap = fixturesStore.reproduce(fixtureName);
T inst = (T) ConstructHelper.construct(targetClass, fixtureMap, fixtureConverter);
return bear(fixtureName, inst);
return _bear(inst, fixtureMap, postProcessor);
}

@Override
public <T> List<T> bear(String fixtureName, Class<T> targetClass, int size) {
return bear(fixtureName, targetClass, size, null);
}

@Override
public <T> List<T> bear(String fixtureName, Class<T> targetClass, int size, PostProcessor<T> postProcessor) {
List<T> result = new ArrayList<>();
for (int i = 0 ; i < size ; i++) {
result.add(bear(fixtureName, targetClass));
result.add(bear(fixtureName, targetClass, postProcessor));
}
return result;
}
Expand All @@ -78,22 +92,51 @@ public BeanMother addFixtureLocation(String path) {
return this;
}

public String[] defaultFixturePaths() {
private <T> T _bear(T target, FixtureMap fixtureMap, PostProcessor<T> postProcessor) {
handleScriptFixtureValue(fixtureMap);

fixtureMapper.map(fixtureMap, target);

List<PostProcessor<T>> postProcessors = postProcessorFactory.get((Class<T>) target.getClass());
if (postProcessor != null) {
postProcessors.add(postProcessor);
Collections.sort(postProcessors);
}
for (PostProcessor<T> pp : postProcessors) {
pp.process(target, fixtureMap);
}

return target;
}

protected String[] defaultFixturePaths() {
return new String[] { "fixtures" };
}

/**
* Configure the ConverterFactory
*/
protected void configureConverterFactory(ConverterFactory converterFactory) {
// Do nothing.
}

/**
* Configure the ScriptHandler
*/
protected void configureScriptHandler(ScriptHandler scriptHandler) {
// Do nothing.
}

/**
* Configure the PostProcessorFactory
*/
protected void configurePostProcessorFactory(PostProcessorFactory postProcessorFactory) {
// Do nothing.
}

/**
* Initialize beanmother
*/
protected void initialize() {
for ( String path : defaultFixturePaths()) {
this.fixturesStore.addLocation(new Location(path));
Expand All @@ -103,19 +146,6 @@ protected void initialize() {
configurePostProcessorFactory(postProcessorFactory);
}

private <T> T _bear(T target, FixtureMap fixtureMap) {
handleScriptFixtureValue(fixtureMap);

fixtureMapper.map(fixtureMap, target);

List<PostProcessor<T>> postProcessors = postProcessorFactory.get((Class<T>) target.getClass());
for (PostProcessor<T> postProcessor : postProcessors) {
postProcessor.process(target, fixtureMap);
}

return target;
}

private void handleScriptFixtureValue(FixtureMap fixtureMap) {
FixtureMapTraversal.traverse(fixtureMap, new FixtureMapTraversal.Processor() {
@Override
Expand All @@ -127,10 +157,4 @@ public void visit(FixtureValue edge) {
}
});
}

private void assertFixtureMapExists(String fixtureName) {
if (!fixturesStore.exists(fixtureName)) {
throw new IllegalArgumentException("can not find " + fixtureName);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package io.beanmother.core;

import io.beanmother.core.postprocessor.PostProcessor;

import java.util.List;

public interface BeanMother {
<T> T bear(String fixtureName, T target);
<T> T bear(String fixtureName, Class<T> targetClass);
<T> T bear(String fixtureName, T target, PostProcessor<T> postProcessor);
<T> T bear(String fixtureName, Class<T> targetClass, PostProcessor<T> postProcessor);
<T> List<T> bear(String fixtureName, Class<T> targetClass, int size);
<T> List<T> bear(String fixtureName, Class<T> targetClass, int size, PostProcessor<T> postProcessor);

BeanMother addFixtureLocation(String path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* FixtureMap decorates a map from fixture
*
* The source of FixtureMap generally {@link LinkedHashMap<String, Object>} that is parsed by {@link io.beanmother.core.loader.parser.FixtureParser}.
* The source of FixtureMap generally {@link LinkedHashMap} that is parsed by {@link io.beanmother.core.loader.parser.FixtureParser}.
* It can be the root fixture template.
*/
public class FixtureMap extends LinkedHashMap<String, FixtureTemplate> implements FixtureTemplate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public class FixtureMapTraversal {
public interface Processor {
/**
* Run when visiting a edge.
* @param edge
* @param edge the edge
*/
void visit(FixtureValue edge);
}


/**
* Traverse each edges({@link FixtureValue}) and run {@link Processor} when it meets a edge.
* @param fixtureMap
* @param processor
* @param fixtureMap the FixtureMap
* @param processor the Processor
*/
public static void traverse(FixtureMap fixtureMap, final Processor processor) {
for (Map.Entry<String, FixtureTemplate> entry : fixtureMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public interface FixtureTemplate extends Serializable {

/**
* Check If a FixtureTemplate has a parent.
* @return
*/
boolean hasParent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class FixtureValue extends Object implements FixtureTemplate {

/**
* Create FixtureValue
* @param value
* @param value the value
*/
public FixtureValue(Object value) {
this.value = value;
Expand All @@ -41,7 +41,6 @@ public void setValue(Object value) {

/**
* Check the value is null.
* @return
*/
public boolean isNull() {
return value == null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ public interface Converter extends Comparable<Converter> {
/**
* Convert to given type
*
* @param source
* @param targetTypeToken
*
* @param source the source
* @param targetTypeToken the TypeToken of the type
* @return converted object
*/
Object convert(Object source, TypeToken<?> targetTypeToken);

/**
* Check that source can convert to given type
*
* @param source
* @param targetTypeToken
*
* @param source the source
* @param targetTypeToken the TypeToken of the type
* @return true if it can convert
*/
boolean canHandle(Object source, TypeToken<?> targetTypeToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ public abstract class KnownConverterModuleLoader {
knownConverterModules = new String[]{
"io.beanmother.core.converter.std.StandardConverterModule",
"io.beanmother.java8.converter.JavaTimeConverterModule",
"io.beanmother.java8.converter.JavaOptionalConverterModule",
"io.beanmother.core.DummyConverterModule" // for test
};
}

/**
* Load instances of converters in known converter modules
* @return
*/
@SuppressWarnings("unchecked")
public static List<ConverterModule> load() {
List<ConverterModule> modules = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

/**
* {@link String} to {@link Date} converter.
* The converter uses natty library {@see http://natty.joestelmach.com}.
* The converter uses natty library
*
* @see <a href="http://natty.joestelmach.com">natty</a>
*/
public class StringToDateConverter extends AbstractGenericConverter<String, Date> {
private final static Parser dateParser = new Parser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Map;

/**
* The root interface for parsing fixture string to {@link java.util.Map<String, Object>}
* The root interface for parsing fixture string to {@link java.util.Map}
*/
public interface FixtureParser {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class YamlFixtureScanner extends AbstractFixtureScanner {

/**
* Create a YamlFixtureScanner wiath a ClassLoader.
* @param classLoader
* @param classLoader the ClassLoader
*/
public YamlFixtureScanner(ClassLoader classLoader) {
super(classLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public FixtureMap get(String fixtureKey) {
@Override
public FixtureMap reproduce(String fixtureKey) {
FixtureMap fixtureMap = get(fixtureKey);
return fixtureMap == null ? null : fixtureMap.reproduce();
if (fixtureMap == null) throw new IllegalArgumentException("can not find " + fixtureKey);
return fixtureMap.reproduce();
}

@Override
Expand Down Expand Up @@ -122,23 +123,20 @@ public void reset() {

/**
* Get registered fixture locations
* @return
*/
public Set<Location> getFixtureLocations() {
return fixtureLocations;
}

/**
* Get registered fixture files
* @return
*/
public Set<File> getFixtureFiles() {
return fixtureFiles;
}

/**
* Get fixtureMap
* @return
*/
public Map<String, FixtureMap> getFixtureMaps() {
return fixtureMaps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,38 @@
/**
* The root interface for storing fixtureMap data.
*
* It can add fixture file locations and {@code #refresh} for loading and parsing fixture file.
* It can add fixture yml file locations and find a FixtureMap by key.
*/
public interface FixturesStore {

/**
* Get a FixtureMap.
* @param fixtureKey the fixtureKey
* @return the FixtureMap
*/
FixtureMap get(String fixtureKey);

/**
* Reproduce a FixtureMap.
* @param fixtureKey the fixtureKey
* @return the reproduced FixtureMap
*/
FixtureMap reproduce(String fixtureKey);

/**
* Check if a FixtureMap exists.
* @param fixtureKey the fixtureKey
*/
boolean exists(String fixtureKey);

/**
* Add fixture yml files location.
* @param location the location
*/
void addLocation(Location location);

/**
* Reset a FixtureStore.
*/
void reset();
}
Loading

0 comments on commit 2410336

Please sign in to comment.