001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): i18n : Pedro Casals Fradera (2006/05/16)
018: *
019: ********************************************************************************/package org.openi.feeds.ws;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.log4j.Logger;
029: import org.openi.feeds.FeedsMetaData;
030: import org.openi.feeds.ws.FeedsMetaDataService;
031: import org.openi.feeds.ws.FeedsMetaDataServiceImpl;
032: import org.openi.feeds.ws.config.FeedsConfig;
033:
034: /**
035: * @author Dipendra Pokhrel <br>
036: *
037: * test class for org.openi.feeds.ws.FeedsMetaDataService(web service)
038: *
039: */
040: public class FeedsMetaDataServiceTest extends TestCase {
041: private static Logger logger = Logger
042: .getLogger(FeedsMetaDataServiceTest.class);
043:
044: public static String FEEDS_BASE_DIR = "C:/Tomcat 5.0/webapps/openi-projects";
045: private FeedsMetaDataService service;
046:
047: public FeedsMetaDataServiceTest() {
048: FeedsConfig.getInstance().setBaseDir(FEEDS_BASE_DIR);
049: }
050:
051: protected void setUp() throws Exception {
052: service = new FeedsMetaDataServiceImpl();
053: super .setUp();
054: }
055:
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: }
059:
060: public void testFeedsConfig() throws Exception {
061: logger.debug("testing config");
062: assertEquals("Base dir is not equal", FeedsConfig.getInstance()
063: .getBaseDir(), FEEDS_BASE_DIR);
064: }
065:
066: /**
067: * tests metadata lister method which resturna array of all metadadata object available under base dir
068: */
069: public void testListMetaData() throws Exception {
070: logger.debug("testing listMetaData()");
071: FeedsMetaData[] metadatas = service.listMetaData("");
072: assertNotNull("listMetaData should not return null", metadatas);
073: }
074:
075: /**
076: * tests listAll method which returns the tree map of all
077: * available feeds available under basedir filtered with extensions
078: *
079: */
080: public void testListAll() throws Exception {
081: logger.debug("testing listAll()");
082: Map treemap = service.listAll("", ".csv,.txt,.text");
083: assertNotNull("ListAll should not return null", treemap);
084: List all = new ArrayList();
085: findFeedsForTesting(treemap, all);
086: logger.debug("Found " + all.size() + " feeds");
087: }
088:
089: /**
090: * tests listDir method which returns the tree map of all
091: * available files/folders available under specfied dir
092: *
093: */
094: public void testListDir() throws Exception {
095: logger.debug("testing listDir() on root");
096: Map treemap = service.listDir("", false, "");
097: assertNotNull("ListAll should not return null", treemap);
098: List all = new ArrayList();
099: findFeedsForTesting(treemap, all);
100: logger.debug("Found " + all.size() + " files");
101: }
102:
103: /**
104: * tests Reparse method which parses the specified feed
105: * and returns the parsed metadata
106: */
107: public void testReparse() throws Exception {
108: logger.debug("testing reparse()");
109: FeedsMetaData metadata = service.reparse((String) service
110: .listMetaData("")[0].getAttributes().get("path"));
111: assertNotNull("metadata should not be null", metadata);
112: }
113:
114: private void findFeedsForTesting(Map treemap, List paths)
115: throws Exception {
116: if (treemap == null)
117: return;
118: for (Iterator iter = treemap.keySet().iterator(); iter
119: .hasNext();) {
120: String key = (String) iter.next();
121: if ("type".equals(key))
122: continue;
123: Map value = (Map) treemap.get(key);
124: if ("file".equals(value.get("type"))) {
125: paths.add(key);
126: } else if ("dir".equals(value.get("type"))) {
127: findFeedsForTesting(value, paths);
128: }
129: }
130: }
131:
132: /**
133: * tests parse method which parses the specified feed based
134: * on delimeter and samplesize provided in specified metadata
135: * and returns parsed metadata object
136: */
137: public void testParse() throws Exception {
138: logger.debug("testing parse()");
139: logger.debug("searching a feed to test");
140: Map treemap = service.listAll("", ".csv,.txt,.text");
141: List all = new ArrayList();
142: findFeedsForTesting(treemap, all);
143: String afeed = "";
144: if (all.size() != 0) {
145: afeed = (String) all.iterator().next();
146: logger.debug("found feed :" + afeed);
147: } else {
148: logger
149: .debug("could not find any feed to test under base dir "
150: + FEEDS_BASE_DIR);
151: return;
152: }
153: FeedsMetaData metadata = new FeedsMetaData();
154: metadata.setDelimeter(",");
155: metadata.setSampleSize(-1);
156: metadata = service.parse(afeed, metadata);
157: assertNotNull("metadata should not be null", metadata);
158: }
159: }
|