001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/storers/RelationalPropertyStorageStrategyForURITest.java,v 1.1.1.1 2004/03/25 12:08:36 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:36 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040: package es.udc.mypersonalizer.kernel.model.repository.sql.storers;
041:
042: import java.sql.Connection;
043: import java.sql.Statement;
044: import java.util.Collections;
045: import java.util.Map;
046:
047: import es.udc.mypersonalizer.kernel.model.annotators.sql.SQLPersistenceTypeAnnotationHelper;
048: import es.udc.mypersonalizer.kernel.model.metainfo.MetaCompoundProperty;
049: import es.udc.mypersonalizer.kernel.model.metainfo.MetaService;
050: import es.udc.mypersonalizer.kernel.model.metainfo.MetaServiceRegistrySingleton;
051: import es.udc.mypersonalizer.kernel.model.metainfo.MetaSimpleProperty;
052: import es.udc.mypersonalizer.kernel.model.properties.CompoundProperty;
053: import es.udc.mypersonalizer.kernel.model.properties.PropertyStructure;
054: import es.udc.mypersonalizer.kernel.model.properties.SimpleProperty;
055: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfigManager;
056: import es.udc.mypersonalizer.kernel.model.repository.sql.plain.ConnectionPoolAdapterSingleton;
057:
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: /**
062: * Test case for manipulating user registration information. This is tested
063: * independently becouse it uses a <code>String</code> key instead of an
064: * augenerated <code>Long</code> key.
065: *
066: * @author Abel Muinho
067: * @since 1.0
068: */
069: public class RelationalPropertyStorageStrategyForURITest extends
070: TestCase {
071:
072: private static final String SIMPLE_MULTIVALUED_PROPERTY = "interests";
073:
074: private Connection connection;
075:
076: private String uriTable = null;
077:
078: private String uriInterestsTable = null;
079:
080: private String loginColumn = null;
081:
082: private RelationalPropertyStorageStrategy strategy = null;
083:
084: public RelationalPropertyStorageStrategyForURITest(String name)
085: throws Exception {
086: super (name);
087: MetaService uri = MetaServiceRegistrySingleton.getInstance()
088: .getUserRegistrationInformationMetaService();
089: MetaCompoundProperty root = (MetaCompoundProperty) uri
090: .getMetaRootProperty();
091: uriTable = root
092: .getAnnotations()
093: .get(
094: SQLPersistenceTypeAnnotationHelper.TABLE_NAME_ANNOTATION);
095: MetaSimpleProperty preferences = (MetaSimpleProperty) root
096: .findMetaProperty(SIMPLE_MULTIVALUED_PROPERTY);
097: uriInterestsTable = preferences
098: .getAnnotations()
099: .get(
100: SQLPersistenceTypeAnnotationHelper.TABLE_NAME_ANNOTATION);
101: loginColumn = DatabaseConventionsConfigManager.getConfig()
102: .getLoginColumn();
103: }
104:
105: protected void setUp() throws Exception {
106: connection = null;
107: strategy = new RelationalPropertyStorageStrategy();
108: strategy.setMetainfo(MetaServiceRegistrySingleton.getInstance()
109: .getUserRegistrationInformationMetaService());
110: connection = ConnectionPoolAdapterSingleton.getInstance()
111: .getConnection();
112: cleanUpTables(connection);
113: Statement st = connection.createStatement();
114: st.executeUpdate("INSERT INTO " + uriTable + " (" + loginColumn
115: + ") VALUES ('testUser123')");
116: st.executeUpdate("INSERT INTO " + uriTable + " (" + loginColumn
117: + ") VALUES ('testUser456')");
118: st
119: .executeUpdate("INSERT INTO "
120: + uriInterestsTable
121: + " ("
122: + loginColumn
123: + ", genId, interests) VALUES ('testUser123', 1, 'Football')");
124: st
125: .executeUpdate("INSERT INTO "
126: + uriInterestsTable
127: + " ("
128: + loginColumn
129: + ", genId, interests) VALUES ('testUser123', 2, 'Soccer')");
130: st
131: .executeUpdate("INSERT INTO "
132: + uriInterestsTable
133: + " ("
134: + loginColumn
135: + ", genId, interests) VALUES ('testUser456', 1, 'Basket')");
136: }
137:
138: protected void tearDown() throws Exception {
139: try {
140: cleanUpTables(connection);
141: } finally {
142: if (connection != null) {
143: connection.close();
144: }
145: }
146: }
147:
148: private void cleanUpTables(Connection c) throws Exception {
149: Statement st = connection.createStatement();
150: st.executeUpdate("DELETE FROM " + uriTable + " WHERE "
151: + loginColumn + " = 'testUser123' OR " + loginColumn
152: + " = 'testUser456' OR " + loginColumn
153: + " = 'testUser789'");
154: st.executeUpdate("DELETE FROM " + uriInterestsTable + " WHERE "
155: + loginColumn + " = 'testUser123' OR " + loginColumn
156: + " = 'testUser456' OR " + loginColumn
157: + " = 'testUser789'");
158: }
159:
160: public void testFindProperty() throws Exception {
161: CompoundProperty cp = (CompoundProperty) strategy.findProperty(
162: connection, Collections.singletonMap(loginColumn,
163: "testUser123"));
164: SimpleProperty sp = (SimpleProperty) cp.findProperty("0."
165: + SIMPLE_MULTIVALUED_PROPERTY);
166: assertNotNull(sp);
167: assertEquals(sp.getValuesAsString().length, 2);
168: assertEquals(sp.getValuesAsString()[0], "Football");
169: assertEquals(sp.getValuesAsString()[1], "Soccer");
170: }
171:
172: public void testUpdateProperty() {
173:
174: }
175:
176: public void testAddProperty() throws Exception {
177: PropertyStructure values[] = new PropertyStructure[1];
178: values[0] = new PropertyStructure();
179: values[0].put(new SimpleProperty(SIMPLE_MULTIVALUED_PROPERTY,
180: new String[] { "TV", "Radio" }));
181: CompoundProperty cp = new CompoundProperty("properties", values);
182:
183: Map key = Collections.singletonMap(loginColumn, "testUser789");
184: strategy.addProperty(connection, key, cp);
185:
186: CompoundProperty foundRoot = (CompoundProperty) strategy
187: .findProperty(connection, key);
188: assertEquals(cp.getSimpleName(), foundRoot.getSimpleName());
189: assertEquals(cp
190: .findProperty("0." + SIMPLE_MULTIVALUED_PROPERTY),
191: foundRoot.findProperty("0."
192: + SIMPLE_MULTIVALUED_PROPERTY));
193: }
194:
195: public static TestSuite suite() throws Exception {
196: TestSuite suite = new TestSuite(
197: RelationalPropertyStorageStrategyForURITest.class
198: .getName());
199: suite.addTest(new RelationalPropertyStorageStrategyForURITest(
200: "testFindProperty"));
201: suite.addTest(new RelationalPropertyStorageStrategyForURITest(
202: "testAddProperty"));
203: suite.addTest(new RelationalPropertyStorageStrategyForURITest(
204: "testUpdateProperty"));
205: return suite;
206: }
207: }
|