View Javadoc

1   /*
2    *  Copyright 2008 mkleint.
3    * 
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    * 
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *  under the License.
16   */
17  
18  package org.codehaus.mojo.nbm;
19  
20  import java.util.Arrays;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.regex.Pattern;
24  import junit.framework.TestCase;
25  import org.apache.tools.ant.taskdefs.Manifest;
26  
27  public class NetBeansManifestUpdateMojoTest extends TestCase {
28      
29      public NetBeansManifestUpdateMojoTest(String testName) {
30          super(testName);
31      }
32  
33      public void testCreateCompiledPatternList()
34      {
35          List<String> subpackages = Arrays.asList( new String[] {
36                  "org.milos.**",
37                  "org.tomas.**"
38          });
39          List<Pattern> result = NetBeansManifestUpdateMojo.createCompiledPatternList( subpackages );
40          assertTrue( matches( "org.milos.Test", result));
41          assertTrue( matches( "org.milos.pack.Test", result));
42          assertTrue( matches( "org.tomas.pack.Test$Inside", result));
43          assertFalse( matches( "org.milan", result));
44          assertFalse( matches( "org.milosclass", result));
45  
46          List<String> packages = Arrays.asList( new String[] {
47                  "org.milos.*",
48                  "org.tomas.*"
49          });
50          result = NetBeansManifestUpdateMojo.createCompiledPatternList( packages );
51          assertTrue( matches( "org.milos.Test", result));
52          assertFalse( matches( "org.milos.pack.Test", result));
53          assertFalse( matches( "org.tomas.pack.Test$Inside", result));
54          assertTrue( matches( "org.tomas.Test$Inside", result));
55          assertFalse( matches( "org.milan", result));
56          assertFalse( matches( "org.milosclass", result));
57  
58      }
59  
60      private boolean matches(String className, List<Pattern> matchers) {
61          for (Pattern patt : matchers) {
62              if (patt.matcher( className ).matches()) {
63                  return true;
64              }
65          }
66          return false;
67      }
68  
69      public void testShorten()
70      {
71          Locale old = Locale.getDefault();
72          Locale.setDefault( Locale.US );
73          try
74          {
75              assertEquals( null, NetBeansManifestUpdateMojo.shorten ( null ) );
76              assertEquals( null, NetBeansManifestUpdateMojo.shorten ( "" ) );
77              assertEquals( "I typed some description here", NetBeansManifestUpdateMojo.shorten ( "I typed some description here" ) );
78              assertEquals( "Now I'm trying to be serious.", NetBeansManifestUpdateMojo.shorten ( "Now I'm trying to be serious." ) );
79              assertEquals( "A meaningful description.", NetBeansManifestUpdateMojo.shorten ( "A meaningful description. But will it work?" ) );
80              assertEquals( "I have no idea what this module does, do you?", NetBeansManifestUpdateMojo.shorten ( "I have no idea what this module does, do you? No? Fine." ) );
81          }
82          finally
83          {
84              Locale.setDefault( old );
85          }
86      }
87  
88      public void testNewlines()
89          throws Exception
90      {
91          Manifest m = new Manifest();
92          Manifest.Section s = m.getMainSection();
93          new NetBeansManifestUpdateMojo().conditionallyAddAttribute( s, "Desc", "Something.\n   Else.\n" );
94          assertEquals( "Something. Else.", s.getAttributeValue( "Desc" ));
95      }
96  
97  }