#355 finalize example

This commit is contained in:
qza 2016-06-04 20:06:32 +02:00
parent c229ec23b3
commit afdeba4f9a
14 changed files with 568 additions and 119 deletions

View File

@ -12,15 +12,20 @@ tags:
## Intent
Achieve flexibility of untyped languages and keep the type-safety
![alt text](./etc/abstract-document_1.png "Abstract Document")
![alt text](./etc/abstract-document-base.png "Abstract Document Base")
![alt text](./etc/abstract-document.png "Abstract Document Traits and Domain")
## Applicability
Use the Abstract Document Pattern when
* there is a need for dynamic properties
* you want a better way to organize domain
* you want loosely coupled system with flexibility of untyped languages
* there is a need to add new properties on the fly
* you want a flexible way to organize domain in tree like structure
* you want more loosely coupled system
## Real world examples
* [Speedment](https://github.com/speedment/speedment)
## Credits
* [Wikipedia: Abstract Document Pattern](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
* [Martin Fowler: Dealing with properties](http://martinfowler.com/apsupp/properties.pdf)

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument;
import java.util.List;
@ -7,44 +29,44 @@ import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Abstract implementation of Document interface
*/
public abstract class AbstractDocument implements Document {
private final Map<String, Object> properties;
private final Map<String, Object> properties;
protected AbstractDocument(Map<String, Object> properties) {
Objects.requireNonNull(properties, "properties map is required");
this.properties = properties;
}
protected AbstractDocument(Map<String, Object> properties) {
Objects.requireNonNull(properties, "properties map is required");
this.properties = properties;
}
@Override
public Void put(String key, Object value) {
properties.put(key, value);
return null;
}
@Override
public Void put(String key, Object value) {
properties.put(key, value);
return null;
}
@Override
public Object get(String key) {
return properties.get(key);
}
@Override
public Object get(String key) {
return properties.get(key);
}
@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
Optional<List<Map<String, Object>>> any = Stream.of(get(key))
.filter(el -> el != null)
.map(el -> (List<Map<String, Object>>) el)
.findAny();
return any.isPresent() ? any.get().stream().map(constructor) : Stream.empty();
}
@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(el -> el != null)
.map(el -> (List<Map<String, Object>>) el).findAny();
return any.isPresent() ? any.get().stream().map(constructor) : Stream.empty();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(getClass().getName()).append("[");
properties.entrySet().forEach(e ->
builder.append("[").append(e.getKey()).append(" : ").append(e.getValue()).append("]")
);
builder.append("]");
return builder.toString();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(getClass().getName()).append("[");
properties.entrySet()
.forEach(e -> builder.append("[").append(e.getKey()).append(" : ").append(e.getValue()).append("]"));
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,88 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument;
import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.HasModel;
import com.iluwatar.abstractdocument.domain.HasParts;
import com.iluwatar.abstractdocument.domain.HasPrice;
import com.iluwatar.abstractdocument.domain.HasType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* The Abstract Document pattern enables handling additional, non-static
* properties. This pattern uses concept of traits to enable type safety and
* separate properties of different classes into set of interfaces.
* <p>
* <p>
* In Abstract Document pattern,({@link AbstractDocument}) fully implements
* {@link Document}) interface. Traits are then defined to enable access to
* properties in usual, static way.
*/
public class App {
/**
* Executes the App
*/
public App() {
System.out.println("Constructing parts and car");
Map<String, Object> carProperties = new HashMap<>();
carProperties.put(HasModel.PROPERTY, "300SL");
carProperties.put(HasPrice.PROPERTY, 10000L);
Map<String, Object> wheelProperties = new HashMap<>();
wheelProperties.put(HasType.PROPERTY, "wheel");
wheelProperties.put(HasModel.PROPERTY, "15C");
wheelProperties.put(HasPrice.PROPERTY, 100L);
Map<String, Object> doorProperties = new HashMap<>();
doorProperties.put(HasType.PROPERTY, "door");
doorProperties.put(HasModel.PROPERTY, "Lambo");
doorProperties.put(HasPrice.PROPERTY, 300L);
carProperties.put(HasParts.PROPERTY, Arrays.asList(wheelProperties, doorProperties));
Car car = new Car(carProperties);
System.out.println("Here is our car:");
System.out.println("-> model: " + car.getModel().get());
System.out.println("-> price: " + car.getPrice().get());
System.out.println("-> parts: ");
car.getParts().forEach(p -> System.out
.println("\t" + p.getType().get() + "/" + p.getModel().get() + "/" + p.getPrice().get()));
}
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
new App();
}
}

View File

@ -1,34 +1,59 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Document interface
*/
public interface Document {
/**
* Puts the value related to the key
*
* @param key
* @param value
* @return Void
*/
Void put(String key, Object value);
/**
* Puts the value related to the key
*
* @param key element key
* @param value element value
* @return Void
*/
Void put(String key, Object value);
/**
* Gets the value for the key
*
* @param key
* @return value or null
*/
Object get(String key);
/**
* Gets the value for the key
*
* @param key element key
* @return value or null
*/
Object get(String key);
/**
* Gets the stream of child documents
*
* @param key
* @param constructor
* @return child documents
*/
<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
/**
* Gets the stream of child documents
*
* @param key element key
* @param constructor constructor of child class
* @return child documents
*/
<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
}

View File

@ -1,18 +1,38 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument.domain;
import java.util.HashMap;
import java.util.Map;
import com.iluwatar.abstractdocument.AbstractDocument;
/**
* Car entity
*/
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
protected Car() {
super(new HashMap<>());
}
protected Car(Map<String,Object> properties) {
super(properties);
}
public Car(Map<String, Object> properties) {
super(properties);
}
}

