Package org.apache.cayenne.lifecycle.id

Source Code of org.apache.cayenne.lifecycle.id.StringIdQueryTest

/*****************************************************************
*   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.cayenne.lifecycle.id;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

import org.apache.cayenne.DataRow;
import org.apache.cayenne.QueryResponse;
import org.apache.cayenne.configuration.server.ServerRuntime;
import org.apache.cayenne.test.jdbc.DBHelper;
import org.apache.cayenne.test.jdbc.TableHelper;

public class StringIdQueryTest extends TestCase {

    private ServerRuntime runtime;
    private DBHelper dbHelper;
    private TableHelper e1Helper;
    private TableHelper e2Helper;

    @Override
    protected void setUp() throws Exception {
        runtime = new ServerRuntime("cayenne-lifecycle.xml");
        dbHelper = new DBHelper(runtime.getDataSource("lifecycle-db"));
        e1Helper = new TableHelper(dbHelper, "E1", "ID");
        e2Helper = new TableHelper(dbHelper, "E2", "ID");
    }

    @Override
    protected void tearDown() throws Exception {
        runtime.shutdown();
    }

    public void testConstructor() {
        StringIdQuery q1 = new StringIdQuery();
        assertEquals(0, q1.getStringIds().size());

        StringIdQuery q2 = new StringIdQuery("a", "b", "c", "c");
        assertEquals(3, q2.getStringIds().size());
        assertTrue(q2.getStringIds().contains("a"));
        assertTrue(q2.getStringIds().contains("b"));
        assertTrue(q2.getStringIds().contains("c"));

        StringIdQuery q3 = new StringIdQuery(Arrays.asList("a", "b", "b", "c"));
        assertEquals(3, q3.getStringIds().size());
        assertTrue(q3.getStringIds().contains("a"));
        assertTrue(q3.getStringIds().contains("b"));
        assertTrue(q3.getStringIds().contains("c"));
    }

    public void testPerformQuery_SingleEntity() throws Exception {
        e1Helper.deleteAll();
        e1Helper.insert(3).insert(4);

        StringIdQuery query = new StringIdQuery("E1:3", "E1:4", "E1:5");
        QueryResponse response = runtime.newContext().performGenericQuery(query);
        assertEquals(1, response.size());
        assertEquals(2, response.firstList().size());

        Set<Number> ids = new HashSet<Number>();

        DataRow r1 = (DataRow) response.firstList().get(0);
        ids.add((Number) r1.get("ID"));

        DataRow r2 = (DataRow) response.firstList().get(1);
        ids.add((Number) r2.get("ID"));

        assertTrue(ids.contains(3l));
        assertTrue(ids.contains(4l));
    }

    public void testPerformQuery_MultipleEntities() throws Exception {
        e1Helper.deleteAll();
        e1Helper.insert(3).insert(4);

        e2Helper.deleteAll();
        e2Helper.insert(5).insert(6).insert(7);

        StringIdQuery query = new StringIdQuery("E1:3", "E1:4", "E2:6", "E1:5");
        QueryResponse response = runtime.newContext().performGenericQuery(query);
        assertEquals(2, response.size());

        Set<String> ids = new HashSet<String>();

        while (response.next()) {
            List<DataRow> list = (List<DataRow>) response.currentList();
            for (DataRow row : list) {
                ids.add(row.getEntityName() + ":" + row.get("ID"));
            }
        }

        assertEquals(3, ids.size());
        assertTrue(ids.contains("E1:3"));
        assertTrue(ids.contains("E1:4"));
        assertTrue(ids.contains("E2:6"));
    }
}
TOP

Related Classes of org.apache.cayenne.lifecycle.id.StringIdQueryTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.