http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/pom.xml ---------------------------------------------------------------------- diff --git a/juneau-core-test/pom.xml b/juneau-core-test/pom.xml new file mode 100644 index 0000000..b5f66ef --- /dev/null +++ b/juneau-core-test/pom.xml @@ -0,0 +1,112 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + *************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations under the License. * + *************************************************************************************************************************** +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <artifactId>juneau-core-test</artifactId> + <name>Apache Juneau Core Tests</name> + <description>Core API tests.</description> + <packaging>bundle</packaging> + + <parent> + <groupId>org.apache.juneau</groupId> + <artifactId>juneau</artifactId> + <version>6.0.2-incubating-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <dependencies> + <dependency> + <groupId>org.apache.juneau</groupId> + <artifactId>juneau-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.juneau</groupId> + <artifactId>juneau-core-rdf</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.jena</groupId> + <artifactId>jena-core</artifactId> + <optional>false</optional> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> + + <properties> + <!-- Skip javadoc generation since we generate them in the aggregate pom --> + <maven.javadoc.skip>true</maven.javadoc.skip> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <version>3.2.0</version> + <extensions>true</extensions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.19.1</version> + <configuration> + <includes> + <include>**/*Test.class</include> + </includes> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-source-plugin</artifactId> + <version>3.0.1</version> + <executions> + <execution> + <id>attach-sources</id> + <phase>verify</phase> + <goals> + <goal>jar-no-fork</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <version>0.7.2.201409121644</version> + <executions> + <execution> + <id>default-prepare-agent</id> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + <execution> + <id>default-report</id> + <phase>prepare-package</phase> + <goals> + <goal>report</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/AnnotationsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/AnnotationsTest.java b/juneau-core-test/src/test/java/org/apache/juneau/AnnotationsTest.java new file mode 100755 index 0000000..378f088 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/AnnotationsTest.java @@ -0,0 +1,82 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import org.apache.juneau.annotation.*; +import org.junit.*; + + +@SuppressWarnings({"rawtypes","javadoc"}) +public class AnnotationsTest { + + //==================================================================================================== + // Bean with explicitly specified properties. + //==================================================================================================== + @Test + public void testBeanWithExplicitProperties() throws Exception { + BeanSession session = BeanContext.DEFAULT.createSession(); + BeanMap bm = null; + + // Basic test + bm = session.newBeanMap(Person1.class).load("{age:21,name:'foobar'}"); + assertNotNull(bm); + assertNotNull(bm.getBean()); + assertEquals(bm.get("age"), 21); + assertEquals(bm.get("name"), "foobar"); + + bm.put("age", 65); + bm.put("name", "futbol"); + assertEquals(bm.get("age"), 65); + assertEquals(bm.get("name"), "futbol"); + } + + /** Class with explicitly specified properties */ + @Bean(properties="age,name") + public static class Person1 { + public int age; + private String name; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + } + + //==================================================================================================== + // Private/protected/default fields should be ignored. + //==================================================================================================== + @Test + public void testForOnlyPublicFields() throws Exception { + BeanSession session = BeanContext.DEFAULT.createSession(); + BeanMap bm = null; + + // Make sure only public fields are detected + bm = session.newBeanMap(A.class).load("{publicField:123}"); + assertNotNull("F1", bm); + assertNotNull("F2", bm.getBean()); + assertObjectEquals("{publicField:123}", bm.getBean()); + + } + + public static class A { + public int publicField; + protected int protectedField; + @SuppressWarnings("unused") + private int privateField; + int defaultField; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/BeanConfigTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/BeanConfigTest.java b/juneau-core-test/src/test/java/org/apache/juneau/BeanConfigTest.java new file mode 100755 index 0000000..de7fe8b --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/BeanConfigTest.java @@ -0,0 +1,861 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau; + +import static org.apache.juneau.BeanContext.*; +import static org.junit.Assert.*; + +import java.io.*; +import java.lang.reflect.*; +import java.util.*; + +import org.apache.juneau.annotation.*; +import org.apache.juneau.json.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.transform.*; +import org.junit.*; + +@SuppressWarnings({"unchecked","rawtypes","serial","javadoc"}) +public class BeanConfigTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + + BeanSession session = BeanContext.DEFAULT.createSession(); + + Person p1 = new Person(); + p1.setName("John Doe"); + p1.setAge(25); + + Address a = new Address("101 Main St.", "Las Vegas", "NV", "89101"); + AddressablePerson p2 = new AddressablePerson(); + p2.setName("Jane Doe"); + p2.setAge(21); + p2.setAddress(a); + + // setup the reference results + Map m1 = new LinkedHashMap(); + m1.put("name", p1.getName()); + m1.put("age", new Integer(p1.getAge())); + + Map m2 = new LinkedHashMap(); + m2.put("street", a.getStreet()); + m2.put("city", a.getCity()); + m2.put("state", a.getState()); + m2.put("zip", a.getZip()); + + Map m3 = new LinkedHashMap(); + m3.put("name", p2.getName()); + m3.put("age", new Integer(p2.getAge())); + m3.put("address", p2.getAddress()); + + Map pm1 = session.toBeanMap(p1); + + if (pm1.size() != m1.size()) + fail("Bean Map size failed for: " + p1 + " / " + pm1.size()+ " / " + m1.size()); + + if (!pm1.keySet().equals(m1.keySet())) + fail("Bean Map key set equality failed for: " + p1 + " / " + pm1.keySet() + " / " + m1.keySet()); + + if (!m1.keySet().equals(pm1.keySet())) + fail("Bean Map key set reverse equality failed for: " + p1 + " / " + pm1.keySet() + " / " + m1.keySet()); + + if (!pm1.equals(m1)) + fail("Bean Map equality failed for: " + p1 + " / " + pm1 + " / " + m1); + + if (!m1.equals(pm1)) + fail("Bean Map reverse equality failed for: " + p1 + " / " + pm1 + " / " + m1); + + BeanMap bm1 = null; + try { + bm1 = session.newBeanMap(Address.class); + fail("Address returned as a new bean type, but shouldn't be since it doesn't have a default constructor."); + } catch (BeanRuntimeException e) { + // Good. + } + bm1 = session.toBeanMap(new Address("street", "city", "state", "zip")); + + BeanMap bm2 = session.newBeanMap(java.lang.Integer.class); + if (bm2 != null) + fail("java.lang.Integer incorrectly desingated as bean type."); + + BeanMap bm3 = session.newBeanMap(java.lang.Class.class); + if (bm3 != null) + fail("java.lang.Class incorrectly desingated as bean type."); + + Map m4 = bm1; + if (m4.keySet().size() != m2.size()) + fail("Bean Adapter map's key set has wrong size: " + a + " / " + m4.keySet().size() + " / " + m2.size()); + + Iterator iter = m4.keySet().iterator(); + Set temp = new HashSet(); + int count = 0; + while (iter.hasNext()) { + temp.add(iter.next()); + count++; + } + if (count != m2.size()) + fail("Iteration count over bean adpater key set failed: " + a + " / " + count + " / " + m2.size()); + + if (!m2.keySet().equals(temp)) + fail("Iteration over bean adpater key set failed: " + a + " / " + m4.keySet() + " / " + m2.keySet()); + + BeanMap bm4 = session.toBeanMap(p2); + if (bm4 == null) { + fail("Failed to identify class as bean type: " + p2.getClass()); + return; + } + + Map m5 = bm4; + Set es1 = m5.entrySet(); + + if (!es1.equals(m3.entrySet())) + fail("Entry set equality failed: " + p2 + " / " + es1 + " / " + m3.entrySet()); + + if (!m3.entrySet().equals(es1)) + fail("Entry set reverse equality failed: " + p2 + " / " + es1 + " / " + m3.entrySet()); + + iter = es1.iterator(); + temp = new HashSet(); + count = 0; + while (iter.hasNext()) { + temp.add(iter.next()); + count++; + } + if (count != m3.size()) + fail("Iteration count over bean adpater entry set failed: " + a + " / " + count + " / " + m3.size()); + + if (!m3.entrySet().equals(temp)) + fail("Iteration over bean adpater entry set failed: " + a + " / " + es1 + " / " + m3.entrySet()); + } + + public static class Person { + private String name; + private int age; + + public Person() { + this.name = null; + this.age = -1; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return this.age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override /* Object */ + public String toString() { + return ("Person(name: " + this.getName() + ", age: " + + this.getAge() + ")"); + } + } + + public static class Address { + protected String street; + protected String city; + protected String state; + protected String zip; + + public Address(String street, String city, String state, String zip) { + this.street = street; + this.city = city; + this.state = state; + this.zip = zip; + } + + public String getStreet() { + return this.street; + } + + public String getCity() { + return this.city; + } + + public String getState() { + return this.state; + } + + public String getZip() { + return this.zip; + } + + @Override /* Object */ + public boolean equals(Object o) { + if (o == null) + return false; + if (this == o) + return true; + if (this.getClass() != o.getClass()) + return false; + Address a = (Address) o; + + String v1 = this.getStreet(); + String v2 = a.getStreet(); + if ((v1 == null) ? (v2 != null) : (!v1.equals(v2))) + return false; + + v1 = this.getCity(); + v2 = a.getCity(); + if ((v1 == null) ? (v2 != null) : (!v1.equals(v2))) + return false; + + v1 = this.getState(); + v2 = a.getState(); + if ((v1 == null) ? (v2 != null) : (!v1.equals(v2))) + return false; + + v1 = this.getZip(); + v2 = a.getZip(); + return ((v1 == null) ? (v2 == null) : (v1.equals(v2))); + } + + @Override /* Object */ + public int hashCode() { + int code = 0; + if (this.street != null) + code ^= this.street.hashCode(); + if (this.city != null) + code ^= this.city.hashCode(); + if (this.state != null) + code ^= this.state.hashCode(); + if (this.zip != null) + code ^= this.zip.hashCode(); + return code; + } + + @Override /* Object */ + public String toString() { + return ("Address(street: " + this.getStreet() + ", city: " + + this.getCity() + ", state: " + this.getState() + + ", zip: " + this.getZip() + ")"); + } + } + + public static class AddressablePerson extends Person { + private Address address; + + public AddressablePerson() { + this.address = null; + } + + public Address getAddress() { + return this.address; + } + + public void setAddress(Address addr) { + this.address = addr; + } + + @Override /* Object */ + public String toString() { + return super.toString() + "@" + this.address; + } + } + + //==================================================================================================== + // Exhaustive test of BeanContext.convertToType(); + //==================================================================================================== + @Test + public void testBeanContextConvertToType() throws Exception { + BeanSession session = BeanContext.DEFAULT.createSession(); + Object o; + + // Primitive nulls. + o = null; + assertEquals(new Integer(0), session.convertToType(o, Integer.TYPE)); + assertEquals(new Short((short) 0), session.convertToType(o, Short.TYPE)); + assertEquals(new Long(0), session.convertToType(o, Long.TYPE)); + assertEquals(new Float(0), session.convertToType(o, Float.TYPE)); + assertEquals(new Double(0), session.convertToType(o, Double.TYPE)); + assertEquals(new Byte((byte) 0), session.convertToType(o, Byte.TYPE)); + assertEquals(new Character((char) 0), session.convertToType(o, Character.TYPE)); + assertEquals(Boolean.FALSE, session.convertToType(o, Boolean.TYPE)); + + o = "1"; + + assertEquals(new Integer(1), session.convertToType(o, Integer.class)); + assertEquals(new Short((short) 1), session.convertToType(o, Short.class)); + assertEquals(new Long(1), session.convertToType(o, Long.class)); + assertEquals(new Float(1), session.convertToType(o, Float.class)); + assertEquals(new Double(1), session.convertToType(o, Double.class)); + assertEquals(new Byte((byte) 1), session.convertToType(o, Byte.class)); + assertEquals(new Character('1'), session.convertToType(o, Character.class)); + assertEquals(Boolean.FALSE, session.convertToType(o, Boolean.class)); + + assertEquals(new Integer(1), session.convertToType(o, Integer.TYPE)); + assertEquals(new Short((short) 1), session.convertToType(o, Short.TYPE)); + assertEquals(new Long(1), session.convertToType(o, Long.TYPE)); + assertEquals(new Float(1), session.convertToType(o, Float.TYPE)); + assertEquals(new Double(1), session.convertToType(o, Double.TYPE)); + assertEquals(new Byte((byte) 1), session.convertToType(o, Byte.TYPE)); + assertEquals(new Character('1'), session.convertToType(o, Character.TYPE)); + assertEquals(Boolean.FALSE, session.convertToType(o, Boolean.TYPE)); + + o = new Integer(1); + + assertEquals(new Integer(1), session.convertToType(o, Integer.TYPE)); + assertEquals(new Short((short) 1), session.convertToType(o, Short.TYPE)); + assertEquals(new Long(1), session.convertToType(o, Long.TYPE)); + assertEquals(new Float(1), session.convertToType(o, Float.TYPE)); + assertEquals(new Double(1), session.convertToType(o, Double.TYPE)); + assertEquals(new Byte((byte) 1), session.convertToType(o, Byte.TYPE)); + assertEquals(new Character('1'), session.convertToType(o, Character.TYPE)); + assertEquals(Boolean.TRUE, session.convertToType(o, Boolean.TYPE)); + + o = new Integer(0); + assertEquals(Boolean.FALSE, session.convertToType(o, Boolean.TYPE)); + + // Bean + o = "{name:'x',age:123}"; + assertEquals("x", session.convertToType(o, Person.class).getName()); + assertEquals(123, session.convertToType(o, Person.class).getAge()); + + // Read-only bean + o = "{name:'x',age:123}"; + assertEquals("x", session.convertToType(o, ReadOnlyPerson.class).getName()); + assertEquals(123, session.convertToType(o, ReadOnlyPerson.class).getAge()); + + // Class with forString(String) method. + o = UUID.randomUUID(); + assertEquals(o, session.convertToType(o.toString(), UUID.class)); + + // Class with Constructor(String). + o = "xxx"; + File file = session.convertToType(o, File.class); + assertEquals("xxx", file.getName()); + + // List of ints to array + o = new ObjectList(1, 2, 3); + assertEquals(1, session.convertToType(o, int[].class)[0]); + + // List of beans to array + o = new ObjectList(new ReadOnlyPerson("x", 123)); + assertEquals("x", session.convertToType(o, ReadOnlyPerson[].class)[0].getName()); + + // Multi-dimensional array of beans. + o = new ObjectList().append(new ObjectList(new ReadOnlyPerson("x", 123))); + assertEquals("x", session.convertToType(o, ReadOnlyPerson[][].class)[0][0].getName()); + + // Array of strings to array of ints + o = new String[] { "1", "2", "3" }; + assertEquals(new Integer(1), session.convertToType(o, Integer[].class)[0]); + assertEquals(1, session.convertToType(o, int[].class)[0]); + + // Array to list + o = new Integer[] { 1, 2, 3 }; + assertEquals(new Integer(1), session.convertToType(o, LinkedList.class).get(0)); + + // HashMap to TreeMap + o = new HashMap<Integer, String>() {{ put(1, "foo"); }}; + assertEquals("foo", session.convertToType(o, TreeMap.class).firstEntry().getValue()); + + // String to TreeMap + o = "{1:'foo'}"; + assertEquals("foo", session.convertToType(o, TreeMap.class).firstEntry().getValue()); + + // String to generic Map + assertEquals("foo", session.convertToType(o, Map.class).values().iterator().next()); + + // Array to String + o = new Object[] { "a", 1, false }; + assertEquals("['a',1,false]", session.convertToType(o, String.class)); + o = new Object[][] { { "a", 1, false } }; + assertEquals("[['a',1,false]]", session.convertToType(o, String.class)); + + } + + //==================================================================================================== + // Test properties set through a constructor. + //==================================================================================================== + @Test + public void testReadOnlyProperties() throws Exception { + BeanSession session = BeanContext.DEFAULT.createSession(); + Object o; + + // Bean to String + o = new ReadOnlyPerson("x", 123); + assertEquals("{name:'x',age:123}", session.convertToType(o, String.class)); + + // List of Maps to array of beans. + o = new ObjectList(new ObjectMap("{name:'x',age:1}"), new ObjectMap("{name:'y',age:2}")); + assertEquals(1, session.convertToType(o, ReadOnlyPerson[].class)[0].getAge()); + } + + + @Bean(properties="name,age") + public static class ReadOnlyPerson { + private final String name; + private final int age; + + @BeanConstructor(properties="name,age") + public ReadOnlyPerson(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return this.name; + } + + public int getAge() { + return this.age; + } + + @Override /* Object */ + public String toString() { + return "toString():name=" + name + ",age=" + age; + } + } + + //==================================================================================================== + // testEnums + //==================================================================================================== + @Test + public void testEnums() throws Exception { + BeanSession session = BeanContext.DEFAULT.createSession(); + Object o; + + // Enum + o = "ENUM2"; + assertEquals(TestEnum.ENUM2, session.convertToType(o, TestEnum.class)); + assertEquals("ENUM2", session.convertToType(TestEnum.ENUM2, String.class)); + + // Array of enums + o = new String[] { "ENUM2" }; + assertEquals(TestEnum.ENUM2, session.convertToType(o, TestEnum[].class)[0]); + } + + public enum TestEnum { + ENUM1, ENUM2, ENUM3 + } + + //==================================================================================================== + // testProxyHandler + //==================================================================================================== + @Test + public void testProxyHandler() throws Exception { + BeanSession session = ContextFactory.create().setClassLoader(A.class.getClassLoader()).getBeanContext().createSession(); + + A f1 = (A) Proxy.newProxyInstance(this.getClass() + .getClassLoader(), new Class[] { A.class }, + new AHandler()); + + BeanMap bm1 = session.toBeanMap(f1); + if (bm1 == null) { + fail("Failed to obtain bean adapter for proxy: " + f1); + return; + } + + BeanMap bm2 = session.newBeanMap(A.class); + if (bm2 == null) { + fail("Failed to create dynamic proxy bean for interface: " + A.class.getName()); + return; + } + bm2.put("a", "Hello"); + bm2.put("b", new Integer(50)); + f1.setA("Hello"); + f1.setB(50); + + if (!bm2.get("a").equals("Hello")) + fail("Failed to set string property 'a' on dynamic proxy bean. " + bm2); + + if (!bm2.get("b").equals(new Integer(50))) + fail("Failed to set string property 'b' on dynamic proxy bean. " + bm2); + + if (!bm1.equals(bm2)) + fail("Failed equality test of dynamic proxies beans: " + bm1 + " / " + bm2); + + if (!bm2.equals(bm1)) + fail("Failed reverse equality test of dynamic proxies beans: " + bm1 + " / " + bm2); + } + + public static interface A { + String getA(); + + void setA(String a); + + int getB(); + + void setB(int b); + } + + public static class AHandler implements InvocationHandler { + private Map map; + + public AHandler() { + this.map = new HashMap(); + this.map.put("a", ""); + this.map.put("b", new Integer(0)); + } + + @Override /* InvocationHandler */ + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + String methodName = method.getName(); + if (methodName.equals("getA")) { + return this.map.get("a"); + } + if (methodName.equals("setA")) { + this.map.put("a", args[0]); + return null; + } + if (methodName.equals("getB")) { + return this.map.get("b"); + } + if (methodName.equals("setB")) { + this.map.put("b", args[0]); + return null; + } + if (methodName.equals("toString")) { + return this.map.toString(); + } + return null; + } + } + + //==================================================================================================== + // testGetClassMetaFromString + //==================================================================================================== + @Test + public void testGetClassMetaFromString() throws Exception { + BeanContext bc = BeanContext.DEFAULT; + assertEquals("java.lang.String[]", bc.getClassMetaFromString("java.lang.String[]").toString()); + assertEquals("java.lang.String[]", bc.getClassMetaFromString("[Ljava.lang.String;").toString()); + assertEquals("java.lang.String[][]", bc.getClassMetaFromString("java.lang.String[][]").toString()); + assertEquals("java.lang.String[][]", bc.getClassMetaFromString("[[Ljava.lang.String;").toString()); + assertEquals("boolean", bc.getClassMetaFromString("boolean").toString()); + } + + //==================================================================================================== + // testFluentStyleSetters + //==================================================================================================== + @Test + public void testFluentStyleSetters() throws Exception { + B2 t = new B2().init(); + BeanMap m = BeanContext.DEFAULT.createSession().toBeanMap(t); + m.put("f1", 2); + assertEquals(t.f1, 2); + } + + public static class B { + int f1; + public int getF1() { return f1; } + public B setF1(int f1) { this.f1 = f1; return this; } + } + + public static class B2 extends B { + @Override /* B */ + public B2 setF1(int f1) { this.f1 = f1; return this; } + public B2 init() { this.f1 = 1; return this;} + } + + //==================================================================================================== + // testClassMetaCaching + //==================================================================================================== + @Test + public void testClassMetaCaching() throws Exception { + Parser p1, p2; + + p1 = new JsonParser(); + p2 = new JsonParser(); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beansRequireDefaultConstructor, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beansRequireDefaultConstructor, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beansRequireSerializable, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beansRequireSerializable, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beansRequireSettersForGetters, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beansRequireSettersForGetters, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beansRequireSomeProperties, false); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beansRequireSomeProperties, false); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beanMapPutReturnsOldValue, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanMapPutReturnsOldValue, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beanConstructorVisibility, Visibility.DEFAULT); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanConstructorVisibility, Visibility.DEFAULT); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanConstructorVisibility, Visibility.NONE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanConstructorVisibility, Visibility.NONE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanConstructorVisibility, Visibility.PRIVATE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanConstructorVisibility, Visibility.PRIVATE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanConstructorVisibility, Visibility.PROTECTED); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanConstructorVisibility, Visibility.PROTECTED); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beanClassVisibility, Visibility.DEFAULT); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanClassVisibility, Visibility.DEFAULT); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanClassVisibility, Visibility.NONE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanClassVisibility, Visibility.NONE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanClassVisibility, Visibility.PRIVATE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanClassVisibility, Visibility.PRIVATE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanClassVisibility, Visibility.PROTECTED); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanClassVisibility, Visibility.PROTECTED); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_beanFieldVisibility, Visibility.DEFAULT); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanFieldVisibility, Visibility.DEFAULT); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanFieldVisibility, Visibility.NONE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanFieldVisibility, Visibility.NONE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanFieldVisibility, Visibility.PRIVATE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanFieldVisibility, Visibility.PRIVATE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_beanFieldVisibility, Visibility.PROTECTED); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_beanFieldVisibility, Visibility.PROTECTED); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_methodVisibility, Visibility.DEFAULT); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_methodVisibility, Visibility.DEFAULT); + assertSameCache(p1, p2); + p1.setProperty(BEAN_methodVisibility, Visibility.NONE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_methodVisibility, Visibility.NONE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_methodVisibility, Visibility.PRIVATE); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_methodVisibility, Visibility.PRIVATE); + assertSameCache(p1, p2); + p1.setProperty(BEAN_methodVisibility, Visibility.PROTECTED); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_methodVisibility, Visibility.PROTECTED); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_useJavaBeanIntrospector, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_useJavaBeanIntrospector, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_useInterfaceProxies, false); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_useInterfaceProxies, false); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_ignoreUnknownBeanProperties, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_ignoreUnknownBeanProperties, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_ignoreUnknownNullBeanProperties, false); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_ignoreUnknownNullBeanProperties, false); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_ignorePropertiesWithoutSetters, false); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_ignorePropertiesWithoutSetters, false); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_ignoreInvocationExceptionsOnGetters, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_ignoreInvocationExceptionsOnGetters, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_ignoreInvocationExceptionsOnSetters, true); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_ignoreInvocationExceptionsOnSetters, true); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_notBeanPackages_add, "foo"); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_notBeanPackages_add, "foo"); + assertSameCache(p1, p2); + p1.setProperty(BEAN_notBeanPackages_add, "bar"); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_notBeanPackages_add, "bar"); + assertSameCache(p1, p2); + p1.setProperty(BEAN_notBeanPackages_add, "baz"); + p1.setProperty(BEAN_notBeanPackages_add, "bing"); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_notBeanPackages_add, "bing"); + p2.setProperty(BEAN_notBeanPackages_add, "baz"); + assertSameCache(p1, p2); + + p1.setProperty(BEAN_notBeanPackages_remove, "bar"); + assertDifferentCache(p1, p2); + p2.setProperty(BEAN_notBeanPackages_remove, "bar"); + assertSameCache(p1, p2); + + p1.addPojoSwaps(DummyPojoSwapA.class); + assertDifferentCache(p1, p2); + p2.addPojoSwaps(DummyPojoSwapA.class); + assertSameCache(p1, p2); + p1.addPojoSwaps(DummyPojoSwapB.class,DummyPojoSwapC.class); // Order of filters is important! + p2.addPojoSwaps(DummyPojoSwapC.class,DummyPojoSwapB.class); + assertDifferentCache(p1, p2); + + p1 = new JsonParser(); + p2 = new JsonParser(); + p1.addBeanFilters(DummyBeanFilterA.class); + assertDifferentCache(p1, p2); + p2.addBeanFilters(DummyBeanFilterA.class); + assertSameCache(p1, p2); + p1.addBeanFilters(DummyBeanFilterB.class,DummyBeanFilterC.class); // Order of filters is important! + p2.addBeanFilters(DummyBeanFilterC.class,DummyBeanFilterB.class); + assertDifferentCache(p1, p2); + } + + public static class DummyPojoSwapA extends MapSwap<A> {} + public static class DummyPojoSwapB extends MapSwap<B> {} + public static class DummyPojoSwapC extends MapSwap<C> {} + public static class DummyBeanFilterA extends BeanFilterBuilder { + public DummyBeanFilterA() { + super(A.class); + } + } + public static class DummyBeanFilterB extends BeanFilterBuilder { + public DummyBeanFilterB() { + super(B.class); + } + } + public static class DummyBeanFilterC extends BeanFilterBuilder { + public DummyBeanFilterC() { + super(C.class); + } + } + public static class C {} + + private void assertSameCache(Parser p1, Parser p2) { + assertTrue(p1.getBeanContext().hasSameCache(p2.getBeanContext())); + assertTrue(p1.getBeanContext().hashCode() == p2.getBeanContext().hashCode()); + } + + private void assertDifferentCache(Parser p1, Parser p2) { + assertFalse(p1.getBeanContext().hasSameCache(p2.getBeanContext())); + assertFalse(p1.getBeanContext().hashCode() == p2.getBeanContext().hashCode()); + } + + //==================================================================================================== + // testNotABeanReasons + //==================================================================================================== + @Test + public void testNotABeanNonStaticInnerClass() throws Exception { + BeanContext bc = BeanContext.DEFAULT; + ClassMeta cm = bc.getClassMeta(C1.class); + assertFalse(cm.canCreateNewInstance()); + } + + public class C1 { + public int f1; + } + + //==================================================================================================== + // testAddingToArrayProperty + // This tests the speed of the BeanMap.add() method against array properties. + // For performance reasons, array properties are stored as temporary ArrayLists until the + // BeanMap.getBean() method is called. + //==================================================================================================== + @Test(timeout=1000) // Should be around 100ms at most. + public void testAddingToArrayProperty() throws Exception { + BeanSession session = BeanContext.DEFAULT.createSession(); + BeanMap<D> bm = session.newBeanMap(D.class); + for (int i = 0; i < 5000; i++) { + bm.add("f1", i); + bm.add("f2", i); + bm.add("f3", i); + bm.add("f4", i); + } + D d = bm.getBean(); + assertEquals(d.f1.length, 5000); + assertEquals(d.f2.length, 5000); + assertEquals(d.f3.length, 5003); + assertEquals(d.f4.length, 5003); + } + + public class D { + public int[] f1; + private int[] f2; + public int[] f3 = new int[]{1,2,3}; + private int[] f4 = new int[]{1,2,3}; + public int[] getF2() {return f2;} + public void setF2(int[] f2) {this.f2 = f2;} + public int[] getF4() {return f4;} + public void setF4(int[] f4) {this.f4 = f4;} + } + + //==================================================================================================== + // testClassClassMeta + // Make sure we can get ClassMeta objects against the Class class. + //==================================================================================================== + @Test + public void testClassClassMeta() throws Exception { + ClassMeta cm = BeanContext.DEFAULT.getClassMeta(Class.class); + assertNotNull(cm); + + cm = BeanContext.DEFAULT.getClassMeta(Class[].class); + assertNotNull(cm); + } + + //==================================================================================================== + // testBlanks + //==================================================================================================== + @Test + public void testBlanks() throws Exception { + BeanSession session = BeanContext.DEFAULT.createSession(); + + // Blanks get interpreted as the default value for primitives and null for boxed objects. + assertEquals(0, (int)session.convertToType("", int.class)); + assertNull(session.convertToType("", Integer.class)); + + // Booleans are handled different since 'new Boolean("")' is valid and resolves to false + // while 'new Integer("")' produces an exception. + assertEquals(false, (boolean)session.convertToType("", boolean.class)); + assertEquals(false, session.convertToType("", Boolean.class)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/BeanFilterTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/BeanFilterTest.java b/juneau-core-test/src/test/java/org/apache/juneau/BeanFilterTest.java new file mode 100755 index 0000000..fb27219 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/BeanFilterTest.java @@ -0,0 +1,143 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.annotation.*; +import org.apache.juneau.json.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class BeanFilterTest { + + //==================================================================================================== + // Test sub types + //==================================================================================================== + @Test + public void testSubTypes() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX; + JsonParser p = JsonParser.DEFAULT; + + A1 a1 = new A1(); + a1.f1 = "f1"; + a1.fb = new B2(); + ((B2)a1.fb).f2 = "f2"; + String r = s.serialize(a1); + assertEquals("{subType:'A1',f0:'f0',fb:{subType:'B2',f0b:'f0b',f2:'f2'},f1:'f1'}", r); + + A a = p.parse(r, A.class); + assertTrue(a instanceof A1); + assertTrue(a.fb instanceof B2); + assertEquals("f1", ((A1)a).f1); + assertEquals("f2", ((B2)a.fb).f2); + + // Try out-of-order creation. + r = "{f0:'f0',f1:'f1',subType:'A1',fb:{f0b:'f0b',f2:'f2',subType:'B2'}}"; + a = p.parse(r, A.class); + assertTrue(a instanceof A1); + assertTrue(a.fb instanceof B2); + assertEquals("f1", ((A1)a).f1); + assertEquals("f2", ((B2)a.fb).f2); + } + + @Bean( + subTypeProperty="subType", + subTypes={A1.class,A2.class} + ) + public static abstract class A { + public String f0 = "f0"; + public B fb; + } + + @Bean(typeName="A1") + public static class A1 extends A { + public String f1; + } + + @Bean(typeName="A2") + public static class A2 extends A { + public String f2; + } + + @Bean( + subTypeProperty="subType", + subTypes={B1.class,B2.class} + ) + public static abstract class B { + public String f0b = "f0b"; + } + + @Bean(typeName="B1") + public static class B1 extends B { + public String f1; + } + + @Bean(typeName="B2") + public static class B2 extends B { + public String f2; + } + + //==================================================================================================== + // Test parent class used as filter + //==================================================================================================== + @Test + public void testParentClassFilter() throws Exception { + JsonSerializer s = new JsonSerializer.Simple().addBeanFilters(C1.class); + + C1 c1 = new C2(); + String r = s.serialize(c1); + assertEquals("{f0:'f0'}", r); + + List<C1> l = new LinkedList<C1>(); + l.add(new C2()); + r = s.serialize(l); + assertEquals("[{f0:'f0'}]", r); + } + + public static class C1 { + public String f0 = "f0"; + } + + public static class C2 extends C1 { + public String f1 = "f1"; + } + + //==================================================================================================== + // Test non-static parent class used as filter + //==================================================================================================== + @Test + public void testParentClassFilter2() throws Exception { + JsonSerializer s = new JsonSerializer.Simple().addBeanFilters(D1.class); + + D1 d1 = new D2(); + String r = s.serialize(d1); + assertEquals("{f0:'f0'}", r); + + List<D1> l = new LinkedList<D1>(); + l.add(new D2()); + r = s.serialize(l); + assertEquals("[{f0:'f0'}]", r); + } + + public class D1 { + public String f0 = "f0"; + } + + public class D2 extends D1 { + public String f1 = "f1"; + } + +}