View File

@ -1,13 +1,40 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument.domain;
import java.util.Optional;
import com.iluwatar.abstractdocument.Document;
/**
* HasModel trait for static access to 'model' property
*/
public interface HasModel extends Document {
default Optional<String> getModel() {
return Optional.ofNullable((String) get("model"));
}
String PROPERTY = "model";
default Optional<String> getModel() {
return Optional.ofNullable((String) get(PROPERTY));
}
}

View File

@ -1,13 +1,40 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument.domain;
import java.util.stream.Stream;
import com.iluwatar.abstractdocument.Document;
/**
* HasParts trait for static access to 'parts' property
*/
public interface HasParts extends Document {
default Stream<Part> getParts() {
return children("parts", Part::new);
}
String PROPERTY = "parts";
default Stream<Part> getParts() {
return children(PROPERTY, Part::new);
}
}

View File

@ -1,13 +1,40 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument.domain;
import java.util.Optional;
import com.iluwatar.abstractdocument.Document;
/**
* HasPrice trait for static access to 'price' property
*/
public interface HasPrice extends Document {
default Optional<Number> getPartner() {
return Optional.ofNullable((Number) get("price"));
}
String PROPERTY = "price";
default Optional<Number> getPrice() {
return Optional.ofNullable((Number) get(PROPERTY));
}
}

View File

@ -0,0 +1,40 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument.domain;
import com.iluwatar.abstractdocument.Document;
import java.util.Optional;
/**
* HasType trait for static access to 'type' property
*/
public interface HasType extends Document {
String PROPERTY = "type";
default Optional<String> getType() {
return Optional.ofNullable((String) get(PROPERTY));
}
}

View File

