/*
 *  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.bval.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Map;

import org.apache.bval.MetaBeanFinder;
import org.apache.bval.example.BusinessObject;
import org.apache.bval.model.MetaBean;
import org.junit.Before;
import org.junit.Test;

/**
 * Description: <br>
 * User: roman.stumm<br>
 * Date: 17.06.2010<br>
 * Time: 10:28:48<br>
 */
public class XMLMetaBeanManagerTest {
    XMLMetaBeanManager mbm = new XMLMetaBeanManager();

    @Before
    public void setUp() throws Exception {
        mbm.addLoader(new XMLMetaBeanURLLoader(BusinessObject.class.getResource("test-beanInfos.xml")));
    }

    @Test
    public void testEnrichCopies() throws Exception {
        Map<String, MetaBean> copies =
            mbm.enrichCopies(new XMLMetaBeanURLLoader(BusinessObject.class.getResource("test-beanInfos-custom.xml"))
                .load());
        assertNotNull(copies);
        MetaBean mb = copies.get(BusinessObject.class.getName());
        assertFalse(mb.getProperty("lastName").isMandatory());
        MetaBean mb2 = mbm.findForClass(BusinessObject.class);
        assertTrue(mb2.getProperty("lastName").isMandatory());
    }

    @Test
    public void testCopy() {
        MetaBean mb = mbm.findForClass(BusinessObject.class);
        MetaBean mb2 = mb.copy();
        assertTrue(mb2 != mb);
        assertTrue(mb2.getProperty("dateBirth") != mb.getProperty("dateBirth"));
    }

    @SuppressWarnings("deprecation")
    @Test
    public void testFindForClass() throws Exception {
        MetaBeanFinder finder = mbm;
        MetaBean info = finder.findForClass(BusinessObject.class);
        assertNotNull(info);
        assertTrue(info == info.getProperty("address").getMetaBean().getProperty("owner").getMetaBean());
        assertTrue(info == info.getProperty("addresses").getMetaBean().getProperty("owner").getMetaBean());
        assertTrue(info.getProperty("email").getJavaScriptValidations().length > 0);
    }

    @Test
    public void testFindAll() {
        Map<String, MetaBean> all = mbm.findAll();
        assertNotNull(all);
        Map<String, MetaBean> all2 = mbm.findAll();
        assertEquals(all.size(), all2.size());
        assertTrue(all.get(BusinessObject.class.getName()) == all2.get(BusinessObject.class.getName()));
        assertTrue(all.get(BusinessObject.class.getName()) != null);
        MetaBean bean = all.get(BusinessObject.class.getName());
        assertTrue(bean == bean.getProperty("address").getMetaBean().getProperty("owner").getMetaBean());
        assertTrue(bean == bean.getProperty("addresses").getMetaBean().getProperty("owner").getMetaBean());
    }

}

@@ -971,16 +971,59 @@
 otNull;%0A
+import static org.junit.Assert.assertSame;%0A
 import s
@@ -1229,16 +1229,21 @@
 t.Before
+Class
 ;%0Aimport
@@ -1402,16 +1402,31 @@
 t %7B%0A    
+private static 
 XMLMetaB
@@ -1480,16 +1480,21 @@
  @Before
+Class
 %0A    pub
@@ -1493,24 +1493,31 @@
 %0A    public 
+static 
 void setUp()
@@ -2340,22 +2340,23 @@
 sert
-True(mb2 !=
+NotSame(mb,
  mb
+2
 );%0A 
@@ -2364,32 +2364,34 @@
       assert
-Tru
+NotSam
 e(mb
-2
 .getProperty
@@ -2407,14 +2407,13 @@
 th%22)
- !=
+,
  mb
+2
 .get
@@ -2687,36 +2687,34 @@
       assert
-Tru
+Sam
 e(info
- ==
+,
  info.getPro
@@ -2799,20 +2799,18 @@
 sert
-Tru
+Sam
 e(info
- ==
+,
  inf
@@ -3205,35 +3205,35 @@
 ;%0A        assert
-Tru
+Sam
 e(all.get(Busine
@@ -3257,19 +3257,17 @@
 tName())
- ==
+,
  all2.ge
@@ -3316,20 +3316,23 @@
   assert
-True
+NotNull
 (all.get
@@ -3367,16 +3367,8 @@
 e())
- != null
 );%0A 
@@ -3441,36 +3441,34 @@
       assert
-Tru
+Sam
 e(bean
- ==
+,
  bean.getPro
@@ -3553,20 +3553,18 @@
 sert
-Tru
+Sam
 e(bean
- ==
+,
  bea
@@ -3648,10 +3648,101 @@
 %0A    %7D%0A%0A
+    static void assertNotSame(Object o1, Object o2) %7B%0A        assertFalse(o1 == o2);%0A    %7D%0A
 %7D%0A

test cleanup