Skip to content

Commit

Permalink
Support fuer Rekursion in BeanUtil.get()
Browse files Browse the repository at this point in the history
  • Loading branch information
willuhn committed Feb 21, 2014
1 parent e15b2f1 commit d7b81c8
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" output="test-bin" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="lib/mckoidb.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/util"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/releases
/test-bin
13 changes: 13 additions & 0 deletions src/de/willuhn/datasource/BeanUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ public static Object get(Object bean, String attribute) throws RemoteException

if (attribute == null)
return toString(bean);

for (int i=0;i<10;++i) // Rekursion in die Kind-Beans - maximal aber 10 Stufen. Wenn es mehr sind, ist irgendwas faul ;)
{
int dot = attribute.indexOf(".");
if (dot == -1)
break;

String s = attribute.substring(0,dot);
bean = get(bean,s);
if (bean == null)
return null; // Hier gehts nicht mehr weiter
attribute = attribute.substring(dot+1);
}

if (bean instanceof GenericObject)
return ((GenericObject)bean).getAttribute(attribute);
Expand Down
143 changes: 143 additions & 0 deletions test/de/willuhn/datasource/TestBeanUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**********************************************************************
*
* Copyright (c) by Olaf Willuhn
* All rights reserved
*
**********************************************************************/

package de.willuhn.datasource;

import junit.framework.Assert;

import org.junit.Test;

/**
* Unit-Tests fuer die Bean-Utils.
*/
public class TestBeanUtil
{

/**
* @throws Exception
*/
@Test
public void test001() throws Exception
{
Customer c = new Customer();
String s = (String) BeanUtil.get(c,"project.task.name");
Assert.assertEquals("Test-Task",s);

Project p = (Project) BeanUtil.get(c,"project");
Assert.assertEquals(p.getClass(),Project.class);

Task t = (Task) BeanUtil.get(c,"project.task");
Assert.assertEquals(t.getClass(),Task.class);

s = (String) BeanUtil.get(t,"name");
Assert.assertEquals("Test-Task",s);
}

/**
* @throws Exception
*/
@Test
public void test002() throws Exception
{
Project p = new Project();
String s = (String) BeanUtil.get(p,"task.name");
Assert.assertEquals("Test-Task",s);

Task t = (Task) BeanUtil.get(p,"task");
Assert.assertEquals(t.getClass(),Task.class);
}

/**
* @throws Exception
*/
@Test
public void test003() throws Exception
{
Project p = new Project();
String s = (String) BeanUtil.get(p,"name");
Assert.assertEquals("Test-Project",s);
}

/**
* @throws Exception
*/
@Test
public void test004() throws Exception
{
Customer c = new Customer();
Task t = (Task) BeanUtil.get(c,"project.task");
Assert.assertEquals(t.getClass(),Task.class);
}

/**
* @throws Exception
*/
@Test
public void test005() throws Exception
{
Customer c = new Customer();
Assert.assertNull(BeanUtil.get(c,"project.invalid"));
}

/**
* @throws Exception
*/
@Test
public void test006() throws Exception
{
Customer c = new Customer();
Assert.assertNull(BeanUtil.get(c,"project.invalid.name"));
}


@SuppressWarnings("javadoc")
public class Customer
{
private String name = "Test-Customer";
private Project project = new Project();

public String getName()
{
return this.name;
}

public Project getProject()
{
return this.project;
}
}

@SuppressWarnings("javadoc")
public class Project
{
private String name = "Test-Project";
private Task task = new Task();

public String getName()
{
return this.name;
}

public Task getTask()
{
return this.task;
}
}

@SuppressWarnings("javadoc")
public class Task
{
private String name = "Test-Task";

public String getName()
{
return this.name;
}
}
}


0 comments on commit d7b81c8

Please sign in to comment.