001: package org.drools.brms.server.util;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.io.UnsupportedEncodingException;
026: import java.util.ArrayList;
027: import java.util.Date;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import junit.framework.TestCase;
032:
033: import org.apache.commons.fileupload.FileItem;
034: import org.drools.brms.client.common.AssetFormats;
035: import org.drools.brms.client.packages.PackageSnapshotView;
036: import org.drools.brms.server.ServiceImplementation;
037: import org.drools.brms.server.files.FileManagerUtils;
038: import org.drools.repository.AssetItem;
039: import org.drools.repository.PackageItem;
040: import org.drools.repository.RulesRepository;
041:
042: public class FileManagerUtilsTest extends TestCase {
043:
044: public void testAttachFile() throws Exception {
045:
046: FileManagerUtils uploadHelper = new FileManagerUtils();
047:
048: RulesRepository repo = new RulesRepository(
049: TestEnvironmentSessionHelper.getSession());
050: uploadHelper.repository = repo;
051: AssetItem item = repo.loadDefaultPackage().addAsset(
052: "testUploadFile", "description");
053: FormData upload = new FormData();
054:
055: upload.setFile(new MockFile());
056: upload.setUuid(item.getUUID());
057:
058: uploadHelper.attachFile(upload);
059:
060: AssetItem item2 = repo.loadDefaultPackage().loadAsset(
061: "testUploadFile");
062: byte[] data = item2.getBinaryContentAsBytes();
063:
064: assertNotNull(data);
065: assertEquals("foo bar", new String(data));
066: assertEquals("foo.bar", item2
067: .getBinaryContentAttachmentFileName());
068: }
069:
070: public void testUploadXmlFile() throws Exception {
071: RulesRepository repo = new RulesRepository(
072: TestEnvironmentSessionHelper.getSession());
073:
074: repo.createPackage("testUploadXmlFile", "comment");
075: repo.importRulesRepository(repo.dumpRepositoryXml());
076: assertTrue(repo.containsPackage("testUploadXmlFile"));
077: }
078:
079: public void testGetFilebyUUID() throws Exception {
080: FileManagerUtils uploadHelper = new FileManagerUtils();
081: RulesRepository repo = new RulesRepository(
082: TestEnvironmentSessionHelper.getSession());
083: uploadHelper.repository = repo;
084: AssetItem item = repo.loadDefaultPackage().addAsset(
085: "testGetFilebyUUID", "description");
086: FormData upload = new FormData();
087:
088: upload.setFile(new MockFile());
089: upload.setUuid(item.getUUID());
090: uploadHelper.attachFile(upload);
091:
092: ByteArrayOutputStream out = new ByteArrayOutputStream();
093:
094: String filename = uploadHelper.loadFileAttachmentByUUID(item
095: .getUUID(), out);
096:
097: assertNotNull(out.toByteArray());
098: assertEquals("foo bar", new String(out.toByteArray()));
099: assertEquals("foo.bar", filename);
100: }
101:
102: public void testGetBinaryPackage() throws Exception {
103:
104: RulesRepository repo = new RulesRepository(
105: TestEnvironmentSessionHelper.getSession());
106: ServiceImplementation impl = new ServiceImplementation();
107: impl.repository = repo;
108:
109: long before = System.currentTimeMillis();
110: Thread.sleep(20);
111: FileManagerUtils uploadHelper = new FileManagerUtils();
112:
113: uploadHelper.repository = repo;
114: PackageItem pkg = repo.createPackage(
115: "testGetBinaryPackageServlet", "");
116: pkg.updateHeader("import java.util.List");
117: pkg.updateCompiledPackage(new ByteArrayInputStream("foo"
118: .getBytes()));
119: pkg.checkin("");
120:
121: assertTrue(before < uploadHelper.getLastModified(pkg.getName(),
122: "LATEST"));
123:
124: impl
125: .createPackageSnapshot(pkg.getName(), "SNAPPY 1",
126: false, "");
127:
128: ByteArrayOutputStream out = new ByteArrayOutputStream();
129: String fileName = uploadHelper.loadBinaryPackage(pkg.getName(),
130: PackageSnapshotView.LATEST_SNAPSHOT, true, out);
131: assertEquals("testGetBinaryPackageServlet.pkg", fileName);
132: byte[] file = out.toByteArray();
133: assertNotNull(file);
134: assertEquals("foo", new String(file));
135:
136: out = new ByteArrayOutputStream();
137: fileName = uploadHelper.loadBinaryPackage(pkg.getName(),
138: "SNAPPY 1", false, out);
139: assertEquals("testGetBinaryPackageServlet_SNAPPY+1.pkg",
140: fileName);
141: file = out.toByteArray();
142: assertNotNull(file);
143: assertEquals("foo", new String(file));
144:
145: Thread.sleep(100);
146: impl.createPackageSnapshot(pkg.getName(), "SNAPX", false, "");
147:
148: long lastMod = uploadHelper.getLastModified(pkg.getName(),
149: "SNAPPY 1");
150: assertTrue(pkg.getLastModified().getTimeInMillis() < lastMod);
151:
152: Thread.sleep(100);
153:
154: impl
155: .createPackageSnapshot(pkg.getName(), "SNAPX", true,
156: "yeah");
157:
158: long lastMod2 = uploadHelper.getLastModified(pkg.getName(),
159: "SNAPX");
160: assertTrue(lastMod < lastMod2);
161:
162: }
163:
164: public void testClassicDRLImport() throws Exception {
165: FileManagerUtils fm = new FileManagerUtils();
166: fm.repository = new RulesRepository(
167: TestEnvironmentSessionHelper.getSession());
168: String drl = "package testClassicDRLImport\n import blah \n rule 'ola' \n when \n then \n end \n rule 'hola' \n when \n then \n end";
169: InputStream in = new ByteArrayInputStream(drl.getBytes());
170: fm.importClassicDRL(in);
171:
172: PackageItem pkg = fm.repository
173: .loadPackage("testClassicDRLImport");
174: assertNotNull(pkg);
175:
176: List<AssetItem> rules = iteratorToList(pkg.getAssets());
177: assertEquals(2, rules.size());
178:
179: final AssetItem rule1 = rules.get(0);
180: assertEquals("ola", rule1.getName());
181: assertNotNull(rule1.getContent());
182: assertEquals(AssetFormats.DRL, rule1.getFormat());
183: assertTrue(rule1.getContent().indexOf("when") > -1);
184:
185: final AssetItem rule2 = rules.get(1);
186: assertEquals("hola", rule2.getName());
187: assertNotNull(rule2.getContent());
188: assertEquals(AssetFormats.DRL, rule2.getFormat());
189: assertTrue(rule2.getContent().indexOf("when") > -1);
190:
191: assertNotNull(pkg.getHeader());
192: assertTrue(pkg.getHeader().indexOf("import") > -1);
193:
194: //now lets import an existing thing
195: drl = "package testClassicDRLImport\n import should not see \n rule 'ola2' \n when \n then \n end \n rule 'hola' \n when \n then \n end";
196: in = new ByteArrayInputStream(drl.getBytes());
197: fm.importClassicDRL(in);
198:
199: pkg = fm.repository.loadPackage("testClassicDRLImport");
200: assertNotNull(pkg);
201:
202: //it should not overwrite this.
203: assertTrue(pkg.getHeader().indexOf("import should not see") == -1);
204:
205: rules = iteratorToList(pkg.getAssets());
206: assertEquals(3, rules.size());
207:
208: }
209:
210: public void testClassicDRLImportWithDSL() throws Exception {
211: FileManagerUtils fm = new FileManagerUtils();
212: fm.repository = new RulesRepository(
213: TestEnvironmentSessionHelper.getSession());
214: String drl = "package testClassicDRLImportDSL\n import blah \n expander goo \n rule 'ola' \n when \n then \n end \n rule 'hola' \n when \n then \n end";
215: InputStream in = new ByteArrayInputStream(drl.getBytes());
216: fm.importClassicDRL(in);
217:
218: PackageItem pkg = fm.repository
219: .loadPackage("testClassicDRLImportDSL");
220: assertNotNull(pkg);
221:
222: List<AssetItem> rules = iteratorToList(pkg.getAssets());
223: assertEquals(2, rules.size());
224:
225: final AssetItem rule1 = rules.get(0);
226: assertEquals("ola", rule1.getName());
227: assertNotNull(rule1.getContent());
228: assertEquals(AssetFormats.DSL_TEMPLATE_RULE, rule1.getFormat());
229: assertTrue(rule1.getContent().indexOf("when") > -1);
230:
231: final AssetItem rule2 = rules.get(1);
232: assertEquals("hola", rule2.getName());
233: assertNotNull(rule2.getContent());
234: assertEquals(AssetFormats.DSL_TEMPLATE_RULE, rule2.getFormat());
235: assertTrue(rule2.getContent().indexOf("when") > -1);
236:
237: assertNotNull(pkg.getHeader());
238: assertTrue(pkg.getHeader().indexOf("import") > -1);
239:
240: }
241:
242: private List iteratorToList(Iterator assets) {
243: List<AssetItem> list = new ArrayList<AssetItem>();
244: for (Iterator iter = assets; iter.hasNext();) {
245: AssetItem rule = (AssetItem) iter.next();
246: list.add(rule);
247: }
248: return list;
249:
250: }
251:
252: }
253:
254: class MockFile implements FileItem {
255:
256: private static final long serialVersionUID = 400L;
257: InputStream stream = new ByteArrayInputStream("foo bar".getBytes());
258:
259: public void setInputStream(InputStream is) throws IOException {
260: stream.close();
261: stream = is;
262: }
263:
264: public void delete() {
265: }
266:
267: public byte[] get() {
268:
269: return null;
270: }
271:
272: public String getContentType() {
273:
274: return null;
275: }
276:
277: public String getFieldName() {
278:
279: return null;
280: }
281:
282: public InputStream getInputStream() throws IOException {
283: return stream;
284: }
285:
286: public String getName() {
287: return "foo.bar";
288: }
289:
290: public OutputStream getOutputStream() throws IOException {
291:
292: return null;
293: }
294:
295: public long getSize() {
296: return 0;
297: }
298:
299: public String getString() {
300: return null;
301: }
302:
303: public String getString(String arg0)
304: throws UnsupportedEncodingException {
305: return null;
306: }
307:
308: public boolean isFormField() {
309: return false;
310: }
311:
312: public boolean isInMemory() {
313: return false;
314: }
315:
316: public void setFieldName(String arg0) {
317:
318: }
319:
320: public void setFormField(boolean arg0) {
321:
322: }
323:
324: public void write(File arg0) throws Exception {
325:
326: }
327:
328: }
|