/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License, version 2 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/gpl-2.0.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*
* Copyright 2005-2008 Pentaho Corporation. All rights reserved.
*
* @created Jul 8, 2005
* @author Marc Batchelor
*
*/
package org.pentaho.test.platform.repository.content;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.api.repository.ContentException;
import org.pentaho.platform.api.repository.IContentItem;
import org.pentaho.platform.api.repository.IContentLocation;
import org.pentaho.platform.api.repository.IContentRepository;
import org.pentaho.platform.api.repository.RepositoryException;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.repository.content.ContentRepository;
import org.pentaho.platform.repository.hibernate.HibernateUtil;
import org.pentaho.platform.repository.messages.Messages;
import org.pentaho.test.platform.repository.RepositoryTestCase;
@SuppressWarnings("nls")
public class ContentRepositoryTest extends RepositoryTestCase {
private static final String SOLUTION_PATH = "test-src/solution";
public String getSolutionPath() {
return SOLUTION_PATH;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(ContentRepositoryTest.class);
}
public ContentRepositoryTest(String str) {
super(str);
}
// public void testContentRepository() {
// startTest();
//
// IContentLocation contLoc = null;
// try {
// String folderName = "test"; //$NON-NLS-1$
// String description = "Test Description"; //$NON-NLS-1$
// String solnRoot = "ca825b3b-eb03-11d9-ad29-005056c00008"; //$NON-NLS-1$
// contLoc = createContentLocation(getPentahoSession(), getSolutionPath(), folderName, description, solnRoot);
//
// String itemName = "MyXML.xml"; //$NON-NLS-1$
// String itemTitle = "Test Title"; //$NON-NLS-1$
// String itemExtension = "xml"; //$NON-NLS-1$
// String mimeType = "text/xml"; //$NON-NLS-1$
// StringBuffer content = new StringBuffer();
// content.append("node example 1"); //$NON-NLS-1$
// info(Messages.getString("CONTREPTEST.USER_CREATING_ITEM")); //$NON-NLS-1$
// createContentItem(getPentahoSession(), getSolutionPath(), itemName, itemTitle, itemExtension, mimeType, content, "mytestaction.action"); //$NON-NLS-1$
//
// content = new StringBuffer();
// content.append("node example 2"); //$NON-NLS-1$
// info(Messages.getString("CONTREPTEST.USER_CREATING_ITEM")); //$NON-NLS-1$
// createContentItem(getPentahoSession(), getSolutionPath(), itemName, itemTitle, itemExtension, mimeType, content, "mytestaction1.action"); //$NON-NLS-1$
//
// info(Messages.getString("CONTREPTEST.USER_LOADING_ITEM")); //$NON-NLS-1$
// IContentItem item3 = getContentItem(getPentahoSession(), getSolutionPath(), itemName);
// exerciseContentItem(item3);
// } finally {
// try {
// HibernateUtil.flushSession();
// } catch (Exception e) {
// error(e.getLocalizedMessage(), e);
// }
//
// if (contLoc != null) {
// cleanup(contLoc);
// }
// finishTest();
// }
//
// }
public void testDummyTest() {
// do nothing, get the above test to pass!
}
public void setUp() {
// TODO: remove once tests are passing
}
public void tearDown() {
// TODO: remove once tests are passing
}
private void cleanup(IContentLocation contLoc) {
try {
HibernateUtil.beginTransaction();
HibernateUtil.makeTransient(contLoc); // Cleanup
HibernateUtil.commitTransaction();
HibernateUtil.flushSession();
HibernateUtil.clear();
} catch (Exception e) {
error(e.getLocalizedMessage(), e);
}
}
@SuppressWarnings("unused")
private IContentLocation createContentLocation(IPentahoSession sess, String path, String folderName,
String folderDesc, String solnId) {
// IContentRepository repo = ContentRepository.getInstance(sess);
IContentRepository repo = PentahoSystem.get(IContentRepository.class, sess);
// Check to see if it's there first...
IContentLocation contLoc = repo.getContentLocationByPath(path);
if (contLoc != null) {
info(Messages.getInstance().getString("CONTREPTEST.USER_CLEANUPFIRST"));//$NON-NLS-1$
cleanup(contLoc);
HibernateUtil.beginTransaction();
}
contLoc = repo.newContentLocation(path, folderName, folderDesc, solnId, true);
try {
return contLoc;
} finally {
try {
HibernateUtil.commitTransaction();
HibernateUtil.flushSession(); // Force Write - For testing
// only
} catch (Exception e) {
error(e.getLocalizedMessage(), e);
}
}
}
@SuppressWarnings("unused")
private IContentItem createContentItem(IPentahoSession sess, String contPath, String itemName, String itemTitle,
String itemExtension, String mimeType, StringBuffer theContent, String actionName) {
// IContentRepository repo = ContentRepository.getInstance(sess);
IContentRepository repo = PentahoSystem.get(IContentRepository.class, sess);
try {
IContentLocation contLoc = repo.getContentLocationByPath(contPath);
assertNotNull(Messages.getInstance().getString("CONTREPTEST.ASSERT_CONTENT_LOCATION_NULL"), contLoc); //$NON-NLS-1$
IContentItem contItem;
contItem = contLoc.getContentItemByName(itemName);
if (contItem == null) {
contItem = contLoc.newContentItem(itemName, itemTitle, itemExtension, mimeType, null,
IContentItem.WRITEMODE_KEEPVERSIONS);
}
assertNotNull(Messages.getInstance().getString("CONTREPTEST.ASSERT_CONTENT_ITEM_NULL"), contItem); //$NON-NLS-1$
createContentFile(contItem, theContent, actionName);
return contItem;
} finally {
HibernateUtil.commitTransaction();
HibernateUtil.flushSession(); // Force Write - for testing only
}
}
@SuppressWarnings("unused")
private IContentItem getContentItem(IPentahoSession sess, String folderPath, String itemName) {
IContentRepository repo = ContentRepository.getInstance(sess);
try {
IContentLocation contLoc = repo.getContentLocationByPath(folderPath);
assertNotNull(Messages.getInstance().getString("CONTREPTEST.ASSERT_CONTENT_LOCATION_NOT_LOADED"), contLoc); //$NON-NLS-1$
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_RETRIEVED_LOCATION") + contLoc.getDirPath()); //$NON-NLS-1$
IContentItem contItem;
contItem = contLoc.getContentItemByPath(folderPath + "/" + itemName); //$NON-NLS-1$
assertNotNull(Messages.getInstance().getString("CONTREPTEST.ASSERT_CONTENT_ITEM_NOT_LOADED"), contItem); //$NON-NLS-1$
return contItem;
} finally {
HibernateUtil.commitTransaction();
}
}
private void createContentFile(IContentItem contItem, StringBuffer theContent, String actionName)
throws RepositoryException {
try {
OutputStream os = contItem.getOutputStream(actionName);
byte[] cnt = theContent.toString().getBytes();
os.write(cnt);
os.flush();
os.close();
} catch (IOException ex) {
throw new ContentException(Messages.getInstance().getString("CONTREPTEST.EXCEPTION_WRITING_FILE"), ex); //$NON-NLS-1$
}
}
@SuppressWarnings("unused")
private void exerciseContentItem(IContentItem contItem) {
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_CONTENT_ITEM") + contItem.getName()); //$NON-NLS-1$
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_PATH") + contItem.getPath()); //$NON-NLS-1$
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_MIME_TYPE") + contItem.getMimeType()); //$NON-NLS-1$
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_TITLE") + contItem.getTitle()); //$NON-NLS-1$
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_LATEST_FILE_ID") + contItem.getFileId()); //$NON-NLS-1$
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_LATEST_FILE_SIZE") + contItem.getFileSize()); //$NON-NLS-1$
info(Messages.getInstance().getString("CONTREPTEST.DEBUG_LATEST_FILE_DATE") + contItem.getFileDateTime()); //$NON-NLS-1$
BufferedReader rdr = new BufferedReader(contItem.getReader());
assertNotNull(rdr);
StringBuffer sb = new StringBuffer();
String aLine;
try {
while ((aLine = rdr.readLine()) != null) {
sb.append(aLine).append("\r"); //$NON-NLS-1$
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static Test suite() {
return new TestSuite(ContentRepositoryTest.class);
}
}