javamanager/javainstaller/installer/tsrc/javasrc/com/nokia/mj/impl/installer/utils/AttributeValidatorTest.java
changeset 21 2a9601315dfc
child 67 63b81d807542
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.installer.utils;
       
    20 
       
    21 import com.nokia.mj.impl.installer.utils.InstallerMain;
       
    22 import com.nokia.mj.impl.utils.Attribute;
       
    23 
       
    24 import java.util.Hashtable;
       
    25 
       
    26 import j2meunit.framework.Test;
       
    27 import j2meunit.framework.TestCase;
       
    28 import j2meunit.framework.TestMethod;
       
    29 import j2meunit.framework.TestSuite;
       
    30 
       
    31 /**
       
    32  * AttributeValidator unit tests.
       
    33  */
       
    34 public class AttributeValidatorTest extends TestCase implements InstallerMain
       
    35 {
       
    36     private MidpAttributeValidator iMav = null;
       
    37 
       
    38     // Begin j2meunit test framework setup
       
    39 
       
    40     public void installerMain(String[] args)
       
    41     {
       
    42         TestSuite suite = new TestSuite(this.getClass().getName());
       
    43 
       
    44         suite.addTest(new AttributeValidatorTest("testMidletN", new TestMethod()
       
    45         {
       
    46             public void run(TestCase tc)
       
    47             {
       
    48                 ((AttributeValidatorTest)tc).testMidletN();
       
    49             }
       
    50         }));
       
    51 
       
    52         suite.addTest(new AttributeValidatorTest("testNokiaMidletUid", new TestMethod()
       
    53         {
       
    54             public void run(TestCase tc)
       
    55             {
       
    56                 ((AttributeValidatorTest)tc).testNokiaMidletUid();
       
    57             }
       
    58         }));
       
    59 
       
    60         suite.addTest(new AttributeValidatorTest("testMidletJarSize", new TestMethod()
       
    61         {
       
    62             public void run(TestCase tc)
       
    63             {
       
    64                 ((AttributeValidatorTest)tc).testMidletJarSize();
       
    65             }
       
    66         }));
       
    67 
       
    68         suite.addTest(new AttributeValidatorTest("testMidletDataSize", new TestMethod()
       
    69         {
       
    70             public void run(TestCase tc)
       
    71             {
       
    72                 ((AttributeValidatorTest)tc).testMidletDataSize();
       
    73             }
       
    74         }));
       
    75 
       
    76         suite.addTest(new AttributeValidatorTest("testMidletVersion", new TestMethod()
       
    77         {
       
    78             public void run(TestCase tc)
       
    79             {
       
    80                 ((AttributeValidatorTest)tc).testMidletVersion();
       
    81             }
       
    82         }));
       
    83 
       
    84         suite.addTest(new AttributeValidatorTest("testRuntimeExecutionEnvironment", new TestMethod()
       
    85         {
       
    86             public void run(TestCase tc)
       
    87             {
       
    88                 ((AttributeValidatorTest)tc).testRuntimeExecutionEnvironment();
       
    89             }
       
    90         }));
       
    91 
       
    92         suite.addTest(new AttributeValidatorTest("testMicroEditionProfile", new TestMethod()
       
    93         {
       
    94             public void run(TestCase tc)
       
    95             {
       
    96                 ((AttributeValidatorTest)tc).testMicroEditionProfile();
       
    97             }
       
    98         }));
       
    99 
       
   100         suite.addTest(new AttributeValidatorTest("testMicroEditionConfiguration", new TestMethod()
       
   101         {
       
   102             public void run(TestCase tc)
       
   103             {
       
   104                 ((AttributeValidatorTest)tc).testMicroEditionConfiguration();
       
   105             }
       
   106         }));
       
   107 
       
   108         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
       
   109     }
       
   110 
       
   111     public AttributeValidatorTest()
       
   112     {
       
   113     }
       
   114 
       
   115     public AttributeValidatorTest(String aTestName, TestMethod aTestMethod)
       
   116     {
       
   117         super(aTestName, aTestMethod);
       
   118     }
       
   119 
       
   120     public void assertFalse(String aMsg, boolean aCondition)
       
   121     {
       
   122         assertTrue(aMsg, !aCondition);
       
   123     }
       
   124 
       
   125     // End j2meunit test framework setup
       
   126 
       
   127     protected void setUp()
       
   128     {
       
   129         iMav = new MidpAttributeValidator();
       
   130     }
       
   131 
       
   132     protected void tearDown()
       
   133     {
       
   134         iMav = null;
       
   135     }
       
   136 
       
   137     public void testMidletN()
       
   138     {
       
   139         Hashtable tbl = getTestTbl();
       
   140         String attrName = "MIDlet-1";
       
   141         assertValidValue(tbl, attrName, " M1 , \t  ,  p.M1 ");
       
   142         assertValidValue(tbl, attrName, " M1 ,  /m1icon.png  ,  \t p.M1 ");
       
   143         assertInvalidValue(tbl, attrName, ", , p.M1"); // missing name
       
   144         assertInvalidValue(tbl, attrName, "M1, , "); // missing class
       
   145         assertInvalidValue(tbl, attrName, "M1, , p.M1, extra"); // extra part
       
   146     }
       
   147 
       
   148     public void testNokiaMidletUid()
       
   149     {
       
   150         Hashtable tbl = getTestTbl();
       
   151         String attrName = "Nokia-MIDlet-UID-1";
       
   152         assertValidValue(tbl, attrName, "0xeeff0000");
       
   153         assertValidValue(tbl, attrName, "[eeff0000]");
       
   154         assertValidValue(tbl, attrName, "12345678");
       
   155     }
       
   156 
       
   157     public void testMidletJarSize()
       
   158     {
       
   159         Hashtable tbl = getTestTbl();
       
   160         String attrName = "MIDlet-Jar-Size";
       
   161         assertValidValue(tbl, attrName, "0");
       
   162         assertValidValue(tbl, attrName, "100");
       
   163         assertInvalidValue(tbl, attrName, ""); // invalid, empty value is not accepted
       
   164         assertInvalidValue(tbl, attrName, " "); // invalid, empty value is not accepted
       
   165         assertInvalidValue(tbl, attrName, "100a");
       
   166         assertInvalidValue(tbl, attrName, "0x100");
       
   167     }
       
   168 
       
   169     public void testMidletDataSize()
       
   170     {
       
   171         Hashtable tbl = getTestTbl();
       
   172         String attrName = "MIDlet-Data-Size";
       
   173         assertValidValue(tbl, attrName, "0");
       
   174         assertValidValue(tbl, attrName, "100");
       
   175         assertInvalidValue(tbl, attrName, ""); // invalid, empty value is not accepted
       
   176         assertInvalidValue(tbl, attrName, " "); // invalid, empty value is not accepted
       
   177         assertInvalidValue(tbl, attrName, "100a");
       
   178         assertInvalidValue(tbl, attrName, "0x100");
       
   179     }
       
   180 
       
   181     public void testMidletVersion()
       
   182     {
       
   183         Hashtable tbl = getTestTbl();
       
   184         String attrName = "MIDlet-Version";
       
   185         assertValidValue(tbl, attrName, "1");
       
   186         assertValidValue(tbl, attrName, "1."); // OPEN: Is this valid? If we are lenient it is.
       
   187         assertValidValue(tbl, attrName, "1.1");
       
   188         assertValidValue(tbl, attrName, "1.1."); // OPEN: Is this valid? If we are lenient it is.
       
   189         assertValidValue(tbl, attrName, "1.1.1");
       
   190         assertInvalidValue(tbl, attrName, ""); // invalid, attr is mandatory
       
   191         assertInvalidValue(tbl, attrName, " "); // invalid, attr is mandatory
       
   192         assertInvalidValue(tbl, attrName, "1.1:1"); // invalid char
       
   193         assertInvalidValue(tbl, attrName, "1.1.1."); // invalid, only three parts allowed
       
   194         assertInvalidValue(tbl, attrName, "1.1.1.1"); // invalid, only three parts allowed
       
   195         assertInvalidValue(tbl, attrName, "abc"); // invalid chars
       
   196     }
       
   197 
       
   198     public void testRuntimeExecutionEnvironment()
       
   199     {
       
   200         Hashtable tbl = getTestTbl();
       
   201         String attrName = "Runtime-Execution-Environment";
       
   202         assertValidValue(tbl, attrName, "MIDP.CLDC");
       
   203         assertValidValue(tbl, attrName, ""); // valid, empty value is accepted
       
   204         assertValidValue(tbl, attrName, " "); // valid, empty value is accepted
       
   205         assertInvalidValue(tbl, attrName, "MIDP.CDC");
       
   206     }
       
   207 
       
   208     public void testMicroEditionProfile()
       
   209     {
       
   210         Hashtable tbl = getTestTbl();
       
   211         String attrName = "MicroEdition-Profile";
       
   212         assertValidValue(tbl, attrName, "MIDP-1.0");
       
   213         assertValidValue(tbl, attrName, "MIDP-2.0");
       
   214         assertValidValue(tbl, attrName, "MIDP-2.1");
       
   215         assertValidValue(tbl, attrName, "MIDP-1.0 MIDP-1.1 MIDP-2.1");
       
   216         assertValidValue(tbl, attrName, "  MIDP-1.0  \t MIDP-1.1   MIDP-2.1 ");
       
   217         assertInvalidValue(tbl, attrName, ""); // invalid, empty value is not accepted
       
   218         assertInvalidValue(tbl, attrName, " "); // invalid, empty value is not accepted
       
   219         assertInvalidValue(tbl, attrName, "J2SE-1.3");
       
   220         assertInvalidValue(tbl, attrName, "MIDP-1.0 MIDP-3.0");
       
   221         assertInvalidValue(tbl, attrName, "MIDP-2.2");
       
   222         assertInvalidValue(tbl, attrName, "MIDP-2.1.1");
       
   223         assertInvalidValue(tbl, attrName, "MIDP-2.1.0.1");
       
   224     }
       
   225 
       
   226     public void testMicroEditionConfiguration()
       
   227     {
       
   228         Hashtable tbl = getTestTbl();
       
   229         String attrName = "MicroEdition-Configuration";
       
   230         assertValidValue(tbl, attrName, "CLDC-1.0");
       
   231         assertValidValue(tbl, attrName, "CLDC-1.1");
       
   232         assertInvalidValue(tbl, attrName, ""); // invalid, empty value is not accepted
       
   233         assertInvalidValue(tbl, attrName, " "); // invalid, empty value is not accepted
       
   234         assertInvalidValue(tbl, attrName, "CLDC-1.1.0.1");
       
   235         assertInvalidValue(tbl, attrName, "CLDC-1.2");
       
   236         assertInvalidValue(tbl, attrName, "CLDC-1.1 CLDC-1.2");
       
   237         assertInvalidValue(tbl, attrName, "CDC-1.1");
       
   238     }
       
   239 
       
   240     /**
       
   241      * Asserts that given attribute value is valid.
       
   242      */
       
   243     private void assertValidValue(Hashtable aTbl, String aName, String aValue)
       
   244     {
       
   245         aTbl.put(aName, new Attribute(aName, aValue, false));
       
   246         assertTrue(aName + ": " + aValue + " ==> expected to succeed",
       
   247                    isValid(aTbl, MidpAttributeValidator.JAR));
       
   248     }
       
   249 
       
   250     /**
       
   251      * Asserts that given attribute value is invalid.
       
   252      */
       
   253     private void assertInvalidValue(Hashtable aTbl, String aName, String aValue)
       
   254     {
       
   255         aTbl.put(aName, new Attribute(aName, aValue, false));
       
   256         assertTrue(aName + ": " + aValue + " ==> expected to fail",
       
   257                    !isValid(aTbl, MidpAttributeValidator.JAR));
       
   258     }
       
   259 
       
   260     /**
       
   261      * Returns true if given attribute table is validated succesfully,
       
   262      * false otherwise.
       
   263      */
       
   264     private boolean isValid(Hashtable aTbl, int aFileType)
       
   265     {
       
   266         boolean result = false;
       
   267         try
       
   268         {
       
   269             iMav.validate(aTbl, aFileType);
       
   270             result = true; // Validation succeeded.
       
   271         }
       
   272         catch (Exception ex)
       
   273         {
       
   274             // Validation failed.
       
   275             //System.out.println(ex);
       
   276         }
       
   277         return result;
       
   278     }
       
   279 
       
   280     /**
       
   281      * Constructs a default attribute table for test cases.
       
   282      */
       
   283     private static Hashtable getTestTbl()
       
   284     {
       
   285         Hashtable tbl = new Hashtable();
       
   286         tbl.put("MIDlet-Name", new Attribute("MIDlet-Name", "M1", false));
       
   287         tbl.put("MIDlet-Vendor", new Attribute("MIDlet-Vendor", "Unknown", false));
       
   288         tbl.put("MIDlet-Version", new Attribute("MIDlet-Version", "1.0", false));
       
   289         tbl.put("MIDlet-1", new Attribute("MIDlet-1", "M1, , p.M1", false));
       
   290         return tbl;
       
   291     }
       
   292 }