001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import com.methodhead.sitecontext.*;
031:
032: public class HtmlFragmentTest extends TestCase {
033:
034: private HtmlFragment fragment = null;
035: private HtmlFragment fragment2 = null;
036: private List list = null;
037:
038: private SiteContext siteContext1_ = null;
039: private SiteContext siteContext2_ = null;
040:
041: private HtmlFragment fragment1_ = null;
042: private HtmlFragment fragment2_ = null;
043: private HtmlFragment fragment3_ = null;
044:
045: private void createData() {
046:
047: siteContext1_ = new SiteContext();
048: siteContext1_.getDomains().add("methodhead.com");
049: siteContext1_.saveNew();
050:
051: siteContext2_ = new SiteContext();
052: siteContext2_.getDomains().add("danhensgen.com");
053: siteContext2_.saveNew();
054:
055: fragment1_ = new HtmlFragment();
056: fragment1_.setSiteContext(siteContext1_);
057: fragment1_.setString("name", "Fragment1");
058: fragment1_.setString("value", "Value1");
059: fragment1_.saveNew();
060:
061: fragment2_ = new HtmlFragment();
062: fragment2_.setSiteContext(siteContext1_);
063: fragment2_.setString("name", "Fragment2");
064: fragment2_.setString("value", "Value2");
065: fragment2_.saveNew();
066:
067: fragment3_ = new HtmlFragment();
068: fragment3_.setSiteContext(siteContext2_);
069: fragment3_.setString("name", "Fragment3");
070: fragment3_.setString("value", "Value3");
071: fragment3_.saveNew();
072: }
073:
074: static {
075: TestUtils.initLogger();
076: TestUtils.initDb();
077: }
078:
079: public HtmlFragmentTest(String name) {
080: super (name);
081: }
082:
083: protected void setUp() {
084: try {
085: ConnectionSingleton.runBatchUpdate(new FileReader(
086: "webapp/WEB-INF/db/transfer-reset.sql"));
087: } catch (Exception e) {
088: fail(e.getMessage());
089: }
090: }
091:
092: protected void tearDown() {
093: }
094:
095: public void testProperties() {
096: try {
097: createData();
098:
099: fragment = new HtmlFragment();
100:
101: try {
102: fragment.getSiteContext();
103: fail("Exception not thrown.");
104: } catch (Exception e) {
105: }
106:
107: fragment.setSiteContext(siteContext1_);
108: assertEquals(siteContext1_, fragment.getSiteContext());
109: } catch (Exception e) {
110: e.printStackTrace();
111: fail();
112: }
113: }
114:
115: public void testSaveNew() {
116: try {
117: createData();
118:
119: fragment = new HtmlFragment();
120: fragment.setSiteContext(siteContext1_);
121: fragment.setString("name", "name");
122: fragment.setString("value", "value");
123: fragment.saveNew();
124:
125: fragment2 = new HtmlFragment();
126: fragment2.load("sitecontext_id=1 AND id="
127: + fragment.getInt("id"));
128: assertEquals("name", fragment2.getString("name"));
129: assertEquals("value", fragment2.getString("value"));
130: } catch (Exception e) {
131: e.printStackTrace();
132: fail();
133: }
134: }
135:
136: public void testLoadAll() {
137: try {
138: createData();
139:
140: fragment = new HtmlFragment();
141: fragment.setSiteContext(siteContext1_);
142:
143: list = fragment.loadAll();
144: assertEquals(2, list.size());
145:
146: fragment = (HtmlFragment) list.get(0);
147: assertEquals("Fragment1", fragment.getString("name"));
148:
149: fragment = (HtmlFragment) list.get(1);
150: assertEquals("Fragment2", fragment.getString("name"));
151: } catch (Exception e) {
152: e.printStackTrace();
153: fail();
154: }
155: }
156:
157: public void testDeleteAll() {
158: try {
159: createData();
160:
161: fragment = new HtmlFragment();
162: fragment.setSiteContext(siteContext1_);
163: fragment.deleteAll();
164:
165: fragment = new HtmlFragment();
166: fragment.setSiteContext(siteContext1_);
167: list = fragment.loadAll();
168: assertEquals(0, list.size());
169:
170: fragment = new HtmlFragment();
171: fragment.setSiteContext(siteContext2_);
172: list = fragment.loadAll();
173: assertEquals(1, list.size());
174: } catch (Exception e) {
175: e.printStackTrace();
176: fail();
177: }
178: }
179: }
|