Skip to content

Commit

Permalink
Rename packages to org.itsallcode (#6)
Browse files Browse the repository at this point in the history
* Rename package to org.itsallcode.matcher

* Fix links

* Add import to example

* Update eclipse config

* Remove unused imports

* Fix sonar warnings

* Fix warnings

* Suppress sonar warning

* Add changelog

* Update release instructions

---------

Co-authored-by: kaklakariada <[email protected]>
  • Loading branch information
kaklakariada and kaklakariada authored Jan 31, 2024
1 parent 939cfea commit ae63db0
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 100 deletions.
1 change: 1 addition & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.classpath.outputOverlappingAnotherSource=ignore
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.source=11
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.0] - unreleased

### Breaking Changes

* [#6](https://github.com/itsallcode/hamcrest-auto-matcher/pull/6): Rename packages to `org.itsallcode`
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ Writing a hamcrest matcher for your model classes by extending [`TypeSafeDiagnos
* Good layout of actual and expected property values is hard to get right.
* Each property occurs in multiple places which violates the [DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself) principle.

## Requirements
## Project Information

Java 11
* [Changelog](CHANGELOG.md)
* Requirements
* Java 11

## How to use hamcrest-auto-matcher in your project

Expand All @@ -44,9 +46,9 @@ dependencies {
</dependency>
```

### Using [`AutoMatcher`](src/main/java/com/github/hamstercommunity/matcher/auto/AutoMatcher.java)
### Using [`AutoMatcher`](src/main/java/org/itsallcode/matcher/auto/AutoMatcher.java)

Assume you have two model classes [`DemoModel`](src/test/java/com/github/hamstercommunity/matcher/model/DemoModel.java) and [`DemoAttribute`](src/test/java/com/github/hamstercommunity/matcher/model/DemoAttribute.java):
Assume you have two model classes [`DemoModel`](src/test/java/org/itsallcode/matcher/model/DemoModel.java) and [`DemoAttribute`](src/test/java/org/itsallcode/matcher/model/DemoAttribute.java):

```java
public class DemoModel {
Expand Down Expand Up @@ -116,9 +118,11 @@ public class DemoAttribute {
}
```

Use [`AutoMatcher.equalTo()`](src/main/java/com/github/hamstercommunity/matcher/auto/AutoMatcher.java) to create a matcher for your expected model instance. This will use reflection to determine expected property values based on getter methods:
Use [`AutoMatcher.equalTo()`](src/main/java/org/itsallcode/matcher/auto/AutoMatcher.java) to create a matcher for your expected model instance. This will use reflection to determine expected property values based on getter methods:

```java
org.itsallcode.matcher.auto.AutoMatcher;

DemoModel expected = ...;
DemoModel actual = ...;
assertThat(actual, AutoMatcher.equalTo(expected));
Expand All @@ -130,8 +134,8 @@ Expected: {id=<4711>, longVal=null, name="name1", attr=null, stringArray=null, c
but: {longVal was <42L>}
```

### Using [`ConfigurableMatcher`](src/main/java/com/github/hamstercommunity/matcher/config/ConfigurableMatcher.java)
If `AutoMatcher` does not work for your model classes, you can still use [`ConfigurableMatcher`](src/main/java/com/github/hamstercommunity/matcher/config/ConfigurableMatcher.java) and [`MatcherConfig`](src/main/java/com/github/hamstercommunity/matcher/config/MatcherConfig.java) which allows you to specify properties and custom matchers explicitly but is much easier to use than `TypeSafeDiagnosingMatcher`.
### Using [`ConfigurableMatcher`](src/main/java/org/itsallcode/matcher/config/ConfigurableMatcher.java)
If `AutoMatcher` does not work for your model classes, you can still use [`ConfigurableMatcher`](src/main/java/org/itsallcode/matcher/config/ConfigurableMatcher.java) and [`MatcherConfig`](src/main/java/org/itsallcode/matcher/config/MatcherConfig.java) which allows you to specify properties and custom matchers explicitly but is much easier to use than `TypeSafeDiagnosingMatcher`.

```java
public class DemoModelMatcher {
Expand All @@ -148,7 +152,7 @@ public class DemoModelMatcher {
}
}
```
Also see [`DemoModelMatcher`](src/test/java/com/github/hamstercommunity/matcher/model/DemoModelMatcher.java) as an example.
Also see [`DemoModelMatcher`](src/test/java/org/itsallcode/matcher/model/DemoModelMatcher.java) as an example.

## Development

Expand Down Expand Up @@ -198,7 +202,7 @@ open build/reports/jacoco/test/html/index.html
signing.secretKeyRingFile=<path to secret keyring file>
```

2. Increment version number in `build.gradle` and `README.md`, commit and push.
2. Increment version number in `build.gradle` and `README.md`, update `CHANGELOG.md`, commit and push.
3. Optional: run the following command to do a dry-run:

```sh
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hamstercommunity.matcher;
package org.itsallcode.matcher;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hamstercommunity.matcher;
package org.itsallcode.matcher;

import org.hamcrest.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hamstercommunity.matcher;
package org.itsallcode.matcher;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hamstercommunity.matcher.auto;
package org.itsallcode.matcher.auto;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
Expand All @@ -20,10 +20,9 @@
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsArray;
import org.hamcrest.collection.IsMapContaining;

import com.github.hamstercommunity.matcher.config.ConfigurableMatcher;
import com.github.hamstercommunity.matcher.config.MatcherConfig;
import com.github.hamstercommunity.matcher.config.MatcherConfig.Builder;
import org.itsallcode.matcher.config.ConfigurableMatcher;
import org.itsallcode.matcher.config.MatcherConfig;
import org.itsallcode.matcher.config.MatcherConfig.Builder;

class AutoConfigBuilder<T> {

Expand All @@ -37,7 +36,7 @@ class AutoConfigBuilder<T> {
private final T expected;
private final Builder<T> configBuilder;

private AutoConfigBuilder(T expected) {
private AutoConfigBuilder(final T expected) {
this.expected = expected;
this.configBuilder = MatcherConfig.builder(expected);
}
Expand All @@ -54,7 +53,7 @@ private MatcherConfig<T> build() {
return configBuilder.build();
}

public static <T> Matcher<T> createEqualToMatcher(T expected) {
public static <T> Matcher<T> createEqualToMatcher(final T expected) {
if (expected.getClass().isArray()) {
return createArrayMatcher(expected);
}
Expand All @@ -72,7 +71,7 @@ public static <T> Matcher<T> createEqualToMatcher(T expected) {
}

@SuppressWarnings("unchecked")
private static <T> Matcher<T> createArrayMatcher(Object expected) {
private static <T> Matcher<T> createArrayMatcher(final Object expected) {
final Class<T> componentType = (Class<T>) expected.getClass().getComponentType();
if (componentType.isPrimitive()) {
return (Matcher<T>) Matchers.equalTo(expected);
Expand All @@ -91,7 +90,7 @@ private static <T> Matcher<T> createArrayMatcher(Object expected) {
}

@SuppressWarnings("unchecked")
private static <T, K, V> Matcher<T> createMapContainsMatcher(T expected) {
private static <T, K, V> Matcher<T> createMapContainsMatcher(final T expected) {
final Map<K, V> expectedMap = (Map<K, V>) expected;

final Collection<Matcher<? super T>> matchers = new ArrayList<>();
Expand All @@ -108,11 +107,11 @@ private static <T, K, V> Matcher<T> createMapContainsMatcher(T expected) {
private static <T, K, V> ConfigurableMatcher<T> mapSizeMatcher(final Map<K, V> expectedMap) {
@SuppressWarnings("unchecked")
final MatcherConfig<T> config = (MatcherConfig<T>) MatcherConfig.builder(expectedMap)
.addEqualsProperty("size", map -> map.size()).build();
.addEqualsProperty("size", Map::size).build();
return new ConfigurableMatcher<>(config);
}

private static <T> Matcher<T> createIterableContainsMatcher(T expected) {
private static <T> Matcher<T> createIterableContainsMatcher(final T expected) {
@SuppressWarnings("unchecked")
final Iterable<T> expectedIterable = (Iterable<T>) expected;
final Object[] elements = StreamSupport.stream(expectedIterable //
Expand All @@ -123,38 +122,38 @@ private static <T> Matcher<T> createIterableContainsMatcher(T expected) {
return matcher;
}

private boolean isNotBlackListed(Method method) {
private boolean isNotBlackListed(final Method method) {
final Set<String> blacklist = new HashSet<>(
asList("getClass", "getProtectionDomain", "getClassLoader", "getURLs"));
return !blacklist.contains(method.getName());
}

private boolean isGetterMethodSignature(Method method) {
private boolean isGetterMethodSignature(final Method method) {
return method.getParameterCount() == 0 //
&& !method.getReturnType().equals(Void.TYPE);
}

private boolean isGetterMethodName(Method method) {
private boolean isGetterMethodName(final Method method) {
final String methodName = method.getName();
return methodName.startsWith("get") //
|| methodName.startsWith("is");
}

private void addConfigForGetter(Method method) {
private void addConfigForGetter(final Method method) {
final String propertyName = getPropertyName(method.getName());
LOG.finest(() -> "Adding general property '" + propertyName + "' for getter " + method);
configBuilder.addProperty(propertyName, createGetter(method), AutoMatcher::equalTo);
}

private boolean hasArrayReturnType(Method method) {
private boolean hasArrayReturnType(final Method method) {
return method.getReturnType().isArray();
}

private <P> Function<T, P> createGetter(Method method) {
return (object) -> getPropertyValue(method, object);
private <P> Function<T, P> createGetter(final Method method) {
return object -> getPropertyValue(method, object);
}

private boolean hasSimpleReturnType(Method method) {
private boolean hasSimpleReturnType(final Method method) {
final Class<? extends Object> type = method.getReturnType();
if (type.isPrimitive() || type.isEnum()) {
return true;
Expand All @@ -171,7 +170,7 @@ private static boolean isSimpleType(final Class<? extends Object> type) {
return false;
}

private String getPropertyName(String methodName) {
private String getPropertyName(final String methodName) {
int prefixLength = 3;
if (methodName.startsWith("is")) {
prefixLength = 2;
Expand All @@ -184,8 +183,8 @@ private String decapitalize(final String string) {
return Character.toLowerCase(string.charAt(0)) + string.substring(1);
}

@SuppressWarnings("unchecked")
private static <T, P> P getPropertyValue(Method method, T object) {
@SuppressWarnings({ "unchecked", "java:S3011" }) // Need to use reflection and setAccessible()
private static <T, P> P getPropertyValue(final Method method, final T object) {
final Class<?> declaringClass = method.getDeclaringClass();
if (!declaringClass.isInstance(object)) {
throw new AssertionError("Expected object of type " + declaringClass.getName() + " but got "
Expand All @@ -197,7 +196,7 @@ private static <T, P> P getPropertyValue(Method method, T object) {
try {
return (P) method.invoke(object);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException("Error invoking method " + method + " on object " + object + " of type "
throw new IllegalStateException("Error invoking method " + method + " on object " + object + " of type "
+ object.getClass().getName(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hamstercommunity.matcher.auto;
package org.itsallcode.matcher.auto;

import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.emptyIterable;
Expand All @@ -8,8 +8,7 @@

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

import com.github.hamstercommunity.matcher.config.ConfigurableMatcher;
import org.itsallcode.matcher.config.ConfigurableMatcher;

/**
* This class configures and creates a {@link ConfigurableMatcher} using
Expand All @@ -21,27 +20,29 @@ private AutoMatcher() {
// not instantiable
}

public static <T> Matcher<T> equalTo(T expected) {
public static <T> Matcher<T> equalTo(final T expected) {
return AutoConfigBuilder.createEqualToMatcher(expected);
}

@SafeVarargs
public static <T> Matcher<Iterable<? extends T>> contains(T... expected) {
@SuppressWarnings({ "java:S1452", "varargs" }) // Wildcard type required here
public static <T> Matcher<Iterable<? extends T>> contains(final T... expected) {
if (expected.length == 0) {
return emptyIterable();
}
return Matchers.contains(getMatchers(expected));
}

@SafeVarargs
public static <T> Matcher<Iterable<? extends T>> containsInAnyOrder(T... expected) {
@SuppressWarnings({ "java:S1452", "varargs" }) // Wildcard type required here
public static <T> Matcher<Iterable<? extends T>> containsInAnyOrder(final T... expected) {
if (expected.length == 0) {
return emptyIterable();
}
return Matchers.containsInAnyOrder(getMatchers(expected));
}

private static <T> List<Matcher<? super T>> getMatchers(T[] expected) {
private static <T> List<Matcher<? super T>> getMatchers(final T[] expected) {
return Arrays.stream(expected) //
.map(AutoMatcher::equalTo) //
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.hamstercommunity.matcher.config;
package org.itsallcode.matcher.config;

import com.github.hamstercommunity.matcher.*;
import org.itsallcode.matcher.*;

public class ConfigurableMatcher<T> extends BaseTypeSafeDiagnosingMatcher<T> {
private final MatcherConfig<T> config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hamstercommunity.matcher.config;
package org.itsallcode.matcher.config;

import static java.util.stream.Collectors.toList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hamstercommunity.matcher.config;
package org.itsallcode.matcher.config;

import java.util.function.Function;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.hamstercommunity.matcher.auto;
package org.itsallcode.matcher.auto;

import static com.github.hamstercommunity.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static com.github.hamstercommunity.matcher.auto.TestUtil.assertValuesMatch;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesMatch;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.hamstercommunity.matcher.auto;
package org.itsallcode.matcher.auto;

import static com.github.hamstercommunity.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static com.github.hamstercommunity.matcher.auto.TestUtil.assertValuesMatch;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesMatch;

import java.util.ArrayList;
import java.util.LinkedList;

import org.itsallcode.matcher.model.DemoAttribute;
import org.junit.Test;

import com.github.hamstercommunity.matcher.model.DemoAttribute;

public class AutoMatcherListTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.hamstercommunity.matcher.auto;
package org.itsallcode.matcher.auto;

import static com.github.hamstercommunity.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static com.github.hamstercommunity.matcher.auto.TestUtil.assertValuesMatch;
import static java.util.Collections.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesMatch;

import java.util.HashMap;
import java.util.TreeMap;
Expand Down
Loading

0 comments on commit ae63db0

Please sign in to comment.