@ -1,18 +1,38 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument.domain;
import java.util.HashMap;
import java.util.Map;
import com.iluwatar.abstractdocument.AbstractDocument;
public class Part extends AbstractDocument implements HasModel, HasPrice {
/**
* Part entity
*/
public class Part extends AbstractDocument implements HasType, HasModel, HasPrice {
protected Part() {
super(new HashMap<>());
}
protected Part(Map<String, Object> properties) {
super(properties);
}
public Part(Map<String, Object> properties) {
super(properties);
}
}

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument;
import org.junit.Test;
@ -11,44 +33,56 @@ import java.util.stream.Stream;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
/**
* AbstractDocument test class
*/
public class AbstractDocumentTest {
private static final String KEY = "key";
private static final Object VALUE = "value";
private static final String KEY = "key";
private static final String VALUE = "value";
private class DocumentImplementation extends AbstractDocument {
private class DocumentImplementation extends AbstractDocument {
DocumentImplementation(Map<String, Object> properties) {
super(properties);
}
DocumentImplementation(Map<String, Object> properties) {
super(properties);
}
}
private DocumentImplementation document = new DocumentImplementation(new HashMap<>());
private DocumentImplementation document = new DocumentImplementation(new HashMap<>());
@Test
public void shouldPutAndGetValue() {
document.put(KEY, VALUE);
assertEquals(VALUE, document.get(KEY));
}
@Test
public void shouldPutAndGetValue() {
document.put(KEY, VALUE);
assertEquals(VALUE, document.get(KEY));
}
@Test
public void shouldRetrieveChildren() {
Map<String,Object> child1 = new HashMap<>();
Map<String,Object> child2 = new HashMap<>();
List<Map<String, Object>> children = Arrays.asList(child1, child2);
@Test
public void shouldRetrieveChildren() {
Map<String, Object> child1 = new HashMap<>();
Map<String, Object> child2 = new HashMap<>();
List<Map<String, Object>> children = Arrays.asList(child1, child2);
document.put(KEY, children);
document.put(KEY, children);
Stream<DocumentImplementation> childrenStream = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(2, childrenStream.count());
}
Stream<DocumentImplementation> childrenStream = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(2, childrenStream.count());
}
@Test
public void shouldRetrieveEmptyStreamForNonExistinChildren() {
Stream<DocumentImplementation> children = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(0, children.count());
}
@Test
public void shouldRetrieveEmptyStreamForNonExistingChildren() {
Stream<DocumentImplementation> children = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(0, children.count());
}
@Test
public void shouldIncludePropsInToString() {
Map<String, Object> props = new HashMap<>();
props.put(KEY, VALUE);
DocumentImplementation document = new DocumentImplementation(props);
assertNotNull(document.toString().contains(KEY));
assertNotNull(document.toString().contains(VALUE));
}
}

View File

@ -0,0 +1,37 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument;
import org.junit.Test;
/**
* Simple App test
*/
public class AppTest {
@Test
public void shouldExecuteAppWithoutException() {
App.main(null);
}
}

View File

@ -0,0 +1,77 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.abstractdocument;
import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.HasModel;
import com.iluwatar.abstractdocument.domain.HasParts;
import com.iluwatar.abstractdocument.domain.HasPrice;
import com.iluwatar.abstractdocument.domain.HasType;
import com.iluwatar.abstractdocument.domain.Part;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static junit.framework.TestCase.assertEquals;
/**
* Test for Part and Car
*/
public class DomainTest {
private static final String TEST_PART_TYPE = "test-part-type";
private static final String TEST_PART_MODEL = "test-part-model";
private static final long TEST_PART_PRICE = 0L;
private static final String TEST_CAR_MODEL = "test-car-model";
private static final long TEST_CAR_PRICE = 1L;
@Test
public void shouldConstructPart() {
Map<String, Object> partProperties = new HashMap<>();
partProperties.put(HasType.PROPERTY, TEST_PART_TYPE);
partProperties.put(HasModel.PROPERTY, TEST_PART_MODEL);
partProperties.put(HasPrice.PROPERTY, TEST_PART_PRICE);
Part part = new Part(partProperties);
assertEquals(TEST_PART_TYPE, part.getType().get());
assertEquals(TEST_PART_MODEL, part.getModel().get());
assertEquals(TEST_PART_PRICE, part.getPrice().get());
}
@Test
public void shouldConstructCar() {
Map<String, Object> carProperties = new HashMap<>();
carProperties.put(HasModel.PROPERTY, TEST_CAR_MODEL);
carProperties.put(HasPrice.PROPERTY, TEST_CAR_PRICE);
carProperties.put(HasParts.PROPERTY, Arrays.asList(new HashMap<>(), new HashMap<>()));
Car car = new Car(carProperties);
assertEquals(TEST_CAR_MODEL, car.getModel().get());
assertEquals(TEST_CAR_PRICE, car.getPrice().get());
assertEquals(2, car.getParts().count());
}
}

View File

@ -124,7 +124,7 @@
<module>mute-idiom</module>
<module>mutex</module>
<module>semaphore</module>
<module>hexagonal</module>
<module>hexagonal</module>
<module>abstract-document</module>
</modules>