Skip to content

Commit

Permalink
Merge pull request Azure#26 from anihojnadel/dev
Browse files Browse the repository at this point in the history
Updated getTable class type validation to allow only concrete classes
  • Loading branch information
carlosfigueira committed Mar 6, 2013
2 parents ea596c3 + b8ce562 commit 6e03e76
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Expand Up @@ -23,6 +23,7 @@
package com.microsoft.windowsazure.mobileservices;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -307,7 +308,7 @@ public MobileServiceJsonTable getTable(String name) {
* @return MobileServiceTable with the given name
*/
public <E> MobileServiceTable<E> getTable(String name, Class<E> clazz) {
validateClassIdProperty(clazz);
validateClass(clazz);
return new MobileServiceTable<E>(name, this, clazz);
}

Expand All @@ -320,7 +321,7 @@ public <E> MobileServiceTable<E> getTable(String name, Class<E> clazz) {
* @return MobileServiceTable with the given name
*/
public <E> MobileServiceTable<E> getTable(Class<E> clazz) {
validateClassIdProperty(clazz);
validateClass(clazz);

return new MobileServiceTable<E>(clazz.getSimpleName(), this, clazz);
}
Expand All @@ -329,7 +330,12 @@ public <E> MobileServiceTable<E> getTable(Class<E> clazz) {
* Validates the class has an id property defined
* @param clazz
*/
private <E> void validateClassIdProperty(Class<E> clazz) {
private <E> void validateClass(Class<E> clazz) {
if(clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers()))
{
throw new IllegalArgumentException("The class type used for creating a MobileServiceTable must be a concrete class");
}

int idPropertyCount = 0;
for (Field field : clazz.getDeclaredFields()) {
SerializedName serializedName = field.getAnnotation(SerializedName.class);
Expand Down
Expand Up @@ -260,6 +260,51 @@ public void testGetTableWithClassWithMultipleIdPropertiesShouldFail() throws Thr
}

}

public void testGetTableWithInterfaceShouldThrowException() {
MobileServiceClient client = null;
try {
client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());
} catch (MalformedURLException e1) {
fail("This should not fail");
}

try {

client.getTable(MyInterface.class);
fail("Expected Exception IllegalArgumentException");
} catch (IllegalArgumentException e) {
// do nothing, it's OK
}
}

public void testGetTableWithAbstractClassShouldThrowException() {
MobileServiceClient client = null;
try {
client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());
} catch (MalformedURLException e1) {
fail("This should not fail");
}

try {

client.getTable(MyAbstractClass.class);
fail("Expected Exception IllegalArgumentException");
} catch (IllegalArgumentException e) {
// do nothing, it's OK
}
}

interface MyInterface
{

}

abstract class MyAbstractClass
{
int id;
String name;
}

public void testLoginShouldParseJsonUserCorreclty() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Expand Down

0 comments on commit 6e03e76

Please sign in to comment.