File: GrtTest.php

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (175 lines) | stat: -rw-r--r-- 6,095 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/*
 Generic Runtime Library (GRT)
 Copyright (C) 2005 MySQL AB
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 */

class GrtTest {
	function __construct() {
		assert_options(ASSERT_ACTIVE, 1);
		assert_options(ASSERT_WARNING, 0);
		assert_options(ASSERT_QUIET_EVAL, 1);
		assert_options(ASSERT_CALLBACK, 'grtAssertHandler');
	}
	
	function __destruct() {
		assert_options(ASSERT_ACTIVE, 0);
	}
	
	/**
	 * Tests the prepareGrtXml function
	 */
	public function testPrepareGrtXml() {
		$grtXml = "<value type=\"string\">test</value>";
		$xml = Grt::prepareGrtXml($grtXml);

		assert($xml == "<?xml version=\"1.0\"?>\n<data>\n" . $grtXml
				. "</data>\n");
	}
	
	/**
	 * Tests XML parsing and generation
	 */
	public function testGetObjectsFromGrtXml() {
		$grtXml = "<value type=\"dict\">\n"
				. "<value type=\"dict\" struct-name=\"db.SimpleDatatype\" key=\"simpleType\">\n"
				. "<value type=\"string\" key=\"_id\">423423:89734566:746345234</value>\n"
				. "<value type=\"string\" key=\"name\">VARCHAR</value>\n"
				. "<value type=\"int\" key=\"characterMaximumLength\">255</value>\n"
				. "<value type=\"int\" key=\"characterOctetLength\">0</value>\n"
				. "<value type=\"int\" key=\"numericPrecision\">0</value>\n"
				. "<value type=\"int\" key=\"numericPrecisionRadix\">0</value>\n"
				. "<value type=\"int\" key=\"numericScale\">0</value>\n"
				. "<value type=\"int\" key=\"dateTimePrecision\">0</value>\n"
				. "</value>\n"
				. "<value type=\"list\" content-type=\"dict\" content-struct-name=\"db.Column\" key=\"columns\">\n"
				. "<value type=\"dict\" struct-name=\"db.Column\">\n"
				. "<value type=\"string\" key=\"_id\">328758:2732734:32434234</value>\n"
				. "<value type=\"string\" key=\"name\">Host</value>\n"
				. "<value type=\"int\" key=\"precision\">0</value>\n"
				. "<value type=\"int\" key=\"scale\">0</value>\n"
				. "<value type=\"int\" key=\"isNullable\">1</value>\n"
				. "<value type=\"int\" key=\"length\">30</value>\n"
				. "<value type=\"string\" key=\"datatypeName\">varchar</value>\n"
				. "<value type=\"string\" key=\"defaultValue\">testing</value>\n"
				. "<value type=\"string\" key=\"characterSetName\">default</value>\n"
				. "<value type=\"string\" key=\"collationName\">default</value>\n"
				. "<value type=\"string\" option=\"ref\" key=\"simpleType\">423423:89734566:746345234</value>\n"
				. "</value>\n" . "</value>\n" . "</value>\n";
		$xml = Grt::prepareGrtXml($grtXml);

		$root = Grt::getObjectsFromGrtXml($xml);
		assert($root != null);

		$list = $root->getObject("columns");

		assert($list != null);

		$columnList = $list;

		$column = $columnList->get(0);

		assert($column->getDatatypeName() == "varchar");

		$simpleDatatype = $column->getSimpleType();

		assert($simpleDatatype->getCharacterMaximumLength() == 255);
		
		$xml2 = Grt::prepareGrtXml($root->getGrtXml(""));

		assert($xml2 == $xml);
	}
	
	/**
	 * Tests the callModuleFunction function by calling a non-existing function
	 */
	public function testCallModuleFunctionNonExistingFunction() {
		$xml = Grt::callModuleFunction("PhpTestModule",
				"nonExistingFuntion", "", "");

		$result = Grt::getObjectsFromGrtXml($xml);

		assert($result->getObject("error") != null);
		assert($result->getObject("detail") != null);
	}

	/**
	 * Tests the callModuleFunction function by calling a function that raises
	 * an exception
	 */
	public function testException() {
		$xml = Grt::callModuleFunction("PhpTestModule", "throwException",
				"", "");

		$result = Grt::getObjectsFromGrtXml($xml);

		assert($result->getObject("error") != null);
		assert($result->getObject("detail") != null);
		assert($result->getObject("error") == "Exception Test");
	}

	public function testGetGlobalObjects() {
		// Initialize callback with dummy implementation
		Grt::getInstance()->setCallback("GrtCallbackTest", "");
		
		// Get a global object from that dummy implementation
		$obj = Grt::getInstance()->getGrtGlobalAsGrtObject(
				"/testObject");

		// Check if members return the correct values
		assert($obj->getName() == "TestObject");
		assert($obj->get_id() == "123456789");

		// Get a Column object
		$col = Grt::getInstance()->getGrtGlobalAsGrtObject("/column");

		// Check its name
		assert($col->getName() == "TestColumn");

		// Now follow a object reference by id
		$columnDatatype = $col->getSimpleType();

		// Check if the reference worked and we get the correct member values
		//Assert.assertEquals(columnDatatype.getName(), "VARCHAR");
		//Assert.assertEquals(columnDatatype.getCharacterMaximumLength(), 255);		
		
		// Get the SimpleDatatype directly
		$datatype = Grt::getInstance()->getGrtGlobalAsGrtObject("/simpleDatatype");
		
		assert($datatype->getName() == "VARCHAR");
		assert($datatype->getCharacterMaximumLength() == 255);	

		// Test is the two objects we have created are represented by
		// the "same" PHP object (not ===)
		assert($datatype == $columnDatatype);
	}
}

include('GrtInit.php');
include('modules/PhpTestModule.php');

// run tests
$testClass = new GrtTest();

$testClass->testPrepareGrtXml();
$testClass->testGetObjectsFromGrtXml();
$testClass->testCallModuleFunctionNonExistingFunction();
$testClass->testException();
$testClass->testGetGlobalObjects();

unset($testClass);
?>