Hi Nicole,
I got your example working on my machine.
This line will return always false:
if(this.context.getCallerPrincipal().getName() == "admin1") {
Try to change to use .equals() method.
Also, did you create users.proeprties and roles.properties for
your JBoss installation?
If this does not help I will send you my configurations to take a look.
Cheers, Petar.
2009/1/8, Nicole Luneburg <[email protected]>:
>
> Hi Petar,
>
> Thanks once again for your reply.
> I thought I would spend today trying to resolve my issues before I
> responded ...
> So I've stripped all my code (commented out, rather!!!) so see what you
> were referring to below.
> (I should have seen that here:
> http://jakarta.apache.org/cactus/how_it_works.html . sorry, i've been
> reading so much on the net for days that docos have begun melting in front
> of me!)
>
> Ok so I understand what you said about the Authentication object.
> I thought I was doing that with the WebRequest? (see below)
>
> I hope I'm not taking up too much of yours or anyone else's time :(
> I'm really stuck!
>
> Before I explain what I have, just a quick word on how I run my test.
> I create my ear file and deploy that to JBoss.
> I cactify my project war and deploy that to JBoss.
> I then run my test through my web browser by accessing:
> http://localhost:8080/myapp/ServletTestRunner?suite=MyAppTestSuite
>
> (I know the docs somewhere say to do it other ways, but I can't even get
> that far!)
> So this is what I have:
>
> ==================================================================
> cactus.properties:
>
> cactus.contextURL=http://localhost:8080/myapp
> cactus.port=8080
> ==================================================================
> build.xml:
>
> <target name="test" depends="test.prepare" description="Run the
> tests on the defined containers">
>
> <echo message="Running test target ..."/>
>
> <cactus warfile="${project.build.dir}/test.war" fork="yes"
> failureproperty="tests.failed">
> <classpath>
> <path refid="project.classpath"/>
> <pathelement location="${httpunit.jar}"/>
> <pathelement location="${nekohtml.jar}"/>
> <pathelement location="${target.classes.java.dir}"/>
> <pathelement location="${target.classes.cactus.dir}"/>
> </classpath>
> <containerset timeout="180000"/>
> <formatter type="brief" usefile="false"/>
> <formatter type="xml"/>
> <batchtest>
> <fileset dir="${src.cactus.dir}">
> <include name="**/Test*.java"/>
> <exclude name="**/Test*All.java"/>
> </fileset>
> </batchtest>
> </cactus>
>
> <echo message="Finished test target ..."/>
> </target>
>
> <target name="test.prepare" depends="war,compile.cactus">
> <!-- Cactify the web-app archive -->
> <cactifywar srcfile="${project.build.dir}/${projectname}.war"
> destfile="${project.build.dir}/test.war">
> <servletredirector/>
> <classes dir="${project.build.dir}/test/"/>
> </cactifywar>
> </target>
>
> <target name="compile.cactus" depends="compile">
> <mkdir dir="${project.build.dir}/test/" />
> <javac destdir="${project.build.dir}/test/"
> debug="true" optimize="true"
> deprecation="off">
> <src path="${project.src.dir}/junit/" />
> <include name="com/**/*.java" />
> <classpath refid="project.classpath.junit" />
> </javac>
> </target>
> ==================================================================
> MyAppTestSuite.java:
>
> public class MyAppTestSuite extends junit.framework.TestCase {
>
> public static Test suite() {
> final junit.framework.TestSuite suite = new
> junit.framework.TestSuite ();
> suite.addTestSuite(MyTest.class);
> return suite;
> }
> }
> ==================================================================
> MyTest.java:
>
> public class MyTest extends org.apache.cactus.ServletTestCase {
> public void begin(WebRequest theRequest) {
> theRequest.setRedirectorName("ServletRedirectorSecure");
> theRequest.setAuthentication(new FormAuthentication("admin1",
> "admin1"));
> }
>
> public void setUp() throws Exception {
> Context context = new InitialContext();
> MyEJB3ServiceBean myService = (MyEJB3ServiceBean)
> context.lookup(...);
>
> If(myService.checkPermissions()) { //IT FAILS RIGHT
> HERE!!!!!!!!!!!!!!!!!!
> //do something
> }
> }
>
> public void tearDown() throws Exception {
> //no code
> }
> }
> ==================================================================
> MyEJB3ServiceBean.java:
>
> @Stateless
> public class MyEJB3ServiceBean {
>
> @Resource
> protected SessionContext context;
>
> public boolean checkPermissions () {
> if(this.context.getCallerPrincipal().getName() == "admin1")
> {
> return true;
> } else {
> return false;
> }
> }
> }
> ==================================================================
>
> I can see through some log info statements that it does it exactly in the
> sequence you specified:
>
> begin ...
> setUp ...
> tearDown ...
> (I didn't specify an endXXX method)
>
> So you see my dilemma, my test fails because I need to check who is logged
> in (with that getCallerPrincipal() ... call)
> But in able to do that, I need to log someone in, namely "admin1" as seen
> in my begin method.
> I thought I could "log" someone in by using the WebRequest in the beginXXX
> method.
>
> I am obviously doing this wrong, but I am really at a dead end as to how I
> am to go about this ...
> I hope my problem makes more sense :(
>
> Thnks again,
> nic
>
>
>
> -----Original Message-----
> From: Petar Tahchiev [mailto:[email protected]]
> Sent: Wednesday, 7 January 2009 7:45 PM
> To: Cactus Users List
> Subject: Re: How to set user principal in setUp()
>
> Hi Nicole,
>
> I think you misunderstand the purpose of Cactus.
> Your test will always be executed in the server side.
> This means that the testXXX methods are executed on the
> server side. You can, however, execute two methods - beginXXX and endXXX
> on the client side. This means the execution flow would be this:
>
> beginXXX() - >client side
>
> startUp() -> serverside
> testXXX() -> serverside
> tearDown() ->serverside
>
> endXXX() -> client side.
>
>
> If you see the documentation of the WebRequest (the one you use in
> beginXXX methods) there is no set-up for a caller principal. You can,
> however
> setup an Authentication object.
>
> HTH, Petar.
>
>
>
> The contents of this email are confidential and may be subject to legal or
> professional privilege and copyright. No representation is made that this
> email is free of viruses or other defects. If you have received this
> communication in error, you may not copy or distribute any part of it or
> otherwise disclose its contents to anyone. Please advise the sender of your
> incorrect receipt of this correspondence.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
--
Regards, Petar!
Karlovo, Bulgaria.
- - - - - - - -
| Author @ Manning Publications.
| Senior Solution Architect @ Unic
| BGJUG-Bulgarian Java User Group Leader.
| Apache Jakarta PMC member.
| Jakarta Cactus Lead Developer.
| Blogger: http://weblogs.java.net/blog/paranoiabla/
- - - - - - - -
Public PGP Key at:
https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611