More exceptions and tests for Asset.pm

This commit is contained in:
Colin Kuskie 2010-01-21 19:52:48 -08:00
parent 9004007b0e
commit 52d5883b77
2 changed files with 42 additions and 39 deletions

View file

@ -804,8 +804,7 @@ sub getClassById {
return $className if $className; return $className if $className;
$session->errorHandler->error("Couldn't find className for asset '$assetId'"); WebGUI::Error::InvalidParam->throw(error => "Couldn't lookup classname", param => $assetId);
return undef;
} }
@ -1715,7 +1714,7 @@ Must be a valid assetId
=head3 revisionDate =head3 revisionDate
A specific revision date for the asset to retrieve. If not specified, the most recent one will be used. An optional, specific revision date for the asset to retrieve. If not specified, the most recent one will be used.
=cut =cut
@ -1723,19 +1722,14 @@ sub newById {
my $requestedClass = shift; my $requestedClass = shift;
my $session = shift; my $session = shift;
my $assetId = shift; my $assetId = shift;
if (!$assetId) {
WebGUI::Error::InvalidParam->throw(error => 'newById must get an assetId');
}
my $revisionDate = shift; my $revisionDate = shift;
# Some code requires that these situations not die.
# confess "newById requires WebGUI::Session"
# unless $session && blessed $session eq 'WebGUI::Session';
# confess "newById requires assetId"
# unless $assetId;
# So just return instead
return undef unless ( $session && blessed $session eq 'WebGUI::Session' )
&& $assetId;
my $className = WebGUI::Asset->getClassById($session, $assetId); my $className = WebGUI::Asset->getClassById($session, $assetId);
my $class = WebGUI::Asset->loadModule($className); my $class = WebGUI::Asset->loadModule($className);
return $class->new($session, $assetId, $revisionDate); return $class->new($session, $assetId, $revisionDate);
} }

View file

@ -176,14 +176,14 @@ note "loadModule";
# Test the default constructor # Test the default constructor
my $defaultAsset = WebGUI::Asset->getDefault($session); my $defaultAsset = WebGUI::Asset->getDefault($session);
is(ref $defaultAsset, 'WebGUI::Asset::Wobject::Layout', 'default constructor'); is($defaultAsset, 'WebGUI::Asset::Wobject::Layout');
# Test the new constructor # Test the new constructor
my $assetId = "PBnav00000000000000001"; # one of the default nav assets my $assetId = "PBnav00000000000000001"; # one of the default nav assets
# - explicit class # - explicit class
my $asset = WebGUI::Asset->newById($session, $assetId); my $asset = WebGUI::Asset->newById($session, $assetId);
is (ref $asset, 'WebGUI::Asset::Wobject::Navigation','new constructor explicit - ref check'); isa_ok ($asset, 'WebGUI::Asset::Wobject::Navigation');
is ($asset->getId, $assetId, 'new constructor explicit - returns correct asset'); is ($asset->getId, $assetId, 'new constructor explicit - returns correct asset');
# - new by hashref properties # - new by hashref properties
@ -192,13 +192,13 @@ $asset = WebGUI::Asset->newByPropertyHashRef($session, {
className=>"WebGUI::Asset::Wobject::Navigation", className=>"WebGUI::Asset::Wobject::Navigation",
assetId=>$assetId assetId=>$assetId
}); });
is (ref $asset, 'WebGUI::Asset::Wobject::Navigation', 'new constructor newByHashref - ref check'); isa_ok ($asset, 'WebGUI::Asset::Wobject::Navigation');
is ($asset->getId, $assetId, 'new constructor newByHashref - returns correct asset'); is ($asset->getId, $assetId, 'new constructor newByHashref - returns correct asset');
# - implicit class # - implicit class
$asset = undef; $asset = undef;
$asset = WebGUI::Asset::Wobject::Navigation->new($session, $assetId); $asset = WebGUI::Asset::Wobject::Navigation->new($session, $assetId);
is (ref $asset, 'WebGUI::Asset::Wobject::Navigation', 'new constructor implicit - ref check'); isa_ok ($asset, 'WebGUI::Asset::Wobject::Navigation');
is ($asset->getId, $assetId, 'new constructor implicit - returns correct asset'); is ($asset->getId, $assetId, 'new constructor implicit - returns correct asset');
# - die gracefully # - die gracefully
@ -224,37 +224,46 @@ isa_ok ($primevalAsset, 'WebGUI::Asset');
# Test the newById Constructor # Test the newById Constructor
$asset = undef; $asset = undef;
note "newById"; note "new";
$asset = WebGUI::Asset->newById($session, $assetId); use WebGUI::Asset::Wobject::Navigation;
is (ref $asset, 'WebGUI::Asset::Wobject::Navigation', 'newById constructor - ref check'); $asset = WebGUI::Asset::Wobject::Navigation->new($session, $assetId);
is ($asset->getId, $assetId, 'newById constructor - returns correct asset'); isa_ok ($asset, 'WebGUI::Asset::Wobject::Navigation');
is ($asset->getId, $assetId, 'new constructor - returns correct asset when invoked with correct class');
# - die gracefully # - die gracefully
my $deadAsset = 1; my $deadAsset = 1;
# -- invalid asset id # -- invalid asset id
$deadAsset = WebGUI::Asset->newById($session, 'RoysNonExistantAssetId'); note "getClassById";
is ($deadAsset, undef,'newByDynamicClass constructor with invalid assetId returns undef'); {
my $deadAsset = eval { WebGUI::Asset->getClassById($session, 'RoysNonExistantAssetId'); };
my $e = Exception::Class->caught;
isa_ok($e, 'WebGUI::Error::InvalidParam', 'getClassById must have a valid assetId');
cmp_deeply(
$e,
methods(
error => "Couldn't lookup classname",
param => 'RoysNonExistantAssetId',
),
'... checking error message',
);
}
# -- no assetId note "newById";
is( {
WebGUI::Asset->newByDynamicClass( $session ), my $deadAsset = eval { WebGUI::Asset->newById($session); };
undef, my $e = Exception::Class->caught;
"newByDynamicClass constructor returns 'undef' with no assetId", isa_ok($e, 'WebGUI::Error::InvalidParam', "newById won't work without an assetId");
); cmp_deeply(
$e,
methods(
error => "newById must get an assetId",
),
'... checking error message',
);
}
# -- no session # -- no session
is(
WebGUI::Asset->newByDynamicClass( ),
undef,
"newByDynamicClass constructor returns 'undef' with no valid WebGUI::Session",
);
is(
WebGUI::Asset->newByDynamicClass( "nothing" ),
undef,
"newByDynamicClass constructor returns 'undef' with no valid WebGUI::Session",
);
# Root Asset # Root Asset
isa_ok($rootAsset, 'WebGUI::Asset'); isa_ok($rootAsset, 'WebGUI::Asset');
is($rootAsset->getId, 'PBasset000000000000001', 'Root Asset ID check'); is($rootAsset->getId, 'PBasset000000000000001', 'Root Asset ID check');