Ticket #492: UpdateTest.java

File UpdateTest.java, 4.4 KB (added by Dimitar Misev, 11 years ago)
Line 
1/*
2 * This file is part of rasdaman community.
3 *
4 * Rasdaman community is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Rasdaman community is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright 2003 - 2010 Peter Baumann / rasdaman GmbH.
18 *
19 * For more information please see <http://www.rasdaman.org>
20 * or contact Peter Baumann via <baumann@rasdaman.com>.
21 */
22/**
23 * Test updates via rasj.
24 *
25 * @author Dimitar Misev
26 */
27package tests;
28
29import java.io.File;
30import java.io.FileInputStream;
31import java.util.Iterator;
32import org.odmg.Database;
33import org.odmg.OQLQuery;
34import org.odmg.Transaction;
35
36import rasj.RasGMArray;
37import rasj.RasImplementation;
38import rasj.RasMInterval;
39import rasj.odmg.RasBag;
40
41public class UpdateTest {
42
43 public static final String RASDAMAN_URL = "http://localhost:7001";
44 public static final String RASDAMAN_DATABASE = "RASBASE";
45 public static final String RASDAMAN_USER = "rasadmin";
46 public static final String RASDAMAN_PASS = "rasadmin";
47 public static final String TESTDATA_FILE = "../systemtest/testcases_services/test_wcps/testdata/mean_summer_airtemp.tif";
48 public static final String TESTDATA_TYPE = "GreyString";
49 public static final String TESTDATA_SDOM = "[0:885,0:710]";
50 public static final int TESTDATA_SIZE = 631212;
51 public static final String COLL = "my_collection";
52
53 public static void main(String[] args) {
54 System.out.println("---------------------------------------");
55 System.out.println("rasj update test");
56
57 RasImplementation rasImpl = new RasImplementation(RASDAMAN_URL);
58 Transaction transaction = null;
59
60 try {
61
62 rasImpl.setUserIdentification(RASDAMAN_USER, RASDAMAN_PASS);
63 Database rasDb = rasImpl.newDatabase();
64
65 rasDb.open(RASDAMAN_DATABASE, Database.OPEN_READ_WRITE);
66
67 transaction = rasImpl.newTransaction();
68 transaction.begin();
69
70 OQLQuery myQu = rasImpl.newOQLQuery();
71 myQu.create("create collection " + COLL + " GreySet");
72 myQu.execute();
73
74 myQu = rasImpl.newOQLQuery();
75 myQu.create("insert into " + COLL + " values inv_tiff($1)");
76 myQu.bind(createMDDFromFile(TESTDATA_FILE));
77 myQu.execute();
78
79 myQu = rasImpl.newOQLQuery();
80 myQu.create("select sdom(c) from " + COLL + " as c");
81 Object result = myQu.execute();
82 try {
83 RasBag bag = (RasBag) result;
84 Iterator it = bag.iterator();
85 while (it.hasNext()) {
86 RasMInterval res = (RasMInterval) it.next();
87 if (!res.toString().equals(TESTDATA_SDOM)) {
88 System.out.println("Test failed, expected domain " + TESTDATA_SDOM + ", got " + res);
89 System.out.println("---------------------------------------");
90 error(transaction, null);
91 } else {
92 System.out.println("Test passed.\n");
93 System.out.println("---------------------------------------");
94 }
95 }
96 } catch (Exception ex) {
97 error(transaction, ex);
98 }
99
100 } catch (Exception ex) {
101 error(transaction, ex);
102 } finally {
103 try {
104 if (!transaction.isOpen()) {
105 transaction.begin();
106 }
107 OQLQuery myQu = rasImpl.newOQLQuery();
108 myQu.create("drop collection " + COLL);
109 myQu.execute();
110 transaction.commit();
111 } catch (Exception ex) {
112 }
113 if (transaction.isOpen()) {
114 transaction.commit();
115 }
116 }
117
118 }
119
120 public static void error(Transaction transaction, Exception ex) {
121 if (ex != null) {
122 ex.printStackTrace();
123 }
124 if (transaction.isOpen()) {
125 transaction.abort();
126 }
127 System.exit(1);
128 }
129
130 public static RasGMArray createMDDFromFile(String fileName) throws Exception {
131 RasGMArray mdd = new RasGMArray(new RasMInterval("[0:" + (TESTDATA_SIZE - 1) + "]"), 1);
132
133 FileInputStream fr = new FileInputStream(new File(fileName));
134 byte[] array = new byte[TESTDATA_SIZE];
135 fr.read(array);
136 mdd.setArray(array);
137 mdd.setObjectTypeName(TESTDATA_TYPE);
138
139 return mdd;
140 }
141}