Skip to content

Commit bff144c

Browse files
authored
Fix DtoServer error messages (eclipse-che#5764)
* Fix error message of getting DTO implementation * Handle non-interface types and types with DTO annotation in parent Signed-off-by: Dmytro Nochevnov <dnochevnov@codenvy.com> * Small refactor Signed-off-by: Dmytro Nochevnov <dnochevnov@codenvy.com> * Small fixes Signed-off-by: Dmytro Nochevnov <dnochevnov@codenvy.com>
1 parent 84a4de6 commit bff144c

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

core/che-core-api-dto/src/main/java/org/eclipse/che/dto/server/DtoFactory.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import java.util.ServiceLoader;
4343
import java.util.concurrent.ConcurrentHashMap;
4444

45+
import static java.lang.String.format;
46+
4547
/**
4648
* Provides implementations of DTO interfaces.
4749
*
@@ -386,11 +388,41 @@ public <T> JsonStringMap<T> createMapDtoFromJson(InputStream json, Class<T> dtoI
386388
private <T> DtoProvider<T> getDtoProvider(Class<T> dtoInterface) {
387389
DtoProvider<?> dtoProvider = dtoInterface2Providers.get(dtoInterface);
388390
if (dtoProvider == null) {
389-
throw new IllegalArgumentException("Unknown DTO type " + dtoInterface);
391+
if (!dtoInterface.isInterface()) {
392+
throw new IllegalArgumentException(format("Only interfaces can be DTO, but %s is not", dtoInterface));
393+
}
394+
395+
if (hasDtoAnnotation(dtoInterface)) {
396+
throw new IllegalArgumentException(format("Provider of implementation for DTO type %s isn't found", dtoInterface));
397+
} else {
398+
throw new IllegalArgumentException(dtoInterface + " is not a DTO type");
399+
}
390400
}
401+
391402
return (DtoProvider<T>)dtoProvider;
392403
}
393404

405+
/**
406+
* Checks if dtoInterface or its parent have DTO annotation.
407+
*
408+
* @param dtoInterface
409+
* DTO interface
410+
* @return true if only dtoInterface or one of its parent have DTO annotation.
411+
*/
412+
private boolean hasDtoAnnotation(Class dtoInterface) {
413+
if (dtoInterface.isAnnotationPresent(DTO.class)) {
414+
return true;
415+
}
416+
417+
for (Class parent: dtoInterface.getInterfaces()) {
418+
if (hasDtoAnnotation(parent)) {
419+
return true;
420+
}
421+
}
422+
423+
return false;
424+
}
425+
394426
/**
395427
* Registers DtoProvider for DTO interface.
396428
*

core/che-core-api-dto/src/test/java/org/eclipse/che/dto/ServerDtoTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
import org.eclipse.che.dto.definitions.ComplicatedDto;
2121
import org.eclipse.che.dto.definitions.DTOHierarchy;
22+
import org.eclipse.che.dto.definitions.DTOHierarchy.GrandchildDto;
2223
import org.eclipse.che.dto.definitions.DtoWithAny;
2324
import org.eclipse.che.dto.definitions.DtoWithDelegate;
2425
import org.eclipse.che.dto.definitions.DtoWithFieldNames;
2526
import org.eclipse.che.dto.definitions.SimpleDto;
26-
import org.eclipse.che.dto.definitions.DTOHierarchy.GrandchildDto;
2727
import org.eclipse.che.dto.definitions.model.Model;
2828
import org.eclipse.che.dto.definitions.model.ModelComponentDto;
2929
import org.eclipse.che.dto.definitions.model.ModelDto;
@@ -108,7 +108,7 @@ public void testSerializerWithFieldNames() throws Exception {
108108
}
109109

110110
@Test
111-
public void testDeerializerWithFieldNames() throws Exception {
111+
public void testDeserializerWithFieldNames() throws Exception {
112112
final String fooString = "Something";
113113
final String _default = "test_default_keyword";
114114

@@ -380,4 +380,16 @@ public void shouldBeAbleToExtendsNotDTOInterfacesHierarchyWithDTOInterface() {
380380
assertEquals(childDto.getChildField(), "child-field");
381381
assertEquals(childDto.getParentField(), "parent-field");
382382
}
383+
384+
@Test(expectedExceptions = IllegalArgumentException.class,
385+
expectedExceptionsMessageRegExp = "Only interfaces can be DTO, but class java.lang.String is not")
386+
public void shouldThrowExceptionWhenThereIsClassType() {
387+
DtoFactory.newDto(String.class);
388+
}
389+
390+
@Test(expectedExceptions = IllegalArgumentException.class,
391+
expectedExceptionsMessageRegExp = "interface org.eclipse.che.dto.definitions.DTOHierarchy\\$GrandchildWithoutDto is not a DTO type")
392+
public void shouldThrowExceptionWhenInterfaceIsNotAnnotatedAsDto() {
393+
DtoFactory.newDto(DTOHierarchy.GrandchildWithoutDto.class);
394+
}
383395
}

core/che-core-api-dto/src/test/java/org/eclipse/che/dto/definitions/DTOHierarchy.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import org.eclipse.che.dto.shared.DTO;
1515

16+
import java.io.Closeable;
17+
1618
/**
1719
* Keeps DTO interfaces for hierarchy test
1820
*
@@ -60,4 +62,10 @@ public interface GrandchildDto extends ChildDto {
6062
void setShadowedField(GrandchildDto v);
6163
}
6264

65+
public interface Child2 extends Parent {
66+
String getChild2Field();
67+
}
68+
69+
public interface GrandchildWithoutDto extends Child, Child2 {
70+
}
6371
}

0 commit comments

Comments
 (0)